Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Experiment 1:
- ------>
- import java.util.Scanner;
- public class ECommerceEntryExitControl {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int totalCustomers = 0;
- int currentCustomers = 0;
- boolean isOpen = false;
- // Entry Control Loop - Using do-while loop
- do {
- System.out.print("Enter '1' to open the store, '0' to exit: ");
- int choice = scanner.nextInt();
- if (choice == 1) {
- isOpen = true;
- System.out.println("Store is now open!");
- break;
- } else if (choice == 0) {
- System.out.println("Exiting the program...");
- return;
- } else {
- System.out.println("Invalid input, please try again.");
- }
- } while (true);
- // While loop to control customer entry and exit
- while (isOpen) {
- System.out.print("Enter '1' for a customer entering, '2' for a customer leaving, '0' to close the store: ");
- int action = scanner.nextInt();
- if (action == 0) {
- isOpen = false;
- break;
- } else if (action == 1) {
- currentCustomers++;
- totalCustomers++;
- System.out.println("Customer entered. Current customers: " + currentCustomers);
- } else if (action == 2) {
- if (currentCustomers > 0) {
- currentCustomers--;
- System.out.println("Customer left. Current customers: " + currentCustomers);
- } else {
- System.out.println("No customers to leave.");
- }
- } else {
- System.out.println("Invalid input, please try again.");
- }
- }
- // Exit Control Loop - Using for loop
- System.out.println("Store is now closed.");
- System.out.println("Total customers served today: " + totalCustomers);
- scanner.close();
- }
- }
- 2. Experiment 2:
- --------->
- import java.io.*;
- import java.util.Scanner;
- public class EcomSwitch {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Welcome to the E-Commerce Store!");
- System.out.println("Select a product to view its price:");
- System.out.println("1. Laptop");
- System.out.println("2. Smartphone");
- System.out.println("3. Headphones");
- System.out.println("4. Exit");
- int choice;
- do {
- System.out.print("Enter your choice (1-4): ");
- choice = scanner.nextInt();
- switch (choice) {
- case 1:
- System.out.println("Laptop: $800");
- break;
- case 2:
- System.out.println("Smartphone: $500");
- break;
- case 3:
- System.out.println("Headphones: $50");
- break;
- case 4:
- System.out.println("Thank you for visiting the store!");
- break;
- default:
- System.out.println("Invalid choice. Please enter a valid option.");
- }
- } while (choice != 4);
- scanner.close();
- }
- }
- 3. Experiment 3:
- -------->
- import java.io.*;
- import java.util.Scanner;
- class Ecomm {
- public static void main(String args[]) throws IOException {
- Scanner d = new Scanner(System.in);
- String num[] = {"Soap", "Pencil", "Notebook", "Detergent(1 Kg)", "Pen", "Clothes"};
- System.out.println("Item present in List");
- for (int i = 0; i < num.length; i++) {
- System.out.println((i + 1) + ". " + num[i]);
- }
- System.out.print("Enter how many items you want to add: ");
- int m = d.nextInt();
- d.nextLine();
- String arr[][] = new String[m][2];
- for (int j = 0; j < m; j++) {
- System.out.print("Enter the item " + (j + 1) + ": ");
- arr[j][0] = d.nextLine();
- System.out.print("Enter the quantity: ");
- arr[j][1] = d.nextLine();
- }
- boolean itemsMatch = true;
- for (int j = 0; j < m; j++) {
- boolean found = false;
- for (int i = 0; i < num.length; i++) {
- if (arr[j][0].equalsIgnoreCase(num[i])) {
- found = true;
- break;
- }
- }
- if (!found) {
- itemsMatch = false;
- break;
- }
- }
- if (itemsMatch) {
- System.out.println("\nYour item\tQuantity\tPrice");
- for (int j = 0; j < m; j++) {
- double price = calculatePrice(arr[j][0].toLowerCase(), Integer.parseInt(arr[j][1]));
- System.out.println(arr[j][0] + "\t\t" + arr[j][1] + "\t\t$" + price);
- }
- } else {
- System.out.println("Your item list doesn't match with the items in the store.");
- }
- }
- private static double calculatePrice(String item, int quantity) {
- double pricePerUnit;
- switch (item) {
- case "soap":
- pricePerUnit = 1.0;
- break;
- case "pencil":
- pricePerUnit = 0.5;
- break;
- case "notebook":
- pricePerUnit = 2.0;
- break;
- case "detergent(1 kg)":
- pricePerUnit = 3.5;
- break;
- case "pen":
- pricePerUnit = 1.2;
- break;
- case "clothes":
- pricePerUnit = 10.0;
- break;
- default:
- pricePerUnit = 0.0; // Item not found in the list
- }
- return pricePerUnit * quantity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement