Você está na página 1de 10

6/13/13

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)

File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Java Threads and Synchronization

Author

Booking bus tickets with each member only max of 4 seats


posted 3/28/2009 4:44:08 PM

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
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 .

i m p o r tj a v a . u t i l . S c a n n e r ; i m p o r tj a v a . u t i l . D a t e ; p u b l i cc l a s sB u s R e s e r ve x t e n d sT h r e a d { / /C r e a t ea na r r a yo f6 0s e a t s ,3 0w i n d o wa n d3 0a i s l e . p r i v a t es t a t i ci n t [ ]s e a t s=n e wi n t [ 6 0 ] ; p u b l i cv o i dr u n ( ) { / /S e t u po u rs c a n n e ra n dd e f a u l tt h ec h o i c et ow i n d o w . S c a n n e rs=n e wS c a n n e r ( S y s t e m . i n ) ; i n tc h o i c e=1 ; / /v a r i a b l eji sf o rn oo fb o o k i n g sb yam e m b e r i n tj = 0 ; / /A s ku s e rf o raw i n d o wo ra na i s l es e a ta n ds t o r et h e i rc o i c e . S y s t e m . o u t . p r i n t ( & q u o t ; P l e a s ee n t e r1f o rw i n d o w ,2f o ra i s l e ,o r0t oe x i t :& q u o t ; ) ; c h o i c e=s . n e x t I n t ( ) ; j + + ; / /W h i l et h e i rc h o i c ei sn o tt h eo n ef o re x i t ,a n dam e m b e rh a sm a xo fo n l yf o u rt i c k e t s . w h i l e( c h o i c e! =0& &j & l t ; = 3 ){ i n ts e a t n u m b e r=0 ; / /I ft h e yc h o s eaw i n d o ws e a t ,a t t e m p tt ob o o ki t . i f( c h o i c e= =1 ){ s e a t n u m b e r=b o o k W i n d o w ( ) ; / /N ow i n d o ws e a t sa v a i l a b l e ,t r yb o o k i n ga na i s l es e a tf o rt h e mi n s t e a d . i f( s e a t n u m b e r= =1 ){ s e a t n u m b e r=b o o k A i s l e ( ) ; i f( s e a t n u m b e r! =1 ){ S y s t e m . o u t . p r i n t l n ( & q u o t ; S o r r y ,w ew e r en o ta b l et ob o o kaw i n d o ws e a t .B u td oh a v ea na i s l e s e a t . & q u o t ; ) ; p r i n t B o a r d i n g P a s s ( s e a t n u m b e r ) ; } } e l s e{ / /B o o k i n gaw i n d o ws e a tw a ss u c c e s s f u l . S y s t e m . o u t . p r i n t l n ( & q u o t ; Y o ua r ei nl u c k ,w eh a v eaw i n d o ws e a ta v a i l a b l e ! & q u o t ; ) ; p r i n t B o a r d i n g P a s s ( s e a t n u m b e r ) ; }

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

1/10

6/13/13
5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . 7 6 . 7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 . 9 3 . 9 4 . 9 5 . 9 6 . 9 7 . 9 8 . 9 9 . 1 0 0 . 1 0 1 . 1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 . 1 0 7 . 1 0 8 . 1 0 9 . 1 1 0 . 1 1 1 . 1 1 2 . 1 1 3 . 1 1 4 . 1 1 5 . 1 1 6 . 1 1 7 . 1 1 8 . 1 1 9 . 1 2 0 . 1 2 1 . 1 2 2 . 1 2 3 . 1 2 4 . 1 2 5 . 1 2 6 . 1 2 7 . 1 2 8 . 1 2 9 . 1 3 0 . 1 3 1 . 1 3 2 . 1 3 3 . 1 3 4 . 1 3 5 . 1 3 6 . 1 3 7 . 1 3 8 . 1 3 9 . 1 4 0 . 1 4 1 . 1 4 2 . 1 4 3 . 1 4 4 . 1 4 5 . 1 4 6 . 1 4 7 . 1 4 8 . 1 4 9 . 1 5 0 . 1 5 1 . 1 5 2 . 1 5 3 . 1 5 4 . 1 5 5 . 1 5 6 . 1 5 7 . 1 5 8 . 1 5 9 .

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)
} e l s ei f( c h o i c e= =2 ){ / /I ft h e yc h o s eb o o k i n ga ni s l e ,c h e c kt os e ei fi ti sa v a i l a b l e . s e a t n u m b e r=b o o k A i s l e ( ) ; / /I fn o ta v a i l a b l e ,s e ei fw eh a v ew i n d o ws e a t sa v a i l a b l e . i f( s e a t n u m b e r= =1 ){ s e a t n u m b e r=b o o k W i n d o w ( ) ; i f( s e a t n u m b e r! =1 ){ S y s t e m . o u t . p r i n t l n ( & q u o t ; S o r r y ,w ew e r en o ta b l et ob o o ka na i s l es e a t .B u td oh a v eaw i n d o w s e a t . & q u o t ; ) ; p r i n t B o a r d i n g P a s s ( s e a t n u m b e r ) ; } } e l s e{ / /B o o k i n ga na i s l es e a tw a ss u c c e s s f u l . S y s t e m . o u t . p r i n t l n ( & q u o t ; Y o ua r ei nl u c k ,w eh a v ea na i s l es e a ta v a i l a b l e ! & q u o t ; ) ; p r i n t B o a r d i n g P a s s ( s e a t n u m b e r ) ; } } e l s e{ / /P r i n ta ne r r o rm e s s a g ei ft h e yd i dn o tc h o o s e1 ,2 ,o r0f o rt h e i rc h o i c e . S y s t e m . o u t . p r i n t l n ( & q u o t ; I n v a l i dc h o i c em a d e .P l e a s et r ya g a i n ! & q u o t ; ) ; c h o i c e=0 ; } / /N ow i n d o wo ra i s l es e a t sw e r ea v a i l a b l e . i f( s e a t n u m b e r= =1 ){ S y s t e m . o u t . p r i n t l n ( & q u o t ; W ea r es o r r y ,t h e r ea r en ow i n d o wo ra i s l es e a t sa v a i l a b l e . & q u o t ; ) ; S y s t e m . o u t . p r i n t l n ( ) ; } / /R e p r o m p tf o rac h o i c e S y s t e m . o u t . p r i n t ( & q u o t ; P l e a s ee n t e r1f o rw i n d o w ,2f o ra i s l e ,o r0t oe x i t :& q u o t ; ) ; c h o i c e=s . n e x t I n t ( ) ; }

} p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ){ / /L e t ss t a r tb ys e t t i n ga l ls e a t se q u a lt o0 f o r( i n ti=0 ;i& a m p ; a m p ; l t ;6 0 ;i + + ){ s e a t s [ i ]=0 ; } / /C r e a t i n gt h r e a d sf o rb o o k i n gt i c k e t s B u s R e s e r vb 1 = n e wB u s R e s e r v ( ) ; b 1 . s t a r t ( ) ; / / Iw a n ti nn i c er o u n d r o b i nf a s h i o ns oim a k et h r e a d ss l e e p t r y {T h r e a d . s l e e p ( 1 0 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { } B u s R e s e r vb 2 = n e wB u s R e s e r v ( ) ; b 2 . s t a r t ( ) ; t r y {T h r e a d . s l e e p ( 1 0 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { } B u s R e s e r vb 3 = n e wB u s R e s e r v ( ) ; b 3 . s t a r t ( ) ; } / /T h i sf u n c t i o nc h e c k sf o rw i n d o ws e a t sa n dr e t u r n ss e a tn u m b e ro r1i ff u l l . p r i v a t es t a t i cs y n c h r o n i z e di n tb o o k W i n d o w ( ){ f o r( i n ti=0 ;i& a m p ; a m p ; l t ; 3 0 ;i + + ){ i f( s e a t s [ i ]= =0 ){ s e a t s [ i ]=1 ; r e t u r ni+1 ; } } r e t u r n1 ; } / /T h i sf u n c t i o nc h e c k st os e ei fa i s l es e a t sw e r ea v a i l a b l e ,1i ff u l l . p r i v a t es t a t i cs y n c h r o n i z e di n tb o o k A i s l e ( ){ f o r( i n ti=3 0 ;i& a m p ; a m p ; l t ;6 0 ;i + + ){ i f( s e a t s [ i ]= =0 ){ s e a t s [ i ]=1 ; r e t u r ni+1 ; } }

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

2/10

6/13/13
1 6 0 . 1 6 1 . 1 6 2 . 1 6 3 . 1 6 4 . 1 6 5 . 1 6 6 . 1 6 7 . 1 6 8 . 1 6 9 . 1 7 0 . 1 7 1 . 1 7 2 . 1 7 3 . }

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)
r e t u r n1 ;

p r i v a t es t a t i cv o i dp r i n t B o a r d i n g P a s s ( i n ts e a t n u m b e r ){ D a t et i m e n o w=n e wD a t e ( ) ; S y s t e m . o u t . p r i n t l n ( ) ; S y s t e m . o u t . p r i n t l n ( & a m p ; a m p ; q u o t ; D a t e :& a m p ; a m p ; q u o t ;+t i m e n o w . t o S t r i n g ( ) ) ; S y s t e m . o u t . p r i n t l n ( & a m p ; a m p ; q u o t ; B o a r d i n gp a s sf o rs e a tn u m b e r :& a m p ; a m p ; q u o t ;+s e a t n u m b e r ) ; S y s t e m . o u t . p r i n t l n ( ) ; } }

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: 26448

posted 3/28/2009 9:20:44 PM

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.

78
I like...

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos] Blogging on C erts: SC EA Part 1, Part 2 & 3, C ore Spring 3, OC AJP, OC PJP beta, TOGAF part 1 and part 2

harish thiru Ranch Hand Joined: Aug 02, 2008 Posts: 32

posted 3/29/2009 9:41:47 AM

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: 26448

posted 3/29/2009 9:05:07 PM

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.

78
I like...

harish thiru Ranch Hand Joined: Aug 02, 2008 Posts: 32

posted 3/30/2009 7:27 AM

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 Ranch Hand Joined: Sep 13, 2007 Posts: 1254

posted 3/30/2009 3:42:36 PM

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

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

3/10

6/13/13
I like...

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)
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.

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . 7 6 . 7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 .

