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

public class Calculator {

ArrayList<Product> products = new ArrayList<>();
public void addProduct(Product product){
if (products.add(product))
System.out.println("Товар успешно добавлен");
}

public void printProducts() {
System.out.println("Добавленные товары:");
for (Product it : products){
System.out.println(it.name + ": " + it.price);
}
}

public void calculateForPersons(int persons) {
double sum = 0f;
for (Product it : products){
sum += it.price;
}
double total = sum / persons;
Formatter format = new Formatter(total);
System.out.println("Сумма, которую должен заплатить каждый: " + format.getStringPrice());
}
}
23 changes: 23 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Formatter {
double price;
public Formatter(double price){
this.price = price;
}
public String getStringPrice() {
double prc = Math.floor(price);
prc %= 100;
if (prc >= 5 && prc <= 20){
return String.format("%.2f", price) + " рублей";
}
else {
prc %= 10;
if (prc == 1) {
return String.format("%.2f", price) + " рубль";
} else if (prc > 1 && prc < 5){
return String.format("%.2f", price) + " рубля";
} else {
return String.format("%.2f", price) + " рублей";
}
}
}
}
48 changes: 47 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
int persons = -1;
Scanner scaner = new Scanner(System.in);
while (persons <= 1) {
System.out.println("Введите количество человек");
if (scaner.hasNextInt()) {
persons = scaner.nextInt();
if (persons <= 1){
System.out.println("Введено некорректное значение (должно быть больше 1)");
}
} else {
System.out.println("Введено некорректное значение (должно быть число)");
scaner.next();
}
}
System.out.println("Количество человек для расчета " + persons);

Calculator calculator = new Calculator();
while (true) {
System.out.println("Введите название товара (для завершения введите \"Завершить\"):");
String name = scaner.next();
if (name.equalsIgnoreCase("Завершить")) {
break;
} else {
double price = askForPrice(scaner);
calculator.addProduct(new Product(name, price));
}
}
scaner.close();
calculator.printProducts();
calculator.calculateForPersons(persons);
}
public static double askForPrice(Scanner scanerPrice) {
while (true) {
System.out.println("Введите стоимость товара (в формате рубли.копейки)");
if (scanerPrice.hasNextFloat()){
double price = scanerPrice.nextDouble();
if (price > 0) {
return price;
} else {
System.out.println("Стоимость не может быть меньше или равна нулю");
}
}else {
System.out.println("Формат стоимости должен быть: рубли.копейки");
scanerPrice.next();
}
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class Product {
double price;
String name;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
}