0% found this document useful (0 votes)
27 views

Assignment 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Assignment 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT – 2

PROGRAM
// Base class Product

class Product {

private String name;

private double price;

private int quantity;

public Product(String name, double price, int quantity) {

this.name = name;

this.price = price;

this.quantity = quantity;

public String getName() {

return name;

public double getPrice() {

return price;

public int getQuantity() {

return quantity;
}

public void setName(String name) {

this.name = name;

public void setPrice(double price) {

this.price = price;

public void setQuantity(int quantity) {

this.quantity = quantity;

// Method to calculate discount

public double calculateDiscount() {

// Default discount is 5%

return price * 0.05;

// Method to display product details

public void displayProductDetails() {

System.out.println("Product Name: " + name);

System.out.println("Price: $" + price);

System.out.println("Quantity: " + quantity);


System.out.println("Discount: $" + calculateDiscount());

// Subclass ElectronicProduct

class ElectronicProduct extends Product {

private String brand;

private int warranty; // warranty period in months

public ElectronicProduct(String name, double price, int quantity, String brand, int warranty) {

super(name, price, quantity);

this.brand = brand;

this.warranty = warranty;

public String getBrand() {

return brand;

public int getWarranty() {

return warranty;

public void setBrand(String brand) {

this.brand = brand;
}

public void setWarranty(int warranty) {

this.warranty = warranty;

@Override

public double calculateDiscount() {

// Electronics have a 10% discount

return getPrice() * 0.10;

@Override

public void displayProductDetails() {

super.displayProductDetails();

System.out.println("Brand: " + brand);

System.out.println("Warranty: " + warranty + " months");

// Subclass ClothingProduct

class ClothingProduct extends Product {

private String size;

private String fabricType;


public ClothingProduct(String name, double price, int quantity, String size, String fabricType) {

super(name, price, quantity);

this.size = size;

this.fabricType = fabricType;

public String getSize() {

return size;

public String getFabricType() {

return fabricType;

public void setSize(String size) {

this.size = size;

public void setFabricType(String fabricType) {

this.fabricType = fabricType;

@Override

public double calculateDiscount() {

// Clothing items have a 15% discount


return getPrice() * 0.15;

@Override

public void displayProductDetails() {

super.displayProductDetails();

System.out.println("Size: " + size);

System.out.println("Fabric Type: " + fabricType);

// Main class to test the e-commerce system

public class EcommerceSystem {

public static void main(String[] args) {

Product product1 = new ElectronicProduct("Smartphone", 589.99, 20, "Vivo",28);

Product product2 = new ClothingProduct("trousers", 28.99,30, "L", "denim wool");

System.out.println("Electronic Product Details are:");

product1.displayProductDetails();

System.out.println("\nClothing Product Details:");

product2.displayProductDetails();

You might also like