Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Correction and Improvement for Shop Management Project #2

Open
isaiasgh opened this issue Jun 21, 2024 · 0 comments
Open

Code Correction and Improvement for Shop Management Project #2

isaiasgh opened this issue Jun 21, 2024 · 0 comments

Comments

@isaiasgh
Copy link

Hi,

I've reviewed the code for your shop management project and identified some areas that could be improved to enhance efficiency and fix certain errors. Below, I outline the suggested changes:

Code Changes

  1. Correct Errors in List Handling: In the Shop class, ensure to properly initialize the productList and operationsList in the constructor.
  2. Method Implementation: Implement the enlistProduct() and addOperation() methods in the Shop class to correctly add products and operations to their respective lists.
  3. Inheritance Hierarchy and Interface Usage: Clearly define the interfaces and their implementations (TransactionalOperation and ProductOperation).

Modified Code

TransactionalOperation Interface:

public interface TransactionalOperation {
    void execute();
}

ProductOperation Interface:

public interface ProductOperation {
    void apply(Product product);
}

Shop Class

import java.util.ArrayList;
import java.util.List;

public class Shop {
    private List<Product> productList;
    private List<TransactionalOperation> operationsList;

    public Shop() {
        this.productList = new ArrayList<>();
        this.operationsList = new ArrayList<>();
    }

    public void enlistProduct(Product product) {
        this.productList.add(product);
    }

    public void addOperation(TransactionalOperation operation) {
        this.operationsList.add(operation);
    }
}

Product Class

public 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;
    }

    // Getters and Setters
    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 getQuantity() { return quantity; }
    public void setQuantity(int quantity) { this.quantity = quantity; }
}

Purchase Class

public class Purchase implements TransactionalOperation, ProductOperation {
    private Product product;
    private int quantity;

    public Purchase(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }

    @Override
    public void execute() {
        apply(product);
    }

    @Override
    public void apply(Product product) {
        product.setQuantity(product.getQuantity() + quantity);
    }
}

Sale Class

public class Sale implements TransactionalOperation, ProductOperation {
    private Product product;
    private int quantity;

    public Sale(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }

    @Override
    public void execute() {
        apply(product);
    }

    @Override
    public void apply(Product product) {
        product.setQuantity(product.getQuantity() - quantity);
    }
}

Damage Class

public class Damage implements TransactionalOperation, ProductOperation {
    private Product product;
    private int quantity;

    public Damage(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }

    @Override
    public void execute() {
        apply(product);
    }

    @Override
    public void apply(Product product) {
        product.setQuantity(product.getQuantity() - quantity);
    }
}

UML Diagrams

WhatsApp Image 2024-06-16 at 3 57 44 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant