我有一個具有類Groceries的ArrayList的Person類。 讓我們來命名Arraylist shoppingBag
。每當一個新的對象被添加到數組列表時,Reduse整數
學生和雜貨店各有一個字段,int money
和int price
。 初始化新對象時,具體金額和價格由您決定。
因此,每當某人將一個Groceries對象添加到他的shoppingBag時,他所需的金額就需要隨着添加到包中的雜貨總價格而減少。
你是怎麼做的?
我有一個具有類Groceries的ArrayList的Person類。 讓我們來命名Arraylist shoppingBag
。每當一個新的對象被添加到數組列表時,Reduse整數
學生和雜貨店各有一個字段,int money
和int price
。 初始化新對象時,具體金額和價格由您決定。
因此,每當某人將一個Groceries對象添加到他的shoppingBag時,他所需的金額就需要隨着添加到包中的雜貨總價格而減少。
你是怎麼做的?
所以,讓我試着去了解你想要什麼(因爲我也一樣,我的客戶)
你有一類食品雜貨價格字段:
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:請下次描述的代碼和演示一些例子來得到答案:)
_你怎麼做?_你是怎麼做到的?向我們顯示你的代碼.. –