forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
45 lines (39 loc) · 1.71 KB
/
Main.java
File metadata and controls
45 lines (39 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int friendsNumber;
System.out.println("На скольких человек необходимо разделить счёт?");
Scanner scanner = new Scanner(System.in);
while (true){
try{
friendsNumber = Integer.parseInt(scanner.nextLine());
if (friendsNumber > 1) {
break;
}
System.out.println("Упс! Для верного расчёта количество человек должно быть больше 1. Попробуйте ещё раз:");
}
catch (NumberFormatException exception) {
System.out.println("Нужно ввести целое число больше 1. Попробуйте ещё раз:");
}
}
Goods goods = new Goods();
goods.collectNamesAndPrices();
float debtPerFriend = goods.priceSum / friendsNumber;
String rubleForm = getWordForm((int)debtPerFriend);
String debtPhraseTemplate = "С каждого %.2f %s";
System.out.print("Добавленные товары:\n" + goods.goodList);
System.out.print(String.format(debtPhraseTemplate, debtPerFriend, rubleForm));
}
public static String getWordForm(int num){
int num100 = num % 100;
if (num100 <= 4 || num100 >= 21) {
int num10 = num100 % 10;
if (num10 == 1) {
return " рубль";
} else if (num10 > 1 && num10 < 5) {
return " рубля";
}
}
return " рублей";
}
}