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

public class Calculator {


ArrayList<Item> itemList = new ArrayList<>();
private int personCount;

public Calculator(int count) {

this.personCount = count;
}


public void addItem(Item item) {
System.out.println("Товар " + item.name + " успешно добавлен");
itemList.add(item);

}

public void printTotalInfo() {

float totalPrice = 0f;
System.out.println("Список продуктов: ");
int count = 1;
for (Item item : itemList) {
totalPrice += item.price;
System.out.printf("%d)%s\n", count++, item.name);
}

float average = totalPrice / personCount;

System.out.printf("Общая сумма: %.2f %s\n", totalPrice, Formatter.getEnding(totalPrice));
System.out.printf("Каждый должен заплатить: %.2f %s", average, Formatter.getEnding(average));


}


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


public static String getEnding(float cost) {

int roundAmount = (int) Math.floor(cost);

if (roundAmount % 100 >= 11 && roundAmount % 100 <= 19) {
return "рублей";
}

int lastDig = roundAmount % 10;

switch (lastDig) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}


}


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

public String name;
public float price;

public Item(String name, float price) {
this.name = name;
this.price = price;
}

}
100 changes: 98 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,102 @@

import java.util.Scanner;


public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");

Scanner scanner = new Scanner(System.in);

System.out.println("Добро пожаловать в калькулятор счёта!");
int persCount = correctCountPerson();
Calculator calculator = new Calculator(persCount);

while (true) {

System.out.println("Введите название товара, если хотите завершить работу программы, то напишите 'Завершить'");

String itemName = scanner.nextLine();

if (!itemName.isBlank()) {


if (!itemName.equalsIgnoreCase("завершить")) {
System.out.println("Введите стоимость товара");
float itemCost = correctCost();
calculator.addItem(new Item(itemName, itemCost));

System.out.println("Хотите добавить ещё один товар? (да/нет)");
String choose = scanner.nextLine();

if (!choose.equalsIgnoreCase("да")) {
calculator.printTotalInfo();
break;
}
} else {
calculator.printTotalInfo();
break;
}

} else {
System.out.println("Название не может быть пустым!");
}


}
}
}


public static int correctCountPerson() {
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("Введите количество человек");
try {
int count = Integer.parseInt(scanner.nextLine());
if (count < 1) {
System.out.println("Некорректное количество человек");
} else if (count == 1) {
System.out.println("Не имеет смысла производить расчёты для одного человека");
} else {
return count;
}
} catch (NumberFormatException e) {
System.out.println("Введите целое число");
}

}
}


public static float correctCost() {
Scanner scanner = new Scanner(System.in);
float itemCost;

while (true) {
try {
itemCost = Float.parseFloat(scanner.nextLine());
if (itemCost > 0) {
break;
} else {
System.out.println("Стоймость не может быть отрицательной или равной нулю.");
}
} catch (NumberFormatException e) {
System.out.println("Введите корректное число для стоимости товара:");
}
}
return itemCost;
}
}