Advertisement
fsoc131y

complex

Oct 11th, 2023
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 29.61 KB | Source Code | 0 0
  1. Simple:
  2. Old: https://pastebin.com/bsXH7eHU
  3.  
  4. Exp 1: entry/exit loop
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. public class ECommerceLoopingDemo {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         ArrayList<String> shoppingCart = new ArrayList<>();
  12.  
  13.         int choice;
  14.  
  15.         do {
  16.             System.out.println("E-Commerce Shopping Cart:");
  17.             System.out.println("1. Add item to cart");
  18.             System.out.println("2. Remove item from cart");
  19.             System.out.println("3. View cart");
  20.             System.out.println("4. Checkout");
  21.             System.out.println("5. Exit");
  22.             System.out.print("Enter your choice: ");
  23.             choice = scanner.nextInt();
  24.  
  25.             switch (choice) {
  26.                 case 1:
  27.                     System.out.print("Enter the item to add: ");
  28.                     String itemToAdd = scanner.next();
  29.                     shoppingCart.add(itemToAdd);
  30.                     System.out.println(itemToAdd + " added to the cart.");
  31.                     break;
  32.  
  33.                 case 2:
  34.                     System.out.print("Enter the item to remove: ");
  35.                     String itemToRemove = scanner.next();
  36.                     if (shoppingCart.contains(itemToRemove)) {
  37.                         shoppingCart.remove(itemToRemove);
  38.                         System.out.println(itemToRemove + " removed from the cart.");
  39.                     } else {
  40.                         System.out.println("Item not found in the cart.");
  41.                     }
  42.                     break;
  43.  
  44.                 case 3:
  45.                     System.out.println("Shopping Cart Contents:");
  46.                     for (String item : shoppingCart) {
  47.                         System.out.println("- " + item);
  48.                     }
  49.                     break;
  50.  
  51.                 case 4:
  52.                     if (shoppingCart.isEmpty()) {
  53.                         System.out.println("Your cart is empty. Cannot proceed to checkout.");
  54.                     } else {
  55.                         System.out.println("Checkout Successful!");
  56.                         shoppingCart.clear();
  57.                     }
  58.                     break;
  59.  
  60.                 case 5:
  61.                     System.out.println("Exiting the program.");
  62.                     break;
  63.  
  64.                 default:
  65.                     System.out.println("Invalid choice. Please try again.");
  66.             }
  67.         } while (choice != 5);
  68.  
  69.         scanner.close();
  70.     }
  71. }
  72.  
  73. Exp 2: nesting of switch statement
  74. import java.util.Scanner;
  75.  
  76. public class ECommerceOrderProcessing {
  77.     public static void main(String[] args) {
  78.         Scanner scanner = new Scanner(System.in);
  79.         int orderState = 0;
  80.         boolean orderComplete = false;
  81.  
  82.         while (!orderComplete) {
  83.             System.out.println("E-Commerce Order Processing System:");
  84.             System.out.println("1. New Order");
  85.             System.out.println("2. Order Processing");
  86.             System.out.println("3. Shipped");
  87.             System.out.println("4. Delivered");
  88.             System.out.println("5. Cancel Order");
  89.             System.out.println("6. Exit");
  90.             System.out.print("Enter your choice: ");
  91.             int action = scanner.nextInt();
  92.  
  93.             switch (orderState) {
  94.                 case 0: // New Order
  95.                     switch (action) {
  96.                         case 1:
  97.                             System.out.println("Order placed. Awaiting processing.");
  98.                             orderState = 1;
  99.                             break;
  100.                         case 6:
  101.                             orderComplete = true;
  102.                             break;
  103.                         default:
  104.                             System.out.println("Invalid action for the current order state.");
  105.                             break;
  106.                     }
  107.                     break;
  108.  
  109.                 case 1: // Order Processing
  110.                     switch (action) {
  111.                         case 2:
  112.                             System.out.println("Order is being processed.");
  113.                             orderState = 2;
  114.                             break;
  115.                         case 5:
  116.                             System.out.println("Order canceled.");
  117.                             orderState = 0;
  118.                             break;
  119.                         case 6:
  120.                             orderComplete = true;
  121.                             break;
  122.                         default:
  123.                             System.out.println("Invalid action for the current order state.");
  124.                             break;
  125.                     }
  126.                     break;
  127.  
  128.                 case 2: // Shipped
  129.                     switch (action) {
  130.                         case 3:
  131.                             System.out.println("Order has been shipped.");
  132.                             orderState = 3;
  133.                             break;
  134.                         case 5:
  135.                             System.out.println("Order canceled.");
  136.                             orderState = 0;
  137.                             break;
  138.                         case 6:
  139.                             orderComplete = true;
  140.                             break;
  141.                         default:
  142.                             System.out.println("Invalid action for the current order state.");
  143.                             break;
  144.                     }
  145.                     break;
  146.  
  147.                 case 3: // Delivered
  148.                     switch (action) {
  149.                         case 4:
  150.                             System.out.println("Order has been delivered. Thank you for your purchase!");
  151.                             orderState = 0;
  152.                             break;
  153.                         case 5:
  154.                             System.out.println("Order canceled.");
  155.                             orderState = 0;
  156.                             break;
  157.                         case 6:
  158.                             orderComplete = true;
  159.                             break;
  160.                         default:
  161.                             System.out.println("Invalid action for the current order state.");
  162.                             break;
  163.                     }
  164.                     break;
  165.  
  166.                 default:
  167.                     System.out.println("Invalid order state.");
  168.                     break;
  169.             }
  170.         }
  171.  
  172.         System.out.println("Exiting the order processing system.");
  173.         scanner.close();
  174.     }
  175. }
  176.  
  177. Exp 3: single and multi dimensional arrays
  178. import java.util.Scanner;
  179.  
  180. public class Lab3 {
  181.     public static void main(String[] args) {
  182.         Scanner scanner = new Scanner(System.in);
  183.  
  184.         // Single-dimensional array to represent a product catalog
  185.         String[] products = {
  186.             "Product A",
  187.             "Product B",
  188.             "Product C",
  189.             "Product D",
  190.             "Product E"
  191.         };
  192.  
  193.         // Multi-dimensional array to represent a shopping cart (product, quantity)
  194.         String[][] shoppingCart = new String[5][2];
  195.         int cartSize = 0;
  196.  
  197.         boolean shopping = true;
  198.  
  199.         while (shopping) {
  200.             System.out.println("E-Commerce Product Catalog:");
  201.             for (int i = 0; i < products.length; i++) {
  202.                 System.out.println((i + 1) + ". " + products[i]);
  203.             }
  204.             System.out.println("6. View Cart");
  205.             System.out.println("7. Checkout");
  206.             System.out.println("8. Exit");
  207.             System.out.print("Enter your choice: ");
  208.             int choice = scanner.nextInt();
  209.  
  210.             if (choice >= 1 && choice <= products.length) {
  211.                 System.out.print("Enter the quantity: ");
  212.                 int quantity = scanner.nextInt();
  213.                 if (quantity > 0) {
  214.                     shoppingCart[cartSize][0] = products[choice - 1];
  215.                     shoppingCart[cartSize][1] = String.valueOf(quantity);
  216.                     cartSize++;
  217.                     System.out.println("Item added to the cart.");
  218.                 } else {
  219.                     System.out.println("Quantity must be greater than 0.");
  220.                 }
  221.             } else {
  222.                 switch (choice) {
  223.                     case 6:
  224.                         System.out.println("Shopping Cart Contents:");
  225.                         for (int i = 0; i < cartSize; i++) {
  226.                             System.out.println(shoppingCart[i][0] + " (Quantity: " + shoppingCart[i][1] + ")");
  227.                         }
  228.                         break;
  229.                     case 7:
  230.                         System.out.println("Checkout Successful!");
  231.                         cartSize = 0;
  232.                         break;
  233.                     case 8:
  234.                         System.out.println("Exiting the program.");
  235.                         shopping = false;
  236.                         break;
  237.                     default:
  238.                         System.out.println("Invalid choice. Please try again.");
  239.                         break;
  240.                 }
  241.             }
  242.         }
  243.  
  244.         scanner.close();
  245.     }
  246. }
  247.  
  248. Exp 4: constructor & method overloading
  249. class Product {
  250.     private int productId;
  251.     private String productName;
  252.     private double price;
  253.  
  254.     // Default constructor
  255.     public Product() {
  256.         productId = 0;
  257.         productName = "No Product";
  258.         price = 0.0;
  259.     }
  260.  
  261.     // Constructor with product name and price
  262.     public Product(String productName, double price) {
  263.         this.productId = generateProductId();
  264.         this.productName = productName;
  265.         this.price = price;
  266.     }
  267.  
  268.     // Constructor with all attributes
  269.     public Product(int productId, String productName, double price) {
  270.         this.productId = productId;
  271.         this.productName = productName;
  272.         this.price = price;
  273.     }
  274.  
  275.     // Method to display product details
  276.     public void displayProduct() {
  277.         System.out.println("Product ID: " + productId);
  278.         System.out.println("Product Name: " + productName);
  279.         System.out.println("Price: $" + price);
  280.     }
  281.  
  282.     // Method overloading: Calculate and display discounted price
  283.     public void displayProduct(double discountPercentage) {
  284.         double discountedPrice = price - (price * discountPercentage / 100);
  285.         System.out.println("Product ID: " + productId);
  286.         System.out.println("Product Name: " + productName);
  287.         System.out.println("Price: $" + price);
  288.         System.out.println("Discounted Price: $" + discountedPrice);
  289.     }
  290.  
  291.     // Generate a unique product ID (for demonstration purposes)
  292.     private int generateProductId() {
  293.         // In a real e-commerce system, this would be a more sophisticated logic
  294.         return (int) (Math.random() * 1000);
  295.     }
  296. }
  297.  
  298. public class Lab4 {
  299.     public static void main(String[] args) {
  300.         // Create products using overloaded constructors
  301.         Product product1 = new Product();
  302.         Product product2 = new Product("Laptop", 799.99);
  303.         Product product3 = new Product(1001, "Smartphone", 299.99);
  304.  
  305.         // Display product details using overloaded methods
  306.         product1.displayProduct();
  307.         System.out.println();
  308.  
  309.         product2.displayProduct();
  310.         System.out.println();
  311.  
  312.         product3.displayProduct(10); // 10% discount
  313.     }
  314. }
  315.  
  316. Exp 5: static keyword
  317. class Product {
  318.     private int productId;
  319.     private String productName;
  320.     private double price;
  321.  
  322.     // Static variable to keep track of the total number of products
  323.     private static int totalProducts = 0;
  324.  
  325.     public Product(String productName, double price) {
  326.         this.productId = generateProductId();
  327.         this.productName = productName;
  328.         this.price = price;
  329.         totalProducts++;
  330.     }
  331.  
  332.     public void displayProduct() {
  333.         System.out.println("Product ID: " + productId);
  334.         System.out.println("Product Name: " + productName);
  335.         System.out.println("Price: $" + price);
  336.     }
  337.  
  338.     // Static method to get the total number of products
  339.     public static int getTotalProducts() {
  340.         return totalProducts;
  341.     }
  342.  
  343.     // Generate a unique product ID (for demonstration purposes)
  344.     private int generateProductId() {
  345.         // In a real e-commerce system, this would be a more sophisticated logic
  346.         return (int) (Math.random() * 1000);
  347.     }
  348. }
  349.  
  350. public class ECommerceWithStatic {
  351.     public static void main(String[] args) {
  352.         // Create products and display product details
  353.         Product product1 = new Product("Laptop", 799.99);
  354.         Product product2 = new Product("Smartphone", 299.99);
  355.  
  356.         product1.displayProduct();
  357.         System.out.println();
  358.  
  359.         product2.displayProduct();
  360.         System.out.println();
  361.  
  362.         // Get the total number of products using the static method
  363.         int totalProducts = Product.getTotalProducts();
  364.         System.out.println("Total Products in the Store: " + totalProducts);
  365.     }
  366. }
  367.  
  368. Exp 6: multi-level inheritance
  369. // Base class representing a product
  370. class Product {
  371.     private String name;
  372.     private double price;
  373.  
  374.     public Product(String name, double price) {
  375.         this.name = name;
  376.         this.price = price;
  377.     }
  378.  
  379.     public String getName() {
  380.         return name;
  381.     }
  382.  
  383.     public double getPrice() {
  384.         return price;
  385.     }
  386.  
  387.     public void displayProductInfo() {
  388.         System.out.println("Product: " + name);
  389.         System.out.println("Price: $" + price);
  390.     }
  391. }
  392.  
  393. // Subclass representing electronic products
  394. class Electronics extends Product {
  395.     private String brand;
  396.  
  397.     public Electronics(String name, double price, String brand) {
  398.         super(name, price);
  399.         this.brand = brand;
  400.     }
  401.  
  402.     public String getBrand() {
  403.         return brand;
  404.     }
  405.  
  406.     @Override
  407.     public void displayProductInfo() {
  408.         super.displayProductInfo();
  409.         System.out.println("Brand: " + brand);
  410.     }
  411. }
  412.  
  413. // Subclass representing laptops
  414. class Laptop extends Electronics {
  415.     private String processor;
  416.  
  417.     public Laptop(String name, double price, String brand, String processor) {
  418.         super(name, price, brand);
  419.         this.processor = processor;
  420.     }
  421.  
  422.     public String getProcessor() {
  423.         return processor;
  424.     }
  425.  
  426.     @Override
  427.     public void displayProductInfo() {
  428.         super.displayProductInfo();
  429.         System.out.println("Processor: " + processor);
  430.     }
  431. }
  432.  
  433. public class Multi {
  434.     public static void main(String[] args) {
  435.         // Create a Laptop object
  436.         Laptop laptop = new Laptop("Dell XPS 13", 1299.99, "Dell", "Intel Core i7");
  437.  
  438.         // Display product information
  439.         laptop.displayProductInfo();
  440.     }
  441. }
  442.  
  443. Exp 7: super & this keyword
  444. class Product {
  445.     public int productId;
  446.     public String productName;
  447.     public double price;
  448.  
  449.     public Product(int productId, String productName, double price) {
  450.         this.productId = productId;
  451.         this.productName = productName;
  452.         this.price = price;
  453.     }
  454.  
  455.     public void displayProduct() {
  456.         System.out.println("Product ID: " + productId);
  457.         System.out.println("Product Name: " + productName);
  458.         System.out.println("Price: $" + price);
  459.     }
  460.  
  461.     public double getPrice() {
  462.         return price;
  463.     }
  464. }
  465.  
  466. class DiscountedProduct extends Product {
  467.     public double discount;
  468.  
  469.     public DiscountedProduct(int productId, String productName, double price, double discount) {
  470.         super(productId, productName, price);
  471.         this.discount = discount;
  472.     }
  473.  
  474.     public void displayProductWithDiscount() {
  475.         super.displayProduct();
  476.         System.out.println("Discount: " + discount + "%");
  477.         double discountedPrice = getPrice() - (getPrice() * discount / 100);
  478.         System.out.println("Discounted Price: $" + discountedPrice);
  479.     }
  480. }
  481.  
  482. public class Lab7 {
  483.     public static void main(String[] args) {
  484.         Product product = new Product(1, "Laptop", 799.99);
  485.         DiscountedProduct discountedProduct = new DiscountedProduct(2, "Smartphone", 299.99, 10.0);
  486.  
  487.         System.out.println("Product Details:");
  488.         product.displayProduct();
  489.         System.out.println();
  490.  
  491.         System.out.println("Discounted Product Details:");
  492.         discountedProduct.displayProductWithDiscount();
  493.     }
  494. }
  495.  
  496. Exp 8: abstract & final keywords
  497. import java.util.Scanner;
  498.  
  499. abstract class ECommerceProduct {
  500.     private String name;
  501.     private double price;
  502.  
  503.     public ECommerceProduct(String name, double price) {
  504.         this.name = name;
  505.         this.price = price;
  506.     }
  507.  
  508.     public String getName() {
  509.         return name;
  510.     }
  511.  
  512.     public double getPrice() {
  513.         return price;
  514.     }
  515.  
  516.     public abstract void displayProductDetails();
  517. }
  518.  
  519. final class Book extends ECommerceProduct {
  520.     private String author;
  521.     private int numberOfPages;
  522.  
  523.     public Book(String name, double price, String author, int numberOfPages) {
  524.         super(name, price);
  525.         this.author = author;
  526.         this.numberOfPages = numberOfPages;
  527.     }
  528.  
  529.     @Override
  530.     public void displayProductDetails() {
  531.         System.out.println("Book Details:");
  532.         System.out.println("Name: " + getName());
  533.         System.out.println("Price: " + getPrice());
  534.         System.out.println("Author: " + author);
  535.         System.out.println("Number of Pages: " + numberOfPages);
  536.     }
  537. }
  538.  
  539. public class Exp8 {
  540.     public static void main(String[] args) {
  541.         Scanner scanner = new Scanner(System.in);
  542.  
  543.         System.out.println("Enter the book name: ");
  544.         String bookName = scanner.nextLine();
  545.  
  546.         System.out.println("Enter the book price: ");
  547.         double bookPrice = scanner.nextDouble();
  548.  
  549.         System.out.println("Enter the book author: ");
  550.         String bookAuthor = scanner.nextLine();
  551.  
  552.         System.out.println("Enter the number of pages in the book: ");
  553.         int bookNumberOfPages = scanner.nextInt();
  554.  
  555.         Book book = new Book(bookName, bookPrice, bookAuthor, bookNumberOfPages);
  556.  
  557.         book.displayProductDetails();
  558.         scanner.close();
  559.     }
  560. }
  561.  
  562. Exp 9: methods of String class
  563. public class Exp9 {
  564.  
  565.     public static void main(String[] args) {
  566.         String productTitle = "Apple iPhone 13 Pro";
  567.  
  568.         // Get the length of the product title
  569.         int productTitleLength = productTitle.length();
  570.         System.out.println("Product title length: " + productTitleLength);
  571.  
  572.         // Get the first character of the product title
  573.         char firstChar = productTitle.charAt(0);
  574.         System.out.println("First character of the product title: " + firstChar);
  575.  
  576.         // Get the last character of the product title
  577.         int lastIndex = productTitle.length() - 1;
  578.         char lastChar = productTitle.charAt(lastIndex);
  579.         System.out.println("Last character of the product title: " + lastChar);
  580.  
  581.         // Check if the product title contains the word "Apple"
  582.         boolean containsApple = productTitle.contains("Apple");
  583.         System.out.println("Product title contains the word 'Apple': " + containsApple);
  584.  
  585.         // Replace the word "iPhone" with the word "Smartphone" in the product title
  586.         String newProductTitle = productTitle.replace("iPhone", "Smartphone");
  587.         System.out.println("New product title: " + newProductTitle);
  588.  
  589.         // Convert the product title to lowercase
  590.         String lowercaseProductTitle = productTitle.toLowerCase();
  591.         System.out.println("Lowercase product title: " + lowercaseProductTitle);
  592.  
  593.         // Convert the product title to uppercase
  594.         String uppercaseProductTitle = productTitle.toUpperCase();
  595.         System.out.println("Uppercase product title: " + uppercaseProductTitle);
  596.     }
  597. }
  598.  
  599. Exp 10: exception handling
  600. import java.util.ArrayList;
  601. import java.util.Scanner;
  602.  
  603. public class Exp10 {
  604.  
  605.     public static void main(String[] args) {
  606.         Scanner scanner = new Scanner(System.in);
  607.  
  608.         ProductRepository productRepository = new ProductRepository();
  609.  
  610.         System.out.println("Enter the product name: ");
  611.         String productName = scanner.nextLine();
  612.  
  613.         try {
  614.             addProductToCart(productRepository, productName);
  615.         } catch (ECommerceException e) {
  616.             System.out.println(e.getMessage());
  617.             scanner.close();
  618.         }
  619.     }
  620.  
  621.     public static void addProductToCart(ProductRepository productRepository, String productName) throws ECommerceException {
  622.         // Check if the product exists.
  623.         if (!ProductRepository.containsProduct(productName)) {
  624.             throw new ECommerceException("Product does not exist.");
  625.         }
  626.  
  627.         // Get the product from the repository.
  628.         Product product = ProductRepository.getProduct(productName);
  629.  
  630.         // Check if the product is in stock.
  631.         if (!product.isInStock()) {
  632.             throw new ECommerceException("Product is out of stock.");
  633.         }
  634.  
  635.         // Add the product to the cart.
  636.         // ...
  637.     }
  638. }
  639.  
  640. class ECommerceException extends Exception {
  641.  
  642.     public ECommerceException(String message) {
  643.         super(message);
  644.     }
  645. }
  646.  
  647. class ProductRepository {
  648.  
  649.     private static ArrayList<Product> products = new ArrayList<>();
  650.  
  651.     public static void addProduct(Product product) {
  652.         products.add(product);
  653.     }
  654.  
  655.     public static Product getProduct(String productName) {
  656.         for (Product product : products) {
  657.             if (product.getName().equals(productName)) {
  658.                 return product;
  659.             }
  660.         }
  661.         return null;
  662.     }
  663.  
  664.     public static boolean containsProduct(String productName) {
  665.         return getProduct(productName) != null;
  666.     }
  667. }
  668.  
  669. class Product {
  670.  
  671.     private String name;
  672.     private double price;
  673.     private int stockLevel;
  674.  
  675.     public Product(String name, double price, int stockLevel) {
  676.         this.name = name;
  677.         this.price = price;
  678.         this.stockLevel = stockLevel;
  679.     }
  680.  
  681.     public String getName() {
  682.         return name;
  683.     }
  684.  
  685.     public void setName(String name) {
  686.         this.name = name;
  687.     }
  688.  
  689.     public double getPrice() {
  690.         return price;
  691.     }
  692.  
  693.     public void setPrice(double price) {
  694.         this.price = price;
  695.     }
  696.  
  697.     public int getStockLevel() {
  698.         return stockLevel;
  699.     }
  700.  
  701.     public void setStockLevel(int stockLevel) {
  702.         this.stockLevel = stockLevel;
  703.     }
  704.  
  705.     public boolean isInStock() {
  706.         return stockLevel > 0;
  707.     }
  708. }
  709.  
  710. Exp 11: interface (packages aren't here)
  711. import java.util.ArrayList;
  712. import java.util.List;
  713.  
  714. public interface Exp11 {
  715.    void addProductToCart(String productName) throws ECommerceException;
  716.    void removeProductFromCart(String productName) throws ECommerceException;
  717.    void checkout() throws ECommerceException;
  718. }
  719.  
  720. class ECommerceServiceImpl implements Exp11 {
  721.  
  722.    private ShoppingCart shoppingCart;
  723.  
  724.    public ECommerceServiceImpl() {
  725.        this.shoppingCart = new ShoppingCart();
  726.    }
  727.  
  728.    @Override
  729.    public void addProductToCart(String productName) throws ECommerceException {
  730.        // Check if the product exists.
  731.        if (!ProductRepository.containsProduct(productName)) {
  732.            throw new ECommerceException("Product does not exist.");
  733.        }
  734.  
  735.        // Check if the product is in stock.
  736.        Product product = ProductRepository.getProduct(productName);
  737.        if (!product.isInStock()) {
  738.            throw new ECommerceException("Product is out of stock.");
  739.        }
  740.  
  741.        // Add the product to the cart.
  742.        shoppingCart.addProduct(product);
  743.    }
  744.  
  745.    @Override
  746.    public void removeProductFromCart(String productName) throws ECommerceException {
  747.        // Check if the product is in the cart.
  748.        Product product = shoppingCart.getProduct(productName);
  749.        if (product == null) {
  750.            throw new ECommerceException("Product is not in the cart.");
  751.        }
  752.  
  753.        // Remove the product from the cart.
  754.        shoppingCart.removeProduct(product);
  755.    }
  756.  
  757.    @Override
  758.    public void checkout() throws ECommerceException {
  759.        // Check if the cart is empty.
  760.        if (shoppingCart.isEmpty()) {
  761.            throw new ECommerceException("Cart is empty.");
  762.        }
  763.  
  764.        // Process the checkout.
  765.        // ...
  766.    }
  767. }
  768.  
  769. class ECommerceException extends Exception {
  770.  
  771.    public ECommerceException(String message) {
  772.        super(message);
  773.    }
  774. }
  775.  
  776. class ShoppingCart {
  777.  
  778.    private List<Product> products;
  779.  
  780.    public ShoppingCart() {
  781.        this.products = new ArrayList<>();
  782.    }
  783.  
  784.    public void addProduct(Product product) {
  785.        products.add(product);
  786.    }
  787.  
  788.    public Product getProduct(String productName) {
  789.        for (Product product : products) {
  790.            if (product.getName().equals(productName)) {
  791.                return product;
  792.            }
  793.        }
  794.        return null;
  795.    }
  796.  
  797.    public void removeProduct(Product product) {
  798.        products.remove(product);
  799.    }
  800.  
  801.    public boolean isEmpty() {
  802.        return products.isEmpty();
  803.    }
  804. }
  805.  
  806. class Product {
  807.  
  808.    private String name;
  809.    private double price;
  810.    private int stockLevel;
  811.  
  812.    public Product(String name, double price, int stockLevel) {
  813.        this.name = name;
  814.        this.price = price;
  815.        this.stockLevel = stockLevel;
  816.    }
  817.  
  818.    public String getName() {
  819.        return name;
  820.    }
  821.  
  822.    public void setName(String name) {
  823.        this.name = name;
  824.    }
  825.  
  826.    public double getPrice() {
  827.        return price;
  828.    }
  829.  
  830.    public void setPrice(double price) {
  831.        this.price = price;
  832.    }
  833.  
  834.    public int getStockLevel() {
  835.        return stockLevel;
  836.    }
  837.  
  838.    public void setStockLevel(int stockLevel) {
  839.        this.stockLevel = stockLevel;
  840.    }
  841.  
  842.    public boolean isInStock() {
  843.        return stockLevel > 0;
  844.    }
  845. }
  846.  
  847. MASTER:
  848. import java.util.Scanner;
  849.  
  850. // Abstract class representing a product
  851. abstract class Product {
  852.    String name;
  853.    double price;
  854.  
  855.    Product(String name, double price) {
  856.        this.name = name;
  857.        this.price = price;
  858.    }
  859.  
  860.    // Abstract method
  861.    abstract void displayProductInfo();
  862. }
  863.  
  864. // Intermediate class inheriting from Product
  865. class Electronics extends Product {
  866.    String brand;
  867.  
  868.    Electronics(String name, double price, String brand) {
  869.        super(name, price);
  870.        this.brand = brand;
  871.    }
  872.  
  873.    @Override
  874.    void displayProductInfo() {
  875.        System.out.println("Product: " + name);
  876.        System.out.println("Price: $" + price);
  877.        System.out.println("Brand: " + brand);
  878.    }
  879. }
  880.  
  881. // Final class inheriting from Electronics
  882. final class Laptop extends Electronics {
  883.    String processor;
  884.  
  885.    Laptop(String name, double price, String brand, String processor) {
  886.        super(name, price, brand);
  887.        this.processor = processor;
  888.    }
  889.  
  890.    @Override
  891.    void displayProductInfo() {
  892.        super.displayProductInfo();
  893.        System.out.println("Processor: " + processor);
  894.    }
  895. }
  896.  
  897. public class Master {
  898.    public static void main(String[] args) {
  899.        Scanner scanner = new Scanner(System.in);
  900.  
  901.        System.out.println("Welcome to the E-Commerce Store!");
  902.        System.out.print("Enter the name of the laptop: ");
  903.        String laptopName = scanner.nextLine();
  904.        System.out.print("Enter the price of the laptop: ");
  905.        double laptopPrice = scanner.nextDouble();
  906.        scanner.nextLine(); // Consume the newline character
  907.        System.out.print("Enter the brand of the laptop: ");
  908.        String laptopBrand = scanner.nextLine();
  909.        System.out.print("Enter the processor of the laptop: ");
  910.        String laptopProcessor = scanner.nextLine();
  911.  
  912.        Laptop laptop = new Laptop(laptopName, laptopPrice, laptopBrand, laptopProcessor);
  913.  
  914.        System.out.println("\nProduct Information:");
  915.        laptop.displayProductInfo();
  916.  
  917.        scanner. Close();
  918.    }
  919. }
  920.  
  921. Interface:
  922. // Interface representing a generic product
  923. interface Product {
  924.    String getName();
  925.    double getPrice();
  926.    void displayProductInfo();
  927. }
  928.  
  929. // Interface representing electronic products
  930. interface Electronics extends Product {
  931.    String getBrand();
  932. }
  933.  
  934. // Interface representing laptops
  935. interface Laptop extends Electronics {
  936.    String getProcessor();
  937. }
  938.  
  939. // Concrete class implementing the Laptop interface
  940. class DellLaptop implements Laptop {
  941.    private String name;
  942.    private double price;
  943.    private String brand;
  944.    private String processor;
  945.  
  946.    public DellLaptop(String name, double price, String brand, String processor) {
  947.        this.name = name;
  948.        this.price = price;
  949.        this.brand = brand;
  950.        this.processor = processor;
  951.    }
  952.  
  953.    @Override
  954.    public String getName() {
  955.        return name;
  956.    }
  957.  
  958.    @Override
  959.    public double getPrice() {
  960.        return price;
  961.    }
  962.  
  963.    @Override
  964.    public String getBrand() {
  965.        return brand;
  966.    }
  967.  
  968.    @Override
  969.    public String getProcessor() {
  970.        return processor;
  971.    }
  972.  
  973.    @Override
  974.    public void displayProductInfo() {
  975.        System.out.println("Product: " + name);
  976.        System.out.println("Price: $" + price);
  977.        System.out.println("Brand: " + brand);
  978.        System.out.println("Processor: " + processor);
  979.    }
  980. }
  981.  
  982. public class Demo {
  983.    public static void main(String[] args) {
  984.        // Create a DellLaptop object
  985.        Laptop laptop = new DellLaptop("Dell XPS 13", 1299.99, "Dell", "Intel Core i7");
  986.  
  987.        // Display product information
  988.        System.out.println("Welcome to the E-Commerce Store!");
  989.        System.out.println("\nProduct Information:");
  990.        laptop.displayProductInfo();
  991.    }
  992. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement