Based on the advice from shiracamus, I rewrote it in a DDD (domain driven) manner. At that time, we also reviewed some of the logic to make it simpler.
-Instance generation ・ Product list initialization (inventory replenishment) ・ Product list display (display of displayed products) ・ Product selection (input by user) ・ Deposit (input by user) ・ Sales (billing process) ・ Change (refund processing)
Main.java
package vm;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Instance generation
Store venderMachine = new Store();
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
//Product list initialization
Product water = new Product("water", 80);
Product coke = new Product("Cola", 100);
Product orangejuice = new Product("Orange juice", 120);
venderMachine.addStock(water);
venderMachine.addStock(coke);
venderMachine.addStock(orangejuice);
//Product list display
System.out.println("≪It is a product for sale≫");
venderMachine.showProductList();
//Product selection
Product purchase = null;
String purchaseSelectMessage = "Please enter the name of the product you want to purchase.";
int purchaseSelectCount = 0;
do {
if(purchaseSelectCount > 0) {
purchaseSelectMessage = "The product is not available. Please choose from the products on sale.";
}
System.out.println();
System.out.println(purchaseSelectMessage);
purchase = venderMachine.inStocker(scanner.next());
purchaseSelectCount++;
} while(!venderMachine.isExistsStock(purchase));
//payment
String depositMessage = "Please make a deposit.";
int depositCount = 0;
do {
if(depositCount > 0) {
depositMessage = "The deposit amount is insufficient. Please add the shortfall.";
}
System.out.println();
System.out.println(depositMessage);
venderMachine.addDeposit(scanner.nextInt());
depositCount++;
} while(!venderMachine.isEnoughDeposit(purchase));
//Sale
System.out.println();
venderMachine.sale(purchase);
//change
System.out.println();
venderMachine.change();
}
}
Store.java
package vm;
public class Store {
private Stocker stocker;
private Casher casher;
Store() {
stocker = new Stocker();
casher = new Casher();
}
void addDeposit(int deposit) {
casher.addDeposit(deposit);
}
void addStock(Product product) {
stocker.add(product);
}
void change() {
System.out.println("Change is" + casher.change() + "It is a circle.");
}
void showProductList() {
stocker.showProductList();
}
boolean isExistsStock(Product product) {
return stocker.isExistsProduct(product);
}
boolean isEnoughDeposit(Product purchase) {
return casher.isEnoughDeposit(purchase);
}
Product inStocker(String productName) {
return stocker.getProduct(productName);
}
void sale(Product purchase) {
casher.charge(purchase);
System.out.println(purchase.name() + "is!");
System.out.println("Thank you for your purchase.");
}
}
Casher.java
package vm;
public class Casher {
private int deposit;
private int change;
Casher(){
deposit = 0;
change = 0;
}
void addDeposit(int deposit) {
this.deposit += deposit;
}
void charge(Product purchase) {
change = deposit - purchase.price();
}
int change() {
return change;
}
public boolean isEnoughDeposit(Product product) {
if(deposit >= product.price()) {
return true;
}
return false;
}
}
Stocker.java
package vm;
import java.util.ArrayList;
import java.util.List;
public class Stocker {
private List<Product> productList;
Stocker() {
productList = new ArrayList<>();
}
void add(Product product) {
productList.add(product);
}
void showProductList() {
for(Product product: productList) {
System.out.println(product.info());
}
}
boolean isExistsProduct(Product product) {
for(Product stock: productList) {
if(stock.name().equals(product.name())){
return true;
}
}
return false;
}
Product getProduct(String productName) {
for(Product stock: productList) {
if(stock.name().equals(productName)){
return stock;
}
}
return new Product();
}
}
Product.java
package vm;
public class Product {
private String productName;
private int price;
Product(String productName, int price) {
this.productName = productName;
this.price = price;
}
Product() {
this.productName = "";
this.price = 0;
}
String name() {
return productName;
}
int price() {
return price;
}
String info() {
String formatStr = "%s : %d yen";
return String.format(formatStr, productName, price);
}
}
Recommended Posts