2016-10-07 56 views
-11

我有一个具有类Groceries的ArrayList的Person类。 让我们来命名Arraylist shoppingBag每当一个新的对象被添加到数组列表时,Reduse整数

学生和杂货店各有一个字段,int moneyint price。 初始化新对象时,具体金额和价格由您决定。

因此,每当某人将一个Groceries对象添加到他的shoppingBag时,他所需的金额就需要随着添加到包中的杂货总价格而减少。

你是怎么做的?

+3

_你怎么做?_你是怎么做到的?向我们显示你的代码.. –

回答

1

所以,让我试着去了解你想要什么(因为我也一样,我的客户)

你有一类食品杂货价格字段:

class Groceries { 
    private int price; 

    public Groceries(int price) { 
     this.price = price; 
    } 

    public int getPrice() { 
     return price; 
    } 

    @Override 
    public String toString() { 
     return "Groceries{" + 
       "price=" + price + 
       '}'; 
    } 
} 

和类人用INT钱提起和购物袋场杂货清单:

class Person { 

    private List<Groceries> shoppingBag = new ArrayList<>(); 
    private int money; 

    public Person(int money) { 
     this.money = money; 
    } 

    public List<Groceries> getShoppingBag() { 
     return shoppingBag; 
    } 

    public int getMoney() { 
     return money; 
    } 
} 

首先创建的Person实例与一些钱安装:Person person = new Person(150);

然后每次当您向购物袋添加杂货时,例如person.getShoppingBag().add(new Groceries(10));,您都希望减少个人实例的金额。

所以,如果我是正确的,你需要实现几件事情:

1)你应该禁止将杂货的购物袋用之前介绍的方式。当有人试图通过getter将一个元素添加到列表中时,我们需要抛出异常。它可以使用列表的不可修改的拷贝来实现:

public List<Groceries> getShoppingBag() { 
    List<Groceries> bag = new UnmodifiableArrayList<>(shoppingBag.toArray(new Groceries[shoppingBag.size()]), shoppingBag.size()); 
    return bag; 
} 

还是有点精美,使用不久番石榴:

public List<Groceries> getShoppingBag() { 
    List<Groceries> bag = ImmutableList.copyOf(shoppingBag); 
    return bag; 
} 

2)加入,将直接增加一个杂货的方法。您也可以抛出一个异常,如果没有足够的钱没有欠款:

public void addToShoppingBag(Groceries groceries) { 

    if (0 > money - groceries.getPrice()) { 
     throw new IllegalStateException("You have not enough money!"); 
    } 

    shoppingBag.add(groceries); 
    money -= groceries.getPrice(); 
} 

3)也许你需要有可能添加一些钱:

private void addMoney(int amout) { 
    money += amout; 
} 

请参阅完全演示的例子:

class Demo { 

    public static void main(String[] args) throws IOException { 

     Person person = new Person(42); 
     try { 
      System.out.println(person.getMoney()); 
      person.addToShoppingBag(new Groceries(12)); 
      person.addToShoppingBag(new Groceries(20)); 
      person.addToShoppingBag(new Groceries(5)); 
      System.out.println(person.getMoney()); 
      System.out.println(person.getShoppingBag()); 
      person.getShoppingBag().add(new Groceries(1)); 
     } catch (UnsupportedOperationException e) { 
      e.printStackTrace(); 
     } 

     try { 
      person.addToShoppingBag(new Groceries(66)); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } 
    } 
} 


class Person { 

    private List<Groceries> shoppingBag = new ArrayList<>(); 
    private int money; 

    public Person(int money) { 
     this.money = money; 
    } 

    public List<Groceries> getShoppingBag() { 
     List<Groceries> bag = ImmutableList.copyOf(shoppingBag); 
     return bag; 
    } 

    public void addToShoppingBag(Groceries groceries) { 

     if (0 > money - groceries.getPrice()) { 
      throw new IllegalStateException("You have not enough money!"); 
     } 

     shoppingBag.add(groceries); 
     money -= groceries.getPrice(); 
    } 

    private void addMoney(int amout) { 
     money += amout; 
    } 

    public int getMoney() { 
     return money; 
    } 
} 

class Groceries { 
    private int price; 

    public Groceries(int price) { 
     this.price = price; 
    } 

    public int getPrice() { 
     return price; 
    } 

    @Override 
    public String toString() { 
     return "Groceries{" + 
       "price=" + price + 
       '}'; 
    } 
} 

PS:请下次描述的代码和演示一些例子来得到答案:)

相关问题