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/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..6cec569
--- /dev/null
+++ b/.idea/gradle.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..a47d29e
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ 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..1b504fe 100644
--- a/src/main/java/Calculator.java
+++ b/src/main/java/Calculator.java
@@ -1,22 +1,105 @@
-class Calculator {
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
- int friendsCount;
+public class Calculator {
- String cart = "Добавленные товары:";
- double totalPrice = 0;
+ private int humanCount;
+ private static final Scanner scanner = new Scanner(System.in);
+ private List productsList;
+ private static double priceAllProducts = 0;
+
+ public Calculator(){
+ humanCount = divideBuild();
+ this.productsList = addProducts(humanCount);
- Calculator(int friendsCount) {
- this.friendsCount = friendsCount;
}
- void addItem(Item item) {
- totalPrice += item.price;
- cart = cart + "\n" + item.name;
+ public static int divideBuild(){
+ while (true){
+ try{
+ System.out.println("На скольких человек необходимо разделить счёт?");
+ int count = Integer.parseInt(scanner.nextLine());
+ if(count < 1){
+ System.out.println("Ошибка. Введено некорректное значение для подсчёта. Попробуйте снова");
+ }
+ else if(count == 1){
+ System.out.println("Нет смысла ничего считать и делить. Попробуйте снова");
+ }
+ else {
+ return count;
+ }}catch (Exception e){
+ System.out.println("Ошибка. Попробуйте снова.");
+ }
+ }
+ }
- System.out.println(item.name + " в корзине");
+ public static List addProducts(int humanCount){
+ List list = new ArrayList<>();
+ while(true){
+ System.out.println("Введите название одного товара или слово \"Завершить\" для подсчета стоимости");
+ String product = scanner.nextLine();
+ if(product.equalsIgnoreCase("Завершить")){
+ if(list.size() == 0){
+ System.out.println("Товар не был введен, программа завершена.");
+ }
+ else {
+ System.out.println("Добавленные товары:");
+ for(Product products : list){
+ System.out.println(products.getName());
+ }
+ double pay = priceAllProducts / humanCount;
+ //double pay2 = Math.floor(pay);
+ String payformat = String.format("%.2f", pay);
+ String rub = floor(pay);
+ System.out.println("Каждый человек из " + humanCount +" должен заплатить " + payformat + " " + rub);
+ }
+ break;}
+ System.out.println("Введите стоимость товара в формате: 'рубли.копейки'");
+ try{
+ String price0 = scanner.nextLine();
+ Double price = Double.parseDouble(price0);
+ priceAllProducts += price;
+ list.add(new Product(product,price));}
+ catch (NumberFormatException e){
+ System.out.println("Задан неверный формат стоимости, начните заново");
+ }
+ }
+ return list;
}
- double divideSum() {
- return totalPrice / friendsCount;
+ private static String floor(double pay){
+ String rub = Integer.toString((int)Math.floor(pay));
+ if(rub.length() == 1){
+ switch (rub) {
+ case "0":
+ case "5":
+ case "6":
+ case "7":
+ case "8":
+ case "9":
+ return "рублей";
+ case "1":
+ return "рубль";
+ case "2":
+ case "3":
+ case "4":
+ return "рубля";
+ }
+ }
+ else if(rub.length() > 1){
+ int lastDigit = Integer.parseInt(Character.toString(rub.charAt(rub.length()-1)));
+ if(rub.length() == 2 && rub.charAt(0) == '1'){return "рублей!!";}
+ else if(lastDigit >= 5 && lastDigit <= 9 )
+ return "рублей";
+ else if(lastDigit >= 2 && lastDigit <= 4 )
+ return "рубля";
+ else if(lastDigit == 1)
+ return "рубль";
+ else
+ return "рублей";
+ }
+ return "Ошибка";
}
}
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..6311bec 100644
--- a/src/main/java/Main.java
+++ b/src/main/java/Main.java
@@ -1,49 +1,9 @@
-import java.util.Scanner;
-
public class Main {
public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
-
- int friendCount;
- while (true) {
- System.out.println("На сколько человек необходимо разделить счет?");
- friendCount = scanner.nextInt();
-
- if (friendCount > 1) {
- break;
- } else if (friendCount == 1) {
- System.out.println(
- "Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы.");
- } else {
- System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз.");
- }
- }
-
- 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 (answer.equalsIgnoreCase("Завершить")) {
- break;
- }
- }
-
- double result = calculator.divideSum();
- Formatter formatter = new Formatter();
+ Calculator calculator = new Calculator();
+ // ваш код начнется здесь
+ // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
- System.out.println(calculator.cart);
- System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result));
}
}
diff --git a/src/main/java/Product.java b/src/main/java/Product.java
new file mode 100644
index 0000000..be26005
--- /dev/null
+++ b/src/main/java/Product.java
@@ -0,0 +1,18 @@
+public class Product{
+ private String name;
+ private double price;
+
+ public String getName() {
+ return name;
+ }
+
+ public double getPrice() {
+ return price;
+ }
+
+ public Product(String name, double price){
+ this.name = name;
+ this.price = price;
+ }
+
+}
\ No newline at end of file