Advertisement
Go-Ice

Sophomore Java Homework-P6.24

Oct 28th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. /**
  2.  * Name: Two-Dimensional Arrays - Theater Seating Selling System
  3.  * @author LinChuWen
  4.  * Date: 2014.10.28
  5.  *
  6.  * NCHU EE,course number:2335
  7.  * course name: Object Oriented Language
  8.  * Textbook: Big Java:Late Objects-Cay S. Horstmann
  9.  * Problem: P6.18
  10.  *
  11.  * Description: A Theater Seating Selling System
  12.  */
  13. import java.util.*;
  14. public class HW5_P6_24_ {
  15.     private static Scanner input = new Scanner(System.in);
  16.     private static int[][] seatingChart = new int[10][11];
  17.    
  18.     public static void main(String[] args){
  19.         buildDefaultSeatingChart();
  20.         while(true){
  21.             askForEnter();
  22.             printSeatingChart(0);
  23.             askForBuy();
  24.         } //big while end
  25.     } //main end
  26.    
  27.     private static void askForBuy(){
  28.         System.out.print("Pick a seat or a price? (enter seat/price)");
  29.         char reply = input.nextLine().charAt(0);
  30.        
  31.         if(reply == 's' || reply == 'S')
  32.             buySeat();
  33.         else
  34.             buyPrice();
  35.        
  36.     } //askForBuy() end
  37.    
  38.     private static void buySeat(){
  39.         System.out.print("Which row do you want? (enter integer)");
  40.         int row = input.nextInt();
  41.         System.out.print("Which column do you want? (enter integer)");
  42.         int column = input.nextInt();
  43.         int price = seatingChart[row][column];
  44.        
  45.         if(price != 0){
  46.             seatingChart[row][column] = 0;
  47.             System.out.printf("You should pay %d dollars\n", price);
  48.         } else{ // price = 0, means the seat have been sold
  49.             System.out.println("Sorry, this seat is not available.");
  50.         } //if-else end
  51.     } //buySeat() end
  52.    
  53.     private static void buyPrice(){
  54.         System.out.print("Which price do you want to pay? (enter integer)");
  55.         printSeatingChart(input.nextInt());
  56.         buySeat();
  57.     } //buyPrice() end
  58.    
  59.     private static void printSeatingChart( int state ){
  60.         int cnt1, cnt2;
  61.         System.out.println("   row|");
  62.        
  63.         for(cnt1=0 ; cnt1<9 ; cnt1++){
  64.             System.out.printf("%6d|", cnt1);
  65.            
  66.             for(cnt2=0 ; cnt2<10 ; cnt2++){
  67.                 if(state == 0)
  68.                     System.out.printf("%3d", seatingChart[cnt1][cnt2]);
  69.                 else
  70.                     System.out.printf("%3d", seatingChart[cnt1][cnt2]==state?state:1);
  71.             } //small for end
  72.            
  73.             System.out.printf("\n");
  74.         } //big for end
  75.         System.out.println("-------------------------------------");
  76.         System.out.print("column|");
  77.         for(cnt2=0;cnt2<10;cnt2++)
  78.             System.out.printf("%3d", cnt2);
  79.         System.out.printf("\n");
  80.     } //printSeatingChart() end
  81.    
  82.     private static void askForEnter(){
  83.         System.out.print("Enter the System? (enter y/n) ");
  84.         String reply;
  85.         while ((reply= input.nextLine()).equals("")){}
  86.         char ans = reply.charAt(0);
  87.         if(ans == 'n' || ans == 'N'){
  88.             System.out.println("Thank You!");
  89.             System.exit(0);
  90.         } else if(ans == 'y' || ans == 'Y'){    //enter the system
  91.             System.out.println("Welcome to Go-Ice's Theater!");
  92.             System.out.println("Here are our seating chart and the ticket prices");
  93.             System.out.println("(note \"0\" means \"not available\")\n");
  94.         } //if-else if end
  95.     } //askForEnter() end
  96.    
  97.     private static void buildDefaultSeatingChart(){
  98.         int[][] line_6to8 = {{20,20,30,30,40,40,30,30,20,20},
  99.                      {20,30,30,40,50,50,40,30,30,20},
  100.                      {30,40,50,50,50,50,50,50,40,30}};
  101.         int cnt,index=6;
  102.         for(cnt=0;cnt<3;cnt++)
  103.             Arrays.fill(seatingChart[cnt], 10);
  104.         for(cnt=3;cnt<6;cnt++)
  105.             Arrays.fill(seatingChart[cnt], 20);
  106.         for(int cnt1=0 ; cnt1<3 ; cnt1++){
  107.             for(int cnt2=0 ; cnt2<10 ; cnt2++){
  108.                 seatingChart[index][cnt2] = line_6to8[cnt1][cnt2];
  109.             } //small for end
  110.             index++;
  111.         } //big for end
  112.     } //buildDefaultSeatingChart() end
  113.    
  114. } //class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement