2024년 8월 9일
나의 첫번째 자바를 활용한 토이 프로젝트 개발후기를 남겨본다..
선정이유는 자바라는 언어를 배워보면서 배운 기술을 모두 사용해보고 싶다는 욕심이 너무 컸다.
각종 조건문(if문, Switch-case문)과 디자인패턴 프레임워크 등 자바 라이브러리에서 제공하는 배운 오픈소스를 모두 활용하고 싶단 생각에 평소 많이 쓰던 영화예매 기능에서 주제를 정하게 되었다...
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
// 회원정보입력
UserLogin ul = UserLogin.getInstance();
ul.loginGuide(scr);
// 영화관 선택
Cinema c = Cinema.getInstance();
c.cinemaSelect(scr);
// 영화관 좌석 선택
SeatSelect s = new SeatSelect();
s.selectSeats(scr);
// 최종 예매확인
System.out.println("==========================");
System.out.println("예매가 완료되었습니다.");
System.out.println(ul.toString());
System.out.print(c.toString());
System.out.println(s.toString());
System.out.println("즐거운 관람시간 되세요");
scr.close();
}
}
import java.util.Scanner;
import java.util.regex.Pattern;
// 회원등록 클래스
class UserLogin {
// 싱글톤 객체생성 -> 고정 데이터영역으로 클래스 안에 간접적으로 생성
private static UserLogin user;
private UserLogin() {
}
// 객체 생성 메서드
public static UserLogin getInstance() {
if(user == null) {
user = new UserLogin();
} return user;
}
// 멤버변수
private String username; // 회원이름
private String phoneNum; // 전화번호
// getter, setter
public String getUsername() {
return username;
}
public String getPhoneNum() {
return phoneNum;
}
public void setUsername(String username) {
this.username = username;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
@Override
public String toString() {
return username + "( 등록된 번호 : " + phoneNum + " ) 고객님께서 예매하신 내역은 ";
}
// 회원입력 절차
public void loginGuide(Scanner scr) {
// 프로그램을 시작합니다.
System.out.println("=======영화예매 시스템을 실행합니다.========");
System.out.println();
System.out.println("=============회원정보입력==============");
while (true) {
System.out.print("이름 입력 (한글로만 입력해주세요) : ");
String str1 = scr.next();
boolean isKor = Pattern.matches("^[ㄱ-ㅎ가-힣]*$", str1);
if (!isKor) {
System.out.println("한글로만 입력해주세요");
user.loginGuide(scr);
} else {
this.username = str1;
break;
}
}
while (true) {
System.out.print("전화번호 ( - 없이 입력해 주세요) : ");
String str2 = scr.next();
boolean isNum = Pattern.matches("^01(?:0|1|[6-9])(?:\\d{3}|\\d{4})\\d{4}$", str2);
if (!isNum) {
System.out.println("- 없이 입력해 주세요");
user.loginGuide(scr);
} else {
this.phoneNum = str2;
System.out.println("회원등록이 완료되었습니다!");
break;
}
}
}
}
import java.util.Scanner;
class Cinema {
// 싱글톤 객체 생성
private static Cinema cinema1;
private Cinema() {
}
// 객체 생성 메서드
public static Cinema getInstance() {
if(cinema1 == null) {
cinema1 = new Cinema();
} return cinema1;
}
// 멤버변수
private String theaterName; // 영화관 이름
// getter, setter
public String getTheater_name() {
return theaterName;
}
public void setTheater_name(String theaterName) {
this.theaterName = theaterName;
}
@Override
public String toString() {
return "[" + theaterName + "] ";
}
// 영화관 선택
public void cinemaSelect(Scanner scr) {
System.out.println("=============영화관 선택==============");
System.out.println("[1]영화관01, [2]영화관02, [3]영화관03");
System.out.print("상영하실 영화관 번호를 입력해주세요 : ");
int num = scr.nextInt();
switch (num) {
case 1:
this.theaterName = "영화관01";
break;
case 2:
this.theaterName = "영화관02";
break;
case 3:
this.theaterName = "영화관03";
break;
default:
System.out.println("잘못된 번호입니다. 다시 선택해 주세요. : ");
this.theaterName = null;
cinema1.cinemaSelect(scr);
}
}
}
import java.util.Scanner;
public class SeatSelect {
// 멤버변수
private int selectedRow; // 행
private int selectedCol; // 열
public SeatSelect() {
}
// getter, setter
public int getSelectedRow() {
return selectedRow;
}
public int getSelectedCol() {
return selectedCol;
}
public void setSelectedRow(int selectedRow) {
this.selectedRow = selectedRow;
}
public void setSelectedCol(int selectedCol) {
this.selectedCol = selectedCol;
}
@Override
public String toString() {
return selectedRow + " 행 " + selectedCol + " 열 좌석입니다.";
}
// 좌석을 배열
public void selectSeats(Scanner scr) {
String[][] seatArray = new String[3][4];
for (int i = 0; i < seatArray.length; i++) {
for (int j = 0; j < seatArray[i].length; j++) {
seatArray[i][j] = "[o]";
}
}
// 좌석 선택
while (true) {
System.out.println("____|screen|___");
// 현재 좌석 상태 출력
for (int i = 0; i < seatArray.length; i++) {
for (int j = 0; j < seatArray[i].length; j++) {
System.out.print(seatArray[i][j] + " ");
}
System.out.println();
}
System.out.println("===========영화관 좌석선택============");
System.out.println("좌석을 선택해주세요. : ");
System.out.print("행 번호를 입력해주세요 (1행부터 3행까지): ");
int row = scr.nextInt();
System.out.print("열 번호를 입력해주세요 (1열부터 4열까지): ");
int col = scr.nextInt();
if (row >= 1 && row <= 3 && col >= 1 && col <= 4) {
if (seatArray[row - 1][col - 1] == "[o]") {
seatArray[row - 1][col - 1] = "[x]";
this.selectedRow = row;
this.selectedCol = col;
System.out.println("좌석 선택이 완료되었습니다.");
break;
} else {
System.out.println("이미 선택된 좌석입니다.
다른 좌석을 선택해주세요 : ");
}
} else {
System.out.println("올바른 좌석 번호를 다시 입력해주세요 : ");
row = scr.nextInt();
System.out.print("행 번호를 입력해주세요 (1행부터 3행까지): ");
col = scr.nextInt();
System.out.print("열 번호를 입력해주세요 (1열부터 4열까지): ");
}
}
}
}