Gain programming experience with Java
Beginners who learned the basics of Java
You can program based on the flowchart and functional details.
・ Product list initialization The following three products are fixed. Coke 100 yen Orange juice 120 yen Water 80 yen
·payment It is possible to deposit in units of 1 yen. Prompt to deposit until the minimum amount available for purchase is deposited. (In this case, 80 yen or more for water)
・ Product selection Display products within the deposit amount. Select by product name.
・ Sales Offer the selected product.
・ Billing Subtract the purchase price from the deposit amount. Return the change.
package vm;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Product initialization
Map<String, Integer> items = new HashMap<String, Integer>();
items.put("Cola", 100);
items.put("Orange juice", 120);
items.put("water", 80);
//Additional deposit for minimum purchase amount
int deposit = 0;
int minSaleAmount = -1;
Scanner scanner = new Scanner(System.in);
do {
//Deposit processing
System.out.println("Please put in the money.");
deposit = deposit + scanner.nextInt();
//Amount check (minimum purchase amount)
int loopCount = 0;
for (String itemKey: items.keySet()) {
if(loopCount == 0 || minSaleAmount > items.get(itemKey)) {
minSaleAmount = items.get(itemKey);
}
loopCount++;
}
} while(deposit < minSaleAmount);
//Product selection
System.out.println("Please select a product.");
Map<String, Integer> availablePurchases = new HashMap<String, Integer>();
for (String itemKey: items.keySet()) {
if(deposit >= items.get(itemKey)) {
System.out.println(itemKey + ":" + items.get(itemKey) + "Circle");
availablePurchases.put(itemKey, items.get(itemKey));
}
}
//Sale
String itemName;
while(true) {
itemName = scanner.next();
if (availablePurchases.containsKey(itemName)){
break;
}
System.out.println("The product name is specified incorrectly. Please specify the product name again.");
}
scanner.close();
System.out.println(itemName + "is!");
//Billing function
deposit = deposit - availablePurchases.get(itemName).intValue();
System.out.println("Change is" + deposit + "It is a circle.");
}
}
I have created a classified version. https://qiita.com/TakumiKondo/items/9f222f44c973bb2eaa06
Recommended Posts