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

public class Calculated {
Scanner scanner = new Scanner(System.in);
ArrayList<Product> products = new ArrayList<>();
double totalAmount = 0.0;

public void countItems() {
String answer;

do {
System.out.println("Введите название товара:");
String name = readProductName();
double price = readProductPrice();

Product product = new Product(name, price);
products.add(product);
System.out.println("Товар \"" + product.name + "\" добавлен");

totalAmount += price;
System.out.println("Общая сумма всех товаров: " + String.format("%.2f", totalAmount) + " руб.");

do {
System.out.println("Хотите добавить еще один товар? (да/нет)");
answer = scanner.nextLine();
if (!answer.equalsIgnoreCase("да") && !answer.equalsIgnoreCase("нет")) {
System.out.println("Пожалуйста, введите 'да' или 'нет'.");
}
} while (!answer.equalsIgnoreCase("да") && !answer.equalsIgnoreCase("нет"));
} while (!answer.equalsIgnoreCase("нет"));
}

private String readProductName() {
String name;
do {
name = scanner.nextLine();
if (name.trim().isEmpty()) {
System.out.println("Название товара не может быть пустым. Пожалуйста, введите непустое название.");
}
} while (name.trim().isEmpty());
return name;
}

private double readProductPrice() {
double price;
do {
System.out.println("Введите стоимость товара в формате рубли,копейки");
while (!scanner.hasNextDouble()) {
System.out.println("Неверный формат. Пожалуйста, введите число в формате рубли,копейки.");
scanner.nextLine();
}
price = scanner.nextDouble();
scanner.nextLine();
} while (price <= 0);
return price;
}

public void displayProducts() {
System.out.println("Список добавленных товаров:");
for (Product item : products) {
System.out.println("Название: " + item.name + ", Стоимость: " + item.price + " руб.");
}
}

public void calculatePerPerson(int quantity) {
TotalAmountSplitter.calculatePerPerson(totalAmount, quantity);
}
}

class Product {
String name;
double price;

public Product(String name, double price) {
this.name = name;
this.price = price;
}
}
34 changes: 34 additions & 0 deletions src/main/java/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.Scanner;

public class Input {
private final Scanner scanner = new Scanner(System.in);


public void quantityOfPeople() {
Calculated calculated = new Calculated();

while (true) {
System.out.println("На скольких человек необходимо разделить счёт?");
if (scanner.hasNextInt()) {
int quantity = scanner.nextInt();
if (quantity <= 1) {
System.out.println("Количество людей должно быть больше 1.");
} else {
scanner.nextLine();
calculated.countItems();

System.out.println("Все товары добавлены. Показать список и посчитать счет? (да/нет)");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("да")) {
calculated.displayProducts();
calculated.calculatePerPerson(quantity);
}
return;
}
} else {
System.out.println("Введите целое число.");
scanner.next();
}
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Input input = new Input();
input.quantityOfPeople();
}
}
15 changes: 15 additions & 0 deletions src/main/java/TotalAmountSplitter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class TotalAmountSplitter {

public static void calculatePerPerson(double totalAmount, int quantity) {
double perPersonAmount = totalAmount / quantity;

String formattedAmount;
if (perPersonAmount == Math.floor(perPersonAmount)) {
formattedAmount = String.format("%.0f рубль", perPersonAmount);
} else {
formattedAmount = String.format("%.2f рубля", perPersonAmount).replace(".", ",");
}

System.out.println("Каждый человек должен заплатить: " + formattedAmount);
}
}