So I'm making a java program that takes in what a customer bought and uses an array and hash map to figure out how much it cost for CSE homework(was messing around with printing other things with this as well). But now I want to be able for the user to just input firstly the product and then the quantity to be able to make a simple, user friendly "cash-register" program. Basically just want system.out's Any thoughts on how to do this?
Not sure if a loop with multiple scanner/system.in lines of code would work or not, and I'm really unsure on how to tackle this problem.
Oh and also if something similar was asked please tell me! I wasn't able to find anything similar, but I'm probably mistaken.
import java.util.HashMap;
import java.util.ArrayList;
public class preLab3 {
// PreLab Q1: Compute the price of buying a specific quantity of a single item
public static double getPriceSingleItemType(HashMap<String, Double> priceList, String itemType, int quantity) {
double itemPrice = priceList.get(itemType);
return itemPrice * quantity;
}
// PreLab Q2: Compute the price of a single order
public static double getPrice(HashMap<String, Double> priceList, ArrayList<String> basket) {
double totalCost = 0.0;
for(String item: basket) {
double itemPrice = priceList.get(item);
totalCost = totalCost + itemPrice;
}
return totalCost;
}
public static HashMap<String, Double> getPriceList() {
HashMap<String, Double> priceList = new HashMap<String, Double>();
priceList.put("eggs", 1.79); // per dozen
priceList.put("butter", 3.19);
priceList.put("cheese", 5.99);
priceList.put("apple", 0.99); // per pound
priceList.put("banana", 0.39); // per pound
priceList.put("strawberries", 1.99);
priceList.put("peppers", 0.99);
priceList.put("tomato sauce", 0.5);
priceList.put("chocolate chips", 2.29);
return priceList;
}
public static void main(String[] args) {
System.out.println(getPriceList());
// PreLab Q3: I want to buy 3 lbs of strawberries
double price = getPriceSingleItemType(getPriceList(), "strawberries", 3);
System.out.println(price);
ArrayList<String> cart = new ArrayList<>();
cart.add("strawberries");
cart.add("strawberries");
cart.add("strawberries");
cart.add("chocolate chips");
cart.add("banana");
cart.add("banana");
cart.add("apple");
cart.add("eggs");
cart.add("butter");
double totalCost = getPrice(getPriceList(), cart);
System.out.println("The customer must pay: " + totalCost + " USD");
ArrayList<HashMap<String, Integer>> orders = new ArrayList<>();
for(HashMap<String, Integer> maps : orders) {
}
}
}