AWS CLOUD FRAMEWORK/Java

[Day11] VendingMachine

JWJ_Hub 2023. 4. 5. 18:05
package debugging;

class Product {

    private String name;
    private int price;

    public Product(String name, int price) {
        this.name = name;
        this.price = price;
    }
    public String toString() {
        return String.format("%s\t: %,d원", name, price);
    }
    
    public String getName() {
        return name;
    }
    public int getPrice() {
        return price;
    }
}

class Machine { 
    private Product[] tray1 = new Product[2];
    private Product[] tray2 = new Product[2];
    private Product[] tray3 = new Product[2];
    private Product[] tray4 = new Product[2];
    private int money;
    
    private Product[][] arr = { tray1, tray2, tray3, tray4 };
    
    public Machine() {
        for(int i = 0; i < tray1.length; i++) {
            tray1[i] = new Product("콜라", 800);
        }
        for(int i = 0; i < tray2.length; i++) {
            tray2[i] = new Product("사이다", 700);
        }
        for(int i = 0; i < tray3.length; i++) {
            tray3[i] = new Product("게토레이", 900);
        }
        for(int i = 0; i < tray4.length; i++) {
            tray4[i] = new Product("비타500", 500);
        }
    }
    
    private int getMinimunPrice() {
        int minimumPrice = 99999999;
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length; j++) {
                if(arr[i][j] != null && arr[i][j].getPrice() < minimumPrice) {
                    minimumPrice = arr[i][j].getPrice();
                }
            }
        }
        return minimumPrice;
    }
    
    public boolean inputMoney(int money) {
        this.money += money;
        int minimumPrice = getMinimunPrice();
        return this.money >= minimumPrice;
    }
    
    private int countOfTray(Product[] tray) {
        int count = 0;
        for(int i = 0; i < tray.length; i++) {
            if(tray[i] != null) {
                count++;
            }
        }
        return count;
    }
    void show() {
        System.out.printf("%s (%d개)\n", tray1[0].toString(), countOfTray(tray1));
        System.out.printf("%s (%d개)\n", tray2[0].toString(), countOfTray(tray2));
        System.out.printf("%s (%d개)\n", tray3[0].toString(), countOfTray(tray3));
        System.out.printf("%s (%d개)\n", tray4[0].toString(), countOfTray(tray4));
    }
}
public class VendingMachine {
    public static void main(String[] args) {
        Machine machine = new Machine();
        int money;
        while(true) {
            money = 100;
            boolean ready = machine.inputMoney(money); 
            if(ready) {  
                break;   
            }
        }
        machine.show();
    }
}