Skip to content
Open
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
117 changes: 117 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,122 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

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

// Запрашиваем количество людей для разделения счета
int numberOfPeople = getNumberOfPeople(scanner);

// Создаем объект калькулятора
BillCalculator calculator = new BillCalculator(numberOfPeople);

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

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

double itemPrice = getItemPrice(scanner);

calculator.addItem(itemName, itemPrice);
}

// Вывод результатов
calculator.displayBill();
}

private static int getNumberOfPeople(Scanner scanner) {
int numberOfPeople;
do {
System.out.print("Введите количество людей для разделения счета: ");
while (!scanner.hasNextInt()) {
System.out.println("Ошибка: Введите число больше 1.");
scanner.next(); // Пропускаем некорректный ввод
}
numberOfPeople = scanner.nextInt();

if (numberOfPeople <= 0) {
System.out.println("Ошибка: Некорректное значение. Введите число больше 0.");
Comment on lines +43 to +44
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 человека нет
Предыдущее условие было правильным

}
} while (numberOfPeople <= 0);
return numberOfPeople;
}

private static double getItemPrice(Scanner scanner) {
double itemPrice;
do {
System.out.print("Введите стоимость товара (в формате рубли.копейки): ");
while (!scanner.hasNextDouble()) {
System.out.println("Ошибка: Введите корректную стоимость.");
scanner.next(); // Пропускаем некорректный ввод
}
itemPrice = scanner.nextDouble();
if (itemPrice < 0) {
System.out.println("Ошибка: Стоимость не может быть отрицательной.");
}
} while (itemPrice < 0);
return itemPrice;
}
}

class BillCalculator {
private final int numberOfPeople;
private final List<String> items = new ArrayList<>();
private final List<Double> prices = new ArrayList<>();

public BillCalculator(int numberOfPeople) {
this.numberOfPeople = numberOfPeople;
}

public void addItem(String itemName, double itemPrice) {
items.add(itemName);
prices.add(itemPrice);
System.out.println("Товар '" + itemName + "' с ценой " + formatPrice(itemPrice) + " успешно добавлен.");
}

public void displayBill() {
System.out.println("\nДобавленные товары:");
for (int i = 0; i < items.size(); i++) {
System.out.println(items.get(i) + ": " + formatPrice(prices.get(i)));
}

double totalBill = calculateTotalBill();
double sharePerPerson = totalBill / numberOfPeople;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Лучше проверить numberOfPeople на 0 перед делением, это сделает класс более безопасным и надёжным


System.out.println("\nСумма, которую каждый должен заплатить:");
for (int i = 1; i <= numberOfPeople; i++) {
System.out.println("Человек " + i + ": " + formatPrice(sharePerPerson));
}
}

private double calculateTotalBill() {
double total = 0;
for (double price : prices) {
total += price;
}
return total;
}

private String formatPrice(double price) {
int rubles = (int) price;
int kopecks = (int) ((price - rubles) * 100);
int rublesLastDigit = rubles % 10;

if (rubles >= 11 && rubles <= 19) {
return rubles + " рублей " + kopecks + " копеек";
} else {
return switch (rublesLastDigit) {
case 1 -> rubles + " рубль " + kopecks + " копеек";
case 2, 3, 4 -> rubles + " рубля " + kopecks + " копеек";
default -> rubles + " рублей " + kopecks + " копеек";
};
Comment on lines +111 to +117
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Требования выводить слово "копеек" нет, но если выводишь - то нужно склонять тоже

}
}
public static void main(String[] args) {
System.out.println("Hello world!");
}
Expand Down