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: 8 additions & 31 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,8 @@
import java.util.ArrayList;
import java.text.DecimalFormat;
import java.util.Scanner;

class Product {
private String name;
private double price;

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

public String getName() {
return name;
}

public double getPrice() {
return price;
}

public String toString() {
return name + " - " + new DecimalFormat("0.00").format(price) + " рубля";
}
}
import java.util.ArrayList;

class Calculator {
private int persons;
private final int persons; // Добавил модификатор

public Calculator(int persons) {
this.persons = persons;
Expand All @@ -36,7 +13,7 @@ public void start() {
ArrayList<Product> allProductCost = new ArrayList<>();

parentLoop:
while (true) {
while (true) { // Для ревью разбивать не стал, боюсь всё поломать =), но в качестве факультатива разломаю на методы
System.out.println("Введите наименование товара.");
String product = scanner.nextLine();

Expand All @@ -55,7 +32,7 @@ public void start() {

if (cost > 0) {
validCost = true;
System.out.println("Добавлен товар:\n" + product + " - " + new DecimalFormat("0.00").format(cost) + " рубля.");
System.out.println("Добавлен товар:\n" + product + " - " + Formatter.chooseRublesWord(cost));
} else {
System.out.println("Ошибка:\nСумма должна быть больше нуля.");
}
Expand All @@ -79,7 +56,7 @@ public void start() {
break parentLoop;
} else {
System.out.println("Ошибка:\nВы ввели неверное значение." + "\nВведите команду \"Завершить\" чтобы выполнить расчет.");
continue childLoop;
continue; // Убрал лейбл "childLoop"
}
}
}
Expand All @@ -91,7 +68,7 @@ public void start() {
}

double costPerPerson = totalCost / persons;
System.out.println("Всего:\n" + new DecimalFormat("0.00").format(totalCost) + " рублей.");
System.out.println("Каждый должен заплатить по:\n" + new DecimalFormat("0.00").format(costPerPerson) + " рублей.");
System.out.println("Всего:\n" + Formatter.chooseRublesWord(totalCost));
System.out.println("Каждый должен заплатить по:\n" + Formatter.chooseRublesWord(costPerPerson));
}
}
}
33 changes: 12 additions & 21 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
public class Formatter {
public static String formatAmount(double amount) {
int rubles = (int) amount;
int kopecks = (int) ((amount - rubles) * 100);
public class Formatter { // Я и правда забыл им воспользоваться, обрадовался что код без ошибок компилится и забыл =)
public static String chooseRublesWord(double total) {

String rublesWord = chooseRublesWord(rubles);
String rubles = String.format("%.2f", total); // Сначала переписал "formatAmount" со "StringBuilder", но отказался в пользу более простого способа

if (kopecks == 0) {
return rubles + " " + rublesWord;
} else if (kopecks < 10) {
return rubles + "." + "0" + kopecks + " " + rublesWord;
} else {
return rubles + "." + kopecks + " " + rublesWord;
}
}
private static String chooseRublesWord(int rubles) {
int lastDigit = rubles % 10;
int penultimateDigit = (rubles / 10) % 10;
double iki = Math.floor(total) % 100; // Переписал прошлые условия, так как ловил неверное окончание при сумме "1.nn"
double bir = Math.floor(total) % 10;
String currency = " рубл";

if (penultimateDigit == 1 || lastDigit == 0 || (lastDigit >= 5 && lastDigit <= 9)) {
return "рублей";
} else if (lastDigit == 1) {
return "рубль";
if (bir == 0 || (bir >= 5 && bir <= 9) || (iki >= 10 && iki <= 20)) {
currency += "ей";
} else if (bir > 1 && bir < 5) {
currency += "я";
} else {
return "рубля";
currency += "ь";
}
return rubles + currency;
}
}
2 changes: 1 addition & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void main(String[] args) {
System.out.println("На скольких человек необходимо разделить счет?");
if (scanner.hasNextInt()) {
persons = scanner.nextInt();
if (persons > 1) // Введеное значение должно быть целым числом больще 1
if (persons > 1)
break;
else {
System.out.println("Ошибка:\n" + "Введенное значение должно быть больше 1-го!");
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Product { // Вынес из "Calculator"
private final String name;
private final double price;

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

public String getName() {
return name;
}

public double getPrice() {
return price;
}

public String toString() {
String formattedPrice = Formatter.chooseRublesWord(price);
return name + " - " + formattedPrice;
}
}