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

public class Calculator {

static void calculate(int people) {
String prod = "";
double price = 0.0;

while (true) {
System.out.println("Для добавления позиции, Введите название" +
"\nДля завершения подсчёта, Введите 'Завершить'");
Scanner scanner = new Scanner(System.in);
String inputProd = scanner.next();

if (inputProd.equalsIgnoreCase("Завершить")) {
break;
} else {
prod = prod + inputProd + "\n";
price = price + getPrice();
}

}
System.out.println("Добавленные позиции:\n" + prod);
double result = price / people;
String temp = "Каждый человек должен заплатить %.2f" + Formatter.formatted((int) Math.floor(result));
System.out.println(String.format(temp, result));
}


static double getPrice() {
while (true) {
System.out.println("Укажите стоимость в формате: 'рубли.копейки' [например: 5,55 или 11.48]");
double inputPrice = 0;
try {
Scanner scanner = new Scanner(System.in);
inputPrice = scanner.nextDouble();
if (inputPrice <= 0){
System.out.println("Ошибка: неверное значение. Попробуйте снова");
} else {
return inputPrice;
}
} catch (InputMismatchException e) {
System.out.println("Ошибка: неверное значение. Попробуйте снова");
}
}
}

}
18 changes: 18 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Formatter {
public static String formatted(int rouble) {

if ((rouble % 100 / 10) == 1){
return " рублей.";
}
switch (rouble % 10){
case 1:
return " рубль.";
case 2:
case 3:
case 4:
return " рубля.";
default:
return " рублей.";
}
}
}
19 changes: 18 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import java.util.InputMismatchException;
import java.util.Scanner;

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

int people = 0;
while (people < 2) {
System.out.println("На скольких человек необходимо разделить счёт?");
Scanner scanner = new Scanner(System.in);
try {
people = scanner.nextInt();
if (people < 2) {
System.out.println("Ошибка: неверное значение. Попробуйте снова");
}
}
catch (InputMismatchException e) {
System.out.println("Ошибка: неверное значение. Попробуйте снова");
}
}
Calculator.calculate(people);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

По хорошему надо выносить всю эту логику из класса Main. Этот класс обычно призван только стартануть программу, а все остальное должно быть сделано в специальных классах.

Тем более остальная логика приложения вынесена в отдельные классы, а именно ввод количества людей почему-то не вынесен

}
}