java 공부 - 간단한 예약 시스템 만들기
2022. 3. 18. 10:14ㆍJAVA
728x90
package game;
import java.util.Arrays;
import java.util.Scanner;
class Seat{
private static int seatTypeCount;
private int seatCount;
private char seatTypeName;
private int SeatInquireType=0; // 좌석이 1이면 사람 있는 거 0이면 사람 없는 거
private String seatPerson;
public String getSeatPerson() {
return seatPerson;
}
public void setSeatPerson(String seatPerson) {
this.seatPerson = seatPerson;
}
public int getSeatInquireType() {
return SeatInquireType;
}
public void setSeatInquireType(int seatInquireType) {
SeatInquireType = seatInquireType;
}
public int getSeatTypeCount() {
return seatTypeCount;
}
public void setSeatTypeCount(int seatTypeCount) {
this.seatTypeCount = seatTypeCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public char getSeatTypeName() {
return seatTypeName;
}
public void setSeatTypeName(char seatTypeName) {
this.seatTypeName = seatTypeName;
}
public static Seat[][] seatObject() {
Seat[][] seat =new Seat[3][10];
for(int i=0;i<10;i++) {
seat[0][i] = new Seat('S',i);
seat[1][i] = new Seat('A',i);
seat[2][i] = new Seat('B',i);
}
return seat;
}
public Seat(char seatTypeName, int seatCount) {
this.seatTypeName = seatTypeName;
this.seatCount = seatCount;
}
public boolean whetherSeatInquire(char seatTypeName, int seatCount) {
this.seatTypeName = seatTypeName;
this.seatCount = seatCount;
if(seatTypeName == 'S') {
seatTypeCount = 0;
}
else if(seatTypeName == 'A'){
seatTypeCount = 1;
}else if(seatTypeName == 'B') {
seatTypeCount = 2;
}
Seat[][] seat =Seat.seatObject();
if(seat[seatTypeCount][seatCount].SeatInquireType == 0) {
return true;
}else {
return false;
}
}
public int seatInquire(char seatTypeName, int seatCount) {
//좌석 조회
this.seatTypeName = seatTypeName;
this.seatCount = seatCount;
Seat[][] seat =Seat.seatObject();
if(whetherSeatInquire(seatTypeName,seatCount)) {// 좌석이 비었다면
return 0;
}else {// 좌석이 차있다면
return 1;
}
}
public void seatBook(char seatTypeName, int seatCount, String name) {
seatPerson = name;
this.seatTypeName = seatTypeName;
this.seatCount = seatCount;
if(whetherSeatInquire(seatTypeName,seatCount)) {// 자리가 비었다면
Seat[][] seat =Seat.seatObject();
if(seatTypeName == 'S') {
seatTypeCount = 0;
}
else if(seatTypeName == 'A'){
seatTypeCount = 1;
}else if(seatTypeName == 'B') {
seatTypeCount = 2;
}
seat[seatTypeCount][seatCount].SeatInquireType = 1;
}else {// 자리 사용 못한다면
System.out.println("이용불가능한 좌석입니다.");
}
}
public static void seatCancel(Person p) {
Person person = p;
Seat[][] seat =Seat.seatObject();
if(person.inquiredseatType == 'S') {
seatTypeCount = 0;
}
else if(person.inquiredseatType == 'A'){
seatTypeCount = 1;
}else if(person.inquiredseatType == 'B') {
seatTypeCount = 2;
}
seat[seatTypeCount][person.inquiredseatNumber].SeatInquireType = 0;
}
}
class Person {
String name;
char inquiredseatType;
int inquiredseatNumber;
int inquiredPerson = 0;
}
public class Book {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Seat[][] seat =Seat.seatObject();
System.out.println("대선 예약 시스템입니다.");
Person person = new Person();
System.out.println("고객님의 성함을 입력해주세요: ");
person.name = scan.next();
while(true) {
System.out.println("좌석 조회 -> 1, 좌석 예약 -> 2, 좌석 취소 -> 3, 프로그램 종료 ->4 ");
int choice = scan.nextInt();
switch(choice) {
case 1:
System.out.println("좌석 조회 합니다.");
System.out.print("조회할 좌석 타입을 입력해주세요( S타입 좌석 -> 1, A타입 좌석 ->2, B타입 좌석 -> 3: ");
int choiceTypeNumber = scan.nextInt();
switch(choiceTypeNumber) {
case 1:
for(int i=0;i<10;i++) {
System.out.println("이용가능한 좌석 : 0, 이용불가능한 좌석 : 1로 표시됩니다.");
System.out.println("S좌석" + i + "석 : " + seat[0][i].seatInquire('S',i));
}
break;
case 2:
for(int i=0;i<10;i++) {
System.out.println("이용가능한 좌석 : 0, 이용불가능한 좌석 : 1로 표시됩니다.");
System.out.println("A좌석" + i + "석 : " + seat[1][i].seatInquire('A',i));
}
break;
case 3:
for(int i=0;i<10;i++) {
System.out.println("이용가능한 좌석 : 0, 이용불가능한 좌석 : 1로 표시됩니다.");
System.out.println("B좌석" + i + "석 : " + seat[2][i].seatInquire('B',i));
}
break;
}
break;
case 2:
System.out.println("좌석 예약 합니다.");
System.out.print("선택하려는 좌석 타입을 입력해주세요: ");
System.out.print("선택하려는 좌석 타입을 입력해주세요( S타입 좌석 -> 1, A타입 좌석 ->2, B타입 좌석 -> 3: ");
int choiceTypeNumber1 = scan.nextInt();
System.out.println("선택하려는 좌석 번호를 입력해주세요 ( 0 ~ 9)");
int choiceSeatNumber = scan.nextInt();
switch(choiceTypeNumber1) {
case 1:
System.out.println("S타입 좌석 " + choiceSeatNumber +" 예약하겠습니다.");
seat[0][choiceSeatNumber].seatBook('S',choiceSeatNumber,person.name);
person.inquiredseatType = 'S';
person.inquiredseatNumber = choiceSeatNumber;
person.inquiredPerson =1;
break;
case 2:
System.out.println("A타입 좌석 " + choiceSeatNumber +"예약하겠습니다.");
seat[1][choiceSeatNumber].seatBook('A',choiceSeatNumber,person.name);
person.inquiredseatType = 'A';
person.inquiredseatNumber = choiceSeatNumber;
person.inquiredPerson =1;
break;
case 3:
System.out.println("B타입 좌석 " + choiceSeatNumber +"예약하겠습니다.");
seat[2][choiceSeatNumber].seatBook('B',choiceSeatNumber,person.name);
person.inquiredseatType = 'B';
person.inquiredseatNumber = choiceSeatNumber;
person.inquiredPerson =1;
break;
}
break;
case 3:
if(person.inquiredPerson == 1) {
System.out.println("좌석 취소합니다.");
Seat.seatCancel(person);
}
else {
System.out.println("최소할 좌석이 없습니다.");
}
break;
case 4:
System.out.println("프로그램을 종료합니다.");
System.exit(0);
break;
}
}
}
}
여기서 제 치명적 실수 : seatObject() 메소드를 만든 것, 애초에 객체 생성 메소드를 만들면 안됬다.
왜냐하면, 객체 생성 메소드를 사용해서 다른 메소드에서 객체를 만들어버리면 그 객체들의 인스턴스 필드는 서로 공유가 안된다. 그렇다고 Seat 클라스의 필드들을 static 필드로 만들어버리면 그것 그대로 문제가 된다.
따라서, 객체 하나를 만들고 그 객체를 활용하도록 만들어야한다.
첫 번째 생각 : 매개변수에 객체를.. -> 메소드를 만드는 거랑 같은 원리로 안된다.
두 번째 생각 : this를 사용 !!
this의 기초 개념
this는 자바의 중요한 키워드로서 단어 뜻 그대로 객체 자신을 가리키는 레퍼런스이다.
this는 현재 객체 자신에 대한 레퍼런스이다.
보다 정확히 말하면 현재 실행되고 있는 메소드가 속한 객체에 대한 레퍼런스이다.
this의 개념에대해 정확히 알고 있다면 코드를 수정할 수 있다!
package game;
import java.util.InputMismatchException;
import java.util.Scanner;
class Seat{
int seatTypeCount;
int seatCount;
char seatTypeName;
int SeatInquireType=0; // 좌석이 1이면 사람 있는 거 0이면 사람 없는 거
String seatPerson;
public Seat(char seatTypeName, int seatCount) {
this.seatTypeName = seatTypeName;
this.seatCount = seatCount;
}
public boolean whetherSeatInquire(char seatTypeName, int seatCount) {
this.seatTypeName = seatTypeName;
this.seatCount = seatCount;
if(seatTypeName == 'S') {
seatTypeCount = 0;
}
else if(seatTypeName == 'A'){
seatTypeCount = 1;
}else if(seatTypeName == 'B') {
seatTypeCount = 2;
}
if(this.SeatInquireType == 0) {
return true;
}else {
return false;
}
}
public int seatInquire(char seatTypeName, int seatCount) {
//좌석 조회
this.seatTypeName = seatTypeName;
this.seatCount = seatCount;
if(whetherSeatInquire(seatTypeName,seatCount)) {// 좌석이 비었다면
return 0;
}else {// 좌석이 차있다면
return 1;
}
}
public void seatBook(char seatTypeName, int seatCount, String name) {
seatPerson = name;
this.seatTypeName = seatTypeName;
this.seatCount = seatCount;
if(whetherSeatInquire(seatTypeName,seatCount)) {// 자리가 비었다면
// 변수가 지역 변수라서 이름이 다 seat으로 같게 쓸 수 있는 거지, 메소드마다 생성한 게 다른 객체이다. 그렇다면 static으로
if(seatTypeName == 'S') {
seatTypeCount = 0;
}
else if(seatTypeName == 'A'){
seatTypeCount = 1;
}else if(seatTypeName == 'B') {
seatTypeCount = 2;
}
this.SeatInquireType = 1;
}else {// 자리 사용 못한다면
System.out.println("이용불가능한 좌석입니다.");
}
}
public void seatCancel() {
this.SeatInquireType = 0;
}
}
class Person {
String name;
char inquiredseatType;
int inquiredseatNumber;
int inquiredPerson = 0;
}
public class Book {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Seat[][] seat =new Seat[3][10];
for(int i=0;i<10;i++) {
seat[0][i] = new Seat('S',i);
seat[1][i] = new Seat('A',i);
seat[2][i] = new Seat('B',i);
}
System.out.println("대선 예약 시스템입니다.");
Person person = new Person();
System.out.println("고객님의 성함을 입력해주세요: ");
person.name = scan.next();
while(true) {
try {
System.out.println("좌석 조회 -> 1, 좌석 예약 -> 2, 좌석 취소 -> 3, 프로그램 종료 ->4 ");
int choice = scan.nextInt();
switch(choice) {
case 1:
System.out.println("좌석 조회 합니다.");
System.out.print("조회할 좌석 타입을 입력해주세요( S타입 좌석 -> 1, A타입 좌석 ->2, B타입 좌석 -> 3: ");
try {
int choiceTypeNumber = scan.nextInt();
switch(choiceTypeNumber) {
case 1:
for(int i=0;i<10;i++) {
System.out.println("이용가능한 좌석 : 0, 이용불가능한 좌석 : 1로 표시됩니다.");
System.out.println("S좌석" + i + "석 : " + seat[0][i].seatInquire('S',i));
}
break;
case 2:
for(int i=0;i<10;i++) {
System.out.println("이용가능한 좌석 : 0, 이용불가능한 좌석 : 1로 표시됩니다.");
System.out.println("A좌석" + i + "석 : " + seat[1][i].seatInquire('A',i));
}
break;
case 3:
for(int i=0;i<10;i++) {
System.out.println("이용가능한 좌석 : 0, 이용불가능한 좌석 : 1로 표시됩니다.");
System.out.println("B좌석" + i + "석 : " + seat[2][i].seatInquire('B',i));
}
break;
}
}
catch(IllegalStateException e) {
System.out.println("1,2,3 중 하나를 입력해주세요!");
}
break;
case 2:
System.out.println("좌석 예약 합니다.");
System.out.print("선택하려는 좌석 타입을 입력해주세요: ");
System.out.print("선택하려는 좌석 타입을 입력해주세요( S타입 좌석 -> 1, A타입 좌석 ->2, B타입 좌석 -> 3: ");
try {
int choiceTypeNumber1 = scan.nextInt();
System.out.println("선택하려는 좌석 번호를 입력해주세요 ( 0 ~ 9)");
int choiceSeatNumber = scan.nextInt();
switch(choiceTypeNumber1) {
case 1:
System.out.println("S타입 좌석 " + choiceSeatNumber +" 예약하겠습니다.");
seat[0][choiceSeatNumber].seatBook('S',choiceSeatNumber,person.name);
person.inquiredseatType = 'S';
person.inquiredseatNumber = choiceSeatNumber;
person.inquiredPerson =1;
break;
case 2:
System.out.println("A타입 좌석 " + choiceSeatNumber +"예약하겠습니다.");
seat[1][choiceSeatNumber].seatBook('A',choiceSeatNumber,person.name);
person.inquiredseatType = 'A';
person.inquiredseatNumber = choiceSeatNumber;
person.inquiredPerson =1;
break;
case 3:
System.out.println("B타입 좌석 " + choiceSeatNumber +"예약하겠습니다.");
seat[2][choiceSeatNumber].seatBook('B',choiceSeatNumber,person.name);
person.inquiredseatType = 'B';
person.inquiredseatNumber = choiceSeatNumber;
person.inquiredPerson =1;
break;
}
}
catch(IllegalStateException e) {
System.out.println("1,2,3 중 하나를 입력해주세요!");
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("0,1,2,3,4,5,6,7,8,9 중에 하나를 입력해주세요!");
}
break;
case 3:
if(person.inquiredPerson == 1) {
int inquiredSeatTypeNumber = 0;
System.out.println("좌석 취소합니다.");
if(person.inquiredseatType == 'S') {
inquiredSeatTypeNumber = 0;
}
else if(person.inquiredseatType == 'A'){
inquiredSeatTypeNumber = 1;
}else if(person.inquiredseatType == 'B') {
inquiredSeatTypeNumber = 2;
}
seat[inquiredSeatTypeNumber][person.inquiredseatNumber].seatCancel();
}
else {
System.out.println("최소할 좌석이 없습니다.");
}
break;
case 4:
System.out.println("프로그램을 종료합니다.");
System.exit(0);
break;
}
}
catch(IllegalStateException e) {
System.out.println("1,2,3,4 중 하나를 입력해주세요!");
}
}
}
}
위에서 말한 바와같이 수정한 후에 try-catch문을 사용해서 예외처리를 해보았다.
이상입니다.
728x90
'JAVA' 카테고리의 다른 글
java공부 - 배열에서 나타난 오류(초기화 관련) (0) | 2022.03.23 |
---|---|
java- 간단한 스케줄러 만들어보기 (0) | 2022.03.18 |
java 공부 - 명품 JAVA 프로그래밍 4장 Open Challenge (0) | 2022.03.17 |
java공부 - 회원관리 프로그램 만들기 (0) | 2022.03.11 |
java 공부 - 문자를 유니코드 변환하는 방법 (0) | 2022.03.09 |