i m p o r tj a v a . u t i l . S c a n n e r ; i m p o r tj a v a . u t i l . D a t e ; p u b l i cc l a s sB u s R e s e r ve x t e n d sT h r e a d{ p r i v a t es t a t i ci n t [ ]s e a t s=n e wi n t [ 6 0 ] ;/ /3 0w i n d o wa n d3 0a i s l es e a t s p r i v a t ei n tm a x S e a t C o u n t ;/ /m a xc a n b e4 p r i v a t ei n tp r o m p t F o r S e a t C h o i c e ( ){ S y s t e m . o u t . p r i n t ( " P l e a s ee n t e r1f o rw i n d o w ,"+ " 2f o ra i s l e ,o r0t oe x i t :" ) ; S c a n n e rs=n e wS c a n n e r ( S y s t e m . i n ) ; r e t u r ns . n e x t I n t ( ) ; } p r i v a t ei n td e t e r m i n e S e a t N u m b e r ( i n tc h o i c e ){ s w i t c h( c h o i c e ){ c a s e1 :{ i n ts e a t N u m b e r=b o o k W i n d o w ( ) ; i f( s e a t N u m b e r= =1 ){/ /n ow i n d o ws e a t s e a t N u m b e r=b o o k A i s l e ( ) ; i f( s e a t N u m b e r! =1 ){/ /a i s l es e a ta v a i l a b l e S y s t e m . o u t . p r i n t l n ( " N ow i n d o ws e a tb u ta i s l es e a t . " ) ; p r i n t B o a r d i n g P a s s ( s e a t N u m b e r ) ; } e l s e{ S y s t e m . o u t . p r i n t l n ( " N ow i n d o wo ra i s l es e a t s . " ) ; } } S y s t e m . o u t . p r i n t l n ( " W i n d o ws e a tb o o k e d . " ) ; r e t u r ns e a t N u m b e r ; } c a s e2 :{ i n ts e a t N u m b e r=b o o k A i s l e ( ) ; i f( s e a t N u m b e r= =1 ){/ /n oa i s l es e a t s e a t N u m b e r=b o o k W i n d o w ( ) ; i f( s e a t N u m b e r! =1 ){/ /w i n d o ws e a ta v a i l a b l e S y s t e m . o u t . p r i n t l n ( " N oa i s l es e a tb u tw i n d o ws e a t . " ) ; p r i n t B o a r d i n g P a s s ( s e a t N u m b e r ) ; } e l s e{ S y s t e m . o u t . p r i n t l n ( " N ow i n d o wo ra i s l es e a t s . " ) ; } } S y s t e m . o u t . p r i n t l n ( " A i s l es e a tb o o k e d . " ) ; r e t u r ns e a t N u m b e r ; } d e f a u l t :r e t u r n0 ; } } @ O v e r r i d e p u b l i cv o i dr u n ( ){ w h i l e( m a x S e a t C o u n t<4 ){/ /c h e c km a x4 i n tc h o i c e=p r o m p t F o r S e a t C h o i c e ( ) ; i f( c h o i c e= =0 ){ S y s t e m . e x i t ( 0 ) ; }e l s e{ w h i l e( c h o i c e>2 ){/ /p r o m p tf o ri n p u t c h o i c e=p r o m p t F o r S e a t C h o i c e ( ) ; } i n ts e a t N u m b e r=d e t e r m i n e S e a t N u m b e r ( c h o i c e ) ; i f( s e a t N u m b e r! =1 ){ + + m a x S e a t C o u n t ;/ /i n c r e m e n tm a xc o u n t p r i n t B o a r d i n g P a s s ( s e a t N u m b e r ) ; } } } S y s t e m . o u t . p r i n t l n ( " M a x i m u m4s e a t s " ) ; } p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ){ / /L e t ss t a r tb ys e t t i n ga l ls e a t se q u a lt o0 f o r( i n ti=0 ;i<6 0 ;i + + ){ s e a t s [ i ]=0 ; } / /C r e a t i n gt h r e a d sf o rb o o k i n gt i c k e t s B u s R e s e r vb 1=n e wB u s R e s e r v ( ) ; b 1 . s t a r t ( ) ;/ /o n l y1t h r e a di su s e d } / /T h i sf u n c t i o nc h e c k sf o rw i n d o ws e a t sa n dr e t u r n s / /s e a tn u m b e ro r1i ff u l l . p r i v a t es t a t i cs y n c h r o n i z e di n tb o o k W i n d o w ( ){ f o r( i n ti=0 ;i<3 0 ;i + + ){ i f( s e a t s [ i ]= =0 ){ s e a t s [ i ]=1 ; r e t u r ni+1 ; } } r e t u r n1 ; }

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

