1

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) {


    }
}

}

M. Gaddaffi
  • 41
  • 1
  • 7
  • https://stackoverflow.com/questions/5287538/how-can-i-get-the-user-input-in-java and also check out what methods the Scanner object supports. you can grab line by line. also don't close the scanner object until the program is about to exit. – RAZ_Muh_Taz Feb 16 '18 at 23:26

2 Answers2

0

First off you should consider looking at this post for future/current reference on static methods. https://stackoverflow.com/a/7084473/1770303

In this case you should be referencing your hashmap as an object either by declaring it globally in your class and adding key value pairs in a method or creating a separate class that does essentially the same thing. How you organize you code largely depends on the individual. Lastly, each user input should be checked against the key values to see if its a valid key before continuing. If the user input is not contained in your map what is the requirement? How will you handle that?

As for the scanner i would reference the following post which provides the link to documentation on the scanner class. https://stackoverflow.com/a/14679497/1770303

Scott
  • 174
  • 1
  • 13
0

First create a Scanner object for user input, then use the example code you've already got where cart.add(item) adds the user input item, and loop each item for user input quantity. So altogether something like this:

// ... same code above   
ArrayList<String> cart = new ArrayList<>();

Scanner input = new Scanner(System.in);
String user_item = "";
int user_quantity = 0;
System.out.println("What items would you like to buy?");

// Loop to repeatedly get user input and add items to cart
// until user enters "q" for item to quit.
do {
    System.out.print("Item? (q to quit) ");
    user_item = input.nextLine();
    if (user_item.equals("q")){
        break;
    }
    System.out.print("How many " + user_item + "? ");
    user_quantity = input.nextInt();
    System.out.println(user_quantity + " " + user_item);
    for(int q = 0; q <= user_quantity; q++){
       cart.add(user_item);
    }
} while(!input.nextLine().equals("q"));
input.close();

// Remove these
// 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);
// ... same code below

Example run:

{banana=0.39, eggs=1.79, apple=0.99, butter=3.19, peppers=0.99, chocolate chips=2.29, tomato sauce=0.5, strawberries=1.99, cheese=5.99}
What items would you like to buy?
Item? (q to quit) butter
How many butter? 1
1 butter
Item? (q to quit) eggs
How many eggs? 2
2 eggs
Item? (q to quit) q
The customer must pay: 11.75 USD

Of course you probably also want to add error/exception handling for invalid user input.

Hope this helps.