Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class VendingApplication
- {
- static Scanner input = new Scanner(System.in);
- static DecimalFormat currency = new DecimalFormat("0.00");
- static int qtyCoke = 10;
- static int qty7up = 10;
- static int qtyFanta = 2;
- static double totalCash = 0;
- static int numTransaction = 0;
- static int pin = 1234;
- public static void main(String[] args)
- {
- menu();
- }
- public static void menu()
- {
- System.out.println("Welcome to the BFEI vending Machine");
- System.out.println("-----------------------------------");
- System.out.println("Press 1 for Coke");
- System.out.println("Press 2 for 7up");
- System.out.println("Press 3 for Fanta");
- System.out.println("-----------------------------------");
- String choice = input.next();
- switch(choice)
- {
- case "1":
- {
- if(qtyCoke==0)
- {
- System.out.println("Item out of stock. Please choose another.");
- menu();
- }
- String product = "Coke";
- double price = 1.20;
- payment(product, price);
- break;
- }
- case "2":
- {
- if(qty7up==0)
- {
- System.out.println("Item out of stock. Please choose another.");
- menu();
- }
- String product = "7up";
- double price = 1.10;
- payment(product, price);
- break;
- }
- case "3":
- {
- if(qtyFanta==0)
- {
- System.out.println("Item out of stock. Please choose another.");
- menu();
- }
- String product = "Fanta";
- double price = 1;
- payment(product, price);
- break;
- }
- case "a":
- {
- System.out.println("Please enter pin number");
- int enteredPin = input.nextInt();
- if(enteredPin==pin)
- {
- System.out.println("Correc Pin. Loading Admin Mode");
- admin();
- }
- else
- {
- System.out.println("Incorrect Pin. Try again");
- }
- break;
- }
- case "x":
- {
- System.out.println("System closing down");
- System.exit(0);
- break;
- }
- default:
- {
- System.out.println("Invalid choice. Please choose again.");
- break;
- }
- }
- menu();
- }
- public static void payment(String productChoice, double transactionPrice)
- {
- System.out.println("Enter Payment. Balance is " + currency.format(transactionPrice));
- double moneyEntered = input.nextDouble(); //User enters payment
- while(moneyEntered<transactionPrice)
- {
- System.out.println("Enter payment. Balance remaining is " + currency.format((transactionPrice-moneyEntered)));
- moneyEntered = moneyEntered + input.nextDouble();//User tops up payment and overwrites existing amount
- }
- //Product has been purchased at this point
- if(moneyEntered>transactionPrice)
- {
- System.out.println("Changedue is " + currency.format((moneyEntered-transactionPrice)));
- }
- System.out.println("Trasaction Succesful. Please take your product");
- numTransaction++;
- totalCash = totalCash + transactionPrice; // Update total takings with value of the transaction
- if(productChoice.equals("Coke"))
- {
- qtyCoke--;
- }
- else if(productChoice.equals("Fanta"))
- {
- qtyFanta--;
- }
- {
- qty7up--;
- }
- }
- public static void admin()
- {
- System.out.println("Total Takings: €" + currency.format(totalCash));
- System.out.println("Number of Transactions: " + numTransaction);
- System.out.println("Average Value of a Transaction" + currency.format((totalCash/numTransaction)));
- System.out.println("Coke Stock Remaining" + qtyCoke);
- System.out.println("7up Stock Remaining" + qty7up);
- System.out.println("Fanta Stock Remaining" + qtyFanta);
- System.out.println("Press r to restock or any other key to exit");
- String choice = input.next();
- if(choice.equals("r"))
- {
- qtyCoke=10;
- qty7up=10;
- qtyFanta=2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement