Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Name: Two-Dimensional Arrays - Theater Seating Selling System
- * @author LinChuWen
- * Date: 2014.10.28
- *
- * NCHU EE,course number:2335
- * course name: Object Oriented Language
- * Textbook: Big Java:Late Objects-Cay S. Horstmann
- * Problem: P6.18
- *
- * Description: A Theater Seating Selling System
- */
- import java.util.*;
- public class HW5_P6_24_ {
- private static Scanner input = new Scanner(System.in);
- private static int[][] seatingChart = new int[10][11];
- public static void main(String[] args){
- buildDefaultSeatingChart();
- while(true){
- askForEnter();
- printSeatingChart(0);
- askForBuy();
- } //big while end
- } //main end
- private static void askForBuy(){
- System.out.print("Pick a seat or a price? (enter seat/price)");
- char reply = input.nextLine().charAt(0);
- if(reply == 's' || reply == 'S')
- buySeat();
- else
- buyPrice();
- } //askForBuy() end
- private static void buySeat(){
- System.out.print("Which row do you want? (enter integer)");
- int row = input.nextInt();
- System.out.print("Which column do you want? (enter integer)");
- int column = input.nextInt();
- int price = seatingChart[row][column];
- if(price != 0){
- seatingChart[row][column] = 0;
- System.out.printf("You should pay %d dollars\n", price);
- } else{ // price = 0, means the seat have been sold
- System.out.println("Sorry, this seat is not available.");
- } //if-else end
- } //buySeat() end
- private static void buyPrice(){
- System.out.print("Which price do you want to pay? (enter integer)");
- printSeatingChart(input.nextInt());
- buySeat();
- } //buyPrice() end
- private static void printSeatingChart( int state ){
- int cnt1, cnt2;
- System.out.println(" row|");
- for(cnt1=0 ; cnt1<9 ; cnt1++){
- System.out.printf("%6d|", cnt1);
- for(cnt2=0 ; cnt2<10 ; cnt2++){
- if(state == 0)
- System.out.printf("%3d", seatingChart[cnt1][cnt2]);
- else
- System.out.printf("%3d", seatingChart[cnt1][cnt2]==state?state:1);
- } //small for end
- System.out.printf("\n");
- } //big for end
- System.out.println("-------------------------------------");
- System.out.print("column|");
- for(cnt2=0;cnt2<10;cnt2++)
- System.out.printf("%3d", cnt2);
- System.out.printf("\n");
- } //printSeatingChart() end
- private static void askForEnter(){
- System.out.print("Enter the System? (enter y/n) ");
- String reply;
- while ((reply= input.nextLine()).equals("")){}
- char ans = reply.charAt(0);
- if(ans == 'n' || ans == 'N'){
- System.out.println("Thank You!");
- System.exit(0);
- } else if(ans == 'y' || ans == 'Y'){ //enter the system
- System.out.println("Welcome to Go-Ice's Theater!");
- System.out.println("Here are our seating chart and the ticket prices");
- System.out.println("(note \"0\" means \"not available\")\n");
- } //if-else if end
- } //askForEnter() end
- private static void buildDefaultSeatingChart(){
- int[][] line_6to8 = {{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}};
- int cnt,index=6;
- for(cnt=0;cnt<3;cnt++)
- Arrays.fill(seatingChart[cnt], 10);
- for(cnt=3;cnt<6;cnt++)
- Arrays.fill(seatingChart[cnt], 20);
- for(int cnt1=0 ; cnt1<3 ; cnt1++){
- for(int cnt2=0 ; cnt2<10 ; cnt2++){
- seatingChart[index][cnt2] = line_6to8[cnt1][cnt2];
- } //small for end
- index++;
- } //big for end
- } //buildDefaultSeatingChart() end
- } //class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement