develope_kkyu

[JAVA] 카페 메뉴 작업 본문

JAVA/Java SE

[JAVA] 카페 메뉴 작업

developekkyu37 2022. 12. 21. 16:40
728x90

CafeMain.java

import java.util.ArrayList;
import java.util.Scanner;

public class CafeMain {

	public static void main(String[] args) {
		Menu menu = new Menu();
		Sales sales = new Sales();
		
		
		Scanner s = new Scanner(System.in);
		while(true) {
			System.out.println("작업선택 [m:메뉴작업,o:주문관리,s:매출조회,'':종료]");
			String op = s.nextLine();
			if(op.equals("m")) {
				menu.control();
			} else if(op.equals("o")){
				Order order = new Order(menu);
				order.control();
				sales.add(order);
			}  else if(op.equals("s")){
				sales.display();
			} else if(op.equals("")) {
				sales.save();
				break;
			}
		}
		s.close();
		System.out.println("프로그램 종료");
	}
}

Menu.java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;

public class Menu {
	private ArrayList<String> alName;	// 메뉴명 목록
	private ArrayList<Integer> alPrice;	// 메뉴가격 목록
	
	Menu(){
		// alName, alPrice 초기화(Initialization)
		this.alName = new ArrayList<String>();
		this.alPrice = new ArrayList<Integer>();
		// d:\temp\menu.txt를 찾고 열어서 메뉴명/가격을 읽어들인다.
		File file = new File("d:\\temp\\menu.txt");
		
		try (Scanner sc = new Scanner(file, StandardCharsets.UTF_8))
		{
			while(sc.hasNextLine()) {
				String line=sc.nextLine();
				String[] token=line.split(",");	// ["Americano","2500"]
				this.alName.add(token[0]);
				this.alPrice.add(Integer.parseInt(token[1]));
//				System.out.println(line);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private void save() {
		// alName, alPrice의 메뉴명, 가격을 menu.txt에 쓰기(저장)
		try {
			File file = new File("d:\\temp\\menu.txt");
			if(!file.exists()) {
				file.createNewFile();
			}
			FileWriter fw = new FileWriter(file);
			PrintWriter writer = new PrintWriter(fw);
			for(int i=0; i<this.alName.size(); i++) {
				writer.write(this.alName.get(i)+","+this.alPrice.get(i)+"\n");
			}
			writer.close();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
	void add(String name, int price) {
		// alName, alPrice에 name, price를 추가
		this.alName.add(name);
		this.alPrice.add(price);
	}
	void delete(int index) {
		// alName, alPrice 양쪽에서 index 번호를 가진 것을 제거
		this.alName.remove(index);
		this.alPrice.remove(index);
	}
	void update(int index, String name, int price) {
		// alName, alPrice 양쪽에서 index번호의 이름과 가격을 수정
		this.alName.set(index, name);
		this.alPrice.set(index, price);
	}
	String getName(int index) {
		return this.alName.get(index);
	}
	int getPrice(int index) {
		return this.alPrice.get(index);
	}
	void display() {
		// 메뉴명과 가격을 표시
//		Americano, 2500
//		Mocca, 3200
//		Espresso, 2700
//		Cappuccino, 3500
//		Latte, 3000
		for(int i=0; i<this.alName.size(); i++) {
		System.out.println((i+1)+"."+this.alName.get(i) + ", " +this.alPrice.get(i));
		}
	}
	void control() {
		Scanner s1 = new Scanner(System.in);	// 문자열 입력전용
		Scanner s2 = new Scanner(System.in);	// 숫자 입력전용
		
		while(true) {
			System.out.print("메뉴작업을 선택하시오 [c:메뉴추가, r:메뉴목록표시, u:메뉴수정, d:메뉴삭제, '': 프로그램 종료]:");
			String instr = s1.nextLine();
			if(instr.equals("")) {
				// while exit, program terminated.
				this.save();
				break;
			} else if(instr.equals("c")) {
				// 메뉴명 입력받고
				// 가격 입력받고
				// menu.add()
				String name = s1.nextLine();
				int price = s2.nextInt();
				this.add(name, price);
			} else if(instr.equals("r")) {
				this.display();
			} else if(instr.equals("u")) {
				// 수정할 메뉴의 인덱스 번호 입력받고
				// 새 이름 입력받고
				// 새 가격 입력받고
				// menu.update()
				int index = s2.nextInt();
				String name = s1.nextLine();
				int price = s2.nextInt();
				this.update(index, name, price);
			} else if(instr.equals("d")) {
				// 삭제할 메뉴의 인덱스 번호 입력받고
				// menu.delete()
				int index=s2.nextInt();
				this.delete(index-1);
			}
		}
//		System.out.println("프로그램 종료");
//		s1.close();
//		s2.close();
	}
}

Order.java

import java.util.ArrayList;
import java.util.Scanner;

public class Order {
	private ArrayList<String> alName;
	private ArrayList<Integer> alQty;
	private ArrayList<Integer> alPrice;	// 합계 (단가X수량)
	private String mobile;
	private Menu m1;
	
	Order(Menu m){
		// 초기화
		this.alName = new ArrayList<String>();
		this.alQty = new ArrayList<Integer>();
		this.alPrice = new ArrayList<Integer>();
		this.mobile = new String();
		this.m1 = m;
	}
	void add() {
		Scanner s1 = new Scanner(System.in);
		Scanner s2 = new Scanner(System.in);
		
		while(true) {
			this.m1.display();
			// 메뉴 번호를 입력받는다 (""이 아닌 동안)
			System.out.print("메뉴번호를 입력하시오['':종료]");
			String pMenuNum = s1.nextLine();
			if(pMenuNum.equals("")) break;
			
			int index = Integer.parseInt(pMenuNum)-1;	// 메뉴번호->메뉴인덱스로 변환
			String name = this.m1.getName(index);
			int net = this.m1.getPrice(index);
			System.out.println("수량을 입력하시오:");	// 수량을 입력받는다
			int qty = s2.nextInt();
		
			// alName/alQty/alPrice에 각각 추가
			this.alName.add(name);
			this.alQty.add(qty);
			this.alPrice.add(qty*net); // 단가*수량 => 합계를 계산
		}
		System.out.print("모바일번호를 입력하시오");
		this.mobile = s1.nextLine();			// 모바일 번호 입력받는다.
		// Sales 객체에 Order 객체의 내용을 복사
		// alMobile=this.mobile
		// alMenu=this.alName
		// alQty = this.alQty
		// alPrice = this.alPrice
		// alTime = new Date()
	}
		
	void display() {
		// 주문 내역을 출력(화면표시)
		int sum=0;
		for(int i=0; i<this.alName.size(); i++) {
			System.out.println((i+1)+"."+this.alName.get(i) + ", x" + this.alQty.get(i) + ", " + this.alPrice.get(i) +"원");
			sum+=this.alPrice.get(i);
		}
		System.out.println("총액:"+sum+", 모바일번호"+this.mobile);
	}
	void delete() {
		// 취소할 주문번호를 입력받는다. (""이 아닌 동안)
		// 주문 삭제.
		Scanner s1 = new Scanner(System.in);
		
		while(true) {
			this.m1.display();
			System.out.print("지울 주문내역번호를 입력하시오['':종료]");
			String pMenuNum = s1.nextLine();
			if(pMenuNum.equals("")) break;
			
			int index = Integer.parseInt(pMenuNum)-1;
			this.alName.remove(index);
			this.alPrice.remove(index);
			this.alQty.remove(index);
			}
	}
	void control() {
		Scanner s1 = new Scanner(System.in);	// 문자열 입력전용
		Scanner s2 = new Scanner(System.in);	// 숫자 입력전용
		
		while(true) {
			System.out.print("메뉴작업을 선택하시오 [c:메뉴추가, r:메뉴목록표시, d:메뉴삭제, '': 프로그램 종료]:");
			String instr = s1.nextLine();
			if(instr.equals("")) {
				// while exit, program terminated.
				break;
			} else if(instr.equals("c")) {
				this.add();
			} else if(instr.equals("r")) {
				this.display();
			} else if(instr.equals("d")) {
				this.delete();
			} 
	}
	}
	int getSize() {
		return this.alName.size();
	}
	String getName(int index) {
		return this.alName.get(index);
	}
	int getQty(int index) {
		return this.alQty.get(index);
	}
	int getPrice(int index) {
		return this.alPrice.get(index);
	}
	String getMobile() {
		return this.mobile;
	}
}

Sales.java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class Sales {
	ArrayList<String> alMobile;
	ArrayList<String> alMenu;
	ArrayList<Integer> alQty;
	ArrayList<Integer> alPrice;
	ArrayList<String> alTime;
	
	Sales(){
		//초기화
		this.alMobile = new ArrayList<String>();
		this.alMenu = new ArrayList<String>();
		this.alQty = new ArrayList<Integer>();
		this.alPrice = new ArrayList<Integer>();
		this.alTime = new ArrayList<String>();
	}
	void add(Order o1) {
		for(int i=0; i<o1.getSize(); i++) {
			this.alMenu.add(o1.getName(i));
			this.alMobile.add(o1.getMobile());
			this.alPrice.add(o1.getPrice(i));
			this.alQty.add(o1.getQty(i));
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			String dateToStr = dateFormat.format(date);
			this.alTime.add(dateToStr);
		}
	}
	void display() {
		int sum=0;
		for(int i=0; i<this.alMenu.size(); i++) {
		System.out.println(this.alMobile.get(i)+","+
		this.alMenu.get(i)+", x"+
		this.alQty.get(i)+", "+
		this.alPrice.get(i)+", "+
		this.alTime.get(i));
		sum+=this.alPrice.get(i);
		}
		System.out.println("총매출합계:"+sum+"원");
	}
	void save() {
		try {
			DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
			Date date = new Date();
			String dateToStr = dateFormat.format(date);
			
			File file = new File("d:\\temp\\"+dateToStr+".txt");
			if(!file.exists()) {
				file.createNewFile();
			}
			FileWriter fw = new FileWriter(file);
			PrintWriter writer = new PrintWriter(fw);
			int sum=0;
			for(int i=0; i<this.alMenu.size(); i++) {
				writer.write(this.alMobile.get(i)+","+
						this.alMenu.get(i)+", x"+
						this.alQty.get(i)+", "+
						this.alPrice.get(i)+", "+
						this.alTime.get(i)+"\n");
				sum=+this.alPrice.get(i);
			}
			writer.write("총 매출합계"+sum+"원");
			writer.close();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
}
728x90

'JAVA > Java SE' 카테고리의 다른 글

[JAVA] 점수로 장학생 여부 판별  (0) 2022.12.21
[JAVA] 증감 연산자  (0) 2022.12.21
[JAVA] 두 점 사이의 거리 구하기  (0) 2022.12.21
[JAVA] 최대, 최소, 합계 구하기(함수 이용)  (0) 2022.12.21
[JAVA] 홀수단 출력  (1) 2022.12.21