my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Booking bus tickets with each member only max of 4 seats Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Booking bus tickets with each member only max of 4 seats" Watch "Booking bus tickets with each member only max of 4 seats" New topic
Author

Booking bus tickets with each member only max of 4 seats

harish thiru
Ranch Hand

Joined: Aug 02, 2008
Posts: 32
My assignment is Write a java program to book bus tickets maximum seats: 60 and each member should be able to book only four seats.Create two type of seats aisle and window and allow if one filled fill in other. Print pass showing date and seatnumber.
My code

My problem is how to get a member to book only 4 seats

I use bookAisle() and bookWindow() methods that loop through an array of seats. These two functions check a certain number of array subscripts to see if any spots are available, and if there is, it marks it as taken. One function takes the first half of the array and the second function takes the second half of the array. These functions then return a seat number if one has been booked or a -1 if the seat type is taken


Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

And what's the problem? What is happening compared to what you are expecting.

Note that it's confusing putting your own words in quotes. Quotes are for quoting other people. It's not clear what came from you and what is the assignment.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
harish thiru
Ranch Hand

Joined: Aug 02, 2008
Posts: 32
Thanks for responding Jeanne

What my assignment says is a member should book at the max only four tickets I am not able to implement that.

what output i am getting is at the start is :

Please enter 1 for window, 2 for aisle, Please enter 1 for window, 2 for aisle, Please enter 1 for window, 2 for aisle

i understand what is happening the three threads are executing but i want one thread to book at the maximum only four tickets how do i do that



Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Does each member represent one thread? If so, you could store state in the thread so you know how many have been booked.

I'm moving this to our forum about Threads.
harish thiru
Ranch Hand

Joined: Aug 02, 2008
Posts: 32
Does each member represent one thread? If so, you could store state in the thread so you know how many have been booked.


Yeah i wanted to make each thread a member , i don't understand store state in thread I am new to java.

could you help me Thanks a lot
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

Hello there, I kinda like reservation apps these days because that what I'm doing/finishing one such project myself.

Anyway one simple thing is noticed your variable j, put that outside as intance variable (outside the methods). Then when your reservation is successful, increment that instance variable until 4. This variable should be up to 4 "per thread".

Also I personally think if you use switch case statements instead of if statements for some of your sections can improve readibility. But not sure how your while loop will work out. Here's my version of your app. I just used one thread in main to demonstrate 4 max seats.



K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
harish thiru
Ranch Hand

Joined: Aug 02, 2008
Posts: 32
thanks a lot tsang your code is a lot readable than mine
Tsang said
Anyway one simple thing is noticed your variable j, put that outside as intance variable (outside the methods)

I put variable j inside run() method and run() is for that current thread know, could you please tell me the mistake.

I think we have to synchronize the other methods also since sometimes when i ran in cmd they interfered.

I think there is a small mistake in case 1: and case2: in determineSeatNumber(int choice) In case 1:

executes even if window seat not available and similarly in case 2:

I just added this to your code:

I know thread execution is unpredictable but the output should be

Please enter 1 for window, 2 for aisle, or 0 to exit: 1
Boarding pass for seat number: 1 for one
Please enter 1 for window, 2 for aisle, or 0 to exit: 1
Boarding pass for seat number: 2 for two
Please enter 1 for window, 2 for aisle, or 0 to exit: 1
Boarding pass for seat number: 3 for one


Let them be in any order
but now the output i am getting is only for one thread continuously or
Please enter 1 for window, 2 for aisle, or 0 to exit: Please enter 1 for window, 2 for aisle, or 0 to exit: Please enter 1 for window, 2 for aisle, or 0 to exit:
could you please help me Thanks a lot
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

Honestly my version maybe a neater and more readable but ultimately I'm using your code to start with. You know the exact requirements for your program. Behavior-wise you can amend.

In my case statements say case 1 bookWindow() method - if bookWindow() is successful, if statement is skipped, print window seat booked and return seat number. Now if bookWindow is not successful (-1), it try bookAisle() method. If that is successful I should return that and if not return 0.

So it depends on how many return statements you want to use. At the end of the day you either have a valid seat number or not (0) in which case the printBoardingPass will execute. If seatNumber is 0 nothing happens.
harish thiru
Ranch Hand

Joined: Aug 02, 2008
Posts: 32
I get it Tsang, but i am not able to get output in the way it is said like i displayed previously
could you help me on that.

And also that variable j inside run() method and run() is for that current thread know, could you please tell me the mistake.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

Actually you should use separate classes to test this. If you new BusReserv several times in the BusReserv class main then you will get what you are expecting.

To really test this you really also need to "auto-enter" those inputs to simulate. One way to do this is forget/comment out the Scanner stuff and pass in an int to indicate 1, 2, 0. Randomize this is even better.

One thing about the array of seats. When new threads start, these thread should access the same array in order to detect no duplicate seats. You may want to separate the int array to a different class (say Seat) and in your main class use a Map<Integer, Seat) instead of the array.
>
harish thiru
Ranch Hand

Joined: Aug 02, 2008
Posts: 32
I am new to java Tsang I am not able to get output in a neat way of how you should get while booking bus tickets
as i displayed previously that's how i am getting

Even if use Map i am not able to get threads aligned properly. They can be unpredictable but i want to display like i said previously

could you help me Thanks a lot
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

I think you are not getting your behavior is somewhat because you have your main in your Thread class. I have done a bit to allow testing.


Now if you just run it as is you should get:

run:
Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 1

Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 3

Window seat booked.
Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 6

Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 8

Aisle seat booked.
Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 9

Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 11

Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 4

Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 12

Aisle seat booked.
Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 7

Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 15

Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 17

Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 19

Maximum 4 seats
Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 14

Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 16

Maximum 4 seats
Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 13

Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 21

Maximum 4 seats

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 10

Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 18

Maximum 4 seats

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 5

Window seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for window seat number: 23

Maximum 4 seats

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 2

Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 20

Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 22

Aisle seat booked.

Date: Fri Apr 03 11:13:59 GMT+08:00 2009
Boarding pass for aisle seat number: 24

Maximum 4 seats
BUILD SUCCESSFUL (total time: 0 seconds)


This will book how many seats? I should have started 6 threads. So this should be 24 seats (6*4) since 4 seats max per thread.
harish thiru
Ranch Hand

Joined: Aug 02, 2008
Posts: 32
Thanks a lot Tsang
the output is very neat.
I have to really learn to code, Actually no time as I am asked to implement SLR parser in any programming language.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Booking bus tickets with each member only max of 4 seats