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
57 changes: 57 additions & 0 deletions src/main/java/ExpensesCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.Scanner;

public class ExpensesCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberOfGuests = 5;
calculate(scanner, numberOfGuests);
}

public static void calculate(Scanner scanner, int numberOfGuests) {
String listOfExpenses = "";
float totalExpenses = 0.0f;
while (true) {
System.out.println("Введите название товара (для завершения введите 'Завершить'):");
String thing = scanner.next();

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

System.out.println("Введите стоимость товара:");
float price = 0;

if (scanner.hasNextFloat()) {
price = scanner.nextFloat();
}

String formattedPrice = "";
if (price > 0) {
formattedPrice = String.format("%.2f %s", price, getEnding(price, "рубль", "рубля", "рублей"));
listOfExpenses += thing + " " + formattedPrice + "\n";
totalExpenses += price;
System.out.printf("Вы купили %s за %s\n", thing, formattedPrice);
} else {
System.out.println("Неправильная цена! Попробуйте ещё раз.");
scanner.next();
}
System.out.printf("Список расходов:%n%s", listOfExpenses);
float perGuest = totalExpenses / numberOfGuests;
String formattedTotal = String.format("Итого расходов на одного гостя: %.2f %s%n", perGuest, getEnding(perGuest, "рубль", "рубля", "рублей"));
System.out.println(formattedTotal);
}
}

public static String getEnding(float number, String form1, String form2, String form3) {
int numberInt = (int) number;
int mod10 = numberInt % 10;
int mod100 = numberInt % 100;
if (mod10 == 1 && mod100 != 11) {
return form1;
} else if (mod10 >= 2 && mod10 <= 4 && (mod100 < 10 || mod100 >= 20)) {
return form2;
} else {
return form3;
}
}
}
23 changes: 22 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println("На скольких человек необходимо разделить счёт?");
Scanner scanner = new Scanner(System.in);
int numberOfGuests;

while (true) {
try {
numberOfGuests = scanner.nextInt();
if (numberOfGuests > 1) {
break;
} else if (numberOfGuests == 1) {
System.out.println("Нет смысла ничего считать и делить. Пожалуйста введите корректное количество гостей.");
} else {
System.out.println("Это некорректное значение для подсчёта. Пожалуйста введите корректное количество гостей.");
}
} catch (InputMismatchException e) {
System.out.println("Некорректный ввод. Введите целочисленное значение.");
scanner.next(); // Пропускать неверный ввод
}
}
ExpensesCalculator.calculate(scanner, numberOfGuests);
}
}