Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Theatre Seating- program compiles but doesn't work the way it is supposed to

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Theatre Seating- program compiles but doesn't work the way it is supposed to

    This code for my class is supposed to use a 2D array to:
    -display an array of seats and ticket prices
    -prompt the user to enter a seat number or price
    -mark sold seats with the 0
    -determine if a user specified seat is available
    -when a user enters a price, show all seats of that price

    I wrote most the code and as far as I can tell it should work fine. It compiles but when I run it:

    If user chooses to enter the row and seat numbers:
    -seat selection is off lower left hand corner is not 1,1 (how do I set that up?)
    -mix of symbols, numbers, and characters for displayed seat number and row
    -takes user back to the menu whether they enter y or n

    If user chooses to enter the price:
    -same 3 problems as above
    -when i enter the desired price no seats or rows come up
    -if i continue anyways and enter the seat number and row number (1,1) it says the seat is unavailable, otherwise it says the seats are available

    We only just learned how to use 2D arrays today and this is due Friday. Any help will be greatly appreciated. (Please try to be as simple as possible I'm new to Java )


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Theatre Seating- program compiles but doesn't work the way it is supposed to

    import java.util.Scanner;
     
    public class TheatreSeating {
    	public static void main(String[] args) {
     
    //displays the seating including which seats are avalaible
     
    		int[][] seatingPrices = {{10,10,10,10,10,10,10,10,10,10},
    								 {10,10,10,10,10,10,10,10,10,10},
    								 {10,10,10,10,10,10,10,10,10,10},
    								 {10,10,20,20,20,20,20,20,10,10},
    								 {10,10,20,20,20,20,20,20,10,10},
    								 {10,10,20,20,20,20,20,20,10,10},
    								 {20,20,30,30,40,40,30,30,20,20},
    								 {20,30,30,40,50,50,40,30,30,20},
    								 {30,40,50,50,50,50,50,50,40,30}};
     
    		for(int row=0; row<seatingPrices.length; row++) {
    					for(int col = 0; col<seatingPrices[row].length; col++){
    						System.out.print(seatingPrices[row][col] + " ");
     
    					}
    					System.out.println();
    					System.out.println();
    		}
     
    		System.out.println("The seating and corresponding prices are show above. Seats with the price of $0 are not available.");
     
     
    	menu(seatingPrices);
     
     
    }
    //shows menu and runs through program
    	public static void menu(int[][]seatingPrices) {
     
    		Scanner sc = new Scanner(System.in);
    		//creates user entered row, seat, and price variables for selection
    		int row = 0;
    		int seat = 0;
    		int price = 0;
    		int choice;
     
    		do {       //do while loop runs menu until user enters designated number
     
    				System.out.print("1: Enter Desired Row and Seat Number\n2: Enter Desired Price\n3: Finish\n");
    				choice = sc.nextInt();
     
    				switch(choice) {
    					case 1:
    						//get user selection for row and seat number
    						System.out.println("Please make your selection (the bottom left corner is row 1, seat 1). Enter the row number.\n");
    						row = sc.nextInt();
    						System.out.println();
    						System.out.println("Enter the seat number");
    						seat = sc.nextInt();
     
    						boolean sA = seatAvailable(seatingPrices, row, seat);   //calls seatAvailable method
    						if(sA == true) {
    							boolean toPur = purchase(seatingPrices, row, seat); //calls purchase method
    							if(toPur = true) {
    								seatingPrices = changeAvailability(seatingPrices, row, seat); //calls changeAvailability method
    								String purchaseTotal = totalPrice(seatingPrices, row, seat);  //calls purchaseTotal method
    							}
    							else {
    								menu(seatingPrices);
    							}
     
    						}
    						else {
    							System.out.println("This seat is not available");
    							menu(seatingPrices);
    						}
     
    						break;
    					case 2:
    						//gets user desired price
    						System.out.println("Enter the desired price");
    						price = sc.nextInt();
     
    						showAvailableSeatsByPrice(seatingPrices, price);  //calls showAvailableSeatsByPrice method
     
    						System.out.println("Enter your row and seat selections");
    						row = sc.nextInt();
    						seat = sc.nextInt();
     
    						sA = seatAvailable(seatingPrices, row, seat);
    						if(sA == true) {
    							boolean toPur = purchase(seatingPrices, row, seat);
    							if(toPur = true) {
    							seatingPrices = changeAvailability(seatingPrices, row, seat);
    							String purchaseTotal = totalPrice(seatingPrices, row, seat);
    						}
    						else {
    							menu(seatingPrices);
    						}
    					}
    					else {
    						System.out.println("This seat is not available\n");
    						menu(seatingPrices);
    					}
     
    						break;
    					}
    			}while(choice != 3);
    			System.exit(0);
    		}
     
    		/*The seatAvailable method determines if a seat is available or not based on price $0=unavailable
    			@param seatingPrices holds the seatingPrices array
    			@param r holds the selected row number
    			@param s holds the selected seat number
    		*/
    		public static boolean seatAvailable(int[][] seatingPrices, int r, int s) {
    			//determines if seat selected by user is available
     
    			if(seatingPrices[r][s] != 0) {
    				return true;
    			}
    			else {
    				return false;
    			}
    		}
     
    		/*The showAvailableSeatsByPrice method shows seats that are the price the user entered
    			@param seatingPrices holds the seatingPrices array
    			@param p holds the price entered by the user
    			@return seats of user entered price
    		*/
    		public static String showAvailableSeatsByPrice(int[][] seatingPrices, int p) {
    			//show seats that are user-entered price
    			String sASBP= " ";
    			boolean status;
     
    			System.out.println("Seats that match price:\n");
    			for(int row=0; row<seatingPrices.length; row++) {    //searches array for matching prices
    				for(int seat = 0; seat<seatingPrices[row].length; seat++){
    					if(seatingPrices[row][seat] == p) {
    						status = true;
    					}
    					else {
    						status = false;
    					}
    					if(status == true) {
    						sASBP = "Available seat of price: " + seatingPrices[row][seat] + " (Row and Seat respectively)";
    					}
    				}
    			}
    			return sASBP;
    		}
     
    		/*The totalPrice method shows the user how much they purchased their ticket for
    			@param seatingPrices holds the seatingPrices array
    			@param r holds the row entered by the user
    			@param s holds the seat entered by the user
    			@return total price of user's ticket
    		*/
    		public static String totalPrice(int[][] seatingPrices, int r, int s) {
    			int p = seatingPrices[r][s];
    			String totalPrice = " Your purchase amount is " + p;
    			return totalPrice;
    		}
     
    		/*The changeAvailability method changes the array to reflect new availability data
    			@param seatingPrices holds the seatingPrices array
    			@param r holds the row entered by the user
    			@param s holds the seat entered by the user
    			@return changed array for availability
    		*/
    		public static int[][] changeAvailability(int[][] seatingPrices, int r, int s) {
    			//changes seat availabilty if the user purchases the ticket selection
    			seatingPrices[r][s] = 0;
    			return seatingPrices;
    		}
     
    		/*The purchase method determines if a user wants to buy the selected ticket
    			@param seatingPrices holds the seatingPrices array
    			@param r holds the row entered by the user
    			@param s holds the seat entered by the user
    			@return user's decision to buy the ticket or not
    		*/
    		public static boolean purchase(int[][]seatingPrices, int r, int s){
    			//determines if user wants to purchase the selected seat
    			Scanner sc = new Scanner(System.in);
     
    			String purC;
    			boolean stat = true;
     
    			System.out.println("Would you like to purchase seat " + seatingPrices[s] + " in row " + seatingPrices[r] + " for $" + seatingPrices[r][s] + "?");
    			System.out.println("Enter Y for yes or N for no");
    			purC = sc.next();
     
    			if(purC.equalsIgnoreCase("y")) {
    				stat = true;
    			}
    			else if(purC.equalsIgnoreCase("n")) {
    				stat = false;
    			}
    			return stat;
    		}
    	}

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Theatre Seating- program compiles but doesn't work the way it is supposed to

    The seat selection problem is probably occuring since Arrays start at 0,1,2,3..ect so the lower left hand corner is probably row 0 column 0 as for your other problems I am a beginner myself and i dont know what to do either. I actually have this same problem coming up in my book. so if you get it correct please notify me somehow.

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Theatre Seating- program compiles but doesn't work the way it is supposed to

    As Richmond said, arrays start a 0, so to compensate just subtract 1 from your row and seat if the user inputs a number corresponding to 1, 1 in top left corner.

  5. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Theatre Seating- program compiles but doesn't work the way it is supposed to

    1: As Richmond said, arrays start a 0, so to compensate just subtract 1 from your row and seat if the user inputs a number corresponding to 1, 1 in top left corner.
    2: seatingPrices[0] will show you the default toString method of Object, because it is not an int, but rather an array.
    3: At which part?
    4: Never do boolean = true, just do boolean. EG instead of
    if(status == true)
    {
      //Do stuff
    }
    do
    if(status)
    {
      //Do stuff
    }
    Last edited by Tjstretch; November 25th, 2011 at 11:38 AM.

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Theatre Seating- program compiles but doesn't work the way it is supposed to

    Wow I just noticed that this was posted in Febuary >.>

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Theatre Seating- program compiles but doesn't work the way it is supposed to

    Quote Originally Posted by Tjstretch View Post
    Wow I just noticed that this was posted in Febuary >.>
    Lol I had no idea either, i just noticed that I have this exact problem coming up so i decided to try and see what it looked like

Similar Threads

  1. I dont see why this program is not doing what it is supposed to
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2011, 08:24 AM
  2. File IO Compiles, but wont work?
    By StarKannon in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 2nd, 2011, 09:05 AM
  3. [SOLVED] New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!
    By KevinE in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2010, 03:51 PM
  4. making a .bat file that compiles your java files
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2010, 12:55 AM
  5. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM

Tags for this Thread