4/10

6/13/13
9 3 . 9 4 . 9 5 . 9 6 . 9 7 . 9 8 . 9 9 . 1 0 0 . 1 0 1 . 1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 . 1 0 7 . 1 0 8 . 1 0 9 . 1 1 0 . 1 1 1 . 1 1 2 . 1 1 3 .

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)
/ /T h i sf u n c t i o nc h e c k st os e ei fa i s l es e a t sw e r ea v a i l a b l e ,1i ff u l l . p r i v a t es t a t i cs y n c h r o n i z e di n tb o o k A i s l e ( ){ f o r( i n ti=3 0 ;i<6 0 ;i + + ){ i f( s e a t s [ i ]= =0 ){ s e a t s [ i ]=1 ; r e t u r ni+1 ; } } r e t u r n1 ; } p r i v a t es t a t i cv o i dp r i n t B o a r d i n g P a s s ( i n ts e a t N u m b e r ){ D a t et i m e n o w=n e wD a t e ( ) ; S y s t e m . o u t . p r i n t l n ( ) ; S y s t e m . o u t . p r i n t l n ( " D a t e :"+t i m e n o w . t o S t r i n g ( ) ) ; S y s t e m . o u t . p r i n t l n ( " B o a r d i n gp a s sf o rs e a tn u m b e r :"+s e a t N u m b e r ) ; S y s t e m . o u t . p r i n t l n ( ) ; } }

