Notice
Recent Posts
Recent Comments
Link
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags
more
Archives
Today
Total
관리 메뉴

archive

[7주차 실습] 4월 13일 Problem Set 본문

Java/23_1 소프트웨어프로젝트

[7주차 실습] 4월 13일 Problem Set

안정민 2023. 4. 28. 02:34

1. Develop a simple program of reservation system for a concert. The specification of the system is as follows.

 

• There are three seat types: S type, A type, B type.

• The number of seats for each row is fixed to be 16.

• There are four menu for reservation system: Reserve, Search, Cancel, Quit

• For Reserve menu, only one seat can be reserved for each reservation. The program should receive the seat type, the name, and the seat number.

• For Search menu, all seat statuses are printed.

• For Cancel menu, the program should receive the name and cancel the reservation. You need to print the error message for the name that is not reserved.

• If users write invalid menu or invalid seat type or invalid seat number, you need to print the error message.

• You may not modify the attached frame code.

package April13;

public class Seat {
	
	private String name; 

	public void setName(String name) {
		this.name=name;
	}

	public void show() {
		if (name == null) System.out.print("---");
		else System.out.print(name);
      
	}

	public boolean isMatched(String checkName) {
		if (name == null) 
			return false;
		else 
			return name.equals(checkName);
	}

	public void cancel() {
		name = null;
	}	

}
package April13;

public class SeatArray {
	private char seatType;
	private Seat[] seats;
	private static int seatLength = 16;
	
	public SeatArray(char seatType) {
		this.seatType=seatType;
		seats=new Seat[16];
		for (int i = 0; i<seatLength; i++) {
            seats[i] = new Seat();
        }
	}
	
	public void reserve(int seatNumber, String name) {
		
        if(seatNumber < 1 || seatNumber > seatLength){
            System.out.println("You inserted the wrong seat number.");
        }
        seats[seatNumber-1].setName(name);
	}
	
	public void show() {
		System.out.print(seatType + " >> ");
		for ( int i=0; i<seatLength; i++){
            seats[i].show();
        }
        System.out.println();
	}
	
	public void cancel(String name) {
		for (int i = 0; i < seatLength; i++) {
			if (seats[i].isMatched(name)) {
				seats[i].cancel();
				return;
			}
		}
		System.out.println(name + " did not reserve any seat.");
		
     }

}
package April13;
import java.util.*;

public class Reservation {
	private SeatArray[] seatArrays;
	private char[] seatTypes = {'S', 'A', 'B'};
	private Scanner scanner;
	
	
	public Reservation() {
		seatArrays = new SeatArray[seatTypes.length];
		for (int i = 0; i<seatTypes.length; i++) {
			seatArrays[i]= new SeatArray(seatTypes[i]);
        }
	}
	
	private void reserve() {
		scanner=new Scanner(System.in);
		System.out.print("Seat Type S(1), A(2), B(3) >> ");
		int stype=scanner.nextInt();
		seatArrays[stype-1].show(); 
		System.out.print("Name >> ");
		String name=scanner.next();
		System.out.print("Number >> ");
		int num=scanner.nextInt();
		
		seatArrays[stype-1].reserve(num, name);
        	
	}
	
	private void search() {
		for (int i=0; i<seatTypes.length; i++) {
			seatArrays[i].show();
				
        }
        System.out.println("This is end of the search.");
       
	}
	
	private void cancel() {
		scanner=new Scanner(System.in);
		System.out.print("Seat Types S(1), A(2), B(3) >> ");
		int stype=scanner.nextInt();
		if(stype < 1 || stype > 3) {
            System.out.println("Wrong seat type!");
        }
		seatArrays[stype-1].show();
		System.out.print("Name >> ");
		String inputname=scanner.next();
		seatArrays[stype-1].cancel(inputname);
    }
	
	private void quit() {
		System.out.println("Thank you!");
	}
	
	public void run() {
		// to do
		scanner= new Scanner(System.in);
		System.out.print("This is a reservation system for music concert \n\n");
		while(true) {
			System.out.print("Reserve: 1, Search: 2, Cancel:3, Quit: 4 >>> ");
			int number=scanner.nextInt();
			if(number==1)
				reserve();
			else if(number==2)
				search();
			else if(number==3)
				cancel();
			else if(number==4) {
				quit();
				break;
			}
			else
				System.out.println("Not a valid number");
		}
			
	}
}

 

package April13;
import java.util.*;

public class ReservationExample {
		public static void main(String[] args) {
			Reservation reservation = new Reservation();
			reservation.run();
		}
}

실행결과