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

lab task 2

The document outlines three programming tasks focused on design patterns and object-oriented programming principles. The tasks involve managing products in an e-commerce store, managing students and teachers in a school system, and managing bank accounts, each requiring specific extensions or modules. Additionally, it includes a list of IDs associated with each task for tracking purposes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lab task 2

The document outlines three programming tasks focused on design patterns and object-oriented programming principles. The tasks involve managing products in an e-commerce store, managing students and teachers in a school system, and managing bank accounts, each requiring specific extensions or modules. Additionally, it includes a list of IDs associated with each task for tracking purposes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Design Pattern Lab task-2

***Extend the given code only. Don’t copy.


1. E-Commerce Product Management
Manage a list of products in an e-commerce store using the Factory Method and Singleton Pattern.
// Singleton: ProductManager
class ProductManager {
private static ProductManager instance;
private List<Product> products = new ArrayList<>();

private ProductManager() {}

public static ProductManager getInstance() {


if (instance == null) {

instance = new ProductManager();


}
return instance;
}

public void addProduct(Product product) {


products.add(product);
}

public void viewProducts() {


for (Product product : products) {
System.out.println(product);
}
}
}

// Base class: Product


abstract class Product {
protected String name;
protected double price;

public Product(String name, double price) {


this.name = name;
this.price = price;
}
public abstract String getCategory();

@Override
public String toString() {
return "Name: " + name + ", Price: " + price + ", Category: " + getCategory();
}
}

// Subclasses
class Electronics extends Product {
public Electronics(String name, double price) {
super(name, price);
}

public String getCategory() {


return "Electronics";
}
}

class Clothing extends Product {


public Clothing(String name, double price) {
super(name, price);
}

public String getCategory() {


return "Clothing";
}
}

// Factory Method: ProductFactory


class ProductFactory {
public static Product createProduct(String category, String name, double price) {
switch (category.toLowerCase()) {
case "electronics": return new Electronics(name, price);
case "clothing": return new Clothing(name, price);
default: throw new IllegalArgumentException("Unknown category");
}
}
}
Task for Student: Add a module to calculate and apply a discount based on the product category
using the oop concept

2. School Management System


Manage students and teachers using OOP principles and inheritance.

// Base class: Person


abstract class Person {
protected String name;
protected int id;

public Person(String name, int id) {


this.name = name;
this.id = id;
}

public abstract String getRole();

@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Role: " + getRole();
}
}

// Subclasses
class Student extends Person {
private String grade;

public Student(String name, int id, String grade) {


super(name, id);
this.grade = grade;
}

public String getRole() {


return "Student (Grade: " + grade + ")";
}
}

class Teacher extends Person {


private String subject;

public Teacher(String name, int id, String subject) {


super(name, id);
this.subject = subject;
}
public String getRole() {
return "Teacher (Subject: " + subject + ")";
}
}

// Manager Class
class SchoolManager {
private List<Person> people = new ArrayList<>();

public void addPerson(Person person) {


people.add(person);
}

public void viewPeople() {


for (Person person : people) {
System.out.println(person);
}
}
}

Task for Student: Add a module to allow filtering people based on their role or additional attributes
(e.g., list all students of a specific grade). Use the oop concept. Singletone and factory method is not
necessary

3. Bank Account Management


Manage bank accounts using the Factory Method and basic account operations.

// Base class: BankAccount


abstract class BankAccount {
protected String accountNumber;
protected double balance;

public BankAccount(String accountNumber, double balance) {


this.accountNumber = accountNumber;
this.balance = balance;
}

public abstract String getAccountType();

public void deposit(double amount) {


balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds");
}
}

@Override
public String toString() {
return "Account Number: " + accountNumber + ", Balance: " + balance + ", Type: " +
getAccountType();
}
}

// Subclasses
class SavingsAccount extends BankAccount {
public SavingsAccount(String accountNumber, double balance) {
super(accountNumber, balance);
}

public String getAccountType() {


return "Savings";
}
}

class CurrentAccount extends BankAccount {


public CurrentAccount(String accountNumber, double balance) {
super(accountNumber, balance);
}

public String getAccountType() {


return "Current";
}
}

// Factory Method: AccountFactory


class AccountFactory {
public static BankAccount createAccount(String type, String accountNumber, double
balance) {
switch (type.toLowerCase()) {
case "savings": return new SavingsAccount(accountNumber, balance);
case "current": return new CurrentAccount(accountNumber, balance);
default: throw new IllegalArgumentException("Unknown account type");
}
}
}

// Manager Class
class BankManager {
private List<BankAccount> accounts = new ArrayList<>();

public void addAccount(BankAccount account) {


accounts.add(account);
}

public void viewAccounts() {


for (BankAccount account : accounts) {
System.out.println(account);
}
}
}

Task for Student: Add a module for calculating interest for SavingsAccount and overdraft facilities
for CurrentAccount. You can use oop concept or the design pattern concept here.

ID---------------------------------------Task NO
432220005191002 1
432220005191004 2
432220005191005 3
432220005191006 1
432220005191007 3
432220005191008 2
432220005191009 3
432220005191010 1
432220005191011 2
432220005191012 1
432220005191017 2
432220005191018 3
432310005191002 2
432310005191003 1
432310005191004 3
432310005191006 3
432310005191009 2
432310005191010 1
432310005191011 1
432310005191012 2
1914555032 3
1914555036 1
2044855003 2
2114955003 3
2114955004 2
2114955005 3
2114955007 1
2114955011 1
2215155003 2
2215155017 3
1914555009 1
1914555014 2

You might also like