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

public class Calculator {
private final ArrayList<Product> products;
private double totalCost;


public Calculator() {
products = new ArrayList<>();
totalCost = 0;
}

public void addProduct() {
Scanner scanner = new Scanner(System.in);
System.out.print("Введите название товара: ");
String name = scanner.nextLine();

double price;
while (true) {
System.out.print("Введите стоимость товара (в формате рубли,копейки): ");
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price <= 0) {
System.out.println("Стоимость не может быть отрицательной или равна нулю, повторите ввод.");
} else {
break;
}
} else {
System.out.println("Не введена стоимость, повторите ввод.");
scanner.next();
}
}

Product product = new Product(name, price);
totalCost += price;
products.add(product);

System.out.println("Товар успешно добавлен!");
}

public void printProducts() {
if (products.size() == 0) {
System.out.println("Корзина пуста");
return;
}
System.out.println("Список добавленных товаров:");
for (int i = 0; i < products.size(); i++) {
String formattedPrice = Formatter.formatTotalCost(products.get(i).getPrice());
System.out.println((i+1) + ". " + products.get(i).getName() + " - " + formattedPrice);
}
String formattedTotalCost = Formatter.formatTotalCost(totalCost);
System.out.println("Общая стоимость товаров: " + formattedTotalCost);
}
public void makeBasketProducts() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Хотите добавить товар? (Введите Да или Завершить): ");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("Да")) {
addProduct();
} else if (answer.equalsIgnoreCase("Завершить")) {
break;
} else {
System.out.println("Неверный ответ. Попробуйте ещё раз.");
}
}
printProducts();
}

public double getTotalCost() {

return totalCost;
}
}
17 changes: 17 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Formatter {
public static String formatTotalCost(double totalCost) {
String formattedTotalCost;
int integerPart = (int) totalCost;
int decimalPart = (int) ((totalCost - integerPart) * 100);

if (integerPart % 10 == 1 && integerPart % 100 != 11) {
formattedTotalCost = integerPart + "." + String.format("%02d", decimalPart) + " рубль";
} else if (integerPart % 10 >= 2 && integerPart % 10 <= 4 && (integerPart % 100 < 10 || integerPart % 100 >= 20)) {
formattedTotalCost = integerPart + "." + String.format("%02d", decimalPart) + " рубля";
} else {
formattedTotalCost = integerPart + "." + String.format("%02d", decimalPart) + " рублей";
}

return formattedTotalCost;
}
}
34 changes: 30 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int numberOfGuests;

while (true) {
System.out.println("На скольких человек необходимо разделить счёт?");
if (scanner.hasNextInt()) {
numberOfGuests = scanner.nextInt();
if (numberOfGuests == 1) {
System.out.println("Нет смысла делить, вы пришли одни. Повторите попытку.");
} else if (numberOfGuests < 1) {
System.out.println("Некорректное значение для подсчета. Повторите попытку.");
} else {
break;
}
} else {
System.out.println("Введено не число. Повторите попытку.");
scanner.next();
}
}

Calculator calculator = new Calculator();
calculator.makeBasketProducts();

double totalCost = calculator.getTotalCost();
double costPerGuest = Math.round(totalCost / numberOfGuests * 100.0) / 100.0;
String formattedTotalCost = Formatter.formatTotalCost(costPerGuest);
System.out.println("Каждый гость должен заплатить: " + formattedTotalCost);
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Product {
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;
}
}