2日目 段階2 小澤

This commit is contained in:
2026-09-15 14:02:17 +09:00
parent 992bd30434
commit a3ddbaed43
2 changed files with 43 additions and 15 deletions

View File

@@ -6,24 +6,24 @@ public class BasicCheck3 {
items[1] = new Item(); items[1] = new Item();
items[2] = new Item(); items[2] = new Item();
items[0].name = "ノート"; items[0].setName("ノート") ;
items[0].price = 120; items[0].setPrice(120);
items[0].stock = 50; items[0].setStock(50);
items[1].name = "消しゴム"; items[1].setName("消しゴム");
items[1].price = 80; items[1].setPrice(80);
items[1].stock = 30; items[1].setStock(30);
items[2].name = "鉛筆"; items[2].setName("鉛筆");
items[2].price = 60; items[2].setPrice(60);
items[2].stock = 100; items[2].setStock(100);
for(int i=0;i<3;i++) { for(int i=0;i<3;i++) {
items[i].sell(5); items[i].sell(5);
System.out.println(items[i].name+" "+items[i].price+"円 残り"+items[i].stock+"個"); System.out.println(items[i]);
System.out.println("合計金額:"+items[i].totalPrice()); // System.out.println("合計金額:"+items[i].totalPrice());
} }

View File

@@ -1,15 +1,43 @@
public class Item { public class Item {
String name; private String name;
int price; private int price;
int stock; private int stock;
public void sell(int amount) { public void sell(int amount) {
this.stock = this.stock - amount; this.setStock(this.getStock()-amount) ;
} }
public int totalPrice() { public int totalPrice() {
return this.price*this.stock; return this.price*this.stock;
} }
@Override
public String toString() {
return getName()+" "+getPrice()+"円(残り"+getStock()+"個)";
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name=name;
}
public int getPrice() {
return this.price;
}
public void setPrice(int price) {
this.price=price;
}
public int getStock() {
return this.stock;
}
public void setStock(int stock) {
this.stock=stock;
}
} }