K. Tsang JavaRanch SC JP5 SC JD/OC M-JD

harish thiru Ranch Hand Joined: Aug 02, 2008 Posts: 32

posted 3/31/2009 9:59:14 AM

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:
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 .

S y s t e m . o u t . p r i n t l n ( " W i n d o ws e a tb o o k e d . " ) ;

executes even if window seat not available and similarly in case 2: I just added this to your code:
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 .

B u s R e s e r vb 2 =n e wB u s R e s e r v ( ) ; B u s R e s e r vb 4 =n e wB u s R e s e r v ( ) ; B u s R e s e r vb 3 =n e wB u s R e s e r v ( ) ; b 1 . s e t N a m e ( " o n e " ) ; b 1 . s t a r t ( ) ; b 2 . s e t N a m e ( " t w o " ) ; t r y {T h r e a d . s l e e p ( 1 0 0 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { } b 2 . s t a r t ( ) ; b 3 . s e t N a m e ( " t h r e e " ) ; t r y {T h r e a d . s l e e p ( 1 0 0 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { } b 3 . s t a r t ( ) ; b 4 . s e t N a m e ( " f o u r " ) ; t r y {T h r e a d . s l e e p ( 1 0 0 0 ) ; } c a t c h ( E x c e p t i o ne ) { } b 4 . s t a r t ( ) ; / /o n l y1t h r e a di su s e d }

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

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

5/10

6/13/13

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)

K. Tsang Ranch Hand Joined: Sep 13, 2007 Posts: 1254


I like...

posted 3/31/2009 4:50:24 PM

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

posted 3/31/2009 7:17:47 PM

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 Ranch Hand Joined: Sep 13, 2007 Posts: 1254


I like...

posted 3/31/2009 9:07:08 PM

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

posted 4/2/2009 8:10:24 PM

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 Ranch Hand Joined: Sep 13, 2007 Posts: 1254


I like...

posted 4/3/2009 8:54:15 AM

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.
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 .

/ /f i l e1m a i nc l a s s p a c k a g eb u s r e s e r v e ; i m p o r tj a v a . u t i l . H a s h M a p ; i m p o r tj a v a . u t i l . M a p ; p u b l i cc l a s sB u s R e s e r v e{ p r i v a t es t a t i cM a p < I n t e g e r ,S e a t >s e a t M a p=n e wH a s h M a p < I n t e g e r ,S e a t > ( ) ; p u b l i cs t a t i cM a p < I n t e g e r ,S e a t >g e t S e a t M a p ( ){ r e t u r ns e a t M a p ; } p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ f o r( i n ti = 1 ;i< =6 0 ;i + + ){ S e a ts e a t=n e wS e a t ( i ,t r u e ) ; s e a t M a p . p u t ( i ,s e a t ) ; } n e wP r o c e s s ( 1 ) . s t a r t ( ) ; n e wP r o c e s s ( 2 ) . s t a r t ( ) ; n e wP r o c e s s ( 1 ) . s t a r t ( ) ; n e wP r o c e s s ( 2 ) . s t a r t ( ) ; n e wP r o c e s s ( 1 ) . s t a r t ( ) ; n e wP r o c e s s ( 2 ) . s t a r t ( ) ; } }

/ /f i l e2t h r e a dc l a s s p a c k a g eb u s r e s e r v e ; i m p o r tj a v a . u t i l . D a t e ; i m p o r tj a v a . u t i l . M a p ; i m p o r tj a v a . u t i l . S c a n n e r ; p u b l i cc l a s sP r o c e s se x t e n d sT h r e a d{ p r i v a t ei n tm a x S e a t C o u n t ; p r i v a t eM a p < I n t e g e r ,S e a t >s e a t M a p ;

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

6/10

6/13/13
4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . 7 6 . 7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 . 9 3 . 9 4 . 9 5 . 9 6 . 9 7 . 9 8 . 9 9 . 1 0 0 . 1 0 1 . 1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 . 1 0 7 . 1 0 8 . 1 0 9 . 1 1 0 . 1 1 1 . 1 1 2 . 1 1 3 . 1 1 4 . 1 1 5 . 1 1 6 . 1 1 7 . 1 1 8 . 1 1 9 . 1 2 0 . 1 2 1 . 1 2 2 . 1 2 3 . 1 2 4 . 1 2 5 . 1 2 6 . 1 2 7 . 1 2 8 . 1 2 9 . 1 3 0 . 1 3 1 . 1 3 2 . 1 3 3 . 1 3 4 . 1 3 5 . 1 3 6 . 1 3 7 . 1 3 8 . 1 3 9 . 1 4 0 . 1 4 1 . 1 4 2 . 1 4 3 . 1 4 4 . 1 4 5 . 1 4 6 .

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)
p r i v a t ei n tc h o i c e ; p u b l i cP r o c e s s ( i n tc h o i c e ){ t h i s . c h o i c e=c h o i c e ; } p r i v a t ei n tp r o m p t F o r S e a t C h o i c e ( ){ S y s t e m . o u t . p r i n t ( " P l e a s ee n t e r1f o rw i n d o w ,"+ " 2f o ra i s l e ,o r0t oe x i t :" ) ; S c a n n e rs=n e wS c a n n e r ( S y s t e m . i n ) ; r e t u r ns . n e x t I n t ( ) ; } p r i v a t ei n td e t e r m i n e S e a t N u m b e r ( i n tc h o i c e ){ s w i t c h( c h o i c e ){ c a s e1 :{ i n ts e a t N u m b e r=b o o k W i n d o w ( ) ; i f( s e a t N u m b e r= =1 ){/ /n ow i n d o ws e a t s e a t N u m b e r=b o o k A i s l e ( ) ; i f( s e a t N u m b e r! =1 ){/ /a i s l es e a ta v a i l a b l e S y s t e m . o u t . p r i n t l n ( " N ow i n d o ws e a tb u ta i s l es e a t . " ) ; r e t u r ns e a t N u m b e r ; } e l s e{ S y s t e m . o u t . p r i n t l n ( " N ow i n d o wo ra i s l es e a t s . " ) ; r e t u r n1 ; } } S y s t e m . o u t . p r i n t l n ( " W i n d o ws e a tb o o k e d . " ) ; r e t u r ns e a t N u m b e r ; } c a s e2 :{ i n ts e a t N u m b e r=b o o k A i s l e ( ) ; i f( s e a t N u m b e r= =1 ){/ /n oa i s l es e a t s e a t N u m b e r=b o o k W i n d o w ( ) ; i f( s e a t N u m b e r! =1 ){/ /w i n d o ws e a ta v a i l a b l e S y s t e m . o u t . p r i n t l n ( " N oa i s l es e a tb u tw i n d o ws e a t . " ) ; r e t u r ns e a t N u m b e r ; } e l s e{ S y s t e m . o u t . p r i n t l n ( " N ow i n d o wo ra i s l es e a t s . " ) ; r e t u r n1 ; } } S y s t e m . o u t . p r i n t l n ( " A i s l es e a tb o o k e d . " ) ; r e t u r ns e a t N u m b e r ; } d e f a u l t :r e t u r n1 ; } } @ O v e r r i d e p u b l i cv o i dr u n ( ){ s e a t M a p=B u s R e s e r v e . g e t S e a t M a p ( ) ; w h i l e( m a x S e a t C o u n t<4 ){/ /c h e c km a x4 i n tc h o i c e=p r o m p t F o r S e a t C h o i c e ( ) ; i f( c h o i c e= =0 ){ S y s t e m . e x i t ( 0 ) ; }e l s e{ w h i l e( c h o i c e>2 ){/ /p r o m p tf o ri n p u t c h o i c e=p r o m p t F o r S e a t C h o i c e ( ) ; } i n ts e a t N u m b e r=d e t e r m i n e S e a t N u m b e r ( c h o i c e ) ; i f( s e a t N u m b e r! =1 ){ + + m a x S e a t C o u n t ;/ /i n c r e m e n tm a xc o u n t p r i n t B o a r d i n g P a s s ( s e a t N u m b e r ) ; } } } S y s t e m . o u t . p r i n t l n ( " M a x i m u m4s e a t s " ) ; } p r i v a t es y n c h r o n i z e di n tb o o k W i n d o w ( ){ i n ts e a t N u m b e r=1 ; f o r( i n ti = 1 ;i< =s e a t M a p . s i z e ( ) ;i + + ){ S e a ts=s e a t M a p . g e t ( i ) ; / /c h e c ki fe a c hs e a ti sa v a i l a b l ea n ds e a tt y p ei sw i n d o w i f( s . g e t A v a i l a b l e ( )& &s . g e t S e a t T y p e ( )= =1 ){ s . s e t A v a i l a b l e ( f a l s e ) ; s e a t N u m b e r=s . g e t S e a t N o ( ) ; b r e a k ; } } r e t u r ns e a t N u m b e r ; } p r i v a t es y n c h r o n i z e di n tb o o k A i s l e ( ){ i n ts e a t N u m b e r=1 ; f o r( i n ti = 1 ;i< =s e a t M a p . s i z e ( ) ;i + + ){ S e a ts=s e a t M a p . g e t ( i ) ; / /c h e c ki fe a c hs e a ti sa v a i l a b l ea n ds e a tt y p ei sa i s l e i f( s . g e t A v a i l a b l e ( )& &s . g e t S e a t T y p e ( )= =2 ){ s . s e t A v a i l a b l e ( f a l s e ) ; s e a t N u m b e r=s . g e t S e a t N o ( ) ; b r e a k ; } } r e t u r ns e a t N u m b e r ; }

/ / / / / / / / / / / / / /

/ /

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

7/10

6/13/13
1 4 7 . 1 4 8 . 1 4 9 . 1 5 0 . 1 5 1 . 1 5 2 . 1 5 3 . 1 5 4 . 1 5 5 . 1 5 6 . 1 5 7 . 1 5 8 . 1 5 9 . 1 6 0 . 1 6 1 . 1 6 2 . 1 6 3 . 1 6 4 . 1 6 5 . 1 6 6 . 1 6 7 . 1 6 8 . 1 6 9 . 1 7 0 . 1 7 1 . 1 7 2 . 1 7 3 . 1 7 4 . 1 7 5 . 1 7 6 . 1 7 7 . 1 7 8 . 1 7 9 . 1 8 0 . 1 8 1 . 1 8 2 . 1 8 3 . 1 8 4 . 1 8 5 . 1 8 6 . 1 8 7 . 1 8 8 . 1 8 9 . 1 9 0 . 1 9 1 . 1 9 2 . 1 9 3 . 1 9 4 . 1 9 5 . 1 9 6 . 1 9 7 . 1 9 8 . 1 9 9 . 2 0 0 . 2 0 1 . 2 0 2 . 2 0 3 . 2 0 4 . 2 0 5 . 2 0 6 . 2 0 7 . 2 0 8 . 2 0 9 .

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)
p r i v a t ev o i dp r i n t B o a r d i n g P a s s ( i n ts e a t N u m b e r ){ S y s t e m . o u t . p r i n t l n ( " \ n D a t e :"+n e wD a t e ( ) ) ; S e a ts=s e a t M a p . g e t ( s e a t N u m b e r ) ; S t r i n gt y p e=" " ; s w i t c h( s . g e t S e a t T y p e ( ) ){ c a s e1 :{ t y p e=" w i n d o w " ; b r e a k ; } c a s e2 :{ t y p e=" a i s l e " ; b r e a k ; } } S y s t e m . o u t . p r i n t l n ( " B o a r d i n gp a s sf o r"+t y p e+"s e a tn u m b e r :" +s e a t N u m b e r+" \ n " ) ; } }

