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
65 changes: 52 additions & 13 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,71 @@
import java.util.List;
import java.util.Scanner;


public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberPeople;

while (true) {
System.out.print("На скольких человек разделить счет?: ");
System.out.print("На скольких человек необходимо разделить счет: ");

if (scanner.hasNextInt()) {
numberPeople = scanner.nextInt();

if (numberPeople <= 1) {
System.out.println("Некорректное значение! Введите количество людей больше 1 ");
System.out.println("Некорректное значение. Введите количество людей больше 1.");
} else {
break;
}
} else {
System.out.println("Некоректное значение! Введите целое число.");
scanner.next();
System.out.println("Некорректное значение. Введите целое число.");
}
}
System.out.println("Создан калькулятор на " + numberPeople + " человек");

System.out.println("Создан калькулятор для " + numberPeople + " человек");

List<Product> products = new ArrayList<>();

while (true) {
System.out.println("Введите название товара или 'Завершить' для окончания: ");
System.out.print("Введите название товара или 'Завершить' для окончания: ");
String name = scanner.next();

if (name.equalsIgnoreCase("Завершить")) {
break;
}
System.out.println("Введите стоимость товара в формате рубли.копейки: ");
double price = scanner.nextDouble();

System.out.print("Введите стоимость товара в формате рубли.копейки: ");
double price;

while (true) {
String priceInput = scanner.next();
try {
price = Double.parseDouble(priceInput);
if (price < 0) {
System.out.println("Некорректное значение. Введите положительное число.");
} else {
break;
}
} catch (NumberFormatException e) {
System.out.println("Некорректное значение. Введите число.");
}
}

Product product = new Product(name, price);
products.add(product);

System.out.println("Товар успешно добавлен.");
System.out.println("Хотите добавить еще один товар? (Введите 'Завершить' для окончания): ");

System.out.print("Хотите добавить еще один товар? (Введите 'Завершить' для окончания): ");
String continueInput = scanner.next();

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

System.out.println("Список товаров: ");
double total = 0;

Expand All @@ -52,11 +75,27 @@ public static void main(String[] args) {
total += product.getPrice();
}

System.out.println("Общая сумма: " + total);
long roundedTotal = Math.round(total);
System.out.println("Общая сумма: " + roundedTotal);
double amountPerPerson = (double) roundedTotal / numberPeople;
long roundedAmount = Math.round(amountPerPerson);
String currency = getCurrencyString(roundedAmount);
System.out.println("Каждому человеку необходимо заплатить: " + roundedAmount + " " + currency);
}

double amountPerPerson = total / numberPeople;
String currency = (int) amountPerPerson == 1 ? "рубль" : "рубля";
System.out.println("Каждому человеку необходимо заплатить: " + amountPerPerson + " " + currency);
private static String getCurrencyString(long amount) {
int roundedAmount = (int) amount;
int lastDigit = roundedAmount % 10;
int lastTwoDigits = roundedAmount % 100;

if (lastTwoDigits >= 11 && lastTwoDigits <= 19) {
return "рублей";
} else if (lastDigit == 1) {
return "рубль";
} else if (lastDigit >= 2 && lastDigit <= 4) {
return "рубля";
} else {
return "рублей";
}
}
}

8 changes: 5 additions & 3 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
class Product {
public String name;
public double price;
private final String name;
private final double price;

public Product (String name, double price) {
public Product(String name, double price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public double getPrice() {
return price;
}
Expand Down