Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- static int[] shoppingBasket = {0,0,0,0,0};
- // Add names in order of values of store items
- static final String[] ITEMNAMES = {"Broken Computer", "Cookie", "Unknown Video Game", "Modern Art", "Useless Button"};
- // Add values in order of store items
- static final int[] ITEMCOSTS = {40,3,25,80,10};
- static boolean inStore = true;
- static int customerCash = 100;
- public static void main(String[] args) {
- // Loop while we're still in the store
- while (inStore) {
- mainMenu();
- switch(inputHandler(3)) {
- case 1: // I'm going shopping!
- shopMenu();
- break;
- case 2: // Whats in the basket?
- displayBasket(shoppingBasket);
- break;
- case 3: // Exact change only please...
- payAndExit();
- break;
- }
- }
- }
- public static int inputHandler(int n) {
- // Got really sick of putting scanners everywhere.
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- // Need a way to use this without it range checking.
- if (n == 0) {
- return choice;
- } else {
- while (choice < 1 || choice > n) {
- System.out.println("Option out of range please try again.");
- choice = scanner.nextInt();
- }
- }
- return choice;
- }
- public static void mainMenu() {
- // tl;dr wall o text
- System.out.println("\n\nWelcome Customer: You currently have " + itemsInBasket(shoppingBasket) + " items in your shopping basket.");
- System.out.println("What would you like to do?");
- System.out.println("1. Add an item to the shopping basket");
- System.out.println("2. View the content of the shopping basket");
- System.out.println("3. Pay and exit the shop");
- }
- public static void shopMenu() {
- System.out.println("What would you like to add to your shopping basket?");
- System.out.println("1. Broken Computer $40");
- System.out.println("2. Cookie $30");
- System.out.println("3. Unknown video game $25");
- System.out.println("4. Modern art $80");
- System.out.println("5. Useless button $10");
- System.out.println("6. Go Back");
- // switch case to add items
- switch(inputHandler(6)){
- case 1:
- addItem(0);
- break;
- case 2:
- addItem(1);
- break;
- case 3:
- addItem(2);
- break;
- case 4:
- addItem(3);
- break;
- case 5:
- addItem(4);
- break;
- case 6:
- break;
- }
- }
- public static void displayBasket(int[] basket) {
- if (itemsInBasket(shoppingBasket) == 0) {
- System.out.println("Your shopping basket is empty.");
- } else {
- for (int x = 0; x < basket.length; x++) {
- System.out.printf("Quantity of %s: [%d]%n", ITEMNAMES[x], shoppingBasket[x]);
- }
- System.out.println("What would you like to do?");
- System.out.println("1. Remove Item");
- System.out.println("2. Go back");
- switch(inputHandler(2)) {
- case 1:
- // whats in the basket to see what can be removed
- System.out.println("What item would you like to remove?");
- for (int x = 0; x < basket.length; x++) {
- System.out.printf((x+1) + ". Quantity of %s: [%d]%n", ITEMNAMES[x], shoppingBasket[x]);
- }
- System.out.println("6. Go Back");
- // switch case to remove items
- switch(inputHandler(6)) {
- case 1:
- removeItem(0);
- break;
- case 2:
- removeItem(1);
- break;
- case 3:
- removeItem(2);
- break;
- case 4:
- removeItem(3);
- break;
- case 5:
- removeItem(4);
- break;
- case 6:
- break;
- }
- break;
- case 2:
- break;
- }
- }
- }
- public static void payAndExit() {
- // everything i needed, time to go
- int total = 0;
- for ( int x = 0; x < shoppingBasket.length; x++) {
- total = total + (shoppingBasket[x] * ITEMCOSTS[x]);
- }
- if (total > customerCash) {
- System.out.println("Sorry, cannot complete transaction: Insufficient funds.");
- System.out.println("Try removing some items and try again.");
- } else {
- System.out.println("Your Total is: " + total);
- System.out.println("Your change is: " + (customerCash - total));
- System.out.println("Have a nice day!");
- inStore = false;
- }
- }
- public static int itemsInBasket(int[] cart){
- // just a quick sum
- int total = 0;
- for (int item : cart) {
- total = total + item;
- }
- return total;
- }
- public static void addItem(int item) {
- // what i want, and how many
- System.out.println(ITEMNAMES[item] + " costs " + ITEMCOSTS[item] + ". How many would you like?");
- shoppingBasket[item] = shoppingBasket[item] + inputHandler(0);
- }
- public static void removeItem(int item) {
- // take out this many
- System.out.println("Remove how many?");
- shoppingBasket[item] = shoppingBasket[item] - inputHandler(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment