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
139 changes: 138 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,143 @@
import static java.lang.Math.floor;

import java.util.Scanner;
import java.util.function.Consumer;

import calculator.Calculator;
import calculator.Goods;

public class Main {

private static final String MESSAGE_HOW_MUCH_PERSON = "На скольких человек необходимо разделить счёт?";
private static final String MESSAGE_SUFFIX_RETRY = " Пожалуйста, повторите попытку...";
private static final String MESSAGE_PERSON_COUNT_INCORRECT = "Количество человек, разделяющих счет, должно быть больше 1." + MESSAGE_SUFFIX_RETRY;
private static final String MESSAGE_INVALID_INPUT = "Осуществлен некорректный ввод." + MESSAGE_SUFFIX_RETRY;
private static final String MESSAGE_INVALID_INPUT_NEGATIVE_PRICE = "Цена товара не может быть меньше 0." + MESSAGE_SUFFIX_RETRY;
private static final String MESSAGE_GOODS_REQUEST_NAME = "Введите название товара:";
private static final String MESSAGE_GOODS_REQUEST_PRICE = "И его стоимость в формате 00.00 (рубли.копейки):";
private static final String MESSAGE_GOODS_REQUEST_NAME_INVALID = "Название не должно быть пустым. " + MESSAGE_SUFFIX_RETRY;
private static final String MESSAGE_GOODS_ADDED = "Товар успешно добавлен! Для того, чтобы добавить ещё один товар - введите любой символ. Введите \"Завершить\", чтобы завершить ввод товаров.";
private static final String MESSAGE_GOODS_SHOW_TITLE = "Добавленные товары:";
private static final String MESSAGE_GOODS_SHOW_ITEM_FORMAT = "Название: %s, Цена: %.2f";
private static final String MESSAGE_PART_PRICE_FORMAT = "Каждый человек должен заплатить %.2f %s";
private static final String RUBLES_ROOT = "руб";
private static final String[] RUBLES_SUFFIX = {"ль", "ля", "лей"};
private static final int PERSON_COUNT_CORRECT = 2;
private static final String SAFE_WORD = "завершить";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

А это ты молодец, вынес строки для переиспользования при необходимости


public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
Calculator calculator = new Calculator(requestPersonCount(scanner));

requestGoods(scanner, calculator);

showResult(calculator);

scanner.close();
}

private static void showResult(Calculator calculator) {
System.out.println(MESSAGE_GOODS_SHOW_TITLE);

for (Goods item : calculator.getAllGoods()) {
System.out.println(String.format(MESSAGE_GOODS_SHOW_ITEM_FORMAT, item.getName(), item.getPrice()));
}

double onePersonPrice = calculator.getOnePersonPrice();
System.out.println(String.format(MESSAGE_PART_PRICE_FORMAT, onePersonPrice, getRublesText(onePersonPrice)));
}

private static String getRublesText(double price) {
String ret = RUBLES_ROOT;

double priceTransform = floor(price) % 100;
if (priceTransform > 19) priceTransform = priceTransform % 10;

if (priceTransform == 0 || priceTransform > 4) {
ret += RUBLES_SUFFIX[2];
} else if (priceTransform == 1) {
ret += RUBLES_SUFFIX[0];
} else {
ret += RUBLES_SUFFIX[1];
}

return ret;
}

private static void requestGoods(Scanner scanner, Calculator calculator) {
String safeWord = "";

while (!safeWord.equalsIgnoreCase(SAFE_WORD)) {
System.out.println(MESSAGE_GOODS_REQUEST_NAME);
String goodsName = requestName(scanner);

System.out.println(MESSAGE_GOODS_REQUEST_PRICE);
double goodsPrice = requestPrice(scanner);

calculator.addGoods(new Goods(goodsName, goodsPrice));

System.out.println(MESSAGE_GOODS_ADDED);
safeWord = scanner.nextLine();
}
}

private static double requestPrice(Scanner scanner) {
double ret = .0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Зачем это нужно?


try {
Consumer<String> checkInput = (input) -> {
String[] doubleParts = input.split("\\.");
if (doubleParts.length == 1 || (doubleParts.length > 1 && doubleParts[1].length() != 2)) {
throw new IllegalStateException();
}
};

String input = scanner.nextLine();
double inputConverted = Double.parseDouble(input);
checkInput.accept(input);
while (inputConverted < 0) {
System.out.println(MESSAGE_INVALID_INPUT_NEGATIVE_PRICE);
input = scanner.nextLine();
checkInput.accept(input);
inputConverted = Double.parseDouble(input);
}
ret = inputConverted;
} catch (Exception e) {
System.out.println(MESSAGE_INVALID_INPUT);
return requestPrice(scanner);
}

return ret;
}

private static String requestName(Scanner scanner) {
String ret = scanner.nextLine();

while (ret.isBlank()) {
System.out.println(MESSAGE_GOODS_REQUEST_NAME_INVALID);
ret = scanner.nextLine();
}

return ret.trim();
}

private static int requestPersonCount(Scanner scanner) {
int ret = 0;

try {
while (ret < PERSON_COUNT_CORRECT) {
System.out.println(MESSAGE_HOW_MUCH_PERSON);
String input = scanner.nextLine();
ret = Integer.parseInt(input);
if (ret < PERSON_COUNT_CORRECT) {
System.out.println(MESSAGE_PERSON_COUNT_INCORRECT);
}
}
} catch (Exception e) {
System.out.println(MESSAGE_INVALID_INPUT);
return requestPersonCount(scanner);
}

return ret;
}
}
30 changes: 30 additions & 0 deletions src/main/java/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package calculator;

import java.util.ArrayList;

public class Calculator {
private final int personCount;
private final ArrayList<Goods> m_goods = new ArrayList<>();

public Calculator(int personCount) {
this.personCount = personCount;
}

public void addGoods(Goods goods) {
m_goods.add(goods);
}

public ArrayList<Goods> getAllGoods() {
return new ArrayList<>(m_goods);
}

public double getOnePersonPrice() {
double ret = .0;

for (Goods item : m_goods) {
ret += item.getPrice();
}

return ret / personCount;
}
}
19 changes: 19 additions & 0 deletions src/main/java/calculator/Goods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package calculator;

public class Goods {
private final String name;
private final double price;

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

public String getName() {
return this.name;
}

public double getPrice() {
return this.price;
}
}