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

public class Calculator {

private List<String> items = new ArrayList<>();
private final int humanCount;
private double totalPrice;

public Calculator(int humanCount) {
this.humanCount = humanCount;
totalPrice = 0;
}

public void addDish(String dish, Double price) {
this.items.add(dish);
totalPrice += price;
System.out.println("Товар " + dish + " успешно добавлен!");
System.out.println("Сейчас общая сумма: " + getTotalPrice());
}

public List<String> getItems() {
return items;
}

public String getTotalPrice() {
return String.format("%.2f", totalPrice);
}

public double getPriceForPerson() {
double result = 0;
try {
result = totalPrice / humanCount;
} catch (ArithmeticException ex) {
System.out.println("Внимание!! Деление на НОЛЬ!");
}
return result;
}
}
36 changes: 36 additions & 0 deletions src/main/java/InputLoopFunctions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.InputMismatchException;
import java.util.Scanner;

public class InputLoopFunctions {
public static void addingItem(Scanner sc, Calculator calculator){

String itemName;

while (true) {

System.out.println("Введите название товара (либо -> 'завершить' ): ");
itemName = sc.next();

if (itemName.equalsIgnoreCase("завершить")) break;
else {
while (true) {
System.out.println("Введите стоимость товара в формате ХХ,ХХ (рубли,коп.): ");
try {
double itemPrice = sc.nextDouble();
if (itemPrice <= 0) {
System.out.println("Цена должна быть положительным числом!");
} else {
calculator.addDish(itemName, itemPrice);
break;
}
} catch (InputMismatchException ex) {
System.out.println("Формат ввода неверный!" +
" Введите дробное число через запятую!");
sc.nextLine();
}
}
}
}
sc.close();
}
}
40 changes: 39 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
import java.util.InputMismatchException;
import java.util.Scanner;

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

Scanner sc = new Scanner(System.in);
String itemName;
int humanCount;
Calculator calculator;

System.out.println("Приветствую!\n Введите количество человек" +
" на сколько будем делить счет. Введите значение (больше '1'): ");

while (true) { //in separate static too

try {
humanCount = sc.nextInt();
if (humanCount > 1) {
calculator = new Calculator(humanCount);
break;
}

} catch (InputMismatchException ex) {
System.out.println("ERROR! Введите число! ");
sc.nextLine();
}
Comment on lines +17 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Отсюда можно вынести очень хорошую статическую функцию для считывания целого числа, большего единицы

System.out.println("!!! Число должно быть больше '1'! ");
}

InputLoopFunctions.addingItem(sc, calculator);

//sc.close();

System.out.println("Добавленные товары: ");

for (String item : calculator.getItems()) {
System.out.println(" ╰┈➤ " + item + " ** ");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

какая красивая стрелка)

}

Output finalOutput = new Output();
finalOutput.showMessage(calculator.getPriceForPerson());
}
}
18 changes: 18 additions & 0 deletions src/main/java/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Output {
public static void showMessage(double personalPrice) {

if ((int) personalPrice % 100 >= 10 && (int) personalPrice % 100 <= 20) {
System.out.printf("Каждый человек должен заплатить %.2f рублей",
personalPrice);
} else {
switch ((int) personalPrice % 10) {
case 1 -> System.out.printf("Каждый человек должен заплатить %.2f рубль",
personalPrice);
case 2, 3, 4 -> System.out.printf("Каждый человек должен заплатить %.2f рубля",
personalPrice);
default -> System.out.printf("Каждый человек должен заплатить %.2f рублей",
personalPrice);
}
}
}
}