简体   繁体   中英

Passing data of a variable to another class

EDITED to clarify further.

I'm trying to build a simple api which handles the CRUD operation on my simple MySQL database for my simple Ordering Web App. The api will then be used on Angular.

Basically, the conditions for the ordering system is that there would be a 5% off on selected orders, so that would be DiscountedBill job to calculate on how much it would be. Other items do not have the discount, so that would be for RegularBill job to calculate.

As I have understood on this diagram, OrderBill class will have to calculate on how much the TOTAL of ALL orders if the user has DiscountedBill and RegularBill orders on my system.

图表

Would that be possible?

This is the main OrderBill class.

public class OrderBill {

    private List<Order> orderList;
    private CafeClerk clerk;

    public OrderBill(CafeClerk clerk) {
        this.clerk = clerk;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }

    public CafeClerk getClerk() {
        return clerk;
    }

    public void setClerk(CafeClerk clerk) {
        this.clerk = clerk;
    }

    public double getTotalBill() {
        // function here to add the computed data of both DiscountedBill + RegularBill
        return 0;
    }
}

RegularBill class –

public class RegularBill extends OrderBill {

    public RegularBill(CafeClerk clerk) {
        super(clerk);
    }

    double totalRegBill = 0.0;

    @Override
    public double getTotalBill() {
        for (Order order : super.getOrderList()) {
            totalRegBill += (order.getPrice());
        }
        return totalRegBill;
    }

    public double getTotalRegBill() {
        return totalRegBill;
    }

    public void setTotalRegBill(double totalRegBill) {
        this.totalRegBill = totalRegBill;
    }
}

DiscountedBill class –

public class DiscountedBill extends OrderBill {

    public DiscountedBill(CafeClerk clerk) {
        super(clerk);
    }

    final double DISCOUNT = 0.05;
    double totalDiscBill = 0.0;

    @Override
    public double getTotalBill() {
        for (Order order :super.getOrderList()) {
            totalDiscBill += (order.getPrice() - DISCOUNT);
         }
        return totalDiscBill;
    }

    public double getTotalDiscBill() {
        return totalDiscBill;
    }

    public void setTotalDiscBill(double totalDiscBill) {
        this.totalDiscBill = totalDiscBill;
    }
}

I am not sure if I understand the purpose correctly, but if you want to sum up the DiscountedBill and RegularBill in OrderBill , don't you rather need a structure like this?

public class OrderBill {

    private List<Order> orderList;
    private CafeClerk clerk;
    private RegularBill regularBill;
    private DiscountedBill discountedBill;

    public OrderBill(CafeClerk clerk) {
        this.clerk = clerk;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }

    public CafeClerk getClerk() {
        return clerk;
    }

    public void setClerk(CafeClerk clerk) {
        this.clerk = clerk;
    }

    public double getTotalBill() {
        return regularBill.getTotalBill(orderList) + discountedBill.getTotalBill(orderList);
    }
}
public class RegularBill {

    public RegularBill() {}

    double totalRegBill = 0.0;

    public double getTotalBill(List<Order> orders) {
        for (Order order : orders) {
            totalRegBill += (order.getPrice());
        }
        return totalRegBill;
    }

    public double getTotalRegBill() {
        return totalRegBill;
    }

    public void setTotalRegBill(double totalRegBill) {
        this.totalRegBill = totalRegBill;
    }
}
public class DiscountedBill {

    public DiscountedBill() { }

    final double DISCOUNT = 0.05;
    double totalDiscBill = 0.0;

    public double getTotalBill(List<Order> orders) {
        for (Order order : orders) {
            totalDiscBill += (order.getPrice() - DISCOUNT);
        }
        return totalDiscBill;
    }

    public double getTotalDiscBill() {
        return totalDiscBill;
    }

    public void setTotalDiscBill(double totalDiscBill) {
        this.totalDiscBill = totalDiscBill;
    }
}

Of course, enhancing this solution is necessary. But that may be a hint already. You can then implement an interface with getTotalBill() method.

If I understand that right, you could do:

public DiscountedBill(RegularBill bill) {
    this.setOrderList(bill.getOrderList);
    this.setClerk(bill.getClerk());
}

@Override
public double getTotalBill() {
    for (Order order :this.getOrderList()) {
        totalDiscBill += (order.getPrice() - DISCOUNT);
     }
    return totalDiscBill;
}

Notice: If I get it right, you are just subtracting 0.05 in total not in %, mabbe it should be:

@Override
public double getTotalBill() {
    for (Order order :this.getOrderList()) {
        totalDiscBill += (order.getPrice() - (order.getPrice() * DISCOUNT));
     }
    return totalDiscBill;
}

But it is just a guess what I think what you trying to archieve.

If you need to recreate this diagram, then my hint is that you make OrderBill abstract, and getTotalBill() interests you only in the RegularBill and DiscountedBill .

You cannot really add up these two classes inside of OrderBill with the given structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM