danDevlog

Java - day16 (세미프로젝트 - POS구현) 본문

Java

Java - day16 (세미프로젝트 - POS구현)

단데기이 2022. 3. 15. 20:11
728x90

Main.java

package posPackge;

import java.io.IOException;

public class Main {

	public static void main(String[] args) throws IOException {
		PosManager pm = new PosManager();
		
		while(true) {
			Menu.showMenu(pm);
		}
		
	}

}

 

Menu.java

package posPackge;

import java.io.IOException;
import java.util.Scanner;

public class Menu {

	public Menu(PosManager pm) {	}

	static void showMenu(PosManager pm) throws IOException {
		System.out.println("----------------------------");
		System.out.println("POS기기");
		System.out.println("----------------------------");
		System.out.println("1. 상품 관리");
		System.out.println("2. 상품 주문");
		System.out.println("3. 주문 내역");
		System.out.println("4. 현재 매출");
		System.out.println("5. 프로그램 종료");
		System.out.print("번호를 선택하세요. -> ");

		Scanner sc = new Scanner(System.in);
		String input = sc.next();

		switch (input) {
		case "1":
			pm.productManageMenu();
			break;
		case "2":
			pm.productOrder();
			break;
		case "3":
			pm.orderList();
			break;
		case "4":
			pm.presentRevenue();
			break;
		case "5":
			System.out.println("POS를 종료합니다.");
			System.exit(0);
			break;

		default:
			System.out.println("잘못된 번호입니다.");
			break;
		}
	}

}

 

PosInfo.java

package posPackge;

import java.io.Serializable;

public class PosInfo implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8819412280566852625L;
	private String productCode;
	private String productName;
	private int productPrice;
	private int productStock;
	private int cnt;
	private int orderCnt = 0;
	private int sum;
	private String orderNum;
	
	public int getSum() {
		return sum;
	}

	public void setSum(int sum) {
		this.sum = sum;
	}

	public int getOrderCnt() {
		return orderCnt;
	}

	public void setOrderCnt(int orderCnt) {
		this.orderCnt = orderCnt;
	}

	public PosInfo(String pCode, String pName, int pPrice, int pStock) {
		this.productCode = pCode;
		this.productName = pName;
		this.productPrice = pPrice;
		this.productStock = pStock;
	}

	public int getCnt() {
		return cnt;
	}

	public void setCnt(int cnt) {
		this.cnt = cnt;
	}

	public String getProductCode() {
		return productCode;
	}

	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	public int getProductPrice() {
		return productPrice;
	}

	public void setProductPrice(int productPrice) {
		this.productPrice = productPrice;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public int getProductStock() {
		return productStock;
	}

	public void setProductStock(int productStock) {
		this.productStock = productStock;
	}

	public String getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(String oCode) {
		this.orderNum = oCode;
	}
	
	
}

 

PosInterface.java

package posPackge;

public interface PosInterface {
	 
	 void productOrder();
	 
	 void orderList();
	 
	 void presentRevenue();
	 
	 void productEnroll();
	 
	 void productDelete();
	 
	 void productEdit();
	 
	 void productStock();

	void productManageMenu();
	 
	 
}

 

PosManager.java

package posPackge;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

public class PosManager implements PosInterface {
	Scanner sc = new Scanner(System.in);
	HashSet<PosInfo> posList = new HashSet<>();
	HashSet<PosInfo> orderBusket = new HashSet<>();
	String pCode;
	String pName;
	int pPrice;
	int pStock;
	int sum;

	BufferedOutputStream bs = null;

	@Override
	public void productManageMenu() {
//		productLoad();
		System.out.println("1. 상품 등록");
		System.out.println("2. 상품 삭제");
		System.out.println("3. 상품 수정");
		System.out.println("4. 상품 재고량");
		System.out.println("5. 뒤로 가기");
		System.out.print("번호를 선택하세요. -> ");
		String input = sc.next();
		switch (input) {
		case "1":
			productEnroll();
			break;
		case "2":
			productDelete();
			break;
		case "3":
			productEdit();
			break;
		case "4":
			productStock();
			break;
		case "5":
			System.out.println("뒤로 갑니다");
			break;
		default:
			System.out.println("잘못된 번호입니다.");
			productManageMenu();
		}
	}

