From 5a76b1761e58a111d338cf95b890b9a3f1037855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E5=8B=87=E8=BC=9D?= Date: Wed, 9 Oct 2024 10:26:38 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E5=95=8F=E9=A1=8CB076?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/src/Bakery.java | 40 ++++++++++++++++++++++++++++++++ yitou/src/Bread.java | 31 +++++++++++++++++++++++++ yitou/src/Main.java | 47 ++++++++++++++++++++++++++++++++++++++ yitou/test/BakeryTest.java | 41 +++++++++++++++++++++++++++++++++ yitou/test/BreadTest.java | 45 ++++++++++++++++++++++++++++++++++++ 5 files changed, 204 insertions(+) create mode 100644 yitou/src/Bakery.java create mode 100644 yitou/src/Bread.java create mode 100644 yitou/src/Main.java create mode 100644 yitou/test/BakeryTest.java create mode 100644 yitou/test/BreadTest.java diff --git a/yitou/src/Bakery.java b/yitou/src/Bakery.java new file mode 100644 index 0000000..de3e2c7 --- /dev/null +++ b/yitou/src/Bakery.java @@ -0,0 +1,40 @@ +package src; + +public class Bakery { + public Bread[] breads; + + public Bakery(final int numberOfBreads) { + breads = new Bread[numberOfBreads]; + } + + public void addBread(final int index, final int price, final int stock) { + breads[index] = new Bread(price, stock); + } + + public void bake(final int index, final int amount) { + if (index >= 0 && index < breads.length) { + breads[index].bake(amount); + } + } + + public int buy(final int[] quantities) { + int totalCost = 0; + + // 在庫確認と合計金額の計算 + for (int i = 0; i < breads.length; i++) { + if (quantities[i] > breads[i].getStock()) { + return -1; // 在庫不足 + } + totalCost += breads[i].getPrice() * quantities[i]; + } // ここでループを閉じる + + // 在庫を減らす処理 + for (int i = 0; i < breads.length; i++) { + if (quantities[i] > 0) { + breads[i].buy(quantities[i]); + } + } + + return totalCost; // 合計金額を返す + } +} \ No newline at end of file diff --git a/yitou/src/Bread.java b/yitou/src/Bread.java new file mode 100644 index 0000000..dd772d6 --- /dev/null +++ b/yitou/src/Bread.java @@ -0,0 +1,31 @@ +package src; + +public class Bread { + private int price; + private int stock; + + public Bread(final int price, final int stock) { + this.price = price; + this.stock = stock; + } + + public int getPrice() { + return price; + } + + public int getStock() { + return stock; + } + + public void bake(final int amount) { + stock += amount; // 焼いた分だけ在庫を増やす + } + + public boolean buy(final int amount) { + if (stock >= amount) { + stock -= amount; // 在庫が足りている場合のみ減らす + return true; + } + return false; // 在庫不足 + } +} diff --git a/yitou/src/Main.java b/yitou/src/Main.java new file mode 100644 index 0000000..f0bb0c0 --- /dev/null +++ b/yitou/src/Main.java @@ -0,0 +1,47 @@ +package src; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Main { + public static void main(final String[] args) { + final Scanner scanner = new Scanner(System.in); + final int breadKind = scanner.nextInt(); + final int queryCount = scanner.nextInt(); + final Bakery bakery = new Bakery(breadKind); + + // パン情報 + for (int i = 0; i < breadKind; i++) { + final int price = scanner.nextInt(); + final int stock = scanner.nextInt(); + bakery.addBread(i, price, stock); + } + + final List results = new ArrayList<>(); // 結果を格納 + + // クエリの処理 + for (int i = 0; i < queryCount; i++) { + final String query = scanner.next(); + if (query.equals("bake")) { + final int index = scanner.nextInt(); + final int amount = scanner.nextInt(); + bakery.bake(index, amount); + } else if (query.equals("buy")) { + final int[] quantities = new int[breadKind]; + for (int j = 0; j < breadKind; j++) { + quantities[j] = scanner.nextInt(); + } + final int result = bakery.buy(quantities); + results.add(result); // 結果をリストに追加 + } + } + + // 結果をまとめて出力 + for (int result : results) { + System.out.println(result); + } + + scanner.close(); + } +} \ No newline at end of file diff --git a/yitou/test/BakeryTest.java b/yitou/test/BakeryTest.java new file mode 100644 index 0000000..e4ec20d --- /dev/null +++ b/yitou/test/BakeryTest.java @@ -0,0 +1,41 @@ +package test; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; +import src.Bakery; + +public class BakeryTest { + private Bakery bakery; + + @BeforeEach + void パンの準備() { + bakery = new Bakery(2); // 2種類のパンを管理 + bakery.addBread(0, 100, 10); // インデックス0、価格100、在庫10のパンを追加 + bakery.addBread(1, 200, 5); // インデックス1、価格200、在庫5のパンを追加 + } + + @Test + void パンを0番目に5個追加() { + bakery.bake(0, 5); // 0番目のパンを5個追加 + Assertions.assertEquals(15, bakery.breads[0].getStock()); // 在庫が15になるはず + } + + @Test + void パンを0番目に1個1番目に2個追加() { + final int[] quantities = {1, 2}; // 0番目を1個、1番目を2個購入 + final int result = bakery.buy(quantities); + Assertions.assertEquals(500, result); // 合計金額は500になるはず + Assertions.assertEquals(9, bakery.breads[0].getStock()); // 在庫は9になるはず + Assertions.assertEquals(3, bakery.breads[1].getStock()); // 在庫は3になるはず + } + + @Test + void パンが在庫になるパターン() { + final int[] quantities = {11, 1}; // 在庫不足のパターン + final int result = bakery.buy(quantities); + Assertions.assertEquals(-1, result); // 在庫不足のため-1が返るはず + Assertions.assertEquals(10, bakery.breads[0].getStock()); // 在庫はそのまま + Assertions.assertEquals(5, bakery.breads[1].getStock()); // 在庫はそのまま + } +} diff --git a/yitou/test/BreadTest.java b/yitou/test/BreadTest.java new file mode 100644 index 0000000..055fd63 --- /dev/null +++ b/yitou/test/BreadTest.java @@ -0,0 +1,45 @@ +package test; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; +import src.Bread; + +public class BreadTest { + private Bread bread; + + @BeforeEach + void パンの準備() { + bread = new Bread(100, 10); // 価格100、在庫10のパンを作成 + } + + @Test + void パンの価格100を取得() { + assertEquals(100, bread.getPrice()); // 価格が100であるはず + } + + @Test + void パンの在庫10を取得() { + assertEquals(10, bread.getStock()); // 在庫が10であるはず + } + + @Test + void パンを5個ついかして15にする() { + bread.bake(5); // 5個追加 + assertEquals(15, bread.getStock()); // 在庫が15になるはず + } + + @Test + void パンを5個購入して在庫が5になる() { + final boolean result = bread.buy(5); // 5個購入 + assertTrue(result); // 購入が成功する + assertEquals(5, bread.getStock()); // 在庫が5になるはず + } + + @Test + void 個数15により在庫不足() { + final boolean result = bread.buy(15); // 15個購入(在庫不足) + assertFalse(result); // 購入が失敗する + assertEquals(10, bread.getStock()); // 在庫はそのまま + } +} -- GitLab From 00a68ccc8fd06489e8cda1062601684d2e750159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E5=8B=87=E8=BC=9D?= Date: Wed, 9 Oct 2024 17:33:30 +0900 Subject: [PATCH 2/3] =?UTF-8?q?B076=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/src/Bakery.java | 57 +++++++++++++++++++++++-------------------- yitou/src/Main.java | 25 +++++++++++-------- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/yitou/src/Bakery.java b/yitou/src/Bakery.java index de3e2c7..9f54c39 100644 --- a/yitou/src/Bakery.java +++ b/yitou/src/Bakery.java @@ -1,40 +1,43 @@ package src; +import java.util.ArrayList; +import java.util.List; + public class Bakery { - public Bread[] breads; + public List breads; - public Bakery(final int numberOfBreads) { - breads = new Bread[numberOfBreads]; - } +public Bakery() { + breads = new ArrayList<>(); +} + +public void addBread(final int price, final int stock) { + breads.add(new Bread(price, stock)); +} - public void addBread(final int index, final int price, final int stock) { - breads[index] = new Bread(price, stock); +public void bake(final int index, final int amount) { + if (index >= 0 && index < breads.size()) { + breads.get(index).bake(amount); // リストから取得 } +} - public void bake(final int index, final int amount) { - if (index >= 0 && index < breads.length) { - breads[index].bake(amount); +public int buy(final int[] quantities) { + int totalCost = 0; + + // 在庫あれば合計金額計算 + for (int i = 0; i < breads.size(); i++) { + if (quantities[i] > breads.get(i).getStock()) { + return -1; // 在庫不足 } + totalCost += breads.get(i).getPrice() * quantities[i]; } - public int buy(final int[] quantities) { - int totalCost = 0; - - // 在庫確認と合計金額の計算 - for (int i = 0; i < breads.length; i++) { - if (quantities[i] > breads[i].getStock()) { - return -1; // 在庫不足 - } - totalCost += breads[i].getPrice() * quantities[i]; - } // ここでループを閉じる - - // 在庫を減らす処理 - for (int i = 0; i < breads.length; i++) { - if (quantities[i] > 0) { - breads[i].buy(quantities[i]); - } + // 在庫を減らす + for (int i = 0; i < breads.size(); i++) { + if (quantities[i] > 0) { + breads.get(i).buy(quantities[i]); } + } - return totalCost; // 合計金額を返す + return totalCost; // 合計金額を返す } -} \ No newline at end of file +} diff --git a/yitou/src/Main.java b/yitou/src/Main.java index f0bb0c0..db6831b 100644 --- a/yitou/src/Main.java +++ b/yitou/src/Main.java @@ -9,30 +9,35 @@ public class Main { final Scanner scanner = new Scanner(System.in); final int breadKind = scanner.nextInt(); final int queryCount = scanner.nextInt(); - final Bakery bakery = new Bakery(breadKind); + final Bakery bakery = new Bakery(); // パン情報 for (int i = 0; i < breadKind; i++) { final int price = scanner.nextInt(); final int stock = scanner.nextInt(); - bakery.addBread(i, price, stock); + bakery.addBread(price, stock); } final List results = new ArrayList<>(); // 結果を格納 // クエリの処理 for (int i = 0; i < queryCount; i++) { - final String query = scanner.next(); + final String query = scanner.next(); + final int[] quantities = new int[breadKind]; // パンの個数を格納する + + // パンの種類分値を取得する + for (int j = 0; j < breadKind; j++) { + quantities[j] = scanner.nextInt(); // 各パンの個数を取得 + } + + // 分岐の中ではscannerを呼ばない if (query.equals("bake")) { - final int index = scanner.nextInt(); - final int amount = scanner.nextInt(); - bakery.bake(index, amount); - } else if (query.equals("buy")) { - final int[] quantities = new int[breadKind]; + // 焼く場合は数量をそのまま使用する for (int j = 0; j < breadKind; j++) { - quantities[j] = scanner.nextInt(); + bakery.bake(j, quantities[j]); // 各パンを追加 } - final int result = bakery.buy(quantities); + } else if (query.equals("buy")) { + final int result = bakery.buy(quantities); // パンを購入 results.add(result); // 結果をリストに追加 } } -- GitLab From 142f878899ec752abdb72d57cb2ab332359e338e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E5=8B=87=E8=BC=9D?= Date: Thu, 10 Oct 2024 11:47:17 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E5=91=BD=E5=90=8D=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/src/Bakery.java | 14 ++++++++------ yitou/src/Bread.java | 4 ++-- yitou/src/Main.java | 10 +++++----- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/yitou/src/Bakery.java b/yitou/src/Bakery.java index 9f54c39..d98c61c 100644 --- a/yitou/src/Bakery.java +++ b/yitou/src/Bakery.java @@ -14,13 +14,15 @@ public void addBread(final int price, final int stock) { breads.add(new Bread(price, stock)); } -public void bake(final int index, final int amount) { - if (index >= 0 && index < breads.size()) { - breads.get(index).bake(amount); // リストから取得 +public void bakeBreads(final Bread bread, final int amount) { + if (breads.contains(bread)) { + bread.addStock(amount); + } else { + // 例外処理またはエラーメッセージを返す } } -public int buy(final int[] quantities) { +public int buyBreads(final int[] quantities) { int totalCost = 0; // 在庫あれば合計金額計算 @@ -34,10 +36,10 @@ public int buy(final int[] quantities) { // 在庫を減らす for (int i = 0; i < breads.size(); i++) { if (quantities[i] > 0) { - breads.get(i).buy(quantities[i]); + breads.get(i).hasStock(quantities[i]); } } return totalCost; // 合計金額を返す } -} +} \ No newline at end of file diff --git a/yitou/src/Bread.java b/yitou/src/Bread.java index dd772d6..2fe458b 100644 --- a/yitou/src/Bread.java +++ b/yitou/src/Bread.java @@ -17,11 +17,11 @@ public class Bread { return stock; } - public void bake(final int amount) { + public void addStock(final int amount) { stock += amount; // 焼いた分だけ在庫を増やす } - public boolean buy(final int amount) { + public boolean hasStock(final int amount) { if (stock >= amount) { stock -= amount; // 在庫が足りている場合のみ減らす return true; diff --git a/yitou/src/Main.java b/yitou/src/Main.java index db6831b..96848c1 100644 --- a/yitou/src/Main.java +++ b/yitou/src/Main.java @@ -18,7 +18,7 @@ public class Main { bakery.addBread(price, stock); } - final List results = new ArrayList<>(); // 結果を格納 + final List totalCostList = new ArrayList<>(); // 結果を格納 // クエリの処理 for (int i = 0; i < queryCount; i++) { @@ -34,16 +34,16 @@ public class Main { if (query.equals("bake")) { // 焼く場合は数量をそのまま使用する for (int j = 0; j < breadKind; j++) { - bakery.bake(j, quantities[j]); // 各パンを追加 + bakery.bakeBreads(bakery.breads.get(j), quantities[j]); // 各パンを追加 } } else if (query.equals("buy")) { - final int result = bakery.buy(quantities); // パンを購入 - results.add(result); // 結果をリストに追加 + final int totalCost = bakery.buyBreads(quantities); // パンを購入 + totalCostList.add(totalCost); // 結果をリストに追加 } } // 結果をまとめて出力 - for (int result : results) { + for (int result : totalCostList) { System.out.println(result); } -- GitLab