Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/main/java/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Calculate {

private final Scanner scanner = new Scanner(System.in);

String check = "";
Double countProducts = 0.0;

public void calculate(String name) {
ArrayList<Product> products = new ArrayList<>();

System.out.println(name + " Напишите название блюда");
while (true) {
String productName = scanner.next();

if (productName.equalsIgnoreCase("Завершить")) {
if (products.size() == 0) {
System.out.println("У " + name + " не добавлено ни одного блюда");
} else {
System.out.println("Добавденные товары: ");
for (int j = 0; j < products.size(); j++) {
double temp2 = products.get(j).count;
int convertRub2 = (int) temp2;
System.out.println(products.get(j).name + " - " + products.get(j).count + " " + convertRubDeclination(convertRub2));
}
}
break;
}

if (checkString(productName)) {
System.out.println("Вы ввели некорректное название блюда");
continue;
}

System.out.println(name + " Напишите цену блюда");
double productPrice;
while (true) {
try {
productPrice = Double.parseDouble(scanner.next());
if (productPrice <= 0) {
System.out.println("Вы ввели минусовую цену. Попробуйте ще раз.");
continue;
}
break;
} catch (Exception e) {
System.out.println("Вы ввели некорректное значение цены. Попробуйте еще раз.");
}

}

products.add(new Product(productName, productPrice));
double temp1 = productPrice;
int convertRub = (int) temp1;
System.out.println("Вы успешно добавили блюдо " + "\"" + productName + "\"" + " стоимостью " + productPrice + " " + convertRubDeclination(convertRub) + " в счет");
countProducts += productPrice;
check += productName + " - " + productPrice + " " + convertRubDeclination(convertRub) + "\n";

System.out.println("Хотите ли вы добавить ещё один товар?\nЕсли нет, напишите - Завершить, если хотите продолжить, напишите название следующего блюда.");


}


}

public String convertRubDeclination(int number) {
int lastDigit = number % 100 / 10;
if (lastDigit == 1) {
return "рублей";
}

switch (number % 10) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}

private boolean checkString(String s) throws NumberFormatException {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}

}
5 changes: 1 addition & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Person.start();
}
}
46 changes: 46 additions & 0 deletions src/main/java/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Person {

private static final Scanner scanner = new Scanner(System.in);
private static final ArrayList<String> persons = new ArrayList<>();
private static final Calculate calculate = new Calculate();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Некритично, но можно поля и методы Calculate тоже сделать static, чтобы не пришлось создавать единственный экземпляр


public static void start() {
System.out.println("На скольких человек необходимо разделить счёт? Введите число.");

while (true) {
int countPeople;

try {
countPeople = Integer.parseInt(scanner.next());
} catch (Exception e) {
System.out.println("Вы ввели некорректное значение. Введите целое число.");
continue;
}

if (countPeople == 1) {
System.out.println("Количество гостей должно быть больше одного");
} else if (countPeople < 1) {
System.out.println("Количество гостей должно быть больше одного");
} else {
for (int i = 0; i < countPeople; i++) {
persons.add("Гость_" + (i + 1));
}

System.out.println("Счет будет разделён на " + countPeople + " человек");
for (int i = 0; i < persons.size(); i++) {
calculate.calculate(persons.get(i));
}
double result = calculate.countProducts / persons.size();
int rubText = (int) result;
System.out.printf("\nДобавленные блюда всех гостей:\n%s \nСумма к оплате с каждого гостя: %.2f %s", calculate.check, result, calculate.convertRubDeclination(rubText));
break;
}

}
}


}
12 changes: 12 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Product {

String name;
Double count;

public Product(String name, Double count) {
this.name = name;
this.count = count;
}


}