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

public class Calculator {
ArrayList<Goods> goods = new ArrayList<>();
double totalBill;

public void setGoods(String name, double price) {
this.goods.add( new Goods(name, price));
this.totalBill += price;
}

public void printGoods() {
for (Goods good: goods) {
System.out.println("Добавленные товары:" + good.name + " " + good.price);
}
}

public double getSplitBill(int clients) {
return totalBill / clients;
}
}
18 changes: 18 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Formatter {
String roublesFormat;
double paid;

public String formatPaid(double paid) {
this.paid = Math.floor(paid) % 10;

if (this.paid == 1.0) {
this.roublesFormat = "рублю";
} else if (this.paid > 1.0 && this.paid <= 4.0) {
this.roublesFormat = "рубля";
} else {
this.roublesFormat = "рублей";
}

return String.format("%.2f " + this.roublesFormat, paid);
}
}
9 changes: 9 additions & 0 deletions src/main/java/Goods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Goods {
String name;
double price;

Goods(String name, double price) {
this.name = name;
this.price = price;
}
}
80 changes: 79 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,84 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
int clients = 0;
Calculator calculator = new Calculator();
Formatter formatter = new Formatter();


System.out.println("На сколько человек необходимо разделить счет?");

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\n");

while (true) {
if (scanner.hasNextInt()) {
clients = scanner.nextInt();

if (clients == 1) {
System.out.println("Считать нечего. Спасибо!");
break;
} else if (clients > 1) {
scanner.nextLine();
break;
} else {
System.out.println("Введено неверное значение. Просьба повторить еще.");
scanner.nextLine();
}
} else {
System.out.println("Введено неверное значение. Просьба повторить еще.");
scanner.nextLine();
}
}

if (clients > 1) {
String name;
double price;

System.out.println("Необходимо сообщить название товара и его стоимость.");

while (true) {
System.out.println("Введите название товара");
name = scanner.nextLine();

System.out.println("Введите стоимость товара");

while (true) {
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();

if (price > 0) {
scanner.nextLine();
break;
} else {
System.out.println("Введено неверное значение. Просьба повторить еще.");
scanner.nextLine();
}
} else {
System.out.println("Введено неверное значение. Просьба повторить еще.");
scanner.nextLine();
}
}

calculator.setGoods(name, price);

System.out.println("Товар " + name + " по цене " + price + " успешно добавлен");

System.out.println("Если товары закончились введите команду 'Завершить', если нет, то введите любой другой символ");

String answer = scanner.nextLine();

if (answer.equalsIgnoreCase("завершить")) {
System.out.println("Добавление товаров завершено.");
break;
}
}

calculator.printGoods();
System.out.println("Каждый должен заплатить по " + formatter.formatPaid(calculator.getSplitBill(clients)));
}

scanner.close();
}
}