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

public class Calculator {
int people;
float total = 0;
ArrayList<Product> products = new ArrayList<>();
Calculator(int people){
this.people = people;
}
public void addProduct(Product newProduct){
products.add(newProduct);
total += newProduct.price;
}
public void print(){
System.out.println("Добавленные товары:");
for (int i = 0; i < products.size(); i ++){
System.out.println(String.format("Продукт %s с ценой в %.2f %s", products.get(i).name, products.get(i).price, rubleFormat(products.get(i).price)));
}
System.out.println(String.format("\nИтого: %.2f %s", total, rubleFormat(total)));
float perPerson = total / (float) people;
System.out.println(String.format("Получается %.2f %s с человека", perPerson, rubleFormat(perPerson)));
}

public String rubleFormat(float price){
int check = (int) price;
String result = "ОшИбКаааааААаАаА";
if (check % 100 >= 11 && check % 100 <= 19){
result = "рублей";
}
else if (check % 10 == 1){
result = "рубль";
}
else if (check % 10 >= 2 && check % 10 <= 4) {
result = "рубля";
}
else{
result = "рублей";
}

return result;
}
}
64 changes: 63 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calculator;
int peopleCount;
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("На скольких человек необходимо разделить счёт?");

if (scanner.hasNextInt()){
peopleCount = scanner.nextInt();
scanner.nextLine();
}
else {
System.out.println("Некорректное значение для подсчёта. Повторите попытку");
scanner.nextLine();
continue;
}

if (peopleCount == 1){
System.out.println("Зачем что то считать для одного человека?) Повторите попытку");
}
else if (peopleCount < 0) {
System.out.println("Некорректное значение для подсчёта. Повторите попытку");
}
else if (peopleCount > 1){
calculator = new Calculator(peopleCount);
System.out.println("Ок, начинаем считать!");
break;
}
}

String name;
float price;
while (true){
System.out.println(
"Добавим товар в список! \n" +
"Введите название товара:");
name = scanner.nextLine();
System.out.println("Введите цену товара в формате [рубли],[копейки], например 10,45 или 11,40:");

if (scanner.hasNextInt()){
price = scanner.nextFloat();
scanner.nextLine();
}
else{
System.out.println("Некорректное значение цены. Повторите попытку");
scanner.nextLine();
continue;
}

if (price < 0){
System.out.println("Некорректное значение цены. Повторите попытку");
continue;
}

calculator.addProduct(new Product(name, price));
System.out.println(String.format("Товар %s с ценой %.2f %s добавлен!", name, price, calculator.rubleFormat(price)));
System.out.println("Если вы хотите завершить процесс добавления товаров, введите 'Завершить'");
if (scanner.nextLine().equalsIgnoreCase("Завершить")){
break;
}
}

calculator.print();
}
}
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 {
final String name;
final float price;
Product(String name, float price){
this.name = name;
this.price = price;
}
}