如何将三个类的实例绑在一起?

时间:2016-12-10 21:02:29

标签: java

我有一个客户类。

public Customer(boolean regular, boolean payAhead, boolean loyal, String userName, double amountOfStorage) {
    this.regular = regular;
    this.payAhead = payAhead;
    this.loyal = loyal;
    this.userName = userName;
    this.amtOfStore = amountOfStorage;
}

我使用Map为用户通过JTextField选择他们的客户。

Map<String, Customer> map = new HashMap<>();
map.put("Kyle", new Customer(true, false, false, "Kyle", 1000.00));
map.put("Andrew", new Customer(false, true, false, "Andrew", 0.00));
map.put("Connor", new Customer(false, false, true, "Connor", 5000.00));

这一切都有效。我也有一个商店类。

public Store(String name, double cost, double mbAmount, int itemCnt, double ship) {
    this.name = name;
    this.cost = cost;
    this.mbAmount = mbAmount;
    this.itemCnt = itemCnt;
    this.ship = ship;
}

使用多个商店实例。

Store sitem1 = new Store("Movie", 20.00, 500, 0, 0.00);
Store sitem2 = new Store("Lamborghini", 2000000.00, 0, 0, 5000);
Store sitem3 = new Store("Song", .99, 50, 0, 0);
Store sitem4 = new Store("Headphones", 25.00, 0, 0, 3.00);
Store sitem5 = new Store("iPhone", 894.92, 0, 0, 10.00);
Store sitem6 = new Store("Coffe Maker", 50.67, 0, 0, 7.00);
Store sitem7 = new Store("Video Game", 59.99, 40000, 0, 0);
Store sitem8 = new Store("Laptop", 500.00, 0, 0, 6.76);
Store sitem9 = new Store("Old Movie", 2.99, 100, 0, 0);
Store sitem10 = new Store("Tv Show", .99, 20, 0, 0);

和ShoppingCart类。

    public ShoppingCart(double shippingCost, double storageSize, double totalPrice, double temp, int cartCount) {
    this.shippingCost = shippingCost;
    this.storageSize = storageSize;
    this.totalPrice = totalPrice;
    this.temp = temp;
    this.cartCount = cartCount;
}

我需要它,所以当一个客户注销时(程序永不关闭),Store和Shopping Cart类上的另一个日志对他来说是唯一的,同时保留前一个客户输入的信息。在这种情况下,程序永远不会关闭。

1 个答案:

答案 0 :(得分:0)

将ShoppingCart添加到您的Customer类,代表该客户当前的购物车。

通过客户访问和修改购物车。

  

客户有一个购物车。