Advertisement
fsoc131y

final redemption

Nov 1st, 2023 (edited)
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.35 KB | Source Code | 0 0
  1. SWING
  2. 1. CheckBox:
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5.  
  6. class CheckBox {
  7.     JFrame f;
  8.     JLabel label;
  9.     JCheckBox checkbox1, checkbox2;
  10.     JList<String> list1, list2; // Define JList components
  11.  
  12.     CheckBox() {
  13.         f = new JFrame("CheckBox Demo");
  14.         label = new JLabel();
  15.         label.setHorizontalAlignment(JLabel.CENTER);
  16.         label.setSize(400, 100);
  17.         checkbox1 = new JCheckBox("C++");
  18.         checkbox1.setBounds(150, 100, 50, 50);
  19.         checkbox2 = new JCheckBox("Java");
  20.         checkbox2.setBounds(150, 150, 50, 50);
  21.         f.add(checkbox1);
  22.         f.add(checkbox2);
  23.         f.add(label);
  24.  
  25.         checkbox1.addItemListener(new ItemListenerDemo(this));
  26.         checkbox2.addItemListener(new ItemListenerDemo(this));
  27.  
  28.         f.setSize(400, 400);
  29.         f.setLayout(null);
  30.         f.setVisible(true);
  31.     }
  32.  
  33.     public static void main(String args[]) {
  34.         new CheckBox();
  35.     }
  36. }
  37.  
  38. class ItemListenerDemo implements ItemListener {
  39.     CheckBox cbd;
  40.  
  41.     ItemListenerDemo(CheckBox cbd) {
  42.         this.cbd = cbd;
  43.     }
  44.  
  45.     @Override
  46.     public void itemStateChanged(ItemEvent e) {
  47.         String data = "";
  48.         if (cbd.list1.getSelectedIndex() != -1) {
  49.             data = "Programming language Selected: " + cbd.list1.getSelectedValue();
  50.             cbd.label.setText(data);
  51.         }
  52.         if (cbd.list2.getSelectedIndex() != -1) {
  53.             data += ", Framework Selected: ";
  54.             for (Object frame : cbd.list2.getSelectedValuesList()) {
  55.                 data += frame + " ";
  56.             }
  57.         }
  58.         cbd.label.setText(data);
  59.     }
  60. }
  61.  
  62. 2. Border Layout:
  63. import java.awt.*;
  64. import java.awt.event.*;
  65. import javax.swing.*;
  66.  
  67. public class BorderLayoutDemo {
  68.     JFrame f;
  69.     JButton b1;
  70.     JButton b2;
  71.     JButton b3;
  72.     JButton b4;
  73.     JLabel jl;
  74.  
  75.     public BorderLayoutDemo() {
  76.         f = new JFrame();
  77.  
  78.         // Creating buttons
  79.         b1 = new JButton("NORTH");
  80.         b2 = new JButton("SOUTH");
  81.         b3 = new JButton("EAST");
  82.         b4 = new JButton("WEST");
  83.         jl = new JLabel("Label@CENTER");
  84.  
  85.         f.add(b1, BorderLayout.NORTH);
  86.         f.add(b2, BorderLayout.SOUTH);
  87.         f.add(b3, BorderLayout.EAST);
  88.         f.add(b4, BorderLayout.WEST);
  89.         f.add(jl, BorderLayout.CENTER);
  90.  
  91.         b1.addActionListener(new MyActionListener(this));
  92.         b2.addActionListener(new MyActionListener(this));
  93.         b3.addActionListener(new MyActionListener(this));
  94.         b4.addActionListener(new MyActionListener(this));
  95.  
  96.         f.setSize(300, 300);
  97.         f.setVisible(true);
  98.     }
  99.  
  100.     public static void main(String[] args) {
  101.         new BorderLayoutDemo();
  102.     }
  103. }
  104.  
  105. class MyActionListener implements ActionListener {
  106.     BorderLayoutDemo bld;
  107.  
  108.     MyActionListener(BorderLayoutDemo bld) {
  109.         this.bld = bld;
  110.     }
  111.  
  112.     public void actionPerformed(ActionEvent ae) {
  113.         bld.jl.setText(ae.getActionCommand());
  114.     }
  115. }
  116.  
  117. 3. Card Layout:
  118. import java.awt.*;    
  119. import javax.swing.*;    
  120.    
  121. class CardLayoutDemo extends JFrame
  122. {    
  123. javax.swing.JButton b1;  
  124. JButton b2;  
  125. JButton b3;  
  126. JButton b4;  
  127. Container c;
  128. CardLayout cl;
  129. CardLayoutDemo()  
  130. {            
  131.     b1 = new javax.swing.JButton("NORTH");
  132.     b2 = new JButton("SOUTH");  
  133.     b3 = new JButton("EAST");
  134.     b4 = new JButton("WEST");
  135.  
  136.     c = getContentPane();
  137.     cl = new CardLayout(40,30);
  138.     c.setLayout(cl);
  139.  
  140.     c.add(b1);    
  141.     c.add(b2);      
  142.     c.add(b3);      
  143.     c.add(b4);    
  144.  
  145.     b1.addActionListener(new MyActionListener(this));
  146.     b2.addActionListener(new MyActionListener(this));
  147.     b3.addActionListener(new MyActionListener(this));
  148.     b4.addActionListener(new MyActionListener(this));
  149.            
  150.     setSize(300, 300);    
  151.     setVisible(true);    
  152. }    
  153. public static void main(String[] args) {    
  154.     new CardLayoutDemo();    
  155. }    
  156. }    
  157.  
  158. class MyActionListener implements java.awt.event.ActionListener {
  159.   CardLayoutDemo cld;
  160.   MyActionListener(CardLayoutDemo cld){
  161.      this.cld = cld;
  162.   }
  163.   public void actionPerformed(java.awt.event.ActionEvent ae){
  164.     cld.cl.next(cld.c);
  165.   }
  166. }
  167.  
  168. 4. Box Layout:
  169. import javax.swing.*;    
  170.    
  171. class BoxLayoutDemo extends JFrame
  172. {    
  173. javax.swing.JButton b1;  
  174. JButton b2;  
  175. JButton b3;  
  176. JButton b4;  
  177. JLabel jl;
  178. BoxLayoutDemo()  
  179. {            
  180.     b1 = new javax.swing.JButton("NORTH");
  181.     b2 = new JButton("SOUTH");  
  182.     b3 = new JButton("EAST");
  183.     b4 = new JButton("WEST");
  184.     jl = new JLabel("LABEL@CENTRE");
  185.  
  186.     add(b1);    
  187.     add(b2);      
  188.     add(b3);      
  189.     add(b4);    
  190.     add(jl);
  191.  
  192.     setLayout (new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));
  193.  
  194.     b1.addActionListener(new MyActionListener(this));
  195.     b2.addActionListener(new MyActionListener(this));
  196.     b3.addActionListener(new MyActionListener(this));
  197.     b4.addActionListener(new MyActionListener(this));
  198.            
  199.     setSize(300, 300);    
  200.     setVisible(true);    
  201. }    
  202. public static void main(String[] args) {    
  203.     new BoxLayoutDemo();    
  204. }    
  205. }    
  206.  
  207. class MyActionListener implements java.awt.event.ActionListener {
  208.   BoxLayoutDemo bld;
  209.   MyActionListener(BoxLayoutDemo bld){
  210.      this.bld = bld;
  211.   }
  212.   public void actionPerformed(java.awt.event.ActionEvent ae){
  213.     bld.jl.setText(ae.getActionCommand());
  214.   }
  215. }
  216.  
  217. -------------------------------------------------------------------------------------------------------------
  218. 2. Multilevel Inheritance:
  219. // Base class representing a product
  220. class Product {
  221.     private String name;
  222.     private double price;
  223.  
  224.     public Product(String name, double price) {
  225.         this.name = name;
  226.         this.price = price;
  227.     }
  228.  
  229.     public String getName() {
  230.         return name;
  231.     }
  232.  
  233.     public double getPrice() {
  234.         return price;
  235.     }
  236.  
  237.     public void displayProductInfo() {
  238.         System.out.println("Product: " + name);
  239.         System.out.println("Price: $" + price);
  240.     }
  241. }
  242.  
  243. // Subclass representing electronic products
  244. class Electronics extends Product {
  245.     private String brand;
  246.  
  247.     public Electronics(String name, double price, String brand) {
  248.         super(name, price);
  249.         this.brand = brand;
  250.     }
  251.  
  252.     public String getBrand() {
  253.         return brand;
  254.     }
  255.  
  256.     @Override
  257.     public void displayProductInfo() {
  258.         super.displayProductInfo();
  259.         System.out.println("Brand: " + brand);
  260.     }
  261. }
  262.  
  263. // Subclass representing laptops
  264. class Laptop extends Electronics {
  265.     private String processor;
  266.  
  267.     public Laptop(String name, double price, String brand, String processor) {
  268.         super(name, price, brand);
  269.         this.processor = processor;
  270.     }
  271.  
  272.     public String getProcessor() {
  273.         return processor;
  274.     }
  275.  
  276.     @Override
  277.     public void displayProductInfo() {
  278.         super.displayProductInfo();
  279.         System.out.println("Processor: " + processor);
  280.     }
  281. }
  282.  
  283. public class Multi {
  284.     public static void main(String[] args) {
  285.         // Create a Laptop object
  286.         Laptop laptop = new Laptop("Dell XPS 13", 1299.99, "Dell", "Intel Core i7");
  287.  
  288.         // Display product information
  289.         laptop.displayProductInfo();
  290.     }
  291. }
  292. ------------
  293. 3. Exception Handling:
  294. import java.util.Scanner;
  295.  
  296. class InvalidCreditCardException extends Exception {
  297.     public InvalidCreditCardException(String message) {
  298.         super(message);
  299.     }
  300. }
  301.  
  302. public class CCValidate {
  303.     public static void main(String[] args) {
  304.         Scanner scanner = new Scanner(System.in);
  305.  
  306.         System.out.print("Enter your 16-digit credit card number: ");
  307.         String creditCardNumber = scanner.nextLine();
  308.  
  309.         try {
  310.             if (isValidCreditCard(creditCardNumber)) {
  311.                 System.out.println("Credit card is valid.");
  312.             } else {
  313.                 System.out.println("Credit card is invalid.");
  314.             }
  315.         } catch (InvalidCreditCardException e) {
  316.             System.out.println("Invalid Credit Card: " + e.getMessage());
  317.         }
  318.  
  319.         scanner.close();
  320.     }
  321.  
  322.     public static boolean isValidCreditCard(String cardNumber) throws InvalidCreditCardException {
  323.         if (cardNumber == null || cardNumber.length() != 16) {
  324.             throw new InvalidCreditCardException("Credit card number must be exactly 16 digits long.");
  325.         }
  326.  
  327.         return true;
  328.     }
  329. }
  330. -------------
  331. 4. Multi-threading:
  332. class MultithreadingDemo extends Thread {
  333.     public void run() {
  334.         try {
  335.             // Displaying the thread name that is running
  336.             System.out.println(
  337.                 "Thread " + Thread.currentThread().getName()
  338.                 + " is running");
  339.         } catch (Exception e) {
  340.             // Throwing an exception
  341.             System.out.println("Exception is caught");
  342.         }
  343.     }
  344. }
  345.  
  346. public class exp7 {
  347.     public static void main(String[] args) {
  348.         int n = 8; // Number of threads
  349.         for (int i = 0; i < n; i++) {
  350.             MultithreadingDemo object = new MultithreadingDemo();
  351.             object.start(); // Note the lowercase 'start' method
  352.         }
  353.     }
  354. }
  355. --------------------
  356. 5. Interfaces:
  357. import java.util.ArrayList;
  358. import java.util.List;
  359. import java.util.Scanner;
  360.  
  361. // Define the Product interface
  362. interface Product {
  363.     String getName();
  364.     double getPrice();
  365. }
  366.  
  367. // Implement the Product interface for Electronics
  368. class Electronics implements Product {
  369.     private String name;
  370.     private double price;
  371.  
  372.     public Electronics(String name, double price) {
  373.         this.name = name;
  374.         this.price = price;
  375.     }
  376.  
  377.     @Override
  378.     public String getName() {
  379.         return name;
  380.     }
  381.  
  382.     @Override
  383.     public double getPrice() {
  384.         return price;
  385.     }
  386. }
  387.  
  388. // Implement the Product interface for Clothing
  389. class Clothing implements Product {
  390.     private String name;
  391.     private double price;
  392.  
  393.     public Clothing(String name, double price) {
  394.         this.name = name;
  395.         this.price = price;
  396.     }
  397.  
  398.     @Override
  399.     public String getName() {
  400.         return name;
  401.     }
  402.  
  403.     @Override
  404.     public double getPrice() {
  405.         return price;
  406.     }
  407. }
  408.  
  409. public class Interface {
  410.     public static void main(String[] args) {
  411.         Scanner scanner = new Scanner(System.in);
  412.         List<Product> shoppingCart = new ArrayList<>();
  413.  
  414.         while (true) {
  415.             System.out.println("Select a product category:");
  416.             System.out.println("1. Electronics");
  417.             System.out.println("2. Clothing");
  418.             System.out.println("3. Checkout");
  419.  
  420.             int choice = scanner.nextInt();
  421.  
  422.             if (choice == 1) {
  423.                 System.out.print("Enter electronics name: ");
  424.                 String name = scanner.next();
  425.                 System.out.print("Enter price: ");
  426.                 double price = scanner.nextDouble();
  427.                 shoppingCart.add(new Electronics(name, price));
  428.             } else if (choice == 2) {
  429.                 System.out.print("Enter clothing name: ");
  430.                 String name = scanner.next();
  431.                 System.out.print("Enter price: ");
  432.                 double price = scanner.nextDouble();
  433.                 shoppingCart.add(new Clothing(name, price));
  434.             } else if (choice == 3) {
  435.                 break;
  436.             } else {
  437.                 System.out.println("Invalid choice. Please select 1, 2, or 3.");
  438.             }
  439.         }
  440.  
  441.         double totalCost = calculateTotalCost(shoppingCart);
  442.         System.out.println("Total cost of items in the shopping cart: $" + totalCost);
  443.  
  444.         scanner.close();
  445.     }
  446.  
  447.     public static double calculateTotalCost(List<Product> shoppingCart) {
  448.         double totalCost = 0.0;
  449.         for (Product item : shoppingCart) {
  450.             totalCost += item.getPrice();
  451.         }
  452.         return totalCost;
  453.     }
  454. }
  455. -------------
  456. 6. Multiple Array:
  457. import java.util.Scanner;
  458.  
  459. public class ECommerceDemo {
  460.     public static void main(String[] args) {
  461.         Scanner scanner = new Scanner(System.in);
  462.         int maxSize = 100;
  463.         String[] productNames = new String[maxSize];
  464.         double[] productPrices = new double[maxSize];
  465.         int[] productQuantities = new int[maxSize];
  466.         int cartSize = 0;
  467.  
  468.         while (true) {
  469.             System.out.println("E-commerce Management System");
  470.             System.out.println("1. Add Product to Cart");
  471.             System.out.println("2. View Cart");
  472.             System.out.println("3. Calculate Total Cost");
  473.             System.out.println("4. Exit");
  474.  
  475.             int choice = scanner.nextInt();
  476.             scanner.nextLine(); // Consume the newline character
  477.  
  478.             switch (choice) {
  479.                 case 1:
  480.                     System.out.print("Enter Product Name: ");
  481.                     String productName = scanner.nextLine();
  482.                     System.out.print("Enter Product Price: $");
  483.                     double productPrice = scanner.nextDouble();
  484.                     System.out.print("Enter Quantity: ");
  485.                     int quantity = scanner.nextInt();
  486.  
  487.                     productNames[cartSize] = productName;
  488.                     productPrices[cartSize] = productPrice;
  489.                     productQuantities[cartSize] = quantity;
  490.                     cartSize++;
  491.                     break;
  492.                 case 2:
  493.                     System.out.println("Your Shopping Cart:");
  494.                     for (int i = 0; i < cartSize; i++) {
  495.                         System.out.println(
  496.                             productNames[i] + " - $" + productPrices[i] + " x " + productQuantities[i]
  497.                         );
  498.                     }
  499.                     break;
  500.                 case 3:
  501.                     double totalCost = 0.0;
  502.                     for (int i = 0; i < cartSize; i++) {
  503.                         totalCost += productPrices[i] * productQuantities[i];
  504.                     }
  505.                     System.out.println("Total Cost: $" + totalCost);
  506.                     break;
  507.                 case 4:
  508.                     System.out.println("Thank you for using our e-commerce system.");
  509.                     scanner.close();
  510.                     System.exit(0);
  511.                 default:
  512.                     System.out.println("Invalid choice. Please select a valid option.");
  513.                     break;
  514.             }
  515.         }
  516.     }
  517. }
  518. --------------------------------------------------------------------------------------------------------------
  519. Packages:
  520. //packages
  521. //Main.java
  522. import ecommerce.Product;
  523. import ecommerce.Order;
  524. import ecommerce.ShoppingCart;
  525.  
  526. import java.util.Scanner;
  527.  
  528. public class Main {
  529.     public static void main(String[] args) {
  530.         Scanner scanner = new Scanner(System.in);
  531.         ShoppingCart cart = new ShoppingCart();
  532.  
  533.         while (true) {
  534.             System.out.print("Enter product ID (0 to finish): ");
  535.             int productId = scanner.nextInt();
  536.             if (productId == 0) {
  537.                 break;
  538.             }
  539.  
  540.             System.out.print("Enter product name: ");
  541.             scanner.nextLine(); // Consume the newline character
  542.             String productName = scanner.nextLine();
  543.  
  544.             System.out.print("Enter product price: ");
  545.             double productPrice = scanner.nextDouble();
  546.  
  547.             System.out.print("Enter quantity: ");
  548.             int quantity = scanner.nextInt();
  549.  
  550.             Product product = new Product(productId, productName, productPrice);
  551.             cart.addProduct(product, quantity);
  552.         }
  553.  
  554.         // Display the contents of the shopping cart
  555.         System.out.println("Shopping Cart Contents:");
  556.         cart.displayCart();
  557.  
  558.         // Create an order from the shopping cart
  559.         Order order = new Order(cart);
  560.  
  561.         // Display the order details
  562.         System.out.println("\nOrder Details:");
  563.         order.displayOrder();
  564.  
  565.         scanner. Close();
  566.     }
  567. }
  568.  
  569. //ecommerce/Product.java
  570. package ecommerce;
  571.  
  572. public class Product {
  573.     private int id;
  574.     private String name;
  575.     private double price;
  576.  
  577.     public Product(int id, String name, double price) {
  578.         this.id = id;
  579.         this.name = name;
  580.         this.price = price;
  581.     }
  582.  
  583.     // Getters and setters for id, name, and price
  584.     public String toString() {
  585.         return name + " (ID: " + id + ", Price: Rs. " + price + ")";
  586.     }
  587. }
  588.  
  589. //ecommerce/Order.java
  590. package ecommerce;
  591.  
  592. import java.util.Date;
  593.  
  594. public class Order {
  595.     private ShoppingCart cart;
  596.     private Date orderDate;
  597.  
  598.     public Order(ShoppingCart cart) {
  599.         this.cart = cart;
  600.         this.orderDate = new Date();
  601.     }
  602.  
  603.     public void displayOrder() {
  604.         System.out.print("Order Date: " + orderDate);
  605.         System.out.println("\n");
  606.         System.out.println("Order Items:");
  607.         cart.displayCart();
  608.     }
  609. }
  610.  
  611. //ecommerce/ShoppingCart.java
  612. package ecommerce;
  613.  
  614. import java.util.HashMap;
  615. import java.util.Map;
  616.  
  617. public class ShoppingCart {
  618.     private Map<Product, Integer> items;
  619.  
  620.     public ShoppingCart() {
  621.         items = new HashMap<>();
  622.     }
  623.  
  624.     public void addProduct(Product product, int quantity) {
  625.         items.put(product, quantity);
  626.     }
  627.  
  628.     public void displayCart() {
  629.         for (Map.Entry<Product, Integer> entry : items.entrySet()) {
  630.             Product product = entry.getKey();
  631.             int quantity = entry.getValue();
  632.             System.out.println(product + " x" + quantity);
  633.         }
  634.     }
  635. }
  636. --------------------------------------------------------------------------------------------------------------------------
  637. MASTER-----!!!!!
  638. import java.awt.event.*;
  639. import javax.swing.*;
  640. import java.util.Scanner;
  641.  
  642. // Base class representing a product
  643. class Product {
  644.     private String name;
  645.     private double price;
  646.  
  647.     public Product(String name, double price) {
  648.         this.name = name;
  649.         this.price = price;
  650.     }
  651.  
  652.     public String getName() {
  653.         return name;
  654.     }
  655.  
  656.     public double getPrice() {
  657.         return price;
  658.     }
  659.  
  660.     public void displayProductInfo() {
  661.         System.out.println("Product: " + name);
  662.         System.out.println("Price: $" + price);
  663.     }
  664. }
  665.  
  666. // Subclass representing electronic products
  667. class Electronics extends Product {
  668.     private String brand;
  669.  
  670.     public Electronics(String name, double price, String brand) {
  671.         super(name, price);
  672.         this.brand = brand;
  673.     }
  674.  
  675.     public String getBrand() {
  676.         return brand;
  677.     }
  678.  
  679.     @Override
  680.     public void displayProductInfo() {
  681.         super.displayProductInfo();
  682.         System.out.println("Brand: " + brand);
  683.     }
  684. }
  685.  
  686. // Subclass representing laptops
  687. class Laptop extends Electronics {
  688.     private String processor;
  689.  
  690.     public Laptop(String name, double price, String brand, String processor) {
  691.         super(name, price, brand);
  692.         this.processor = processor;
  693.     }
  694.  
  695.     public String getProcessor() {
  696.         return processor;
  697.     }
  698.  
  699.     @Override
  700.     public void displayProductInfo() {
  701.         super.displayProductInfo();
  702.         System.out.println("Processor: " + processor);
  703.     }
  704. }
  705.  
  706. class InvalidCreditCardException extends Exception {
  707.     public InvalidCreditCardException(String message) {
  708.         super(message);
  709.     }
  710. }
  711.  
  712. class CCValidate {
  713.     public static boolean isValidCreditCard(String cardNumber) throws InvalidCreditCardException {
  714.         if (cardNumber == null || cardNumber.length() != 16) {
  715.             throw new InvalidCreditCardException("Credit card number must be exactly 16 digits long.");
  716.         }
  717.         return true;
  718.     }
  719. }
  720.  
  721. class MultithreadingDemo extends Thread {
  722.     public void run() {
  723.         try {
  724.             System.out.println(
  725.                 "Thread " + Thread.currentThread().getName() + " is running"
  726.             );
  727.         } catch (Exception e) {
  728.             System.out.println("Exception is caught");
  729.         }
  730.     }
  731. }
  732.  
  733. // Define the Product interface
  734. interface ProductInterface {
  735.     String getName();
  736.     double getPrice();
  737. }
  738.  
  739. // Implement the Product interface for Electronics
  740. class ElectronicsInterface implements ProductInterface {
  741.     private String name;
  742.     private double price;
  743.  
  744.     public ElectronicsInterface(String name, double price) {
  745.         this.name = name;
  746.         this.price = price;
  747.     }
  748.  
  749.     @Override
  750.     public String getName() {
  751.         return name;
  752.     }
  753.  
  754.     @Override
  755.     public double getPrice() {
  756.         return price;
  757.     }
  758. }
  759.  
  760. // Implement the Product interface for Clothing
  761. class ClothingInterface implements ProductInterface {
  762.     private String name;
  763.     private double price;
  764.  
  765.     public ClothingInterface(String name, double price) {
  766.         this.name = name;
  767.         this.price = price;
  768.     }
  769.  
  770.     @Override
  771.     public String getName() {
  772.         return name;
  773.     }
  774.  
  775.     @Override
  776.     public double getPrice() {
  777.         return price;
  778.     }
  779. }
  780.  
  781. public class Master {
  782.     public static void main(String[] args) {
  783.         Scanner scanner = new Scanner(System.in);
  784.         int maxSize = 100;
  785.         String[] productNames = new String[maxSize];
  786.         double[] productPrices = new double[maxSize];
  787.         int[] productQuantities = new int[maxSize];
  788.         int cartSize = 0;
  789.  
  790.         JFrame f = new JFrame("CheckBox Demo");
  791.         JLabel label = new JLabel();
  792.         label.setHorizontalAlignment(JLabel.CENTER);
  793.         label.setSize(400, 100);
  794.         JCheckBox checkbox1 = new JCheckBox("C++");
  795.         checkbox1.setBounds(150, 100, 50, 50);
  796.         JCheckBox checkbox2 = new JCheckBox("Java");
  797.         checkbox2.setBounds(150, 150, 50, 50);
  798.         f.add(checkbox1);
  799.         f.add(checkbox2);
  800.         f.add(label);
  801.  
  802.         checkbox1.addItemListener(new ItemListener() {
  803.             public void itemStateChanged(ItemEvent e) {
  804.                 if (checkbox1.isSelected()) {
  805.                     label.setText("Programming language Selected: C++");
  806.                 }
  807.             }
  808.         });
  809.  
  810.         checkbox2.addItemListener(new ItemListener() {
  811.             public void itemStateChanged(ItemEvent e) {
  812.                 if (checkbox2.isSelected()) {
  813.                     label.setText("Programming language Selected: Java");
  814.                 }
  815.             }
  816.         });
  817.  
  818.         f.setSize(400, 400);
  819.         f.setLayout(null);
  820.         f.setVisible(true);
  821.  
  822.         while (true) {
  823.             System.out.println("E-commerce Management System");
  824.             System.out.println("1. Add Product to Cart");
  825.             System.out.println("2. View Cart");
  826.             System.out.println("3. Calculate Total Cost");
  827.             System.out.println("4. Exit");
  828.  
  829.             int choice = scanner.nextInt();
  830.             scanner.nextLine();
  831.  
  832.             switch (choice) {
  833.                 case 1:
  834.                     System.out.print("Enter Product Name: ");
  835.                     String productName = scanner.nextLine();
  836.                     System.out.print("Enter Product Price: $");
  837.                     double productPrice = scanner.nextDouble();
  838.                     System.out.print("Enter Quantity: ");
  839.                     int quantity = scanner.nextInt();
  840.  
  841.                     productNames[cartSize] = productName;
  842.                     productPrices[cartSize] = productPrice;
  843.                     productQuantities[cartSize] = quantity;
  844.                     cartSize++;
  845.                     break;
  846.                 case 2:
  847.                     System.out.println("Your Shopping Cart:");
  848.                     for (int i = 0; i < cartSize; i++) {
  849.                         System.out.println(
  850.                             productNames[i] + " - $" + productPrices[i] + " x " + productQuantities[i]
  851.                         );
  852.                     }
  853.                     break;
  854.                 case 3:
  855.                     double totalCost = 0.0;
  856.                     for (int i = 0; i < cartSize; i++) {
  857.                         totalCost += productPrices[i] * productQuantities[i];
  858.                     }
  859.                     System.out.println("Total Cost: $" + totalCost);
  860.                     break;
  861.                 case 4:
  862.                     System.out.println("Thank you for using our e-commerce system.");
  863.                     scanner.close();
  864.                     System.exit(0);
  865.                 default:
  866.                     System.out.println("Invalid choice. Please select a valid option.");
  867.                     break;
  868.             }
  869.         }
  870.     }
  871. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement