0319 자판기 프로그램

Fifty·2025년 3월 19일

2. JAVA

목록 보기
7/33

Usecase Diagram

사용자는 actor
자판기는 system

자판기 프로그램

static 변수

  1. String[] item // 상품명이 저장되어 있는 배열
  2. int[] price // 상품의 가격이 저장되어 있는 배열
  3. int[] item_cnt // 상품의 갯수가 저장되어 있는 배열
  4. boolean[] purchaseCan // 상품 구매 버튼(돈을 넣고, 갯수가 있다면 ON/true, 아니면 OFF/false)
  5. int goStop // 사용자가 구매하고싶은지 종료(잔돈받기)하고 싶은지 → while의 매개변수
  6. int choiceNum // 사용자가 구매하고 싶은 상품 번호
  7. int machine_money // 자판기에 저장된 금액
  8. String userBag // 사용자가 구매한 상품 목록

함수

  1. main();
    가. printItem();
    나. while(goStop)이용하여 반복
    다. while 종료 시 잔돈 반환과 함께 종료메시지

  2. printItem();
    자판기의 내용을 출력해주는 함수
    상품명, 가격, 버튼(구매가능 여부, On/Off)

  3. moneyInput();
    사용자로부터 돈을 입력받는 함수
    machine_money 업데이트

  4. selectItem();
    상품을 고르게 하는 함수
    가. 돈이 충분하지 않다면 p(돈이 부족합니다.) moneyInput();
    나. 돈이 충분하다면 purchaseProcess();

  5. purchaseProcess();
    상품을 구매하는 절차 함수
    가. 상품의 남은 갯수가 없다면 selectItem();
    나. 상품이 남은 갯수가 충분하다면
    1) p(상품 가져가세요)
    2) machine_money - 상품금액
    3) andOrEnd();

  6. addOrEnd();
    추가 구매할 것인지 종료(잔돈 반환)받을 것인지 정하는 함수
    가. 추가 구매하려면 selectItem();
    나. 종료하려면 while() 탈출 후 잔돈 반환

package ex01;

import java.util.Scanner;

public class Test0319_2 {

	static String[] item = {"콜라","사이다","코코아"};
	static int[] price = {1500, 1800, 1000};
	static int[] item_cnt = {2, 1, 0};
	static boolean[] purchaseCan = {false, false, false};
	static int goStop=1;
	static int choiceNum;
	static int machine_money = 0;
	static String userBag="";
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		printItem();
		while(goStop==1) {
			moneyInput();
		}
		System.out.println("잔돈 " + machine_money+"원을 가져가세요. 안녕히 가세요.");
		machine_money=0;
		System.out.println("넣은 돈: " + machine_money+"원");

		System.out.println("내가 구매한 상품: "+userBag);
	}

	static void printItem() {
		System.out.println("--------음료 자판기--------");
		// 상품번호 출력: 사용자는 번호를 입력해서 상품을 선택한다.
		System.out.print("번호: ");
		for(int i=0; i<item.length; i++) {
			System.out.print("  "+i+"  |");
		}
		System.out.println("");
		// 상품명 출력
		System.out.print("상품:  ");
		
		for(int i=0; i<item.length; i++) {
			System.out.print(item[i]+" |");
		}
		System.out.println("");
		
		// 상품가격 출력
		System.out.print("가격: ");
		for(int i=0; i<item.length; i++) {
			System.out.print(price[i]+" |");
		}
		System.out.println("");
		// 상품 구매가능여부 출력
		System.out.print("버튼: ");
		for(int i=0; i<item.length; i++) {
			if(machine_money>=price[i])
				purchaseCan[i]=true;
			else
				purchaseCan[i]=false;
		}
		for(int i=0; i<item.length; i++) {
			if(purchaseCan[i]==true)
				System.out.print("  ON |");
			else
				System.out.print(" OFF |");
		}
		System.out.println("");
		System.out.println("------------------------");
		System.out.println("넣은 돈: " + machine_money+"원");
	}

	static void moneyInput() {
		System.out.println("돈을 넣어주세요>> ");
		Scanner sc = new Scanner(System.in);
		int inputMoney = sc.nextInt();
		machine_money += inputMoney;
		selectItem();
	}

	static void selectItem() {
		printItem();
		
		System.out.println("음료를 골라주세요(버튼 ON인 경우만 구매 가능)");
		Scanner sc = new Scanner(System.in);
		choiceNum = sc.nextInt();
		purchaseProcess();
	}
	
	static void purchaseProcess() {
		// 상품이 있으면
		if(item_cnt[choiceNum]>0) {
			item_cnt[choiceNum]--;
			machine_money -= price[choiceNum];
			System.out.println("선택하신 상품은 "+item[choiceNum]+"입니다.");
			System.out.println("자판기 하단에서 가져가세요");
			userBag += item[choiceNum]+" ";
			System.out.println("넣은 돈: "+machine_money);
			addOrEnd();
		}
		// 상품이 없으면
		else {
			System.out.println("상품이 없습니다.");
			addOrEnd();
		}
	}
	
	static void addOrEnd() {
		System.out.println("계속 구매하시려면 1, 잔돈을 받으시려면 2 >> ");
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		//goStop이 2인 경우는 while문을 종료
		if(input==2)
			goStop = 2;
		else if(input==1){
		//goStop이 1인 경우에는 
			if(machine_money>=1000) {
				selectItem();
			}
			else {		// 돈이 부족하면 돈을 더 넣어라
				moneyInput();
			}
		}
		
		
	}

}




0개의 댓글