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};
- // Have number of shopping choices globally accessible
- static int numOfOptions = shoppingBasket.length;
- 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() {
- int input;
- do {
- 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
- input = inputHandler(numOfOptions + 1);
- if (input != numOfOptions + 1) {
- addItem(input - 1);
- }
- } while (input != numOfOptions+ 1);
- }
- public static void displayBasket(int[] basket) {
- int input;
- 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]);
- }
- do {
- System.out.println("What would you like to do?");
- System.out.println("1. Remove Item");
- System.out.println("2. Go back");
- input = inputHandler(2);
- if (input != 2) {
- 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
- input = inputHandler(numOfOptions + 1);
- if (input != numOfOptions + 1) {
- removeItem(input - 1);
- }
- }
- } while ( input != 2 && itemsInBasket(shoppingBasket) != 0);
- }
- }
- 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);
- removed for lack of error checking - error checking below
- */
- int requestedQuantity = inputHandler(0);
- while (requestedQuantity < 0) {
- System.out.println("Negative quantities are not allowed. Please enter a positive quantity.");
- requestedQuantity = inputHandler(0);
- }
- shoppingBasket[item] = shoppingBasket[item] + requestedQuantity;
- }
- public static void removeItem(int item) {
- // take out this many
- System.out.println("Remove how many?");
- /* shoppingBasket[item] = shoppingBasket[item] - inputHandler(0);
- removed for lack of error checking - error checking below
- */
- int removalQuantity = inputHandler(0);
- while (removalQuantity < 0 || removalQuantity > shoppingBasket[item]) {
- if (removalQuantity < 0) {
- System.out.println("Negative quantities are not allowed. Please enter a positive quantity.");
- }
- if (removalQuantity > shoppingBasket[item]) {
- System.out.println("You can not remove more than " + shoppingBasket[item] + " " + ITEMNAMES[item] + ". Remove how many?");
- }
- removalQuantity = inputHandler(0);
- }
- shoppingBasket[item] = shoppingBasket[item] - removalQuantity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment