Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package onlinestorestable2;
- import static java.lang.System.out;
- import java.util.Scanner;
- /**
- *
- * @author banan
- */
- public class OnlineStoreStable2 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- //Products go here
- String[] fruit = {"Bananas", "Apples", "Grapes", "Apricots"};
- String[] vegetables = {"Cucumbers", "Tomatoes", "Potatoes", "Onions", "Carrots"};
- String[] cheeses = {"Haloumi", "Gouda", "Feta", "Mozzarella"};
- //Prices go here
- double[] fruitprices = {1.59, 1.67, 2.09, 1.23};
- double[] vegetablesprices = {2.24, 1.12, 2.03, 1.73, 2.34};
- double[] cheesesprices = {1.38, 3.21, 2.89, 3.26,};
- //Product Numbers go here
- int[] fruitnumbers = {0, 1, 2, 3};
- int[] vegetablesnumbers = {0, 1, 2, 3 ,4};
- int[] cheesesnumbers= {0, 1 ,2 ,3};
- //Declerations go here
- double donation = 0;
- double basket_price = 0;
- double amount = 0;
- double selection_price = 0;
- int selection = 0;
- int product_selection = 0;
- int amount_int = 0;
- String yn = "a";
- //Program starts here
- Scanner input = new Scanner(System.in);
- boolean check = false;
- while (check == false){
- System.out.println("Please choose one of the following categories, alternatively press 9 to proceed to check out."); // Prints the Category Menu
- System.out.println("1.Fruit");
- System.out.println("2.Vegetables");
- System.out.println("3.Cheeses");
- System.out.println("4.Check out");
- selection = input.nextInt();
- input.nextLine(); //advances the scanner to the next line break
- if (selection > 4 || selection < 1){ //Reminds the user to choose a value between and including 1 and 3
- System.out.println("Please enter a valid number between and including 1 and 3, or 4 to proceed to check out");
- }else if(selection == 1){ // Fruit Selection
- while (true){
- System.out.println("Product No. Prices Products"); // Displays fruit menu
- for (int i = 0; i<4; i++){
- System.out.println(fruitnumbers[i] + " " + fruitprices[i] + " " + fruit[i]);
- }
- System.out.print("Please select the number of the product you want to add to your cart, alternatively press '9' in order to go back.");
- product_selection = input.nextInt(); // Lets the user select their desired fruits
- if (product_selection == 9){
- break;
- }
- System.out.print("You have selected " + fruit[product_selection] + ", is that correct? (y/n)"); //Makes sure that the user has selected the correct item
- input.nextLine();
- yn = input.nextLine();
- if ("n".equals(yn)){ //Sends the user back to the main menu in case they have selected the wrong item
- break;
- }
- else if ("y".equals(yn)){
- System.out.println("Select the amount of the selected products you would like to add to your cart");//Lets the user select the desired amount of products
- amount = input.nextInt();
- selection_price = amount * fruitprices[product_selection];//Calculates how much all of the selected products cost
- amount_int = (int)amount; //turns the amount double into an int in order to use it for calculations
- System.out.printf("Would you like to add " + amount_int + " " + fruit[product_selection] + " to your cart? They cost %.2f" + "£ (y/n)" ,selection_price );//Makes sure the user has selected the right amount of products
- input.nextLine();
- yn = input.nextLine();
- if ("y".equals(yn)){
- basket_price = basket_price+selection_price;
- System.out.printf("Your basket currently costs %.2f" + "£" + ". Would you like to continue or proceed to check out? (press c to continue or q to proceed to check out)", basket_price);
- yn = input.nextLine();
- if ("c".equals(yn)){
- break;
- }
- else if("q".equals(yn)){
- check = true;
- }
- }
- }
- if(check){ // Sends the user to the check out
- break;
- }
- }
- }
- else if(selection == 2){ //Vegetable Selection
- while (true){
- System.out.println("Product No. Prices Products");// Displays the menu
- for (int i = 0; i<4; i++){
- System.out.println(vegetablesnumbers[i] + " " + vegetablesprices[i] + " " + vegetables[i]);
- }
- System.out.print("Please select the number of the product you want to add to your cart, alternatively press '9' in order to go back.");
- product_selection = input.nextInt();// Lets the user select their desired product
- if (product_selection == 9){ // Takes the user back to the main menu
- break;
- }
- System.out.print("You have selected " + vegetables[product_selection] + ", is that correct? (y/n)");// makes sure that the user has selected the correct product
- input.nextLine();
- yn = input.nextLine();
- if ("n".equals(yn)){ // Sends the user back to the menu
- break;
- }
- else{
- System.out.println("Select the amount of the selected products you would like to add to your cart");
- amount = input.nextInt();// Lets the user select the desired amount of the selected product
- selection_price = amount * vegetablesprices[product_selection];
- amount_int = (int)amount;//Turns the amount double into an int in order to do calculations
- System.out.printf("Would you like to add " + amount_int + " " + vegetables[product_selection] + " to your cart? They cost %.2f" + "£ (y/n)" ,selection_price );
- input.nextLine();//Makes sure that the user has selected the correct amount of products
- yn = input.nextLine();
- if ("y".equals(yn)){
- basket_price = basket_price+selection_price;
- System.out.printf("Your basket currently costs %.2f" + "£" + ". Would you like to continue or proceed to check out? (press c to continue or q to proceed to check out)", basket_price );
- yn = input.nextLine();//Lets the user continue with their order or proceed to check out
- if ("c".equals(yn)){ // Continues order
- break;
- }
- else if("q".equals(yn)){ //Sends user to check out
- check = true;
- }
- }
- }
- if(check){ // Sends the user to the check out
- break;
- }
- }
- }
- else if(selection == 3){ //Displays Cheeses Menu
- while (true){
- System.out.println("Product No. Prices Products");
- for (int i = 0; i<4; i++){
- System.out.println(cheesesnumbers[i] + " " + cheesesprices[i] + " " + cheeses[i]);
- }
- System.out.print("Please select the number of the product you want to add to your cart, alternatively press '9' in order to go back.");
- product_selection = input.nextInt(); // Lets the user choose the desired product, or takes them to the check out
- if (product_selection == 9){ //Takes user to the check out
- break;
- }
- System.out.print("You have selected " + cheeses[product_selection] + ", is that correct? (y/n)");
- input.nextLine(); // Makes sure that the user has selected the correct product
- yn = input.nextLine();
- if ("n".equals(yn)){
- break; //Takes user back to the menu
- }
- else{
- System.out.println("Select the amount of the selected products you would like to add to your cart");
- amount = input.nextInt(); //Lets the user select the desired amount of selected products
- selection_price = amount * cheesesprices[product_selection];
- amount_int = (int)amount; // Turns the amount double into an integer for calculations
- System.out.printf("Would you like to add " + amount_int + " " + cheeses[product_selection] + " to your cart? They cost %.2f" + "£ (y/n)" ,selection_price );
- input.nextLine(); // Makes sure that the user has selected the correct amount of the selected products
- yn = input.nextLine();
- if ("y".equals(yn)){
- basket_price = basket_price+selection_price; //Adds products to the cart
- System.out.printf("Your basket currently costs %.2f" + "£" + ". Would you like to continue or proceed to check out? (press c to continue or q to proceed to check out)", basket_price);
- yn = input.nextLine();
- if ("c".equals(yn)){
- break; //Takes user to the menu
- }
- else if("q".equals(yn)){
- check = true; //Takes user to the check out
- }
- }
- }
- if(check){ // Sends the user to the check out
- break;
- }
- }
- }
- else if (selection == 4){ // Sends the user to the check out
- check = true;
- break;
- }
- }
- System.out.printf("The total amount for the products you have selected is %.2f" + " £, is that ok? (y/n)", basket_price );
- yn = input.nextLine();
- if ("y".equals(yn)){ // Asks the user if they would like to make a donation
- System.out.println("Great!, would you like us to round up the cost and donate it to charity? (y/n)");
- yn = input.nextLine();
- }
- if ("y".equals(yn)){
- donation = Math.ceil(basket_price) - basket_price; //Calculates the donation to make the user feel better about themselves
- System.out.printf("Thank you for your %.2f" + "£ , now those orphans won't starve tonight!", donation); //Shows only two numbers after the decimal point
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement