Advertisement
Guest User

javacode

a guest
Nov 18th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.79 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package onlinestorestable2;
  7.  
  8. import static java.lang.System.out;
  9. import java.util.Scanner;
  10. /**
  11.  *
  12.  * @author banan
  13.  */
  14. public class OnlineStoreStable2 {
  15.    
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21.         //Products go here
  22.         String[] fruit = {"Bananas", "Apples", "Grapes", "Apricots"};
  23.         String[] vegetables = {"Cucumbers", "Tomatoes", "Potatoes", "Onions", "Carrots"};
  24.         String[] cheeses = {"Haloumi", "Gouda", "Feta", "Mozzarella"};
  25.         //Prices go here
  26.         double[] fruitprices = {1.59, 1.67, 2.09, 1.23};
  27.         double[] vegetablesprices = {2.24, 1.12, 2.03, 1.73, 2.34};
  28.         double[] cheesesprices = {1.38, 3.21, 2.89, 3.26,};
  29.         //Product Numbers go here
  30.         int[] fruitnumbers = {0, 1, 2, 3};
  31.         int[] vegetablesnumbers = {0, 1, 2, 3 ,4};
  32.         int[] cheesesnumbers= {0, 1 ,2 ,3};
  33.         //Declerations go here
  34.         double donation = 0;
  35.         double basket_price = 0;
  36.         double amount = 0;
  37.         double selection_price = 0;
  38.         int selection = 0;
  39.         int product_selection = 0;
  40.         int amount_int = 0;
  41.         String yn = "a";
  42.         //Program starts here
  43.         Scanner input = new Scanner(System.in);
  44.         boolean check = false;
  45.         while (check == false){
  46.             System.out.println("Please choose one of the following categories, alternatively press 9 to proceed to check out."); // Prints the Category Menu
  47.             System.out.println("1.Fruit");
  48.             System.out.println("2.Vegetables");
  49.             System.out.println("3.Cheeses");
  50.             System.out.println("4.Check out");
  51.             selection = input.nextInt();
  52.             input.nextLine(); //advances the scanner to the next line break
  53.             if (selection > 4 || selection < 1){ //Reminds the user to choose a value between and including 1 and 3
  54.                 System.out.println("Please enter a valid number between and including 1 and 3, or 4 to proceed to check out");
  55.             }else if(selection == 1){ // Fruit Selection
  56.                 while (true){
  57.                     System.out.println("Product No.         Prices        Products"); // Displays fruit menu
  58.                     for (int i = 0; i<4; i++){
  59.                         System.out.println(fruitnumbers[i] + "                   " + fruitprices[i] + "          " + fruit[i]);
  60.                     }
  61.                     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.");
  62.                     product_selection = input.nextInt(); // Lets the user select their desired fruits
  63.                     if (product_selection == 9){
  64.                         break;
  65.                     }
  66.                     System.out.print("You have selected " + fruit[product_selection] + ", is that correct? (y/n)"); //Makes sure that the user has selected the correct item
  67.                     input.nextLine();
  68.                     yn = input.nextLine();
  69.                     if ("n".equals(yn)){ //Sends the user back to the main menu in case they have selected the wrong item
  70.                         break;
  71.                     }
  72.                     else if ("y".equals(yn)){
  73.                         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
  74.                         amount = input.nextInt();
  75.                         selection_price = amount * fruitprices[product_selection];//Calculates how much all of the selected products cost
  76.                         amount_int = (int)amount; //turns the amount double into an int in order to use it for calculations
  77.                         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
  78.                         input.nextLine();
  79.                         yn = input.nextLine();
  80.                         if ("y".equals(yn)){
  81.                             basket_price = basket_price+selection_price;
  82.                             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);
  83.                             yn = input.nextLine();
  84.                             if ("c".equals(yn)){
  85.                                 break;
  86.                             }
  87.                             else if("q".equals(yn)){
  88.                                 check = true;  
  89.                             }
  90.                         }
  91.                     }
  92.                     if(check){ // Sends the user to the check out
  93.                         break;
  94.                     }
  95.                 }
  96.             }
  97.             else if(selection == 2){ //Vegetable Selection
  98.                 while (true){
  99.                     System.out.println("Product No.         Prices        Products");// Displays the menu
  100.                     for (int i = 0; i<4; i++){
  101.                         System.out.println(vegetablesnumbers[i] + "                   " + vegetablesprices[i] + "          " + vegetables[i]);
  102.                     }
  103.                     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.");
  104.                     product_selection = input.nextInt();// Lets the user select their desired product
  105.                     if (product_selection == 9){ // Takes the user back to the main menu
  106.                         break;
  107.                     }
  108.                     System.out.print("You have selected " + vegetables[product_selection] + ", is that correct? (y/n)");// makes sure that the user has selected the correct product
  109.                     input.nextLine();
  110.                     yn = input.nextLine();
  111.                     if ("n".equals(yn)){ // Sends the user back to the menu
  112.                         break;
  113.                     }
  114.                     else{
  115.                         System.out.println("Select the amount of the selected products you would like to add to your cart");
  116.                         amount = input.nextInt();// Lets the user select the desired amount of the selected product
  117.                         selection_price = amount * vegetablesprices[product_selection];
  118.                         amount_int = (int)amount;//Turns the amount double into an int in order to do calculations
  119.                         System.out.printf("Would you like to add " + amount_int + " " + vegetables[product_selection] + " to your cart? They cost %.2f" +  "£ (y/n)" ,selection_price  );
  120.                         input.nextLine();//Makes sure that the user has selected the correct amount of products
  121.                         yn = input.nextLine();
  122.                         if ("y".equals(yn)){
  123.                             basket_price = basket_price+selection_price;
  124.                             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 );
  125.                             yn = input.nextLine();//Lets the user continue with their order or proceed to check out
  126.                             if ("c".equals(yn)){ // Continues order
  127.                                 break;
  128.                             }
  129.                             else if("q".equals(yn)){ //Sends user to check out
  130.                                 check = true;  
  131.                             }
  132.                         }
  133.                     }
  134.                     if(check){ // Sends the user to the check out
  135.                         break;
  136.                     }
  137.                 }
  138.             }
  139.             else if(selection == 3){ //Displays Cheeses Menu
  140.                 while (true){
  141.                     System.out.println("Product No.         Prices        Products");
  142.                     for (int i = 0; i<4; i++){
  143.                         System.out.println(cheesesnumbers[i] + "                   " + cheesesprices[i] + "          " + cheeses[i]);
  144.                     }
  145.                     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.");
  146.                     product_selection = input.nextInt(); // Lets the user choose the desired product, or takes them to the check out
  147.                     if (product_selection == 9){ //Takes user to the check out
  148.                         break;
  149.                     }
  150.                     System.out.print("You have selected " + cheeses[product_selection] + ", is that correct? (y/n)");
  151.                     input.nextLine(); // Makes sure that the user has selected the correct product
  152.                     yn = input.nextLine();
  153.                     if ("n".equals(yn)){
  154.                         break; //Takes user back to the menu
  155.                     }
  156.                     else{
  157.                         System.out.println("Select the amount of the selected products you would like to add to your cart");
  158.                         amount = input.nextInt(); //Lets the user select the desired amount of selected products
  159.                         selection_price = amount * cheesesprices[product_selection];
  160.                         amount_int = (int)amount; // Turns the amount double into an integer for calculations
  161.                         System.out.printf("Would you like to add " + amount_int + " " + cheeses[product_selection] + " to your cart? They cost %.2f" +  "£ (y/n)" ,selection_price  );
  162.                         input.nextLine(); // Makes sure that the user has selected the correct amount of the selected products
  163.                         yn = input.nextLine();
  164.                         if ("y".equals(yn)){
  165.                             basket_price = basket_price+selection_price; //Adds products to the cart
  166.                             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);
  167.                             yn = input.nextLine();
  168.                             if ("c".equals(yn)){
  169.                                 break; //Takes user to the menu
  170.                             }
  171.                             else if("q".equals(yn)){
  172.                                 check = true;  //Takes user to the check out
  173.                             }
  174.                         }
  175.                     }
  176.                     if(check){ // Sends the user to the check out
  177.                         break;
  178.                     }
  179.                 }
  180.             }
  181.             else if (selection == 4){ // Sends the user to the check out
  182.                 check = true;
  183.                 break;
  184.             }
  185.         }
  186.         System.out.printf("The total amount for the products you have selected is %.2f" + " £, is that ok? (y/n)", basket_price );
  187.         yn = input.nextLine();
  188.         if ("y".equals(yn)){ // Asks the user if they would like to make a donation
  189.             System.out.println("Great!, would you like us to round up the cost and donate it to charity? (y/n)");
  190.             yn = input.nextLine();
  191.         }
  192.         if ("y".equals(yn)){
  193.             donation = Math.ceil(basket_price) - basket_price; //Calculates the donation to make the user feel better about themselves
  194.             System.out.printf("Thank you for your %.2f" + "£ , now those orphans won't starve tonight!", donation); //Shows only two numbers after the decimal point
  195.         }
  196.     }          
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement