It's been about a month and a half since I got a job at an IT company as a new graduate and first came into contact with Java in programming training. I should be able to make a simple program to some extent, so I tried to create a program to sell cakes in Java by referring to the lectures and exercises in the training, and put it together in an article as a memorandum, output, and practice of writing sentences. thought. It's a poor result, but I'd be happy if you could see it. I will be happy to cry if you give me advice in the comments.
Before creating the program, I summarized the elements necessary to run a cake shop on the program. The following factors have been determined by the cake shop to be necessary.
・ Information about cakes It is necessary to create an object that has the cake name, price, and inventory as elements, and to inform the customer of the cake name and price, check the inventory, and calculate the purchase price. ・ Hear from customers about the number of purchases You need the ability to ask (enter) the number of purchases and recognize them as numbers. If it cannot be recognized as a number, it is necessary to ask the customer again. ・ Put new products or dispose of inventory We don't always sell the same cake, so we need the ability to add and subtract cake types.
With the above three axes as the axis I created "Main method class", "Cake object class", "Number input class", and "Constant class such as fixed statement". I used openJDK ver12 for development and eclipse for the editor. In addition, as additional conditions, there are restrictions such as "the total number of cakes is 20 per person" and "the business will continue until the stock is sold out".
MainSystem.java
package cake;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainSystem{
public static void main(String[] args) throws IOException{
System.out.println(Constant.MSG_ACT_BAR);
//Create a list of cake molds
List<Cake> cakes = new ArrayList<Cake>();
//Object creation
Cake cake1 = new Cake("Tiramisu",20,400);
Cake cake2 = new Cake("Cheesecake",40,300);
Cake cake3 = new Cake("Strawberry tart",10,500);
//Stored in the list, there are currently 3 types of cakes
cakes.add(cake1);
cakes.add(cake2);
cakes.add(cake3);
//Number of loops
int loopNum =0;
//Total cake inventory initialization(Used for loops)
int sumStockCake =0;
//Create a storage location for the number of cake purchases
List<Integer> numBuyCake =new ArrayList<Integer>();
//Storage location for waste amount
List<Integer> dustCake =new ArrayList<Integer>();
System.out.println(Constant.MSG_ACT_BAR);
System.out.println("\n\n");
//Out of stock (sumStockCake=I will do up to 0)
do{
//b1 ・ ・ ・ Purchase limit b2 ・ ・ ・ Record input error and inventory shortage loop count
boolean b1 =true;
boolean b2 =true;
loopNum++;
//Welcome
System.out.println(Constant.MSG_OPENING_01+"\n");
System.out.println(Constant.MSG_MENU_BAR);
System.out.println(Constant.MSG_OPENING_02);
//Call Cake in the cakes list for a few minutes in stock
for(int i=0; i<cakes.size(); i++){
System.out.println(cakes.get(i).getName() +"\t\t"+cakes.get(i).getPrice()+"Circle\t\t inventory"+cakes.get(i).getStock()+"Pieces");
}
System.out.println(Constant.MSG_MENU_BAR);
System.out.println("\n\n");
//Reset total cake purchases
int sumBuyCake;
//b1 ... Purchase limit error loop
do{
sumBuyCake =0;
for(int i=0; i<cakes.size(); i++){
//Enter the number of cakes, b2 ... Error loop with non-positive integers
do{
System.out.print(cakes.get(i).getName() + Constant.MSG_INPUT_ICON);
String str = InputMany.cakeNum();
try{
//Delete if the current purchase quantity list already contains a value
if(numBuyCake.size()== (i+1)){
numBuyCake.remove(i);
}
//Convert the number of purchases to Int type and add it to List, if it can not be converted, catch and loop
numBuyCake.add(Integer.parseInt(str));
//Loop if the number of purchases is negative
if(numBuyCake.get(i)<0){
System.out.println(Constant.MSG_ERROR_MINUS);
b2 = true;
//Number of purchases> Loop if in stock
}else if(numBuyCake.get(i) > cakes.get(i).getStock() ){
System.out.println(Constant.MSG_ERROR_OVERSTOCK1);
System.out.println(cakes.get(i).getName()+Constant.MSG_ERROR_OVERSTOCK2);
System.out.println(cakes.get(i).getName()+"In stock"+cakes.get(i).getStock()+"It is an individual.");
b2 = true;
}else{
b2 = false;
}
}
catch(NumberFormatException e){
System.out.println(Constant.MSG_ERROR_INPUT);
b2 = true;
}
}while(b2);
}
//Calculate the total number of cakes purchased
for(int i=0; i<cakes.size(); i++){
sumBuyCake +=numBuyCake.get(i);
}
//20 cakes or less in total
if(sumBuyCake >20){
System.out.println(Constant.MSG_ERROR_OVER20);
b1 =true;
//Reset purchase list
numBuyCake.clear();
//Buy something
}else if(sumBuyCake ==0){
System.out.println(Constant.MSG_ERROR_UNDER1);
b1 =true;
numBuyCake.clear();
//Reduce inventory if you can purchase safely
}else{
b1 =false;
for(int i=0; i<cakes.size(); i++){
cakes.get(i).decStock(numBuyCake.get(i));
}
}
System.out.println("\n\n");
}while(b1);
//For calculating total purchase price
int priceSumCake =0;
for(int i=0; i<cakes.size(); i++){
//Total amount processing Cake amount*Purchase number
priceSumCake += (cakes.get(i).getPrice() * numBuyCake.get(i));
//Purchase statement ready
if(numBuyCake.get(i) != 0){
System.out.println(cakes.get(i).getName()+"To"+numBuyCake.get(i)+"Pieces,");
}
}
System.out.println("total"+sumBuyCake+"In terms of points");
System.out.println(priceSumCake + "It is a yen.");
System.out.println(Constant.MSG_THANKYOU);
//Clearing the number of cake purchases for each customer
numBuyCake.clear();
//Shoe addition
if(loopNum ==2){
System.out.println("\n\n"+Constant.MSG_ACT_BAR+"\n\n");
Cake cake4 = new Cake("cream puff",30,150);
cakes.add(cake4);
System.out.println("\n\n"+Constant.MSG_ACT_BAR+"\n\n");
//Inventory disposal
}else if(loopNum %4 ==0){
System.out.println("\n\n"+Constant.MSG_ACT_BAR+"\n\n");
System.out.println(Constant.MSG_DUST_SHOOT);
int i =0;
int j =1;
//i th and i+Compare jth inventory, i+i if j is larger+Bring the jth to the i side and end the loop
while(i+j<cakes.size()){
System.out.print(cakes.get(i).getName()+"When"+cakes.get(i+j).getName()+"Is more in stock");
if(cakes.get(i).getStock() < cakes.get(i+j).getStock()){
System.out.println(cakes.get(i+j).getName()+"is.");
i+=j;
j=1;
}else{
System.out.println(cakes.get(i).getName()+"is.");
j++;
}
}
System.out.println("To dispose"+cakes.get(i).getName()+"is. In stock"+cakes.get(i).getStock()+"It was an individual.");
System.out.println("Loss due to disposal:"+(cakes.get(i).getStock() * cakes.get(i).getPrice())+"Circle");
System.out.println("\n\n"+Constant.MSG_ACT_BAR+"\n\n");
//Add disposal, reduce target inventory to 0
dustCake.add(cakes.get(i).getStock() * cakes.get(i).getPrice());
cakes.get(i).decStock(cakes.get(i).getStock());
}
//If you delete a product with 0 inventory from the menu, the number of elements will decrease, so i will also decrease properly
for(int i=0; i<cakes.size(); i++){
if(cakes.get(i).getStock() == 0){
cakes.remove(i);
i--;
}
}
//The loop ends when the total cake inventory reaches 0
sumStockCake =0;
for(int i=0; i<cakes.size(); i++){
sumStockCake +=cakes.get(i).getStock();
}
//End of loop if out of stock
}while(sumStockCake > 0);
int sumDust =0;
for(int i=0; i<dustCake.size(); i++){
sumDust +=dustCake.get(i);
}
System.out.println(Constant.MSG_CLOSE_SHOP);
System.out.println(Constant.MSG_DUST_SUM+sumDust+"It is a circle.");
}
}
Cake.java
package cake;
public class Cake{
//Cake name, inventory, price
private String name;
private int stock;
private int price;
private Cake(){
System.out.println(Constant.MSG_MAKE_CAKE);
}
//Receive name, inventory, price and create an object
public Cake(String name, int stock, int price){
this();
this.name =name;
this.stock =stock;
this.price =price;
System.out.println(this.name +Constant.MSG_ADD_CAKE);
}
//Returns the name of the cake
public String getName(){
return name;
}
//Returns inventory information
public int getStock(){
return stock;
}
//Return the price of the cake
public int getPrice(){
return price;
}
//Purchase decision
public void decStock(int c){
this.stock -=c;
}
}
InputMany.java
package cake;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputMany{
public static String cakeNum() throws IOException{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
return str;
}
}
Constant.java
package cake;
public class Constant{
public static final String MSG_MAKE_CAKE ="Created a Cake object.";
public static final String MSG_ADD_CAKE ="Was added to the product.";
public static final String MSG_OPENING_01 ="Welcome!\n Welcome to the cake shop "Pastel".";
public static final String MSG_OPENING_02 ="Today's menu:";
public static final String MSG_MENU_BAR ="********************************";
public static final String MSG_INPUT_ICON ="Please enter the quantity to purchase.\n>";
public static final String MSG_ERROR_INPUT = "Please enter a number.";
public static final String MSG_ERROR_OVER20 = "The total purchase is over 20 pieces.\n The total is up to 20.\n";
public static final String MSG_ERROR_UNDER1 ="Please purchase one or more items.";
public static final String MSG_ERROR_OVERSTOCK1 = "I'm sorry.";
public static final String MSG_THANKYOU ="Thank you for your purchase.\n\n\n";
public static final String MSG_ERROR_OVERSTOCK2 = "Is out of stock.";
public static final String MSG_ERROR_MINUS = "Do not enter a negative value.";
public static final String MSG_CLOSE_SHOP = "Today's business is closed.\n Thank you for visiting us.";
public static final String MSG_ACT_BAR ="---------------------------------------------------";
public static final String MSG_DUST_SHOOT ="We will dispose of unsold inventory.";
public static final String MSG_DUST_SUM ="Today's total amount of disposal is";
}
My personal best bet is to use a list to store Cake objects.
MainSystem_v1_2.java
List<Cake> cakes = new ArrayList<Cake>();
Cake cake1 = new Cake("Tiramisu",20,400);
cakes.add(cake1);
By using a list with a variable number of elements to store, it is easy to add with add and dispose with remove. If you use an array, when you use the for syntax to get the elements of Cake, even the elements that are no longer in stock will be read.
Is it possible to describe the cream puff that suddenly interrupted in the second loop a little smarter ...? I think it is possible to add a program for inventory replenishment and modify it so that cakes can be generated from the database.
Recommended Posts