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

public class Calculator {
private double sum;
private Scanner scaner = new Scanner(System.in);
private ArrayList<Product> products = new ArrayList<>();
private String iputName() {
System.out.println("Введите название товара");
String name = scaner.next();
return name;
}

private double inputCost() {
while (true) {
System.out.println("Введите стоимость");
if (scaner.hasNextDouble()) {
double cost = scaner.nextDouble();
if (cost <= 0) {
System.out.println("Стоимость не может быть меньше 0");
scaner.nextLine();
continue;
}
return cost;
} else {
System.out.println("Ошибка, попробуйте еще раз");
scaner.nextLine();
}
}
}

public void addProduct() {
while (true) {
System.out.println("Добавление товара в калькулятор");
Product product = new Product(iputName(), inputCost());
products.add(product);
sum += product.cost;
System.out.println(String.format("Товар %s успешно добавлен!", product.name));
System.out.println("Продолжить добовление товара?");
System.out.println("Ввести команду \"Завершить\" для того, чтоб завершить процесс добавления товаров");

String comand = scaner.next();
if (comand.equalsIgnoreCase("Завершить")) {
break;
}
}
}

public void showProducts() {
FormatterRub formatterRub = new FormatterRub();//для форматирования окончания рубля
System.out.println("Добавленные товары:");
String message = "Товар: %s цена: %.2f %s";
for (Product product : products) {
System.out.println(String.format(message, product.name, product.cost, formatterRub.correctEnding(product.cost)));
}
}

public void showResult(int countHuman) {
FormatterRub formatterRub = new FormatterRub();
System.out.println(String.format("Всего товаров на сумму: %.2f %s", sum, formatterRub.correctEnding(sum)));
double result = sum / countHuman;

String text = "Каждый должен заплатить: %.2f %s";
System.out.println(String.format(text, result, formatterRub.correctEnding(result)));
}

}
20 changes: 20 additions & 0 deletions src/main/java/FormatterRub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class FormatterRub {

public String correctEnding(double value) {
value = Math.floor(value);

value = value % 100;
if (value > 10 && value < 20) {
return "рублей";
} else {
value = value % 10;
if (value == 1) {
return "рубль";
} else if (value > 1 && value < 5) {
return "рубля";
} else {
return "рублей";
}
}
}
}
30 changes: 28 additions & 2 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!");

System.out.println("Добрый день");
Scanner scaner = new Scanner(System.in);
int input = 0;
while (true) {
System.out.println("На сколько человек необходимо разделить счёт?");
if (scaner.hasNextInt()) {
input = scaner.nextInt();
if (input == 1) {
System.out.println("Нет смысла ничего считать и делить");
} else if (input < 1) {
System.out.println("Это некорректное значение для подсчёта");
} else if (input > 1) {
Calculator cal = new Calculator();
cal.addProduct();
cal.showProducts();
cal.showResult(input);
break;
}
} else {
System.out.println("Ошибка, попробуйте еще раз");
scaner.nextLine();
}
}
}
}
}

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

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
return name.equals(product.name);
}

@Override
public int hashCode() {
int result = name == null ? 0 : name.hashCode();
return result;
}
}