Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Simple:
- Old: https://pastebin.com/bsXH7eHU
- Exp 1: entry/exit loop
- import java.util.ArrayList;
- import java.util.Scanner;
- public class ECommerceLoopingDemo {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- ArrayList<String> shoppingCart = new ArrayList<>();
- int choice;
- do {
- System.out.println("E-Commerce Shopping Cart:");
- System.out.println("1. Add item to cart");
- System.out.println("2. Remove item from cart");
- System.out.println("3. View cart");
- System.out.println("4. Checkout");
- System.out.println("5. Exit");
- System.out.print("Enter your choice: ");
- choice = scanner.nextInt();
- switch (choice) {
- case 1:
- System.out.print("Enter the item to add: ");
- String itemToAdd = scanner.next();
- shoppingCart.add(itemToAdd);
- System.out.println(itemToAdd + " added to the cart.");
- break;
- case 2:
- System.out.print("Enter the item to remove: ");
- String itemToRemove = scanner.next();
- if (shoppingCart.contains(itemToRemove)) {
- shoppingCart.remove(itemToRemove);
- System.out.println(itemToRemove + " removed from the cart.");
- } else {
- System.out.println("Item not found in the cart.");
- }
- break;
- case 3:
- System.out.println("Shopping Cart Contents:");
- for (String item : shoppingCart) {
- System.out.println("- " + item);
- }
- break;
- case 4:
- if (shoppingCart.isEmpty()) {
- System.out.println("Your cart is empty. Cannot proceed to checkout.");
- } else {
- System.out.println("Checkout Successful!");
- shoppingCart.clear();
- }
- break;
- case 5:
- System.out.println("Exiting the program.");
- break;
- default:
- System.out.println("Invalid choice. Please try again.");
- }
- } while (choice != 5);
- scanner.close();
- }
- }
- Exp 2: nesting of switch statement
- import java.util.Scanner;
- public class ECommerceOrderProcessing {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int orderState = 0;
- boolean orderComplete = false;
- while (!orderComplete) {
- System.out.println("E-Commerce Order Processing System:");
- System.out.println("1. New Order");
- System.out.println("2. Order Processing");
- System.out.println("3. Shipped");
- System.out.println("4. Delivered");
- System.out.println("5. Cancel Order");
- System.out.println("6. Exit");
- System.out.print("Enter your choice: ");
- int action = scanner.nextInt();
- switch (orderState) {
- case 0: // New Order
- switch (action) {
- case 1:
- System.out.println("Order placed. Awaiting processing.");
- orderState = 1;
- break;
- case 6:
- orderComplete = true;
- break;
- default:
- System.out.println("Invalid action for the current order state.");
- break;
- }
- break;
- case 1: // Order Processing
- switch (action) {
- case 2:
- System.out.println("Order is being processed.");
- orderState = 2;
- break;
- case 5:
- System.out.println("Order canceled.");
- orderState = 0;
- break;
- case 6:
- orderComplete = true;
- break;
- default:
- System.out.println("Invalid action for the current order state.");
- break;
- }
- break;
- case 2: // Shipped
- switch (action) {
- case 3:
- System.out.println("Order has been shipped.");
- orderState = 3;
- break;
- case 5:
- System.out.println("Order canceled.");
- orderState = 0;
- break;
- case 6:
- orderComplete = true;
- break;
- default:
- System.out.println("Invalid action for the current order state.");
- break;
- }
- break;
- case 3: // Delivered
- switch (action) {
- case 4:
- System.out.println("Order has been delivered. Thank you for your purchase!");
- orderState = 0;
- break;
- case 5:
- System.out.println("Order canceled.");
- orderState = 0;
- break;
- case 6:
- orderComplete = true;
- break;
- default:
- System.out.println("Invalid action for the current order state.");
- break;
- }
- break;
- default:
- System.out.println("Invalid order state.");
- break;
- }
- }
- System.out.println("Exiting the order processing system.");
- scanner.close();
- }
- }
- Exp 3: single and multi dimensional arrays
- import java.util.Scanner;
- public class Lab3 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Single-dimensional array to represent a product catalog
- String[] products = {
- "Product A",
- "Product B",
- "Product C",
- "Product D",
- "Product E"
- };
- // Multi-dimensional array to represent a shopping cart (product, quantity)
- String[][] shoppingCart = new String[5][2];
- int cartSize = 0;
- boolean shopping = true;
- while (shopping) {
- System.out.println("E-Commerce Product Catalog:");
- for (int i = 0; i < products.length; i++) {
- System.out.println((i + 1) + ". " + products[i]);
- }
- System.out.println("6. View Cart");
- System.out.println("7. Checkout");
- System.out.println("8. Exit");
- System.out.print("Enter your choice: ");
- int choice = scanner.nextInt();
- if (choice >= 1 && choice <= products.length) {
- System.out.print("Enter the quantity: ");
- int quantity = scanner.nextInt();
- if (quantity > 0) {
- shoppingCart[cartSize][0] = products[choice - 1];
- shoppingCart[cartSize][1] = String.valueOf(quantity);
- cartSize++;
- System.out.println("Item added to the cart.");
- } else {
- System.out.println("Quantity must be greater than 0.");
- }
- } else {
- switch (choice) {
- case 6:
- System.out.println("Shopping Cart Contents:");
- for (int i = 0; i < cartSize; i++) {
- System.out.println(shoppingCart[i][0] + " (Quantity: " + shoppingCart[i][1] + ")");
- }
- break;
- case 7:
- System.out.println("Checkout Successful!");
- cartSize = 0;
- break;
- case 8:
- System.out.println("Exiting the program.");
- shopping = false;
- break;
- default:
- System.out.println("Invalid choice. Please try again.");
- break;
- }
- }
- }
- scanner.close();
- }
- }
- Exp 4: constructor & method overloading
- class Product {
- private int productId;
- private String productName;
- private double price;
- // Default constructor
- public Product() {
- productId = 0;
- productName = "No Product";
- price = 0.0;
- }
- // Constructor with product name and price
- public Product(String productName, double price) {
- this.productId = generateProductId();
- this.productName = productName;
- this.price = price;
- }
- // Constructor with all attributes
- public Product(int productId, String productName, double price) {
- this.productId = productId;
- this.productName = productName;
- this.price = price;
- }
- // Method to display product details
- public void displayProduct() {
- System.out.println("Product ID: " + productId);
- System.out.println("Product Name: " + productName);
- System.out.println("Price: $" + price);
- }
- // Method overloading: Calculate and display discounted price
- public void displayProduct(double discountPercentage) {
- double discountedPrice = price - (price * discountPercentage / 100);
- System.out.println("Product ID: " + productId);
- System.out.println("Product Name: " + productName);
- System.out.println("Price: $" + price);
- System.out.println("Discounted Price: $" + discountedPrice);
- }
- // Generate a unique product ID (for demonstration purposes)
- private int generateProductId() {
- // In a real e-commerce system, this would be a more sophisticated logic
- return (int) (Math.random() * 1000);
- }
- }
- public class Lab4 {
- public static void main(String[] args) {
- // Create products using overloaded constructors
- Product product1 = new Product();
- Product product2 = new Product("Laptop", 799.99);
- Product product3 = new Product(1001, "Smartphone", 299.99);
- // Display product details using overloaded methods
- product1.displayProduct();
- System.out.println();
- product2.displayProduct();
- System.out.println();
- product3.displayProduct(10); // 10% discount
- }
- }
- Exp 5: static keyword
- class Product {
- private int productId;
- private String productName;
- private double price;
- // Static variable to keep track of the total number of products
- private static int totalProducts = 0;
- public Product(String productName, double price) {
- this.productId = generateProductId();
- this.productName = productName;
- this.price = price;
- totalProducts++;
- }
- public void displayProduct() {
- System.out.println("Product ID: " + productId);
- System.out.println("Product Name: " + productName);
- System.out.println("Price: $" + price);
- }
- // Static method to get the total number of products
- public static int getTotalProducts() {
- return totalProducts;
- }
- // Generate a unique product ID (for demonstration purposes)
- private int generateProductId() {
- // In a real e-commerce system, this would be a more sophisticated logic
- return (int) (Math.random() * 1000);
- }
- }
- public class ECommerceWithStatic {
- public static void main(String[] args) {
- // Create products and display product details
- Product product1 = new Product("Laptop", 799.99);
- Product product2 = new Product("Smartphone", 299.99);
- product1.displayProduct();
- System.out.println();
- product2.displayProduct();
- System.out.println();
- // Get the total number of products using the static method
- int totalProducts = Product.getTotalProducts();
- System.out.println("Total Products in the Store: " + totalProducts);
- }
- }
- Exp 6: multi-level inheritance
- // Base class representing a product
- class Product {
- private String name;
- private double price;
- public Product(String name, double price) {
- this.name = name;
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- public void displayProductInfo() {
- System.out.println("Product: " + name);
- System.out.println("Price: $" + price);
- }
- }
- // Subclass representing electronic products
- class Electronics extends Product {
- private String brand;
- public Electronics(String name, double price, String brand) {
- super(name, price);
- this.brand = brand;
- }
- public String getBrand() {
- return brand;
- }
- @Override
- public void displayProductInfo() {
- super.displayProductInfo();
- System.out.println("Brand: " + brand);
- }
- }
- // Subclass representing laptops
- class Laptop extends Electronics {
- private String processor;
- public Laptop(String name, double price, String brand, String processor) {
- super(name, price, brand);
- this.processor = processor;
- }
- public String getProcessor() {
- return processor;
- }
- @Override
- public void displayProductInfo() {
- super.displayProductInfo();
- System.out.println("Processor: " + processor);
- }
- }
- public class Multi {
- public static void main(String[] args) {
- // Create a Laptop object
- Laptop laptop = new Laptop("Dell XPS 13", 1299.99, "Dell", "Intel Core i7");
- // Display product information
- laptop.displayProductInfo();
- }
- }
- Exp 7: super & this keyword
- class Product {
- public int productId;
- public String productName;
- public double price;
- public Product(int productId, String productName, double price) {
- this.productId = productId;
- this.productName = productName;
- this.price = price;
- }
- public void displayProduct() {
- System.out.println("Product ID: " + productId);
- System.out.println("Product Name: " + productName);
- System.out.println("Price: $" + price);
- }
- public double getPrice() {
- return price;
- }
- }
- class DiscountedProduct extends Product {
- public double discount;
- public DiscountedProduct(int productId, String productName, double price, double discount) {
- super(productId, productName, price);
- this.discount = discount;
- }
- public void displayProductWithDiscount() {
- super.displayProduct();
- System.out.println("Discount: " + discount + "%");
- double discountedPrice = getPrice() - (getPrice() * discount / 100);
- System.out.println("Discounted Price: $" + discountedPrice);
- }
- }
- public class Lab7 {
- public static void main(String[] args) {
- Product product = new Product(1, "Laptop", 799.99);
- DiscountedProduct discountedProduct = new DiscountedProduct(2, "Smartphone", 299.99, 10.0);
- System.out.println("Product Details:");
- product.displayProduct();
- System.out.println();
- System.out.println("Discounted Product Details:");
- discountedProduct.displayProductWithDiscount();
- }
- }
- Exp 8: abstract & final keywords
- import java.util.Scanner;
- abstract class ECommerceProduct {
- private String name;
- private double price;
- public ECommerceProduct(String name, double price) {
- this.name = name;
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- public abstract void displayProductDetails();
- }
- final class Book extends ECommerceProduct {
- private String author;
- private int numberOfPages;
- public Book(String name, double price, String author, int numberOfPages) {
- super(name, price);
- this.author = author;
- this.numberOfPages = numberOfPages;
- }
- @Override
- public void displayProductDetails() {
- System.out.println("Book Details:");
- System.out.println("Name: " + getName());
- System.out.println("Price: " + getPrice());
- System.out.println("Author: " + author);
- System.out.println("Number of Pages: " + numberOfPages);
- }
- }
- public class Exp8 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter the book name: ");
- String bookName = scanner.nextLine();
- System.out.println("Enter the book price: ");
- double bookPrice = scanner.nextDouble();
- System.out.println("Enter the book author: ");
- String bookAuthor = scanner.nextLine();
- System.out.println("Enter the number of pages in the book: ");
- int bookNumberOfPages = scanner.nextInt();
- Book book = new Book(bookName, bookPrice, bookAuthor, bookNumberOfPages);
- book.displayProductDetails();
- scanner.close();
- }
- }
- Exp 9: methods of String class
- public class Exp9 {
- public static void main(String[] args) {
- String productTitle = "Apple iPhone 13 Pro";
- // Get the length of the product title
- int productTitleLength = productTitle.length();
- System.out.println("Product title length: " + productTitleLength);
- // Get the first character of the product title
- char firstChar = productTitle.charAt(0);
- System.out.println("First character of the product title: " + firstChar);
- // Get the last character of the product title
- int lastIndex = productTitle.length() - 1;
- char lastChar = productTitle.charAt(lastIndex);
- System.out.println("Last character of the product title: " + lastChar);
- // Check if the product title contains the word "Apple"
- boolean containsApple = productTitle.contains("Apple");
- System.out.println("Product title contains the word 'Apple': " + containsApple);
- // Replace the word "iPhone" with the word "Smartphone" in the product title
- String newProductTitle = productTitle.replace("iPhone", "Smartphone");
- System.out.println("New product title: " + newProductTitle);
- // Convert the product title to lowercase
- String lowercaseProductTitle = productTitle.toLowerCase();
- System.out.println("Lowercase product title: " + lowercaseProductTitle);
- // Convert the product title to uppercase
- String uppercaseProductTitle = productTitle.toUpperCase();
- System.out.println("Uppercase product title: " + uppercaseProductTitle);
- }
- }
- Exp 10: exception handling
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Exp10 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- ProductRepository productRepository = new ProductRepository();
- System.out.println("Enter the product name: ");
- String productName = scanner.nextLine();
- try {
- addProductToCart(productRepository, productName);
- } catch (ECommerceException e) {
- System.out.println(e.getMessage());
- scanner.close();
- }
- }
- public static void addProductToCart(ProductRepository productRepository, String productName) throws ECommerceException {
- // Check if the product exists.
- if (!ProductRepository.containsProduct(productName)) {
- throw new ECommerceException("Product does not exist.");
- }
- // Get the product from the repository.
- Product product = ProductRepository.getProduct(productName);
- // Check if the product is in stock.
- if (!product.isInStock()) {
- throw new ECommerceException("Product is out of stock.");
- }
- // Add the product to the cart.
- // ...
- }
- }
- class ECommerceException extends Exception {
- public ECommerceException(String message) {
- super(message);
- }
- }
- class ProductRepository {
- private static ArrayList<Product> products = new ArrayList<>();
- public static void addProduct(Product product) {
- products.add(product);
- }
- public static Product getProduct(String productName) {
- for (Product product : products) {
- if (product.getName().equals(productName)) {
- return product;
- }
- }
- return null;
- }
- public static boolean containsProduct(String productName) {
- return getProduct(productName) != null;
- }
- }
- class Product {
- private String name;
- private double price;
- private int stockLevel;
- public Product(String name, double price, int stockLevel) {
- this.name = name;
- this.price = price;
- this.stockLevel = stockLevel;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public int getStockLevel() {
- return stockLevel;
- }
- public void setStockLevel(int stockLevel) {
- this.stockLevel = stockLevel;
- }
- public boolean isInStock() {
- return stockLevel > 0;
- }
- }
- Exp 11: interface (packages aren't here)
- import java.util.ArrayList;
- import java.util.List;
- public interface Exp11 {
- void addProductToCart(String productName) throws ECommerceException;
- void removeProductFromCart(String productName) throws ECommerceException;
- void checkout() throws ECommerceException;
- }
- class ECommerceServiceImpl implements Exp11 {
- private ShoppingCart shoppingCart;
- public ECommerceServiceImpl() {
- this.shoppingCart = new ShoppingCart();
- }
- @Override
- public void addProductToCart(String productName) throws ECommerceException {
- // Check if the product exists.
- if (!ProductRepository.containsProduct(productName)) {
- throw new ECommerceException("Product does not exist.");
- }
- // Check if the product is in stock.
- Product product = ProductRepository.getProduct(productName);
- if (!product.isInStock()) {
- throw new ECommerceException("Product is out of stock.");
- }
- // Add the product to the cart.
- shoppingCart.addProduct(product);
- }
- @Override
- public void removeProductFromCart(String productName) throws ECommerceException {
- // Check if the product is in the cart.
- Product product = shoppingCart.getProduct(productName);
- if (product == null) {
- throw new ECommerceException("Product is not in the cart.");
- }
- // Remove the product from the cart.
- shoppingCart.removeProduct(product);
- }
- @Override
- public void checkout() throws ECommerceException {
- // Check if the cart is empty.
- if (shoppingCart.isEmpty()) {
- throw new ECommerceException("Cart is empty.");
- }
- // Process the checkout.
- // ...
- }
- }
- class ECommerceException extends Exception {
- public ECommerceException(String message) {
- super(message);
- }
- }
- class ShoppingCart {
- private List<Product> products;
- public ShoppingCart() {
- this.products = new ArrayList<>();
- }
- public void addProduct(Product product) {
- products.add(product);
- }
- public Product getProduct(String productName) {
- for (Product product : products) {
- if (product.getName().equals(productName)) {
- return product;
- }
- }
- return null;
- }
- public void removeProduct(Product product) {
- products.remove(product);
- }
- public boolean isEmpty() {
- return products.isEmpty();
- }
- }
- class Product {
- private String name;
- private double price;
- private int stockLevel;
- public Product(String name, double price, int stockLevel) {
- this.name = name;
- this.price = price;
- this.stockLevel = stockLevel;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public int getStockLevel() {
- return stockLevel;
- }
- public void setStockLevel(int stockLevel) {
- this.stockLevel = stockLevel;
- }
- public boolean isInStock() {
- return stockLevel > 0;
- }
- }
- MASTER:
- import java.util.Scanner;
- // Abstract class representing a product
- abstract class Product {
- String name;
- double price;
- Product(String name, double price) {
- this.name = name;
- this.price = price;
- }
- // Abstract method
- abstract void displayProductInfo();
- }
- // Intermediate class inheriting from Product
- class Electronics extends Product {
- String brand;
- Electronics(String name, double price, String brand) {
- super(name, price);
- this.brand = brand;
- }
- @Override
- void displayProductInfo() {
- System.out.println("Product: " + name);
- System.out.println("Price: $" + price);
- System.out.println("Brand: " + brand);
- }
- }
- // Final class inheriting from Electronics
- final class Laptop extends Electronics {
- String processor;
- Laptop(String name, double price, String brand, String processor) {
- super(name, price, brand);
- this.processor = processor;
- }
- @Override
- void displayProductInfo() {
- super.displayProductInfo();
- System.out.println("Processor: " + processor);
- }
- }
- public class Master {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Welcome to the E-Commerce Store!");
- System.out.print("Enter the name of the laptop: ");
- String laptopName = scanner.nextLine();
- System.out.print("Enter the price of the laptop: ");
- double laptopPrice = scanner.nextDouble();
- scanner.nextLine(); // Consume the newline character
- System.out.print("Enter the brand of the laptop: ");
- String laptopBrand = scanner.nextLine();
- System.out.print("Enter the processor of the laptop: ");
- String laptopProcessor = scanner.nextLine();
- Laptop laptop = new Laptop(laptopName, laptopPrice, laptopBrand, laptopProcessor);
- System.out.println("\nProduct Information:");
- laptop.displayProductInfo();
- scanner. Close();
- }
- }
- Interface:
- // Interface representing a generic product
- interface Product {
- String getName();
- double getPrice();
- void displayProductInfo();
- }
- // Interface representing electronic products
- interface Electronics extends Product {
- String getBrand();
- }
- // Interface representing laptops
- interface Laptop extends Electronics {
- String getProcessor();
- }
- // Concrete class implementing the Laptop interface
- class DellLaptop implements Laptop {
- private String name;
- private double price;
- private String brand;
- private String processor;
- public DellLaptop(String name, double price, String brand, String processor) {
- this.name = name;
- this.price = price;
- this.brand = brand;
- this.processor = processor;
- }
- @Override
- public String getName() {
- return name;
- }
- @Override
- public double getPrice() {
- return price;
- }
- @Override
- public String getBrand() {
- return brand;
- }
- @Override
- public String getProcessor() {
- return processor;
- }
- @Override
- public void displayProductInfo() {
- System.out.println("Product: " + name);
- System.out.println("Price: $" + price);
- System.out.println("Brand: " + brand);
- System.out.println("Processor: " + processor);
- }
- }
- public class Demo {
- public static void main(String[] args) {
- // Create a DellLaptop object
- Laptop laptop = new DellLaptop("Dell XPS 13", 1299.99, "Dell", "Intel Core i7");
- // Display product information
- System.out.println("Welcome to the E-Commerce Store!");
- System.out.println("\nProduct Information:");
- laptop.displayProductInfo();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement