Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exp1:
- import java.io.*;
- import java.util.Scanner;
- class exp1 {
- public static void main(String args[]) throws IOException {
- String choice;
- Scanner d = new Scanner(System.in);
- for(int i = 0;i<2;i++){
- System.out.println("Welcome to the E-mart!");
- }
- int ab = 0;
- while(ab<2){
- System.out.println("Welcome dear customer");
- ab++;
- }
- do {
- System.out.print("\nEnter the budget amount: ");
- int a = d.nextInt();
- if (a == 0) {
- System.out.println("Nothing can be purchased for free.");
- System.out.println("Thanks for visiting!");
- } else if (a <= 1200) {
- System.out.println("You can purchase some groceries and some clothes.");
- } else if( a>1200 && a<=2000){
- System.out.println("You can purchase shoes and ties");
- } else if(a>2000 && a<=3000){
- System.out.println(" You can buy some suits");
- } else {
- System.out.println("Items are being added");
- System.out.println("Thanks for visiting");
- }
- d.nextLine();
- System.out.print("Enter if you want to order? (Y/N): ");
- choice = d.nextLine();
- } while (choice.equalsIgnoreCase("y"));
- d.close();
- }
- }
- Exp2:
- //switch cases
- import java.util.Scanner;
- public class exp2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Welcome to the E-Commerce Store!");
- System.out.println("Select a product to view its price:");
- System.out.println("1. Laptop");
- System.out.println("2. Smartphone");
- System.out.println("3. Headphones");
- System.out.println("4. Exit");
- int choice;
- do {
- System.out.print("Enter your choice (1-4): ");
- choice = scanner.nextInt();
- switch (choice) {
- case 1:
- System.out.println("Laptop: $800");
- break;
- case 2:
- System.out.println("Smartphone: $500");
- break;
- case 3:
- System.out.println("Headphones: $50");
- break;
- case 4:
- System.out.println("Thank you for visiting the store!");
- break;
- default:
- System.out.println("Invalid choice. Please enter a valid option.");
- }
- } while (choice != 4);
- scanner. Close();
- }
- }
- Exp3:
- //arrays
- import java.io.*;
- import java.util.Scanner;
- class exp3 {
- public static void main(String args[]) throws IOException {
- Scanner d = new Scanner(System.in);
- String num[] = {"Soap", "Pencil", "Notebook", "Detergent(1 Kg)", "Pen", "Clothes"};
- System.out.println("Item present in List");
- for (int i = 0; i < num.length; i++) {
- System.out.println((i + 1) + ". " + num[i]);
- }
- System.out.print("Enter how many items you want to add: ");
- int m = d.nextInt();
- d.nextLine();
- String arr[][] = new String[m][2];
- for (int j = 0; j < m; j++) {
- System.out.print("Enter the item " + (j + 1) + ": ");
- arr[j][0] = d.nextLine();
- System.out.print("Enter the quantity: ");
- arr[j][1] = d.nextLine();
- }
- boolean itemsMatch = true;
- for (int j = 0; j < m; j++) {
- boolean found = false;
- for (int i = 0; i < num.length; i++) {
- if (arr[j][0].equalsIgnoreCase(num[i])) {
- found = true;
- break;
- }
- }
- if (!found) {
- itemsMatch = false;
- break;
- }
- }
- if (itemsMatch) {
- System.out.println("\nYour item\tQuantity\tPrice");
- for (int j = 0; j < m; j++) {
- double price = calculatePrice(arr[j][0].toLowerCase(), Integer.parseInt(arr[j][1]));
- System.out.println(arr[j][0] + "\t\t" + arr[j][1] + "\t\t$" + price);
- }
- } else {
- System.out.println("Your item list doesn't match with the items in the store.");
- }
- d.close();
- }
- private static double calculatePrice(String item, int quantity) {
- double pricePerUnit;
- switch (item) {
- case "soap":
- pricePerUnit = 1.0;
- break;
- case "pencil":
- pricePerUnit = 0.5;
- break;
- case "notebook":
- pricePerUnit = 2.0;
- break;
- case "detergent(1 kg)":
- pricePerUnit = 3.5;
- break;
- case "pen":
- pricePerUnit = 1.2;
- break;
- case "clothes":
- pricePerUnit = 10.0;
- break;
- default:
- pricePerUnit = 0.0; // Item not found in the list
- }
- return pricePerUnit * quantity;
- }
- }
- Exp4:
- //method and constructor overloading
- class Product{
- String name;
- double price;
- Product(String name,double price){
- this.name=name;
- this.price=price;
- }
- String getname(){
- return name;
- }
- double getprice(){
- return price;
- }
- void display(){
- System.out.println("Product: "+name);
- System.out.println("Price: $"+price);
- }
- }
- class shopc{
- int icount;
- double tamount;
- shopc(){
- icount=0;
- tamount=0.0;
- }
- void addc(Product product){
- icount++;
- tamount+=product.getprice();
- System.out.println(product.getname()+" added to cart");
- }
- void addc(Product product,int qty){
- icount+=qty;
- tamount+=product.getprice()*qty;
- System.out.println(qty+""+product.getname()+" added to cart");
- }
- float display(int a, float b){
- /*System.out.println("Shopping Cart:");
- System.out.println("Total Items: " + icount);
- System.out.println("Total Amount: $" + tamount);*/
- return (a+b);
- }
- }
- class exp4{
- public static void main(String args[]){
- Product laptop = new Product("Laptop",1000.0);
- Product phone = new Product("Smartphone",500.0);
- shopc cart = new shopc();
- cart.addc(laptop);
- cart.addc(phone,2);
- System.out.println(cart.display(12,(float)32.5));
- }
- }
- Exp5:
- //exception handling
- import java.util.Scanner;
- class Billing {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter the bill amount: ");
- double billAmount = scanner.nextDouble();
- System.out.print("Enter your credit card number: ");
- String cardNumber = scanner.next();
- try {
- validateCreditCard(cardNumber);
- processPayment(billAmount);
- System.out.println("Payment successful. Thank you for shopping!");
- } catch (InvalidCreditCardException e) {
- System.out.println("Payment failed: " + e.getMessage());
- }
- scanner.close();
- }
- static void validateCreditCard(String cardNumber) throws InvalidCreditCardException {
- if (cardNumber.length() > 16) {
- throw new InvalidCreditCardException("Card number is too long.");
- }
- if (!isVisaOrMastercard(cardNumber)) {
- throw new InvalidCreditCardException("Card not issued by Visa/Mastercard.");
- }
- }
- static boolean isVisaOrMastercard(String cardNumber) {
- // You can implement your card type detection logic here.
- // For simplicity, we assume Visa and Mastercard numbers start with 4 and 5.
- return cardNumber.startsWith("4") || cardNumber.startsWith("5");
- }
- static void processPayment(double amount) {
- // This method would process the payment, but for this example, we'll just print a message.
- System.out.println("Processing payment of $" + amount);
- }
- }
- class InvalidCreditCardException extends Exception {
- public InvalidCreditCardException(String message) {
- super(message);
- }
- }
- Exp6:
- //packages
- //main driver code
- package Packages;
- public class ecommerce {
- public static void main(String[] args) {
- // Create some sample products
- Product product1 = new Product(101, "Laptop", 799.99);
- Product product2 = new Product(102, "Smartphone", 299.99);
- Product product3 = new Product(103, "Headphones", 49.99);
- // Create a shopping cart
- ShoppingCart cart = new ShoppingCart();
- // Add products to the cart
- cart.addProduct(product1, 2);
- cart.addProduct(product2, 1);
- cart.addProduct(product3, 3);
- // Display the contents of the shopping cart
- System.out.println("Shopping Cart Contents:");
- cart.displayCart();
- // Create an order from the shopping cart
- Order order = new Order(cart);
- // Display the order details
- System.out.println("\nOrder Details:");
- order.displayOrder();
- }
- }
- //package - separate file
- package Packages;
- import java.util.Date;
- public class Order {
- private ShoppingCart cart;
- private Date orderDate;
- public Order(ShoppingCart cart) {
- this.cart = cart;
- this.orderDate = new Date();
- }
- public void displayOrder() {
- System.out.print("Order Date: " + orderDate);
- System.out.println("\n");
- System.out.println("Order Items:");
- cart.displayCart();
- }
- }
- //package
- package Packages;
- public class Product {
- private int id;
- private String name;
- private double price;
- public Product(int id, String name, double price) {
- this.id = id;
- this.name = name;
- this.price = price;
- }
- // Getters and setters for id, name, and price
- public String toString() {
- return name + " (ID: " + id + ", Price: Rs. " + price + ")";
- }
- }
- //package
- package Packages;
- import java.util.HashMap;
- import java.util.Map;
- public class ShoppingCart {
- private Map<Product, Integer> items;
- public ShoppingCart() {
- items = new HashMap<>();
- }
- public void addProduct(Product product, int quantity) {
- items.put(product, quantity);
- }
- public void displayCart() {
- for (Map.Entry<Product, Integer> entry : items.entrySet()) {
- Product product = entry.getKey();
- int quantity = entry.getValue();
- System.out.println(product + " x" + quantity);
- }
- }
- }
- Master (except packages):
- import java.util.Scanner;
- public class ECommerceDemo {
- // 1. Entry Exit Control Loop
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int choice;
- do {
- System.out.println("E-Commerce Application Menu");
- System.out.println("1. Browse Products");
- System.out.println("2. Add to 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:
- browseProducts();
- break;
- case 2:
- addToCart();
- break;
- case 3:
- viewCart();
- break;
- case 4:
- checkout();
- break;
- case 5:
- System.out.println("Exiting the application.");
- break;
- default:
- System.out.println("Invalid choice. Please try again.");
- }
- } while (choice != 5);
- }
- // 2. Switch Case with Nested Switch Case
- static void browseProducts() {
- System.out.println("Browse Products Menu");
- System.out.println("1. Electronics");
- System.out.println("2. Clothing");
- System.out.println("3. Back to Main Menu");
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- switch (choice) {
- case 1:
- System.out.println("Electronics Section");
- break;
- case 2:
- System.out.println("Clothing Section");
- break;
- case 3:
- System.out.println("Returning to the Main Menu");
- break;
- default:
- System.out.println("Invalid choice. Please try again.");
- }
- }
- // 3. Arrays - 1D and 2D
- static void viewCart() {
- String[] cartItems = {"Laptop", "T-Shirt", "Headphones", "Shoes"};
- System.out.println("Items in Your Cart:");
- for (String item : cartItems) {
- System.out.println(item);
- }
- int[][] itemQuantities = {{2, 1}, {3, 2}, {1, 1}, {2, 2}};
- System.out.println("Quantity of Items in Your Cart:");
- for (int i = 0; i < cartItems.length; i++) {
- System.out.println(cartItems[i] + ": " + itemQuantities[i][0] + " items, " + itemQuantities[i][1] + " in stock");
- }
- }
- // 4. Constructor & Method Overloading
- static void addToCart() {
- String product = "Laptop";
- int quantity = 2;
- String seller = "E-Store Inc";
- addToCart(product, quantity);
- addToCart(product, quantity, seller);
- }
- static void addToCart(String product, int quantity) {
- System.out.println(quantity + " " + product + "(s) added to your cart.");
- }
- static void addToCart(String product, int quantity, String seller) {
- System.out.println(quantity + " " + product + "(s) from " + seller + " added to your cart.");
- }
- // 5. Exception Handling
- static void checkout() {
- try {
- // Simulate a checkout process
- int totalAmount = 1000;
- int availableBalance = 800;
- int remainingBalance = totalAmount - availableBalance;
- if (remainingBalance > 0) {
- throw new InsufficientBalanceException("Insufficient balance! You need $" + remainingBalance + " more.");
- } else {
- System.out.println("Checkout successful. Your order has been placed.");
- }
- } catch (InsufficientBalanceException e) {
- System.err.println("Error: " + e.getMessage());
- }
- }
- }
- class InsufficientBalanceException extends Exception {
- public InsufficientBalanceException(String message) {
- super(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement