Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Scanner;
- public class leonardshop {
- static HashMap<Integer, leonarditem> leoitems = new HashMap<Integer, leonarditem>();
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- loadVariables();
- showMenu();
- }
- // leonarditem & amount
- static Scanner input = new Scanner(System.in);
- private static HashMap<leonarditem, Integer> shoppingcart = new HashMap<leonarditem, Integer>();
- private static void showMenu() {
- System.out.println("0. CLEAR CART");
- System.out.println("1. SHOW CART CONTENTS");
- System.out.println("2. Pensil : RP.100.000");
- System.out.println("3. eraser : RP.500");
- System.out.println("4. iphone : RP.3000");
- System.out.println("5. microphone : RP.50.000");
- System.out.print("What would you like to purchase? (1-4): ");
- String inputid = input.next();
- int itemcode = -1;
- try {
- itemcode = Integer.parseInt(inputid);
- } catch (NumberFormatException e) {
- System.out.println("Incorrect id.");
- showMenu();
- return;
- }
- // wrong code
- if (itemcode != 0 && itemcode != 1) {
- if (!leoitems.containsKey(itemcode) || itemcode == -1) {
- System.out.println("That item does not exist.");
- showMenu();
- return;
- }
- }
- // RESET CART
- if (itemcode == 0) {
- shoppingcart.clear();
- System.out.println("Cart cleared.");
- showMenu();
- return;
- }
- // SHOW CART CONTENT
- if (itemcode == 1) {
- System.out.println("-=== SHOPPING CART CONTENTS ===-");
- int i = 1;
- int total = 0;
- for (leonarditem car : shoppingcart.keySet()) {
- int amount = shoppingcart.get(car);
- total = total + car.price * amount;
- System.out.println(i + ". '" + car.itemname + "' x" + amount + "");
- i++;
- }
- System.out.println("YOUR TOTAL PRICE:");
- System.out.println("Rp. " + total);
- System.out.println(" ");
- showMenu();
- return;
- }
- leonarditem item = leoitems.get(itemcode);
- System.out.print("[LEO SHOP] Chosen item: " + item.itemname + " Rp. " + item.price);
- System.out.print("\n [LEO SHOP] How many would you like to buy?");
- int amount = input.nextInt();
- shoppingcart.put(item, amount);
- System.out.print("[LEO SHOP] Added " + amount + "x of " + item.itemname + " to your shopping cart. \n");
- showMenu();
- return;
- }
- //int price, String itemname, int itemcode) {
- private final static void loadVariables() {
- ArrayList<leonarditem> items = new ArrayList<leonarditem>();
- items.add(new leonarditem(100000,"jordy PENCIL", 2));
- items.add(new leonarditem(500, "jordy ERASER", 3));
- items.add(new leonarditem(3000, "jordy iPHONE", 4));
- items.add(new leonarditem(50000, "jordy microphone", 5));
- for (leonarditem item : items) {
- leoitems.put(item.itemcode, item);
- }
- }
- }
- // main class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement