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

public class Calculator {
Scanner scanner = new Scanner(System.in);
short person = 0;
String food = "";
float totalCoast = 0.00F;
String rightWord = getRightWord(totalCoast);

public void addPersons() {
short number;
do {
System.out.println("Сколько вас человек:");
while (!scanner.hasNextInt()) {
System.out.println("Введите корректное значение:");
scanner.next();
}
number = scanner.nextShort();
} while (number <= 1);
person = number;
}

public void addFood() {
System.out.println("Введите название товара или введите \"Завершить\"");
while (true) {
String name = scanner.next();
if (name.equalsIgnoreCase("Завершить")) {
break;
} else {
food = food + name + "\n";
System.out.println("Введите цену в формате \"Рубли,Копейки\"");
float coast = scanner.nextFloat();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Пользователь может ввести на вопрос про стоимость товара вместо числа строку, и приложение упадет. Для количества человек ты обрабатываешь такую ситуацию через hasNextInt, стоит добавить обработку и для стоимости, чтобы приложение не падало

while (coast <= 0) {
System.out.println("Введите корректное значение:");
coast = scanner.nextFloat();
}
totalCoast = totalCoast + coast;
System.out.println("Добавлено. \nЧтобы продолжить введите название товара. Чтобы завершить введите \"Завершить\"");
}
}
}

private String getRightWord (float n) {
n = (int) Math.floor(n);
n = Math.abs(n) % 100;
int n1 = (int) (n % 10);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Лучше здесь дать более понятные имена, например, lastDigit

if (n > 10 && n < 20) return "рублей";
if (n1 > 1 && n1 < 5) return "рубля";
if (n1 == 1) return "рубль";
return "рублей";

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 void printResult () {
String template = "Каждый должен заплатить %.2f " + rightWord;
float moneyForPerson = totalCoast / person;
System.out.println("Добавленные товары:" + "\n" + food);
System.out.println(String.format(template, moneyForPerson));
}
}
7 changes: 4 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
public class Main {

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