2段階目終わりました

This commit is contained in:
2026-09-15 14:50:24 +09:00
parent 2cc3a84a2d
commit 12f84a2a83
4 changed files with 57 additions and 35 deletions

View File

@@ -6,13 +6,9 @@ public class BasicCheck1 {
Book book1 = new Book(); Book book1 = new Book();
Book book2 = new Book("銀河鉄道の夜", "宮沢賢治", 500); Book book2 = new Book("銀河鉄道の夜", "宮沢賢治", 500);
System.out.println(book1.title + "\n" + book1.author + "\n" + book1.price); System.out.println(book1.title + "\n" + book1.author + "\n" + book1.price);
System.out.println(book2.title + "\n" + book2.author + "\n" + book2.price); System.out.println(book2.title + "\n" + book2.author + "\n" + book2.price);
} }
} }

View File

@@ -6,23 +6,23 @@ 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[1].name = "消しゴム"; items[1].setName("消しゴム");
items[2].name = "鉛筆"; items[2].setName("鉛筆");
items[0].price = 120; items[0].setPrice(120);
items[1].price = 80; items[1].setPrice(80);
items[2].price = 60; items[2].setPrice(60);
items[0].stock = 50; items[0].setStock(50);
items[1].stock = 30; items[1].setStock(30);
items[2].stock = 100; items[2].setStock(100);
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; 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].getName() + " " + items[i].getPrice() + "円 (残り" + items[i].getStock() + "個) ");
System.out.println("合計金額 " + items[i].totalPrice() + "円"); // System.out.println("合計金額 " + items[i].totalPrice() + "円");
System.out.println(); // System.out.println();
} }

View File

@@ -1,7 +1,6 @@
public class Book { public class Book {
String title; String title;
String author; String author;
int price; int price;
@@ -20,6 +19,4 @@ public class Book {
} }
} }

View File

@@ -1,10 +1,11 @@
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() {
@@ -12,5 +13,33 @@ public class Item {
} }
@Override
public String toString() {
return (getName() + " " + getPrice() + "円 (残り" + getStock() + "個");
}
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;
}
} }