From 8ce520751438682fbb96c552177d407c768a4f86 Mon Sep 17 00:00:00 2001 From: NieCloud Date: 2023年4月25日 14:57:50 +0300 Subject: [PATCH 1/8] Added Guests class, allowing to calculate number of guests based on user console input --- src/main/java/Guests.java | 45 +++++++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Guests.java diff --git a/src/main/java/Guests.java b/src/main/java/Guests.java new file mode 100644 index 000000000..d249250c3 --- /dev/null +++ b/src/main/java/Guests.java @@ -0,0 +1,45 @@ +import java.util.Scanner; + +public class Guests { + private int numberOfGuests = -1; + Scanner scanner = new Scanner(System.in); + + /** + Метод устанавливает количество гостей, на которых необходимо разделить счет, на основе + ввода пользователя через консоль + */ + public void setNumberOfGuests() { + System.out.println("На скольких человек необходимо разделить счет?"); + while (true) { + numberOfGuests = returnCorrectUserInputAsInt(); + if (numberOfGuests> 1) { + break; + } if (numberOfGuests == 1) { + System.out.println("На одного счет делить смысла нет"); + } if (numberOfGuests < 1) { + System.out.println("Число должно быть больше 1"); + } + + } + } + + /** + Метод проверяет, является ли ввод пользователя int + */ + private int returnCorrectUserInputAsInt() { + int correctIntInput = 0; + while (true) { + try { + correctIntInput = Integer.parseInt(scanner.next()); + break; + } catch (NumberFormatException e) { + System.out.println("Введите корректное целое число гостей"); + } + } + return correctIntInput; + } + + public int getNumberOfGuests() { + return numberOfGuests; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..c1e7223fd 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,6 @@ public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + Guests newNumberOfGuest = new Guests(); } } \ No newline at end of file From 1ffd4d9d659fa24120524979d8aaa368d7a236e9 Mon Sep 17 00:00:00 2001 From: NieCloud Date: 2023年4月25日 16:50:35 +0300 Subject: [PATCH 2/8] Created calculator to analyze user input and give an answer on how the bill should be split --- src/main/java/Calculator.java | 79 ++++++++++++++++++++++++++++++ src/main/java/Guests.java | 6 +-- src/main/java/Main.java | 9 +++- src/main/java/RoubleFormatter.java | 15 ++++++ 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/RoubleFormatter.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..4f7971d21 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,79 @@ +import java.util.Scanner; + +public class Calculator { + private double billPerPerson = 0; + private double totalBill = 0; + private String goods = ""; + + /** + Метод создает String со списком товаров и их ценой, а также суммирует все товары + в отдельную переменную totalBill + */ + public void createListOfGoods() { + String tempGoods = ""; + String userInputGoodName; + double userInputGoodValue; + Scanner scanner = new Scanner(System.in); + + while (true) { + if (tempGoods.isEmpty()) { + System.out.println("Введите название товара (Или \"Завершить\" для окончания формирования списка):"); + } else { + System.out.println("Введите название следующего товара (Или \"Завершить\" для окончания формирования списка):"); + } + + userInputGoodName = scanner.nextLine(); + + if (userInputGoodName.equalsIgnoreCase("Завершить")) { + break; + } + + if (tempGoods.isEmpty()) { + tempGoods = tempGoods.concat(userInputGoodName); + } else { + tempGoods = tempGoods.concat("\n" + userInputGoodName); + } + + System.out.println("Введите стоимость товара в формате рубли.копейки (например 10.22):"); + + userInputGoodValue = checkUserInputAsDouble(); + totalBill += userInputGoodValue; + tempGoods = tempGoods.concat(String.format(" - Цена: %.2f", userInputGoodValue)); + } + goods = tempGoods; + } + + /** + Метод проверяет, является ли ввод пользователя double + */ + private double checkUserInputAsDouble() { + Scanner scanner = new Scanner(System.in); + double correctDoubleInput; + + while (true) { + try { + correctDoubleInput = Double.parseDouble(scanner.next()); + break; + } catch (NumberFormatException e) { + System.out.println("Введите корректную стоимость в формате рубли.копейки (например 10.22):"); + } + } + return correctDoubleInput; + } + + /** + Метод выводит, сколько каждый человек должен заплатить в зависимости от + введенного количества гостей + */ + public void calculateAndShowResultsPerGuest(int numberOfGuests) { + if (goods.isEmpty()) { + System.out.println("Никаких товаров не было добавлено"); + return; + } + System.out.println("Добавленные товары: \n" + goods); + billPerPerson = totalBill / numberOfGuests; + String roubles = RoubleFormatter.returnRoubleString(billPerPerson); + System.out.println(String.format("Каждый человек должен заплатить по %.2f %s", billPerPerson, roubles)); + } + +} diff --git a/src/main/java/Guests.java b/src/main/java/Guests.java index d249250c3..4ac1e9790 100644 --- a/src/main/java/Guests.java +++ b/src/main/java/Guests.java @@ -1,8 +1,7 @@ import java.util.Scanner; public class Guests { - private int numberOfGuests = -1; - Scanner scanner = new Scanner(System.in); + private int numberOfGuests = 0; /** Метод устанавливает количество гостей, на которых необходимо разделить счет, на основе @@ -27,7 +26,8 @@ public void setNumberOfGuests() { Метод проверяет, является ли ввод пользователя int */ private int returnCorrectUserInputAsInt() { - int correctIntInput = 0; + Scanner scanner = new Scanner(System.in); + int correctIntInput; while (true) { try { correctIntInput = Integer.parseInt(scanner.next()); diff --git a/src/main/java/Main.java b/src/main/java/Main.java index c1e7223fd..9b236fbd3 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,13 @@ public class Main { public static void main(String[] args) { - Guests newNumberOfGuest = new Guests(); + Guests firstGuestList = new Guests(); + firstGuestList.setNumberOfGuests(); + + + Calculator testCalculator = new Calculator(); + testCalculator.createListOfGoods(); + testCalculator.calculateAndShowResultsPerGuest(firstGuestList.getNumberOfGuests()); + } } \ No newline at end of file diff --git a/src/main/java/RoubleFormatter.java b/src/main/java/RoubleFormatter.java new file mode 100644 index 000000000..88bf7a627 --- /dev/null +++ b/src/main/java/RoubleFormatter.java @@ -0,0 +1,15 @@ +public class RoubleFormatter { + public static String returnRoubleString(double sum) { + int integerValueOfSum = (int) sum; + if (integerValueOfSum>=10 && integerValueOfSum<= 20) { + return "рублей"; + } + if (integerValueOfSum %10 == 1) { + return "рубль"; + } else if (integerValueOfSum %10>= 2 && integerValueOfSum %10 <= 4) { + return "рубля"; + } else { + return "рублей"; + } + } +} From be134c72e52b7d02ad5597daffb242289f9d3f7d Mon Sep 17 00:00:00 2001 From: NieCloud Date: 2023年4月25日 16:55:03 +0300 Subject: [PATCH 3/8] For better understanding of the class purpose, changed it from Calculator to Guests --- src/main/java/{Calculator.java => Goods.java} | 2 +- src/main/java/Main.java | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) rename src/main/java/{Calculator.java => Goods.java} (99%) diff --git a/src/main/java/Calculator.java b/src/main/java/Goods.java similarity index 99% rename from src/main/java/Calculator.java rename to src/main/java/Goods.java index 4f7971d21..38aac92cd 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Goods.java @@ -1,6 +1,6 @@ import java.util.Scanner; -public class Calculator { +public class Goods { private double billPerPerson = 0; private double totalBill = 0; private String goods = ""; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9b236fbd3..d49a0b9d4 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,10 +4,11 @@ public static void main(String[] args) { Guests firstGuestList = new Guests(); firstGuestList.setNumberOfGuests(); + Goods testGoods = new Goods(); - Calculator testCalculator = new Calculator(); - testCalculator.createListOfGoods(); - testCalculator.calculateAndShowResultsPerGuest(firstGuestList.getNumberOfGuests()); + + testGoods.createListOfGoods(); + testGoods.calculateAndShowResultsPerGuest(firstGuestList.getNumberOfGuests()); } } \ No newline at end of file From 5b09eb121ab68a7525e7210f71f02f411bb609d2 Mon Sep 17 00:00:00 2001 From: NieCloud Date: 2023年4月25日 16:55:03 +0300 Subject: [PATCH 4/8] For better understanding of the class purpose, changed it from Calculator to Goods --- src/main/java/{Calculator.java => Goods.java} | 2 +- src/main/java/Main.java | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) rename src/main/java/{Calculator.java => Goods.java} (99%) diff --git a/src/main/java/Calculator.java b/src/main/java/Goods.java similarity index 99% rename from src/main/java/Calculator.java rename to src/main/java/Goods.java index 4f7971d21..38aac92cd 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Goods.java @@ -1,6 +1,6 @@ import java.util.Scanner; -public class Calculator { +public class Goods { private double billPerPerson = 0; private double totalBill = 0; private String goods = ""; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9b236fbd3..d49a0b9d4 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,10 +4,11 @@ public static void main(String[] args) { Guests firstGuestList = new Guests(); firstGuestList.setNumberOfGuests(); + Goods testGoods = new Goods(); - Calculator testCalculator = new Calculator(); - testCalculator.createListOfGoods(); - testCalculator.calculateAndShowResultsPerGuest(firstGuestList.getNumberOfGuests()); + + testGoods.createListOfGoods(); + testGoods.calculateAndShowResultsPerGuest(firstGuestList.getNumberOfGuests()); } } \ No newline at end of file From a8fd9ffc3618f13f89294e521d5b01e6cbbd5778 Mon Sep 17 00:00:00 2001 From: NieCloud Date: 2023年4月25日 17:10:46 +0300 Subject: [PATCH 5/8] Added check for non-negative cost of goods --- src/main/java/Goods.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/Goods.java b/src/main/java/Goods.java index 38aac92cd..749951aaa 100644 --- a/src/main/java/Goods.java +++ b/src/main/java/Goods.java @@ -36,7 +36,15 @@ public void createListOfGoods() { System.out.println("Введите стоимость товара в формате рубли.копейки (например 10.22):"); - userInputGoodValue = checkUserInputAsDouble(); + while (true) { + userInputGoodValue = checkUserInputAsDouble(); + if (userInputGoodValue < 0) { + System.out.println("Стоимость товара должна быть неотрицательной"); + } else { + break; + } + } + totalBill += userInputGoodValue; tempGoods = tempGoods.concat(String.format(" - Цена: %.2f", userInputGoodValue)); } @@ -58,6 +66,8 @@ private double checkUserInputAsDouble() { System.out.println("Введите корректную стоимость в формате рубли.копейки (например 10.22):"); } } + + return correctDoubleInput; } From c0fceaa89c04636b92766014b3652044123e3fbb Mon Sep 17 00:00:00 2001 From: NieCloud Date: 2023年4月25日 17:18:47 +0300 Subject: [PATCH 6/8] Now goods private value doesnt update until it is in line with restrictions --- src/main/java/Goods.java | 2 +- src/main/java/Guests.java | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/Goods.java b/src/main/java/Goods.java index 749951aaa..90dbd35eb 100644 --- a/src/main/java/Goods.java +++ b/src/main/java/Goods.java @@ -83,7 +83,7 @@ public void calculateAndShowResultsPerGuest(int numberOfGuests) { System.out.println("Добавленные товары: \n" + goods); billPerPerson = totalBill / numberOfGuests; String roubles = RoubleFormatter.returnRoubleString(billPerPerson); - System.out.println(String.format("Каждый человек должен заплатить по %.2f %s", billPerPerson, roubles)); + System.out.println(String.format("Каждый человек должен заплатить %.2f %s", billPerPerson, roubles)); } } diff --git a/src/main/java/Guests.java b/src/main/java/Guests.java index 4ac1e9790..18bb1250b 100644 --- a/src/main/java/Guests.java +++ b/src/main/java/Guests.java @@ -10,12 +10,13 @@ public class Guests { public void setNumberOfGuests() { System.out.println("На скольких человек необходимо разделить счет?"); while (true) { - numberOfGuests = returnCorrectUserInputAsInt(); - if (numberOfGuests> 1) { + int tempNumberOfGuests = returnCorrectUserInputAsInt(); + if (tempNumberOfGuests> 1) { + numberOfGuests = tempNumberOfGuests; break; - } if (numberOfGuests == 1) { + } if (tempNumberOfGuests == 1) { System.out.println("На одного счет делить смысла нет"); - } if (numberOfGuests < 1) { + } if (tempNumberOfGuests < 1) { System.out.println("Число должно быть больше 1"); } From 03cdc44ca54b90b879754f7a8ec1facaad2ec59e Mon Sep 17 00:00:00 2001 From: NieCloud Date: 2023年4月26日 17:19:06 +0300 Subject: [PATCH 7/8] Now RoubleFormatter works correctly with values above 100. --- src/main/java/Goods.java | 3 +-- src/main/java/RoubleFormatter.java | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/Goods.java b/src/main/java/Goods.java index 90dbd35eb..30d232407 100644 --- a/src/main/java/Goods.java +++ b/src/main/java/Goods.java @@ -1,7 +1,6 @@ import java.util.Scanner; public class Goods { - private double billPerPerson = 0; private double totalBill = 0; private String goods = ""; @@ -81,7 +80,7 @@ public void calculateAndShowResultsPerGuest(int numberOfGuests) { return; } System.out.println("Добавленные товары: \n" + goods); - billPerPerson = totalBill / numberOfGuests; + double billPerPerson = totalBill / numberOfGuests; String roubles = RoubleFormatter.returnRoubleString(billPerPerson); System.out.println(String.format("Каждый человек должен заплатить %.2f %s", billPerPerson, roubles)); } diff --git a/src/main/java/RoubleFormatter.java b/src/main/java/RoubleFormatter.java index 88bf7a627..91063e049 100644 --- a/src/main/java/RoubleFormatter.java +++ b/src/main/java/RoubleFormatter.java @@ -1,12 +1,12 @@ public class RoubleFormatter { public static String returnRoubleString(double sum) { int integerValueOfSum = (int) sum; - if (integerValueOfSum>=10 && integerValueOfSum<= 20) { + if (integerValueOfSum % 100>= 10 && integerValueOfSum % 100 <= 20) { return "рублей"; } - if (integerValueOfSum %10 == 1) { + if (integerValueOfSum % 10 == 1) { return "рубль"; - } else if (integerValueOfSum %10>= 2 && integerValueOfSum %10 <= 4) { + } else if (integerValueOfSum % 10>= 2 && integerValueOfSum % 10 <= 4) { return "рубля"; } else { return "рублей"; From 3a39bcfb9ba0aed7069ae0f5cf3e88a81293198b Mon Sep 17 00:00:00 2001 From: NieCloud Date: 2023年4月26日 17:21:36 +0300 Subject: [PATCH 8/8] Now RoubleFormatter works correctly with values above 100. --- src/main/java/RoubleFormatter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/RoubleFormatter.java b/src/main/java/RoubleFormatter.java index 91063e049..ee230003f 100644 --- a/src/main/java/RoubleFormatter.java +++ b/src/main/java/RoubleFormatter.java @@ -1,7 +1,7 @@ public class RoubleFormatter { public static String returnRoubleString(double sum) { int integerValueOfSum = (int) sum; - if (integerValueOfSum % 100>= 10 && integerValueOfSum % 100 <= 20) { + if (integerValueOfSum % 100> 10 && integerValueOfSum % 100 < 20) { return "рублей"; } if (integerValueOfSum % 10 == 1) {

AltStyle によって変換されたページ (->オリジナル) /