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

public class Calculator {
ArrayList<Product> listProduct = new ArrayList<>();

public void addProduct(Product product) {
listProduct.add(product);
}

public double amountGuest(int n) {
double sum = 0;
for (Product i : listProduct) {
sum += i.getPrice();
}
return sum / n;
}

public double totalAmount() {
double sum = 0;
for (Product i : listProduct) {
sum += i.getPrice();
}
return sum;
}

public void printListProduct() {
int index = 1;
System.out.println("Добавленные товары:\n-------------------");

for (Product i : listProduct) {
System.out.println(index++ + ": " + i.getName() + " " + Formatter.floorCount(i.getPrice()));
}

System.out.println("-------------------");
}
}
20 changes: 20 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Formatter {
public static String format(Double price) {
String resultPrice;
int floorPrice = (int) Math.floor(price);

if (floorPrice == 11 || floorPrice % 100 == 11 || floorPrice % 10 >= 5 || floorPrice % 10 == 0) {
resultPrice = "рублей";
} else if (floorPrice % 10 == 1) {
resultPrice = "рубль";
} else {
resultPrice = "рубля";
}

return floorCount(price) + " " + resultPrice;
}

public static String floorCount(double n) {
return String.format("%.2f", n);
}
}
74 changes: 72 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,76 @@
import java.util.Scanner;

public class Main {
public static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calculator = new Calculator();
System.out.println("На скольких человек необходимо разделить счёт?");
int numberPeople = getNumberGuest();
addProduct(calculator);
calculator.printListProduct();
System.out.println("Количество гостей: " + numberPeople);
System.out.println("Общая сумма: " + Formatter.format(calculator.totalAmount()));
System.out.println("Сумма к оплате на каждого гостя: "
+ Formatter.format(calculator.amountGuest(numberPeople)));
scanner.close();
}

public static int getNumberGuest() {
int numberPeople;
do {
while (true) {
try {
numberPeople = Integer.parseInt(scanner.next());
break;
} catch (NumberFormatException e) {
System.out.println("Некорректное значение, пожалуйста повторите ввод.");
}
}

if (numberPeople == 0 || numberPeople == 1) {
System.out.println("Ошибка. Количество" +
" человек должно быть минимум двое!\n" +
"Введите корректное количество гостей:");
} else if (numberPeople < 0) {
System.out.println("Ошибка. Количество" +
" человек не может быть отрицательным!\n" +
"Введите корректное количество гостей:");
}
} while (numberPeople <= 1);

return numberPeople;
}
}

public static void addProduct(Calculator calculator) {
do {
System.out.println("Название:");
String name = scanner.next();
System.out.println("Cтоимость:");
double price;

while (true) {
try {
price = scanner.nextDouble();

if (price < 0) {
System.out.println("Некорректное значение, пожалуйста повторите ввод.");
} else {
break;
}

} catch (Exception e) {
System.out.println("Некорректное значение, пожалуйста повторите ввод.");
scanner.nextLine();
}


}

calculator.addProduct(new Product(name, price));
System.out.println("Товар успешно добавлен.\nХотите добавить еще один товар? ");
} while (!scanner.next().equalsIgnoreCase("Завершить"));
}


}
19 changes: 19 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Product {
private final String name;
private final double price;

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

public String getName() {
return name;
}

public double getPrice() {
return price;
}


}