The vending machine I wrote last time: https://qiita.com/TakumiKondo/items/e93aa60031e2fe09d969 Was classified into classes.
Items.java
package vm;
import java.util.HashMap;
import java.util.Map;
public class Items {
private Map<String, Integer> items; //Product List
private Map<String, Integer> availablePurchases; //Items that can be purchased
//Constructor (initialization of product list)
Items() {
this.items = new HashMap<String, Integer>();
items.put("Cola", 100);
items.put("Orange juice", 120);
items.put("water", 80);
}
//Get the minimum purchase price
int minPrice() {
int minPrice = 0;
int loopCount = 0;
for (String itemKey : items.keySet()) {
minPrice = validMinPrice(loopCount, minPrice, itemPrice(itemKey));
loopCount++;
}
return minPrice;
}
//Judgment of minimum purchase amount
int validMinPrice(int loopCount, int minPrice, int price) {
if (loopCount == 0 || minPrice > price) {
minPrice = price;
}
return minPrice;
}
//Product price
private int itemPrice(String itemKey) {
return items.get(itemKey);
}
//Amount of items that can be purchased
private int availablePurchasePrice(String itemKey) {
return availablePurchases.get(itemKey);
}
//Get a list of available products
void createAvailablePurchases(int deposit) {
availablePurchases = new HashMap<String, Integer>();
for (String itemKey : items.keySet()) {
addAvailablePurchase(deposit, itemKey);
}
}
//Addition of available product list
private void addAvailablePurchase(int deposit, String itemKey) {
if (deposit >= items.get(itemKey)) {
availablePurchases.put(itemKey, items.get(itemKey));
}
}
//Display of available products
void showAvailablePurchase() {
for (String itemKey : availablePurchases.keySet()) {
System.out.println(itemKey + ":" + items.get(itemKey) + "Circle");
}
}
//Judgment as to whether the product is available for purchase
boolean isAvailablePurchase(String itemName, Deposit deposit) {
return availablePurchases.containsKey(itemName);
}
//Selling price
int saleAmount(String itemName) {
return availablePurchasePrice(itemName);
}
}
Deposit.java
package vm;
public class Deposit {
private int deposit; //Deposit amount
//Constructor (initial value 0 yen)
Deposit() {
deposit = 0;
}
//payment
void deposit(int amount) {
deposit = deposit + amount;
}
//Current deposit amount
int amount() {
return this.deposit;
}
//Billing
void charge(int amount) {
int charge = this.deposit - amount;
this.deposit = charge;
}
}
VendingMachine.java
package vm;
public class VendingMachine {
private Items items; //Product
private Deposit deposit; //Deposit amount
//constructor
VendingMachine() {
items = new Items();
deposit = new Deposit();
}
//payment
void deposit(int amount) {
deposit.deposit(amount);
}
//Determine if there is a deposit above the lowest priced item
boolean hasMinDeposit() {
if (deposit.amount() < items.minPrice()) {
return false;
}
return true;
}
//Show available items
void showAvailablePurchases() {
items.createAvailablePurchases(deposit.amount());
items.showAvailablePurchase();
}
//Determine if the item is available for purchase
boolean isAvailablePurchases(String itemName) {
return items.isAvailablePurchase(itemName, deposit);
}
//Show purchased items
void selectItem(String itemName) {
System.out.println(itemName + "is!");
}
//Billing
void charge(String itemName) {
int saleAmount = items.saleAmount(itemName);
deposit.charge(saleAmount);
System.out.println("Change is" + deposit.amount() + "It is a circle.");
}
}
Main.java
package vm;
import java.util.Scanner;
/**
*Vending Machine (Level 6) Creating Vending Machine Class
*/
public class Main {
//Main logic
public static void main(String[] args) {
//① Product initialization
VendingMachine vendingMachine = new VendingMachine();
//Additional deposit for minimum purchase amount
Scanner scanner = new Scanner(System.in);
do {
//② Deposit
System.out.println("Please put in the money.");
int amount = scanner.nextInt();
vendingMachine.deposit(amount);
//③ Amount check (minimum purchase amount)
} while (!vendingMachine.hasMinDeposit());
//④ Display available products
System.out.println("");
System.out.println("It is a product that can be purchased.");
vendingMachine.showAvailablePurchases();
//⑤ Select a product
String itemName;
do {
System.out.println("");
System.out.println("Please enter the product name.");
itemName = scanner.next();
} while (!vendingMachine.isAvailablePurchases(itemName));
vendingMachine.selectItem(itemName);
scanner.close();
//⑥ Billing function
vendingMachine.charge(itemName);
}
}
Recommended Posts