You are on page 1of 8

QUIZ NO.

3:
NAME: umaima zahoor

Class: S.E -II


Subject: OOP

Roll No: 122

PROGRAM 1:
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 void setName(String name) {

this.name = name;

public double getPrice() {

return price;

public void setPrice(double price) {


this.price = price;

class Book extends Product {

private String author;

public Book(String name, double price, String author) {

super(name, price);

this.author = author;

public String getAuthor() {

return author;

public void setAuthor(String author) {

this.author = author;

class ElectronicDevice extends Product {

private String brand;

public ElectronicDevice(String name, double price, String brand) {

super(name, price);

this.brand = brand;

}
public String getBrand() {

return brand;

public void setBrand(String brand) {

this.brand = brand;

class ShoppingCart {

private Product[] items;

private int itemCount;

private int capacity;

public ShoppingCart(int capacity) {

this.capacity = capacity;

items = new Product[capacity];

itemCount = 0;

public void addProduct(Product product) {

if (itemCount < capacity) {

items[itemCount] = product;

itemCount++;

} else {

throw new IllegalStateException("Shopping cart is full!");

}
public double calculateTotalPrice() {

double totalPrice = 0.0;

for (int i = 0; i < itemCount; i++) {

totalPrice += items[i].getPrice();

return totalPrice;

public class Main {

public static void main(String[] args) {

Book book = new Book("Java Programming", 29.99, "John Doe");

ElectronicDevice phone = new ElectronicDevice("Smartphone", 499.99, "Samsung");

ShoppingCart cart = new ShoppingCart(5);

cart.addProduct(book);

cart.addProduct(phone);

System.out.println("Total price of the shopping cart: $" + cart.calculateTotalPrice());

OUTPUT 1:

Total price of the shopping cart: $529.98.

PROGRAM 2:

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 setPrice(double price) {


this.price = price;
}
}

interface Promotion {
double calculateDiscount(double price);
}

class PercentageDiscount implements Promotion {


private double discountPercentage;
public PercentageDiscount(double discountPercentage) {
this.discountPercentage = discountPercentage;
}

@Override
public double calculateDiscount(double price) {
return price * (1 - discountPercentage / 100);
}
}

class BuyOneGetOneFree implements Promotion {


@Override
public double calculateDiscount(double price) {
// For buy one get one free, we need to halve the price
return price / 2;
}
}

class ShoppingCart {
private Product[] items;
private int itemCount;
private Promotion promotion;

public ShoppingCart(int capacity, Promotion promotion) {


items = new Product[capacity];
itemCount = 0;
this.promotion = promotion;
}
public void addProduct(Product product) {
if (itemCount < items.length) {
items[itemCount] = product;
itemCount++;
} else {
throw new IllegalStateException("Shopping cart is full!");
}
}

public double calculateTotalPrice() {


double totalPrice = 0.0;
for (int i = 0; i < itemCount; i++) {
totalPrice += promotion.calculateDiscount(items[i].getPrice());
}
return totalPrice;
}
}

public class Main {


public static void main(String[] args) {
Product book = new Product("Java Programming", 29.99);
Product phone = new Product("Smartphone", 499.99);

Promotion percentageDiscount = new PercentageDiscount(10); // 10% discount


Promotion buyOneGetOneFree = new BuyOneGetOneFree();

ShoppingCart cart1 = new ShoppingCart(5, percentageDiscount);


cart1.addProduct(book);
cart1.addProduct(phone);

ShoppingCart cart2 = new ShoppingCart(5, buyOneGetOneFree);


cart2.addProduct(book);
cart2.addProduct(phone);

System.out.println("Total price with 10% discount: $" + cart1.calculateTotalPrice());


System.out.println("Total price with buy one get one free: $" +
cart2.calculateTotalPrice());
}
}

OUTPUT 2:
Total price with 10% discount: $494.99099999999999
Total price with buy one get one free: $464.99

You might also like