	@Override
	public void productOrder() {
		productLoad();

		boolean isTrue = true;
		while (isTrue) {
			Iterator<PosInfo> it = posList.iterator();
			int c = 1;
			while (it.hasNext()) {
				PosInfo p = it.next();
				System.out.println(c + ".  " + p.getProductName());
				p.setCnt(c);
				c++;
			}
			System.out.println("0. 최종주문 \t  100. 뒤로 가기");
			System.out.println("--장바구니 목록--");
			Iterator<PosInfo> it2 = orderBusket.iterator();
			while (it2.hasNext()) {
				PosInfo p = it2.next();
				System.out.println(p.getProductName() + "\t" + p.getProductPrice() + "\t" + p.getOrderCnt());
			}
			System.out.println("총 금액 : " + sum);
			System.out.print("번호 선택 -> ");
			int input = sc.nextInt();

			// Iterator는 1회성이기때문에 사용했으면 다시 선언해줘야한다.
			Iterator<PosInfo> it3 = posList.iterator();
			while (it3.hasNext()) {
				PosInfo p = it3.next();
				int a = p.getProductStock();
				if (input == p.getCnt()) {
					if (p.getProductStock() > 0) {
						orderBusket.add(p);
						p.setProductStock(--a);
						sum = sum + p.getProductPrice();
					} else {
						System.out.println("재고가 없습니다.");
					}
				}
			}

			Iterator<PosInfo> it4 = orderBusket.iterator();
			while (it4.hasNext()) {
				PosInfo p = it4.next();
				int b = p.getOrderCnt();
				if (input == p.getCnt()) {
					if (p.getProductStock() > 0) {
						p.setOrderCnt(++b);
					} else {
						System.out.println("재고가 없습니다.");
					}
				}
			}

			if (input == 0) {
				System.out.println("주문이 완료되었습니다.");
				long time = System.currentTimeMillis();
				SimpleDateFormat dayTime = new SimpleDateFormat("yyyyMMddhhmmss");
				String oCode = dayTime.format(new Date(time));
				System.out.println("주문번호 : " + oCode);
				System.out.println("주문 내역");
				Iterator<PosInfo> it5 = orderBusket.iterator();
				while (it5.hasNext()) {
					PosInfo p = it5.next();
					System.out.println(p.getProductName() + "\t" + p.getProductPrice() + "\t" + p.getOrderCnt());
					p.setSum(sum);
					p.setOrderNum(oCode);
				}
				System.out.println("합계 금액 : " + sum);
				orderSave();
				isTrue = false;
			}

			if (input == 100) {
				System.out.println("뒤로갑니다.");
				try {
					Menu.showMenu(this);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	@Override
	public void orderList() {
		orderLoad();
		Iterator<PosInfo> it1 = orderBusket.iterator();
		PosInfo p0 = it1.next();
		System.out.println("주문 번호 : " + p0.getOrderNum());  
		Iterator<PosInfo> it2 = orderBusket.iterator();
		while (it2.hasNext()) {
			PosInfo p = it2.next();
			System.out.println(p.getProductName() + "\t" + p.getProductPrice() + "\t" + p.getOrderCnt());
		}
		System.out.println("합계 금액 : " + p0.getSum());
		
	}

	@Override
	public void presentRevenue() {
		orderLoad();
		System.out.println("------------------");
		System.out.println("------현재 매출------");
		System.out.println("------------------");
		Iterator<PosInfo> it1 = orderBusket.iterator();
		PosInfo p0 = it1.next();
		Iterator<PosInfo> it2 = orderBusket.iterator();
		while (it2.hasNext()) {
			PosInfo p = it2.next();
			System.out.println(p.getProductName() + " :\t" + p.getOrderCnt());
		}
		System.out.println("총 금액 : " + p0.getSum());
	}

	// 상품 관리하고 나오는 부분
	@Override
	public void productEnroll() {
		// posList.add(new PosInfo("100", "치킨", 15000, 10));
		// posList.add(new PosInfo("200", "피자", 20000, 5));
		// posList.add(new PosInfo("300", "삼겹살", 13000, 7));
		// posList.add(new PosInfo("400", "짜장", 7000, 15));

		// productLoad();

		System.out.println("상품 등록을 시작합니다.");
		System.out.println("**** 예시 ****");
		System.out.println("상품 코드:상품명:상품 가격:상품 재고");
		String Re = sc.next();
		String Re1 = Re + ":0";

		String str[] = Re1.split(":");
		for (int i = 0; i < str.length; i++) {
			if (i == 0) {
				pCode = str[i];
			}
			if (i == 1) {
				pName = str[i];
			}
			if (i == 2) {
				pPrice = Integer.parseInt(str[i]);
			}
			if (i == 3) {
				pStock = Integer.parseInt(str[i]);
			}
		}

		boolean isTrue = true;
		Iterator<PosInfo> it = posList.iterator();
		while (it.hasNext()) {
			PosInfo p = it.next();
			if (pCode.equals(p.getProductCode())) {
				System.out.println("중복된 코드입니다.");
				isTrue = false;
			}
		}

		if (isTrue == true) {
			posList.add(new PosInfo(pCode, pName, pPrice, pStock));
			System.out.println("상품을 등록하였습니다.");
			productSave();
		}
	}

	@Override
	public void productDelete() {
		productLoad();
		sc.nextLine();
		System.out.print("삭제할 상품의 이름을 입력해주세요 : ");
		String d1 = sc.nextLine();
		boolean isTrue = false;

		Iterator<PosInfo> it = posList.iterator();
		while (it.hasNext()) {
			PosInfo p = it.next();
			if (d1.equals(p.getProductName())) {
				it.remove();
				isTrue = true;
				System.out.println(p.getProductName() + "을 삭제합니다.");
				productSave();
			}
		}
		System.out.println(isTrue == true ? "" : "삭제할 상품이 없습니다.");
	}

	@Override
	public void productEdit() {
		productLoad();
		sc.nextLine();
		System.out.print("수정할 상품의 이름을 입력해주세요 : ");
		String e1 = sc.nextLine();
		boolean isTrue = false;

		Iterator<PosInfo> it = posList.iterator();
		while (it.hasNext()) {
			PosInfo p = it.next();
			if (e1.equals(p.getProductName())) {
				System.out.println(p.getProductName() + "의 정보를 수정합니다.");
				System.out.println("가격을 입력해주세요");
				int ePrice = sc.nextInt();
				System.out.println("재고량을 입력해주세요.");
				int eStock = sc.nextInt();
				p.setProductPrice(ePrice);
				p.setProductStock(eStock);
				isTrue = true;
				productSave();
				System.out.println(p.getProductName() + "의 수정이 완료되었습니다..");
			}
		}
		System.out.println(isTrue == true ? "" : "수정할 상품이 없습니다.");
	}

	@Override
	public void productStock() {
		try {
			productLoad();
			Iterator<PosInfo> it = posList.iterator();
			while (it.hasNext()) {
				PosInfo p = it.next();
				System.out.println(p.getProductName() + ":\t" + p.getProductStock());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void productSave() {
		try {
			FileOutputStream fileSteam = new FileOutputStream("C:/DBFILE/goods.txt");
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileSteam);

			objectOutputStream.writeObject(posList);
			objectOutputStream.close();
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	public void orderSave() {
		try {
			FileOutputStream fileSteam = new FileOutputStream("C:/DBFILE/orders.txt");
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileSteam);

			objectOutputStream.writeObject(orderBusket);
			objectOutputStream.close();
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	public HashSet<PosInfo> productLoad() {
		try {
			FileInputStream fileStream = new FileInputStream("C:/DBFILE/goods.txt");
			ObjectInputStream objectInputStream = new ObjectInputStream(fileStream);

			Object object = objectInputStream.readObject();

			posList = (HashSet<PosInfo>) object;

			objectInputStream.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return posList;
	}

	public HashSet<PosInfo> orderLoad() {
		try {
			FileInputStream fileStream = new FileInputStream("C:/DBFILE/orders.txt");
			ObjectInputStream objectInputStream = new ObjectInputStream(fileStream);

			Object object = objectInputStream.readObject();

			orderBusket = (HashSet<PosInfo>) object;

			objectInputStream.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return orderBusket;
	}
}

 

상품등록
추가 등록

 

상품 삭제
상품 수정
상품 주문
주문 내역 및 현재 매출

 

HashSet과 직렬화 / 역직렬화를 이용하여 구현하였다.

겹치는 코드도 많이 보이고 저장 및 불러오기 처리도 미숙한것같다.

매출 합계나 주문번호도 모든 객체에 집어넣게 해버려서 메모리 낭비도 생길듯하다

Comments