Skip to content
This repository was archived by the owner on May 15, 2023. It is now read-only.
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
73 changes: 73 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {

ArrayList<Product> products = new ArrayList<>();

void start(int numberOfPeople, Scanner scanner) {
do {
String productName = getProductName(scanner);
double productPrice = getProductPrice(scanner);
products.add(new Product(productName, productPrice));
successfullyAddedMessage();
} while (shouldContinueAddingProducts(scanner));

printResults(numberOfPeople);
}

String getProductName(Scanner scanner) {
System.out.println("Введите название товара:");
String productName = "";
while (productName.isEmpty()) {
productName = scanner.next();
if (productName.isEmpty()) {
System.out.println("ОШИБКА. Введенное значение не должно быть пустым. Введите еще раз:");
}
}
return productName;
}

double getProductPrice(Scanner scanner) {
double price = -1;
System.out.println("Введите стоимость товара:");
while (price < 0) {
Comment thread
Kileren marked this conversation as resolved.
String priceStr = scanner.next();
try {
price = Double.parseDouble(priceStr);
if (price < 0) {
System.out.println("ОШИБКА. Стоимость товара не может быть меньше 0, введите снова:");
}
} catch (NumberFormatException e) {
System.out.println("ОШИБКА. Введеное значение не является числом, введите снова:");
}
}
return price;
}

void successfullyAddedMessage() {
System.out.println("Товар успешно добавлен.\n");
}

boolean shouldContinueAddingProducts(Scanner scanner) {
System.out.println("Хотите добавить еще один товар?");
System.out.println("Введите \"Завершить\", чтобы прекратить ввод товаров или любой символ, чтобы продолжить добавления товаров.");

String userInput = scanner.next();
return !userInput.equalsIgnoreCase("Завершить");
}

void printResults(int numberOfPeople) {
double totalPrice = 0;

System.out.println("Добавленные товары:");
for (int i = 0; i < products.size(); i++) {
Product product = products.get(i);
System.out.println(product.name);
totalPrice += product.price;
}

String amountPerPerson = PriceFormatter.format(totalPrice / numberOfPeople);
System.out.printf("\nКаждый человек должен заплатить - %s", amountPerPerson);
}
}
30 changes: 29 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
int numberOfPeople = getNumberOfPeople(scanner);

Calculator calculator = new Calculator();
calculator.start(numberOfPeople, scanner);

scanner.close();
Comment thread
Kileren marked this conversation as resolved.
}

static int getNumberOfPeople(Scanner scanner) {
int numberOfPeople = 0;
System.out.println("На скольких человек необходимо разделить счет?");
while (numberOfPeople <= 1) {
String userInput = scanner.next();
try {
numberOfPeople = Integer.parseInt(userInput);
} catch (NumberFormatException e) {
System.out.println("ОШИБКА. Ожидается целочисленное значение. Введите корректное число:");
continue;
}

if (numberOfPeople == 1) {
System.out.println("ОШИБКА. Некорректное число. На одного человека нет смысла ничего считать и делить. Введите корректное число.");
} else if (numberOfPeople < 1) {
System.out.println("ОШИБКА. Некорректное число. Количество людей должно быть больше 1. Введите корректное число");
}
}
return numberOfPeople;
}
}
15 changes: 15 additions & 0 deletions src/main/java/PriceFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class PriceFormatter {

static String format(double amount) {
int mainPart = (int) Math.floor(amount);
String rub;
if (mainPart % 10 == 1 && mainPart % 100 != 11) {
rub = "рубль";
} else if (mainPart % 10 >= 2 && mainPart % 10 <= 4 && (mainPart % 100 < 10 || mainPart % 100 >= 20)) {
rub = "рубля";
} else {
rub = "рублей";
}
return String.format("%.2f %s", amount, rub);
}
}
9 changes: 9 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Product {
String name;
double price;

Product(String name, double price) {
this.name = name;
this.price = price;
}
}
Comment thread
Kileren marked this conversation as resolved.