Skip to content
78 changes: 78 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {
public void calculator() {
double total = 0.0;
Scanner scanner = new Scanner(System.in);
String format = "%.2f";

ArrayList<String> products = new ArrayList<>();
ArrayList<Double> prices = new ArrayList<>();

outerLoop:
while (true) {
System.out.println("Введите наименование товара (или 'Завершить' для завершения)");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Общепринято не оставлять так много пустых строк, это ухудшает восприятие. Золотой стандарт - 1 пустая строка, если надо отделить код

String name = scanner.nextLine().trim();

if (name.equalsIgnoreCase("Завершить")) {
break;
}
products.add(name);

double priceNum;

while (true) {
System.out.println("Введите стоимость товара в формате руб.копейки");

try {
String priceInput = scanner.nextLine().trim().replace(',', '.');
priceNum = Double.parseDouble(priceInput);

if (priceNum <= 0) {
System.out.println("Введите положительное число");
} else {
prices.add(priceNum);
break;
}
} catch (NumberFormatException e) {
System.out.println("\nОшибка: Введенный текст не является числом.\n");
}
}

total += priceNum;

while (true) {
System.out.println("Хотите добавить еще одну позицию?\nВыберети вариант и наберите номер ответа.\n1. ДА\n2. НЕТ");
String question = scanner.nextLine().trim();

if (question.equalsIgnoreCase("1")) {
break;
} else if (question.equalsIgnoreCase("2")) {
break outerLoop;
} else {
System.out.println("\nНе верно. Введите только 1 или 2.\n");
}
}
}
System.out.println("\nCписок продуктов:");

for (int i=0; i<products.size(); i++){
String product = products.get(i);
double priceСonversion = prices.get(i);

System.out.printf("Товар: " + product + " Стоимость %.2f " , priceСonversion );
System.out.println(Ruble.getRuble(priceСonversion));
}
System.out.print("\nОбщая стоимость всех товаров = " + String.format(format,total));
System.out.println(" " + Ruble.getRuble(total) + "\n");

Persons person = new Persons();
double conversion = Double.parseDouble(person.countPersons());
double division = total / conversion;

System.out.printf("Итого с каждого по: %.2f ", division);
System.out.println(Ruble.getRuble(division));
}
}
4 changes: 2 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calc = new Calculator();
calc.calculator();
}
}
28 changes: 28 additions & 0 deletions src/main/java/Persons.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Scanner;

public class Persons {

public String countPersons() {
Scanner scanner = new Scanner(System.in);
System.out.println("Укажите на сколько человек разделить счет?");
String persons;

while (true) {
persons = scanner.nextLine();
try {
int number = Integer.parseInt(persons);
if (number <= 1) {
System.out.println("Некорректоное значение для подсчета");
} else if (number > 1) {
break;
}
}
catch(NumberFormatException e){
System.out.println("Ошибка: Введенный текст не является числом.");
}
}
System.out.println("Вы ввели " + persons);

return persons;
}
}
18 changes: 18 additions & 0 deletions src/main/java/Ruble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Ruble {

public static String getRuble(double rubles) {
int count = (int) rubles % 100;
if (count > 4 && count < 21)
return "Рублей";
switch (count % 10) {
case 1:
return "Рубль";
case 2:
case 3:
case 4:
return "Рубля";
default:
return "Рублей";
}
}
}