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
15 changes: 15 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Formatter {
public static String format(double price) {
String formattedPrice = String.format("%.2f", price);
int rubles = (int) price;
if (rubles % 100 >= 11 && rubles % 100 <= 14) {
return formattedPrice + " рублей";
}
int lastNumbers = rubles % 10;
return switch (lastNumbers) {
case 1 -> formattedPrice + " рубль";
case 2, 3, 4 -> formattedPrice + " рубля";
default -> formattedPrice + " рублей";
};
}
}
17 changes: 17 additions & 0 deletions src/main/java/Goods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Goods {
public String name;
public double price;

public Goods(String name, double price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public double getPrice() {
return price;
}
}
77 changes: 75 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,79 @@
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
SplitCalculator splitCalculator = new SplitCalculator();
splitCalculator.start();
}
}
}

class SplitCalculator {
public int numberOfPeople;
public ArrayList<Goods> goodsList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

public void start() {
System.out.println("Вас приветствует Калькулятор счёта! Пожалуйста, введите число персон, на которых необходимо раздеть счёт:");
while (true)
try {
numberOfPeople = scanner.nextInt();
scanner.nextLine();
if (numberOfPeople <= 1) {
System.out.println("Некорректное значение для подсчёта(значение должно быть больше 1), попробуйте еще раз.");
} else {
break;
}
} catch (InputMismatchException e) {
System.out.println("Некорректное значение для подсчёта, пожалуйста, попробуйте еще раз.");
scanner.nextLine();
}

System.out.println("Введите информацию о товарах.");
while (true) {
System.out.println("Введите название товара:");
String name = scanner.nextLine();
if (name.equalsIgnoreCase("завершить")) {
break;
}

double price = 0;
boolean statusPrice = false;
while (!statusPrice) {
System.out.println("Введите цену товара: ");
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
scanner.nextLine();
if (price > 0) {
statusPrice = true;
} else {
System.out.println("Цена товара должна быть больше 0. Повторите ввод.");
}
} else {
System.out.println("Неверная цена. Цена должна быть представлена числом, пожалуйста, повторите ввод.");
scanner.next();
}
}
goodsList.add(new Goods(name, price));
System.out.println("Товар успешно добавлен!");
System.out.println("Хотите добавить ещё один товар? Если нет - введите завершить");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("завершить")) {
break;
}

}
System.out.println("\nДобавленные товары:");
for (Goods goods : goodsList) {
System.out.println(goods.getName() + ": " + goods.getPrice());
}
double totalCost = 0;
for (Goods goods : goodsList) {
totalCost += goods.getPrice();
}
double costForEach = totalCost / numberOfPeople;
System.out.println("\nКаждый должен оплатить:");
System.out.println(Formatter.format(costForEach));
}
}