/ /f i l e3s e a tc l a s s p a c k a g eb u s r e s e r v e ; p u b l i cc l a s sS e a t{ p r i v a t ei n ts e a t N o ; p r i v a t eb o o l e a na v a i l a b l e ; p r i v a t es t a t i cf i n a li n tw i n d o w S e a t=1 ; p r i v a t es t a t i cf i n a li n ta i s l e S e a t=2 ; p u b l i cS e a t ( i n ts e a t N o ,b o o l e a na v a i l a b l e ){ t h i s . s e a t N o=s e a t N o ; t h i s . a v a i l a b l e=a v a i l a b l e ; } p u b l i cv o i ds e t S e a t N o ( i n ts e a t N o ){ t h i s . s e a t N o=s e a t N o ; } p u b l i ci n tg e t S e a t N o ( ){ r e t u r ns e a t N o ; } p u b l i cv o i ds e t A v a i l a b l e ( b o o l e a na v a i l a b l e ){ t h i s . a v a i l a b l e=a v a i l a b l e ; } p u b l i cb o o l e a ng e t A v a i l a b l e ( ){ r e t u r na v a i l a b l e ; } p u b l i ci n tg e t S e a t T y p e ( ){ i f( s e a t N o % 2= =0 ){ r e t u r na i s l e S e a t ; } e l s e{ r e t u r nw i n d o w S e a t ; } } }

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.

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

8/10

6/13/13

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)
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

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

9/10

6/13/13
Maximum 4 seats

Booking bus tickets with each member only max of 4 seats (Threads forum at JavaRanch)

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 SUC C ESSFUL (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

posted 4/3/2009 7:27:12 PM

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.

subject: Booking bus tickets with each member only max of 4 seats

Similar Threads Write a method that can be called, that will initialize the seating plan. Some do's & don'ts print invalid selction not working help with program Need some help with Java program Airline.java
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 13, 2013 03:29:55 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/438172/threads/java/Booking-bus-tickets-member-max

10/10

Você também pode gostar