Advertisement
fsoc131y

Simple

Oct 15th, 2023 (edited)
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.14 KB | Source Code | 0 0
  1. Exp1:
  2. import java.io.*;
  3. import java.util.Scanner;
  4.  
  5. class exp1 {
  6.     public static void main(String args[]) throws IOException {
  7.         String choice;
  8.         Scanner d = new Scanner(System.in);
  9.         for(int i = 0;i<2;i++){
  10.         System.out.println("Welcome to the E-mart!");
  11.         }
  12.         int ab = 0;
  13.         while(ab<2){
  14.    System.out.println("Welcome dear customer");
  15.             ab++;
  16. }
  17.         do {
  18.             System.out.print("\nEnter the budget amount: ");
  19.             int a = d.nextInt();
  20.             if (a == 0) {
  21.                 System.out.println("Nothing can be purchased for free.");
  22.                 System.out.println("Thanks for visiting!");
  23.             } else if (a <= 1200) {
  24.                 System.out.println("You can purchase some groceries and some clothes.");
  25.             } else if( a>1200 && a<=2000){
  26.                 System.out.println("You can purchase shoes and ties");
  27.             } else if(a>2000 && a<=3000){
  28. System.out.println(" You can buy some suits");
  29.             } else {
  30.                 System.out.println("Items are being added");
  31.                 System.out.println("Thanks for visiting");
  32.             }
  33.             d.nextLine();
  34.             System.out.print("Enter if you want to order? (Y/N): ");
  35.             choice = d.nextLine();
  36.         } while (choice.equalsIgnoreCase("y"));
  37.         d.close();
  38.     }
  39. }
  40.  
  41. Exp2:
  42. //switch cases
  43. import java.util.Scanner;
  44.  
  45. public class exp2 {
  46.     public static void main(String[] args) {
  47.         Scanner scanner = new Scanner(System.in);
  48.  
  49.         System.out.println("Welcome to the E-Commerce Store!");
  50.         System.out.println("Select a product to view its price:");
  51.         System.out.println("1. Laptop");
  52.         System.out.println("2. Smartphone");
  53.         System.out.println("3. Headphones");
  54.         System.out.println("4. Exit");
  55.  
  56.         int choice;
  57.         do {
  58.             System.out.print("Enter your choice (1-4): ");
  59.             choice = scanner.nextInt();
  60.  
  61.             switch (choice) {
  62.                 case 1:
  63.                     System.out.println("Laptop: $800");
  64.                     break;
  65.                 case 2:
  66.                     System.out.println("Smartphone: $500");
  67.                     break;
  68.                 case 3:
  69.                     System.out.println("Headphones: $50");
  70.                     break;
  71.                 case 4:
  72.                     System.out.println("Thank you for visiting the store!");
  73.                     break;
  74.                 default:
  75.                     System.out.println("Invalid choice. Please enter a valid option.");
  76.             }
  77.         } while (choice != 4);
  78.  
  79.         scanner. Close();
  80.     }
  81. }
  82.  
  83. Exp3:
  84. //arrays
  85. import java.io.*;
  86. import java.util.Scanner;
  87.  
  88. class exp3 {
  89.     public static void main(String args[]) throws IOException {
  90.         Scanner d = new Scanner(System.in);
  91.         String num[] = {"Soap", "Pencil", "Notebook", "Detergent(1 Kg)", "Pen", "Clothes"};
  92.  
  93.         System.out.println("Item present in List");
  94.         for (int i = 0; i < num.length; i++) {
  95.             System.out.println((i + 1) + ". " + num[i]);
  96.         }
  97.  
  98.         System.out.print("Enter how many items you want to add: ");
  99.         int m = d.nextInt();
  100.         d.nextLine();
  101.  
  102.         String arr[][] = new String[m][2];
  103.         for (int j = 0; j < m; j++) {
  104.             System.out.print("Enter the item " + (j + 1) + ": ");
  105.             arr[j][0] = d.nextLine();
  106.             System.out.print("Enter the quantity: ");
  107.             arr[j][1] = d.nextLine();
  108.         }
  109.  
  110.         boolean itemsMatch = true;
  111.         for (int j = 0; j < m; j++) {
  112.             boolean found = false;
  113.             for (int i = 0; i < num.length; i++) {
  114.                 if (arr[j][0].equalsIgnoreCase(num[i])) {
  115.                     found = true;
  116.                     break;
  117.                 }
  118.             }
  119.             if (!found) {
  120.                 itemsMatch = false;
  121.                 break;
  122.             }
  123.         }
  124.  
  125.         if (itemsMatch) {
  126.             System.out.println("\nYour item\tQuantity\tPrice");
  127.             for (int j = 0; j < m; j++) {
  128.                 double price = calculatePrice(arr[j][0].toLowerCase(), Integer.parseInt(arr[j][1]));
  129.                 System.out.println(arr[j][0] + "\t\t" + arr[j][1] + "\t\t$" + price);
  130.             }
  131.         } else {
  132.             System.out.println("Your item list doesn't match with the items in the store.");
  133.         }
  134.             d.close();
  135.     }
  136.  
  137.     private static double calculatePrice(String item, int quantity) {
  138.         double pricePerUnit;
  139.         switch (item) {
  140.             case "soap":
  141.                 pricePerUnit = 1.0;
  142.                 break;
  143.             case "pencil":
  144.                 pricePerUnit = 0.5;
  145.                 break;
  146.             case "notebook":
  147.                 pricePerUnit = 2.0;
  148.                 break;
  149.             case "detergent(1 kg)":
  150.                 pricePerUnit = 3.5;
  151.                 break;
  152.             case "pen":
  153.                 pricePerUnit = 1.2;
  154.                 break;
  155.             case "clothes":
  156.                 pricePerUnit = 10.0;
  157.                 break;
  158.             default:
  159.                 pricePerUnit = 0.0; // Item not found in the list
  160.         }
  161.  
  162.         return pricePerUnit * quantity;
  163.     }
  164. }
  165.  
  166. Exp4:
  167. //method and constructor overloading
  168. class Product{
  169.       String name;
  170.       double price;
  171.       Product(String name,double price){
  172.               this.name=name;
  173.               this.price=price;
  174.       }
  175.       String getname(){
  176.                return name;
  177.       }
  178.       double getprice(){
  179.                return price;
  180.       }
  181.       void display(){
  182.                System.out.println("Product: "+name);
  183.                System.out.println("Price: $"+price);
  184. }
  185. }
  186. class shopc{
  187.       int icount;
  188.       double tamount;
  189.       shopc(){
  190.            icount=0;
  191.            tamount=0.0;
  192.       }
  193.       void addc(Product product){
  194.            icount++;
  195.            tamount+=product.getprice();
  196.            System.out.println(product.getname()+" added to cart");
  197.       }
  198.       void addc(Product product,int qty){
  199.            icount+=qty;
  200.            tamount+=product.getprice()*qty;
  201.            System.out.println(qty+""+product.getname()+" added to cart");
  202.       }
  203.       float display(int a, float b){
  204.            /*System.out.println("Shopping Cart:");
  205.            System.out.println("Total Items: " + icount);
  206.            System.out.println("Total Amount: $" + tamount);*/
  207.            return (a+b);
  208.       }
  209. }
  210. class exp4{
  211.       public static void main(String args[]){
  212.              Product laptop = new Product("Laptop",1000.0);
  213.              Product phone = new Product("Smartphone",500.0);
  214.              shopc cart = new shopc();
  215.              cart.addc(laptop);
  216.              cart.addc(phone,2);
  217.              System.out.println(cart.display(12,(float)32.5));
  218. }
  219. }
  220.  
  221. Exp5:
  222. //exception handling
  223. import java.util.Scanner;
  224.  
  225. class Billing {
  226.     public static void main(String[] args) {
  227.         Scanner scanner = new Scanner(System.in);
  228.  
  229.         System.out.print("Enter the bill amount: ");
  230.         double billAmount = scanner.nextDouble();
  231.  
  232.         System.out.print("Enter your credit card number: ");
  233.         String cardNumber = scanner.next();
  234.  
  235.         try {
  236.             validateCreditCard(cardNumber);
  237.             processPayment(billAmount);
  238.             System.out.println("Payment successful. Thank you for shopping!");
  239.         } catch (InvalidCreditCardException e) {
  240.             System.out.println("Payment failed: " + e.getMessage());
  241.         }
  242.         scanner.close();
  243.     }
  244.  
  245.     static void validateCreditCard(String cardNumber) throws InvalidCreditCardException {
  246.         if (cardNumber.length() > 16) {
  247.             throw new InvalidCreditCardException("Card number is too long.");
  248.         }
  249.  
  250.         if (!isVisaOrMastercard(cardNumber)) {
  251.             throw new InvalidCreditCardException("Card not issued by Visa/Mastercard.");
  252.         }
  253.     }
  254.  
  255.     static boolean isVisaOrMastercard(String cardNumber) {
  256.         // You can implement your card type detection logic here.
  257.         // For simplicity, we assume Visa and Mastercard numbers start with 4 and 5.
  258.         return cardNumber.startsWith("4") || cardNumber.startsWith("5");
  259.     }
  260.  
  261.     static void processPayment(double amount) {
  262.         // This method would process the payment, but for this example, we'll just print a message.
  263.         System.out.println("Processing payment of $" + amount);
  264.     }
  265. }
  266.  
  267. class InvalidCreditCardException extends Exception {
  268.     public InvalidCreditCardException(String message) {
  269.         super(message);
  270.     }
  271. }
  272.  
  273. Exp6:
  274. //packages
  275. //main driver code
  276. package Packages;
  277.  
  278. public class ecommerce {
  279.     public static void main(String[] args) {
  280.         // Create some sample products
  281.         Product product1 = new Product(101, "Laptop", 799.99);
  282.         Product product2 = new Product(102, "Smartphone", 299.99);
  283.         Product product3 = new Product(103, "Headphones", 49.99);
  284.  
  285.         // Create a shopping cart
  286.         ShoppingCart cart = new ShoppingCart();
  287.  
  288.         // Add products to the cart
  289.         cart.addProduct(product1, 2);
  290.         cart.addProduct(product2, 1);
  291.         cart.addProduct(product3, 3);
  292.  
  293.         // Display the contents of the shopping cart
  294.         System.out.println("Shopping Cart Contents:");
  295.         cart.displayCart();
  296.  
  297.         // Create an order from the shopping cart
  298.         Order order = new Order(cart);
  299.  
  300.         // Display the order details
  301.         System.out.println("\nOrder Details:");
  302.         order.displayOrder();
  303.     }
  304. }
  305.  
  306. //package - separate file
  307. package Packages;
  308.  
  309. import java.util.Date;
  310.  
  311. public class Order {
  312.     private ShoppingCart cart;
  313.     private Date orderDate;
  314.  
  315.     public Order(ShoppingCart cart) {
  316.         this.cart = cart;
  317.         this.orderDate = new Date();
  318.     }
  319.  
  320.     public void displayOrder() {
  321.         System.out.print("Order Date: " + orderDate);
  322.         System.out.println("\n");
  323.         System.out.println("Order Items:");
  324.         cart.displayCart();
  325.     }
  326. }
  327.  
  328. //package
  329. package Packages;
  330.  
  331. public class Product {
  332.     private int id;
  333.     private String name;
  334.     private double price;
  335.  
  336.     public Product(int id, String name, double price) {
  337.         this.id = id;
  338.         this.name = name;
  339.         this.price = price;
  340.     }
  341.  
  342.     // Getters and setters for id, name, and price
  343.  
  344.     public String toString() {
  345.         return name + " (ID: " + id + ", Price: Rs. " + price + ")";
  346.     }
  347. }
  348.  
  349. //package
  350. package Packages;
  351.  
  352. import java.util.HashMap;
  353. import java.util.Map;
  354.  
  355. public class ShoppingCart {
  356.     private Map<Product, Integer> items;
  357.  
  358.     public ShoppingCart() {
  359.         items = new HashMap<>();
  360.     }
  361.  
  362.     public void addProduct(Product product, int quantity) {
  363.         items.put(product, quantity);
  364.     }
  365.  
  366.     public void displayCart() {
  367.         for (Map.Entry<Product, Integer> entry : items.entrySet()) {
  368.             Product product = entry.getKey();
  369.             int quantity = entry.getValue();
  370.             System.out.println(product + " x" + quantity);
  371.         }
  372.     }
  373. }
  374.  
  375. Master (except packages):
  376. import java.util.Scanner;
  377.  
  378. public class ECommerceDemo {
  379.  
  380.     // 1. Entry Exit Control Loop
  381.     public static void main(String[] args) {
  382.         Scanner scanner = new Scanner(System.in);
  383.         int choice;
  384.  
  385.         do {
  386.             System.out.println("E-Commerce Application Menu");
  387.             System.out.println("1. Browse Products");
  388.             System.out.println("2. Add to Cart");
  389.             System.out.println("3. View Cart");
  390.             System.out.println("4. Checkout");
  391.             System.out.println("5. Exit");
  392.  
  393.             System.out.print("Enter your choice: ");
  394.             choice = scanner.nextInt();
  395.  
  396.             switch (choice) {
  397.                 case 1:
  398.                     browseProducts();
  399.                     break;
  400.                 case 2:
  401.                     addToCart();
  402.                     break;
  403.                 case 3:
  404.                     viewCart();
  405.                     break;
  406.                 case 4:
  407.                     checkout();
  408.                     break;
  409.                 case 5:
  410.                     System.out.println("Exiting the application.");
  411.                     break;
  412.                 default:
  413.                     System.out.println("Invalid choice. Please try again.");
  414.             }
  415.         } while (choice != 5);
  416.     }
  417.  
  418.     // 2. Switch Case with Nested Switch Case
  419.     static void browseProducts() {
  420.         System.out.println("Browse Products Menu");
  421.         System.out.println("1. Electronics");
  422.         System.out.println("2. Clothing");
  423.         System.out.println("3. Back to Main Menu");
  424.  
  425.         Scanner scanner = new Scanner(System.in);
  426.         int choice = scanner.nextInt();
  427.  
  428.         switch (choice) {
  429.             case 1:
  430.                 System.out.println("Electronics Section");
  431.                 break;
  432.             case 2:
  433.                 System.out.println("Clothing Section");
  434.                 break;
  435.             case 3:
  436.                 System.out.println("Returning to the Main Menu");
  437.                 break;
  438.             default:
  439.                 System.out.println("Invalid choice. Please try again.");
  440.         }
  441.     }
  442.  
  443.     // 3. Arrays - 1D and 2D
  444.     static void viewCart() {
  445.         String[] cartItems = {"Laptop", "T-Shirt", "Headphones", "Shoes"};
  446.  
  447.         System.out.println("Items in Your Cart:");
  448.         for (String item : cartItems) {
  449.             System.out.println(item);
  450.         }
  451.  
  452.         int[][] itemQuantities = {{2, 1}, {3, 2}, {1, 1}, {2, 2}};
  453.  
  454.         System.out.println("Quantity of Items in Your Cart:");
  455.         for (int i = 0; i < cartItems.length; i++) {
  456.             System.out.println(cartItems[i] + ": " + itemQuantities[i][0] + " items, " + itemQuantities[i][1] + " in stock");
  457.         }
  458.     }
  459.  
  460.     // 4. Constructor & Method Overloading
  461.     static void addToCart() {
  462.         String product = "Laptop";
  463.         int quantity = 2;
  464.         String seller = "E-Store Inc";
  465.  
  466.         addToCart(product, quantity);
  467.         addToCart(product, quantity, seller);
  468.     }
  469.  
  470.     static void addToCart(String product, int quantity) {
  471.         System.out.println(quantity + " " + product + "(s) added to your cart.");
  472.     }
  473.  
  474.     static void addToCart(String product, int quantity, String seller) {
  475.         System.out.println(quantity + " " + product + "(s) from " + seller + " added to your cart.");
  476.     }
  477.  
  478.     // 5. Exception Handling
  479.     static void checkout() {
  480.         try {
  481.             // Simulate a checkout process
  482.             int totalAmount = 1000;
  483.             int availableBalance = 800;
  484.             int remainingBalance = totalAmount - availableBalance;
  485.  
  486.             if (remainingBalance > 0) {
  487.                 throw new InsufficientBalanceException("Insufficient balance! You need $" + remainingBalance + " more.");
  488.             } else {
  489.                 System.out.println("Checkout successful. Your order has been placed.");
  490.             }
  491.         } catch (InsufficientBalanceException e) {
  492.             System.err.println("Error: " + e.getMessage());
  493.         }
  494.     }
  495. }
  496.  
  497. class InsufficientBalanceException extends Exception {
  498.     public InsufficientBalanceException(String message) {
  499.         super(message);
  500.     }
  501. }
  502.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement