From 74db16042d2d846517ab0e866005907e2b8e33b4 Mon Sep 17 00:00:00 2001 From: RMM2023 Date: 2023年10月24日 10:26:24 +0300 Subject: [PATCH 1/3] Task done --- Task.java | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Task.java diff --git a/Task.java b/Task.java new file mode 100644 index 000000000..4eedadeb7 --- /dev/null +++ b/Task.java @@ -0,0 +1,93 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +public class Main{ + public static void main(String[] args){ + Scanner scanner = new Scanner(System.in); + int people; + while (true){ + System.out.println("Введите количество гостей "); + people = scanner.nextInt(); + if (people<=1){ + System.out.println("Не корректное значение"); + }else{ + break; + } + } + Calculate calculate = new Calculate(people); + while (true){ + System.out.println("Введите название товара:"); + String name = scanner.next(); + System.out.println("Введите цену товара:"); + double price = scanner.nextDouble(); + calculate.addProduct(name, price); + System.out.println("Добавить еще товара? \nВведите Завершить для выхода"); + String input = scanner.next(); + if (input.equalsIgnoreCase("Завершить")) + break; + } + calculate.printCheck(); + } +} + +class Calculate{ + private List list = new ArrayList(); + private int people; + public Calculate(int people){ + this.people=people; + } + public void addProduct(String name, double price){ + Product newProduct = new Product(name, price); + list.add(newProduct); + } + public double getTotal() { + double total = 0; + for(Product i: list){ + total = total + i.getPrice(); + } + return total; + } + public void printCheck(){ + System.out.println("Чек:"); + for (Product i:list){ + System.out.println(i.getName()); + } + double personPrice = getTotal()/people; + String ruble = Formatter.formatRuble(personPrice); + System.out.println("С каждого: " + personPrice + ruble); + } +} +class Product{ + private String name; + private double price; + public Product(String name, double price){ + this.name=name; + this.price=price; + } + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } +} +class Formatter{ + public static String formatRuble(double value) { + String formatted = String.format("%.2f", value); + int rubles = (int)Math.floor(value); + String ruble = getRuble(rubles); + return formatted + " " + ruble; + } + public static String getRuble(int rubles){ + if (rubles % 10 == 1 && rubles % 100 != 11){ + return " рубль"; + } + else if (rubles % 10>= 2 && rubles % 10 <= 4 && (rubles % 100 < 10 || rubles % 100>= 20)){ + return " рубля"; + }else { + return " рублей"; + } + } +} \ No newline at end of file From bacf1fe0cfd77815d37d8b53005f6804b047eb76 Mon Sep 17 00:00:00 2001 From: RMM2023 Date: 2023年10月26日 10:30:54 +0300 Subject: [PATCH 2/3] Project Fix --- Task.java | 93 ------------------------------------ src/main/java/Calculate.java | 32 +++++++++++++ src/main/java/Formatter.java | 20 ++++++++ src/main/java/Main.java | 32 +++++++++++-- src/main/java/Product.java | 18 +++++++ 5 files changed, 99 insertions(+), 96 deletions(-) delete mode 100644 Task.java create mode 100644 src/main/java/Calculate.java create mode 100644 src/main/java/Formatter.java create mode 100644 src/main/java/Product.java diff --git a/Task.java b/Task.java deleted file mode 100644 index 4eedadeb7..000000000 --- a/Task.java +++ /dev/null @@ -1,93 +0,0 @@ -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; -public class Main{ - public static void main(String[] args){ - Scanner scanner = new Scanner(System.in); - int people; - while (true){ - System.out.println("Введите количество гостей "); - people = scanner.nextInt(); - if (people<=1){ - System.out.println("Не корректное значение"); - }else{ - break; - } - } - Calculate calculate = new Calculate(people); - while (true){ - System.out.println("Введите название товара:"); - String name = scanner.next(); - System.out.println("Введите цену товара:"); - double price = scanner.nextDouble(); - calculate.addProduct(name, price); - System.out.println("Добавить еще товара? \nВведите Завершить для выхода"); - String input = scanner.next(); - if (input.equalsIgnoreCase("Завершить")) - break; - } - calculate.printCheck(); - } -} - -class Calculate{ - private List list = new ArrayList(); - private int people; - public Calculate(int people){ - this.people=people; - } - public void addProduct(String name, double price){ - Product newProduct = new Product(name, price); - list.add(newProduct); - } - public double getTotal() { - double total = 0; - for(Product i: list){ - total = total + i.getPrice(); - } - return total; - } - public void printCheck(){ - System.out.println("Чек:"); - for (Product i:list){ - System.out.println(i.getName()); - } - double personPrice = getTotal()/people; - String ruble = Formatter.formatRuble(personPrice); - System.out.println("С каждого: " + personPrice + ruble); - } -} -class Product{ - private String name; - private double price; - public Product(String name, double price){ - this.name=name; - this.price=price; - } - - public String getName() { - return name; - } - - public double getPrice() { - return price; - } -} -class Formatter{ - public static String formatRuble(double value) { - String formatted = String.format("%.2f", value); - int rubles = (int)Math.floor(value); - String ruble = getRuble(rubles); - return formatted + " " + ruble; - } - public static String getRuble(int rubles){ - if (rubles % 10 == 1 && rubles % 100 != 11){ - return " рубль"; - } - else if (rubles % 10>= 2 && rubles % 10 <= 4 && (rubles % 100 < 10 || rubles % 100>= 20)){ - return " рубля"; - }else { - return " рублей"; - } - } -} \ No newline at end of file diff --git a/src/main/java/Calculate.java b/src/main/java/Calculate.java new file mode 100644 index 000000000..c2a570c75 --- /dev/null +++ b/src/main/java/Calculate.java @@ -0,0 +1,32 @@ +package src.main.java; + +import java.util.ArrayList; +import java.util.List; + +class Calculate{ + private List list = new ArrayList(); + private int people; + public Calculate(int people){ + this.people=people; + } + public void addProduct(String name, double price){ + Product newProduct = new Product(name, price); + list.add(newProduct); + } + public double getTotal() { + double total = 0; + for(Product i: list){ + total = total + i.getPrice(); + } + return total; + } + public void printCheck(){ + System.out.println("Чек:"); + for (Product i:list){ + System.out.println(i.getName()); + } + double personPrice = getTotal()/people; + String ruble = Formatter.formatRuble(personPrice); + System.out.println("С каждого: " + personPrice + ruble); + } +} \ No newline at end of file diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java new file mode 100644 index 000000000..a2e2e8594 --- /dev/null +++ b/src/main/java/Formatter.java @@ -0,0 +1,20 @@ +package src.main.java; + +class Formatter{ + public static String formatRuble(double value) { + String formatted = String.format("%.2f", value); + int rubles = (int)Math.floor(value); + String ruble = getRuble(rubles); + return formatted + " " + ruble; + } + public static String getRuble(int rubles){ + if (rubles % 10 == 1 && rubles % 100 != 11){ + return " рубль"; + } + else if (rubles % 10>= 2 && rubles % 10 <= 4 && (rubles % 100 < 10 || rubles % 100>= 20)){ + return " рубля"; + }else { + return " рублей"; + } + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..16d110638 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,32 @@ +package src.main.java; -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); +import java.util.Scanner; + +public class Main{ + public static void main(String[] args){ + Scanner scanner = new Scanner(System.in); + int people; + while (true){ + System.out.println("Введите количество гостей "); + people = scanner.nextInt(); + if (people<=1){ + System.out.println("Не корректное значение"); + }else{ + break; + } + } + Calculate calculate = new Calculate(people); + while (true){ + System.out.println("Введите название товара:"); + String name = scanner.next(); + System.out.println("Введите цену товара:"); + double price = scanner.nextDouble(); + calculate.addProduct(name, price); + System.out.println("Добавить еще товара? \nВведите Завершить для выхода"); + String input = scanner.next(); + if (input.equalsIgnoreCase("Завершить")) + break; + } + calculate.printCheck(); } } \ No newline at end of file diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 000000000..5ac87988a --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,18 @@ +package src.main.java; + +class Product{ + private String name; + private double price; + public Product(String name, double price){ + this.name=name; + this.price=price; + } + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } +} From 5a18d0d7cb8210c708f4daf7f7d3032181eea708 Mon Sep 17 00:00:00 2001 From: RMM2023 Date: 2023年10月29日 10:38:09 +0300 Subject: [PATCH 3/3] Check fix --- src/main/java/Calculate.java | 26 ++++++++++++++---------- src/main/java/Main.java | 38 ++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/main/java/Calculate.java b/src/main/java/Calculate.java index c2a570c75..b566fa17f 100644 --- a/src/main/java/Calculate.java +++ b/src/main/java/Calculate.java @@ -3,30 +3,36 @@ import java.util.ArrayList; import java.util.List; -class Calculate{ +class Calculate { private List list = new ArrayList(); private int people; - public Calculate(int people){ - this.people=people; + + public Calculate(int people) { + this.people = people; } - public void addProduct(String name, double price){ + + public void addProduct(String name, double price) { Product newProduct = new Product(name, price); list.add(newProduct); } + public double getTotal() { double total = 0; - for(Product i: list){ + for (Product i : list) { total = total + i.getPrice(); } return total; } - public void printCheck(){ + + public void printCheck() { System.out.println("Чек:"); - for (Product i:list){ - System.out.println(i.getName()); + for (Product i : list) { + System.out.println(i.getName() + " - " + Formatter.formatRuble(i.getPrice())); } - double personPrice = getTotal()/people; + + double total = getTotal(); + double personPrice = total / people; String ruble = Formatter.formatRuble(personPrice); - System.out.println("С каждого: " + personPrice + ruble); + System.out.println("С каждого: " + ruble); } } \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 16d110638..69fc6108a 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -2,27 +2,41 @@ import java.util.Scanner; -public class Main{ - public static void main(String[] args){ +public class Main { + public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int people; - while (true){ + while (true) { System.out.println("Введите количество гостей "); - people = scanner.nextInt(); - if (people<=1){ - System.out.println("Не корректное значение"); - }else{ - break; + if (scanner.hasNextInt()) { + people = scanner.nextInt(); + if (people <= 1) { + System.out.println("Не корректное значение"); + } else { + break; + } + } else { + System.out.println("Вы ввели не число. Попробуйте еще раз."); + scanner.next(); } } + Calculate calculate = new Calculate(people); - while (true){ + while (true) { System.out.println("Введите название товара:"); String name = scanner.next(); System.out.println("Введите цену товара:"); - double price = scanner.nextDouble(); - calculate.addProduct(name, price); - System.out.println("Добавить еще товара? \nВведите Завершить для выхода"); + if (scanner.hasNextDouble()) { + double price = scanner.nextDouble(); + calculate.addProduct(name, price); + } else { + System.out.println("Вы ввели не число. Попробуйте еще раз."); + scanner.next(); + continue; + } + + System.out.println("Добавить еще товара?"); + System.out.println("Введите Завершить для выхода"); String input = scanner.next(); if (input.equalsIgnoreCase("Завершить")) break;

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