Advertisement
fsoc131y

LavaJab

Aug 6th, 2023 (edited)
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | Source Code | 0 0
  1. 1. Experiment 1:
  2. ------>
  3. import java.util.Scanner;
  4.  
  5. public class ECommerceEntryExitControl {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int totalCustomers = 0;
  9.         int currentCustomers = 0;
  10.         boolean isOpen = false;
  11.  
  12.         // Entry Control Loop - Using do-while loop
  13.         do {
  14.             System.out.print("Enter '1' to open the store, '0' to exit: ");
  15.             int choice = scanner.nextInt();
  16.  
  17.             if (choice == 1) {
  18.                 isOpen = true;
  19.                 System.out.println("Store is now open!");
  20.                 break;
  21.             } else if (choice == 0) {
  22.                 System.out.println("Exiting the program...");
  23.                 return;
  24.             } else {
  25.                 System.out.println("Invalid input, please try again.");
  26.             }
  27.         } while (true);
  28.  
  29.         // While loop to control customer entry and exit
  30.         while (isOpen) {
  31.             System.out.print("Enter '1' for a customer entering, '2' for a customer leaving, '0' to close the store: ");
  32.             int action = scanner.nextInt();
  33.  
  34.             if (action == 0) {
  35.                 isOpen = false;
  36.                 break;
  37.             } else if (action == 1) {
  38.                 currentCustomers++;
  39.                 totalCustomers++;
  40.                 System.out.println("Customer entered. Current customers: " + currentCustomers);
  41.             } else if (action == 2) {
  42.                 if (currentCustomers > 0) {
  43.                     currentCustomers--;
  44.                     System.out.println("Customer left. Current customers: " + currentCustomers);
  45.                 } else {
  46.                     System.out.println("No customers to leave.");
  47.                 }
  48.             } else {
  49.                 System.out.println("Invalid input, please try again.");
  50.             }
  51.         }
  52.  
  53.         // Exit Control Loop - Using for loop
  54.         System.out.println("Store is now closed.");
  55.         System.out.println("Total customers served today: " + totalCustomers);
  56.         scanner.close();
  57.     }
  58. }
  59.  
  60. 2. Experiment 2:
  61. --------->
  62. import java.io.*;
  63. import java.util.Scanner;
  64.  
  65. public class EcomSwitch {
  66.     public static void main(String[] args) {
  67.         Scanner scanner = new Scanner(System.in);
  68.  
  69.         System.out.println("Welcome to the E-Commerce Store!");
  70.         System.out.println("Select a product to view its price:");
  71.         System.out.println("1. Laptop");
  72.         System.out.println("2. Smartphone");
  73.         System.out.println("3. Headphones");
  74.         System.out.println("4. Exit");
  75.  
  76.         int choice;
  77.         do {
  78.             System.out.print("Enter your choice (1-4): ");
  79.             choice = scanner.nextInt();
  80.  
  81.             switch (choice) {
  82.                 case 1:
  83.                     System.out.println("Laptop: $800");
  84.                     break;
  85.                 case 2:
  86.                     System.out.println("Smartphone: $500");
  87.                     break;
  88.                 case 3:
  89.                     System.out.println("Headphones: $50");
  90.                     break;
  91.                 case 4:
  92.                     System.out.println("Thank you for visiting the store!");
  93.                     break;
  94.                 default:
  95.                     System.out.println("Invalid choice. Please enter a valid option.");
  96.             }
  97.         } while (choice != 4);
  98.  
  99.         scanner.close();
  100.     }
  101. }
  102.  
  103. 3. Experiment 3:
  104. -------->
  105. import java.io.*;
  106. import java.util.Scanner;
  107.  
  108. class Ecomm {
  109.     public static void main(String args[]) throws IOException {
  110.         Scanner d = new Scanner(System.in);
  111.         String num[] = {"Soap", "Pencil", "Notebook", "Detergent(1 Kg)", "Pen", "Clothes"};
  112.  
  113.         System.out.println("Item present in List");
  114.         for (int i = 0; i < num.length; i++) {
  115.             System.out.println((i + 1) + ". " + num[i]);
  116.         }
  117.  
  118.         System.out.print("Enter how many items you want to add: ");
  119.         int m = d.nextInt();
  120.         d.nextLine();
  121.  
  122.         String arr[][] = new String[m][2];
  123.         for (int j = 0; j < m; j++) {
  124.             System.out.print("Enter the item " + (j + 1) + ": ");
  125.             arr[j][0] = d.nextLine();
  126.             System.out.print("Enter the quantity: ");
  127.             arr[j][1] = d.nextLine();
  128.         }
  129.  
  130.         boolean itemsMatch = true;
  131.         for (int j = 0; j < m; j++) {
  132.             boolean found = false;
  133.             for (int i = 0; i < num.length; i++) {
  134.                 if (arr[j][0].equalsIgnoreCase(num[i])) {
  135.                     found = true;
  136.                     break;
  137.                 }
  138.             }
  139.             if (!found) {
  140.                 itemsMatch = false;
  141.                 break;
  142.             }
  143.         }
  144.  
  145.         if (itemsMatch) {
  146.             System.out.println("\nYour item\tQuantity\tPrice");
  147.             for (int j = 0; j < m; j++) {
  148.                 double price = calculatePrice(arr[j][0].toLowerCase(), Integer.parseInt(arr[j][1]));
  149.                 System.out.println(arr[j][0] + "\t\t" + arr[j][1] + "\t\t$" + price);
  150.             }
  151.         } else {
  152.             System.out.println("Your item list doesn't match with the items in the store.");
  153.         }
  154.     }
  155.  
  156.     private static double calculatePrice(String item, int quantity) {
  157.         double pricePerUnit;
  158.         switch (item) {
  159.             case "soap":
  160.                 pricePerUnit = 1.0;
  161.                 break;
  162.             case "pencil":
  163.                 pricePerUnit = 0.5;
  164.                 break;
  165.             case "notebook":
  166.                 pricePerUnit = 2.0;
  167.                 break;
  168.             case "detergent(1 kg)":
  169.                 pricePerUnit = 3.5;
  170.                 break;
  171.             case "pen":
  172.                 pricePerUnit = 1.2;
  173.                 break;
  174.             case "clothes":
  175.                 pricePerUnit = 10.0;
  176.                 break;
  177.             default:
  178.                 pricePerUnit = 0.0; // Item not found in the list
  179.         }
  180.  
  181.         return pricePerUnit * quantity;
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement