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

public class Calculator {
String products = "\n"; //Аккумулируем товары
String prodName; //Наименование 1 шт из сканера
String exit = "завершить"; // Команда прерывания
String exitMessage = "Программа завершена"; // Сообщение после прерывания
double price = 0.00; //цена 1 шт вводимая пользователем
double sum = 0.00; // сумма
int countProducts; // Количество введенных товаров

public void consoleProducts() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.printf("Введите название товара или '%s' для выхода\n", exit);
prodName = scanner.next();
if (prodName.equalsIgnoreCase(exit)) {
System.out.println(exitMessage);
break;
}
while (true) {
System.out.println("Введите стоимость товара в формате рубли.копейки. Например: 10,45");
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price > 0.00) {
countProducts++;
products = products + prodName + " ";
sum += price;
break;
}
} else {
System.out.println("Ошибка ввода стоимости");
scanner.next();
}
}
System.out.println("Товар успешно добавлен.");
}
double bill = sum / Main.persons;
System.out.printf("Введены следующие товары, в количестве %d шт.:", countProducts);
System.out.print(products + "\n");
System.out.println("Сумма на одного человека: " + String.format("%.2f", bill) + " " + rubles(bill));
}

public String rubles(double a) {
int rub = (int) Math.floor(a);
if (rub % 100 > 5 && rub <= 20) {
return "рублей";
} else if (rub % 10 == 1) {
return "рубль";
} else if (rub % 10 > 1 && rub % 10 < 5) {
return "рубля";
}
return "рублей";
}
}
12 changes: 8 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
public class Main {

public static int persons;

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");

Persons.enterPersons();

Calculator calculator = new Calculator();
calculator.consoleProducts();
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/Persons.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.Scanner;

public class Persons {

public static void enterPersons() {
Scanner scanner = new Scanner(System.in);
System.out.println("На скольких человек разделить счёт?\nВведите количество посетителей от 2 до 1000");

while (true) {
if (scanner.hasNextInt()) {
Main.persons = scanner.nextInt();

Choose a reason for hiding this comment

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

Лучше количество человек возвращать как результат метода enterPersons, в Main его передавать в метод consoleProducts, а переменную Main.persons убрать. Так код будет более прозрачным


if (Main.persons > 1 && Main.persons < 1000) {
System.out.printf("Введено %d посетителей\n", Main.persons);
break;
}
} else {
System.out.println("Ошибка ввода. Попробуйте ещё раз.");
scanner.next();
}
}
}
}