2日目の2段階目完了しました。

This commit is contained in:
s-ohasi
2026-09-18 16:32:33 +09:00
parent 944456f149
commit 5e25bbd817
2 changed files with 51 additions and 21 deletions

View File

@@ -5,28 +5,30 @@ public class BasicCheck3 {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Item[] items = new Item[3]; Item[] items = new Item[3];
//配列3件分用意
items[0] = new Item(); items[0] = 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] = new Item(); items[1] = new Item();
items[1].name = "消しゴム"; items[1].setName("消しゴム");
items[1].price = 80; items[1].setPrice(80);
items[1].stock = 50; items[1].setStock(30);
items[2] = new Item(); items[2] = new Item();
items[2].name = "鉛筆"; items[2].setName("鉛筆");
items[2].price = 60; items[2].setPrice(60);
items[2].stock = 100; items[2].setStock(100);
items[0].sell(5);
items[1].sell(5);
items[2].sell(5);
for(int i= 0;i<3;i++) {
System.out.println(items[i]);
for (int i = 0; i < items.length; i++) {
items[i].sell(5);
}
for (int i = 0; i < items.length; i++) {
System.out.println(items[i].name + items[i].price + "円" + "残り"+ items[i].stock + "個");
}
} }
} }
}

View File

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