diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
new file mode 100644
index 0000000..1bec35e
--- /dev/null
+++ b/.idea/codeStyles/Project.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml
new file mode 100644
index 0000000..79ee123
--- /dev/null
+++ b/.idea/codeStyles/codeStyleConfig.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..61a9130
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
new file mode 100644
index 0000000..40bd79d
--- /dev/null
+++ b/.idea/gradle.xml
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..bb614ff
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/settings.gradle b/settings.gradle
index 716abf2..be110bf 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -12,4 +12,4 @@ dependencyResolutionManagement {
mavenCentral()
}
}
-rootProject.name = "BillCalculator"
+rootProject.name = "Java-Module-Project"
diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java
index 2fbc00d..57a9cc1 100644
--- a/src/main/java/Calculator.java
+++ b/src/main/java/Calculator.java
@@ -1,22 +1,67 @@
-class Calculator {
+import java.util.Scanner;
- int friendsCount;
+public class Calculator {
+ //класс и метод подсчета общей стоимости товара
+ public void calculate(int guests) {
+ String food = "Dobavlennie tovari: \n";
+ Double price = 0.0;
+ while (true) {
+ System.out.println("Vvedite nazvanie produkta ili zavershit: ");
+ Scanner scanner = new Scanner(System.in);
+ String inputFood = scanner.next();
+//если ничего не добавлено и введено "завершить"
+ if (inputFood.equalsIgnoreCase("Zavershit") && food.equals("Dobavlennie tovari: \n")) {
+ break;
+ } else if (inputFood.equalsIgnoreCase("Zavershit")) {
+ System.out.println(food);
+ System.out.println("Kagdiy chelovek zaplatit: " + String.format("%.2f", price / guests) + " " + getRubleAddition(price / guests));
+ break;
- String cart = "Добавленные товары:";
- double totalPrice = 0;
+ } else {
- Calculator(int friendsCount) {
- this.friendsCount = friendsCount;
+ System.out.println("Vvedite cenu tovara [rub.cop]");
+ Double inputPrice = 0.0;
+ String inputString;
+ // программа принимала значение стоимости через точку,но при вводе запятой крашилась, теперь программа принимает как точку, так и запятую, при вводе
+ // некорректного значения цены выводится сообщение о том, что значение введено некорректно
+ try {
+ inputString = scanner.next();
+ inputString = inputString.replace(",", ".");
+ inputPrice = Double.valueOf(inputString);
+ } catch (Exception e) {
+ System.out.println("Vvedite znachenie stoimosti zanovo i korrektno v ukazanom formate!");
+ continue;
+ }
+ if (inputPrice> 0) {
+
+ food = food + inputFood + "\n";
+ price = price + inputPrice;
+ System.out.println("Tovar " + inputFood + " Dobavlen uspeshno, ego cena sostavlaet: " + inputPrice + " " + getRubleAddition(inputPrice));
+ } else {
+//случай когда цена товара меньше 0
+ System.out.println("Cena tovara ne mozhet bit menshe 0!");
+ }
+ }
+ }
}
- void addItem(Item item) {
- totalPrice += item.price;
- cart = cart + "\n" + item.name;
+ //метод для написания рублей в правильном падеже
+ public String getRubleAddition(Double num) {
+ int preLastDigit = (int) (num % 100 / 10);
+ if (preLastDigit == 1) {
+ return "rubley";
+ }
- System.out.println(item.name + " в корзине");
+ switch ((int) (num % 10)) {
+ case 1:
+ return "rubl";
+ case 2:
+ case 3:
+ case 4:
+ return "rublya";
+ default:
+ return "rubley";
+ }
}
- double divideSum() {
- return totalPrice / friendsCount;
- }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java
deleted file mode 100644
index 3f915b7..0000000
--- a/src/main/java/Formatter.java
+++ /dev/null
@@ -1,17 +0,0 @@
-public class Formatter {
-
- String formatValue(double price) {
- double roundedValue = Math.floor(price);
- if (roundedValue == 1) {
- return "рубль";
- } else if (roundedValue>= 2 && roundedValue <= 4) { - return "рубля"; - } else { - return "рублей"; - } - } - - String roundResult(final double result) { - return String.format("%.2f", result); - } -} diff --git a/src/main/java/Item.java b/src/main/java/Item.java deleted file mode 100644 index fad8a4e..0000000 --- a/src/main/java/Item.java +++ /dev/null @@ -1,10 +0,0 @@ -class Item { - - String name; - double price; - - Item(String name, double price) { - this.name = name; - this.price = price; - } -} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 11ba5d3..b84f176 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,49 +1,32 @@ import java.util.Scanner; public class Main { - + //обработка количества гостей public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - int friendCount; + System.out.println("Vvedite kolichestvo gostey: "); while (true) { - System.out.println("На сколько человек необходимо разделить счет?"); - friendCount = scanner.nextInt(); - - if (friendCount> 1) {
- break;
- } else if (friendCount == 1) {
- System.out.println(
- "Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы.");
- } else {
- System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз.");
+ Scanner scanner = new Scanner(System.in);
+ int guests = 0;
+ // условие проверки ввода текста при запросе количества гостей
+ try {
+ guests = scanner.nextInt();
+ } catch (Exception ex) {
+ System.out.println("Vvedeno nekorektnoe znachenie gostey, vvedite cifru");
+ continue;
}
- }
-
- Calculator calculator = new Calculator(friendCount);
-
- while (true) {
- System.out.println("Введите название товара");
- String name = scanner.next();
-
- System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]");
- double price = scanner.nextDouble();
-
- calculator.addItem(new Item(name, price));
-
- System.out.println(
- "Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления");
- String answer = scanner.next();
+ if (guests <= 0) {
+ System.out.println("Vvedite nekorrektnoe znachenie,vvedite znachenie bolshe 1: ");
+ } else if (guests == 1) {
+ System.out.println("A zachem togta schitat? vvedite znachenie bolshe 1: ");
+ } else {
+ System.out.println("Seyachas pschitaem!");
+ Calculator calculator = new Calculator();
+ calculator.calculate(guests);
- if (answer.equalsIgnoreCase("Завершить")) {
break;
}
}
- double result = calculator.divideSum();
- Formatter formatter = new Formatter();
-
- System.out.println(calculator.cart);
- System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result));
}
}
+