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
39 changes: 39 additions & 0 deletions src/main/java/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Класс корзина предназначен для сохранения введенных товаров,
а также суммирования их цен,
для последующего деления на колличество человек.
*/
import java.util.ArrayList;

public class Basket {
double sum = 0.00;
ArrayList<String> list = new ArrayList<String>();

//метод добавления введенных продуктов
public void addProduct(String product, double price){

list.add(product);
System.out.println("Товар добавлен в корзину.");
sum +=price;

}

// Метод для изменения падежа рубля.
public String convertRub (float sum){

String rub = "рублей";
int preSum =(int) sum % 100;
int lastsum = (int) sum % 10;

if(preSum > 10 && preSum <20)
rub ="рублей";
else if(lastsum >1 && lastsum < 5)
rub ="рубля";
else if(lastsum == 1)
rub ="рубль";
else
rub ="рублей";

return rub;
}
}
78 changes: 77 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,82 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
// Приветствие
System.out.println("Рады приветствовать вас в консольном приложении \"Калькулятор счета\"!");

//Получение входных данных о людях
Scanner scanner = new Scanner(System.in);
int quantityOfPersons = 0;
String nameOfProduct;
double priceOfProduct;


System.out.print("Введите колличество человек: ");

while (true) {
if (scanner.hasNextInt()) {
quantityOfPersons = scanner.nextInt();
if (quantityOfPersons > 1) {
break;
} else {
System.out.print("Ошибка,дальнейший расчет не имеет смысла.\nПовторите попытку ввода: ");
}
} else {
System.out.print("Недопустимый символ, введите число: ");
}
scanner.nextLine();
}
// Создаем корзину калькуляции покупок
boolean flag = true;
Basket basketCalc = new Basket();
System.out.println("Ваша корзина готова к заполнению.");

while (flag) {

System.out.print("Введите наименование товара: ");
nameOfProduct = scanner.next();

System.out.print("Введите цену товара (рубли, копейки): ");
while (true) {
if (scanner.hasNextDouble()) {
priceOfProduct = scanner.nextDouble();
if (priceOfProduct > 0) {
break;
} else {
System.out.print("Цена не может быть отрицательной. Попробуйте еще раз: ");
}
} else {
System.out.print("Ошибка, введите число: ");
}
scanner.nextLine();
}
//вызов метода добавления товара
basketCalc.addProduct(nameOfProduct, priceOfProduct);

while (true) {
System.out.print("Если желаете добавить еще товар введите \"Да\"\nЕсли желаете завершить введите \"Завершить\".\n");
String flagValue = scanner.next();
if (flagValue.equalsIgnoreCase("Завершить")) {
flag = false;
break;
} else if (flagValue.equalsIgnoreCase("да")) {
break;
} else {
System.out.println("Неверный ввод");
}
}
}

//вывод результата

System.out.println("Добавленные товары: ");
for (String el : basketCalc.list) {
System.out.println(el);
}
float lastSum = (float) basketCalc.sum / quantityOfPersons;
String rub = basketCalc.convertRub(lastSum);

System.out.printf("К оплате %.2f %s за человека.", lastSum, rub);
}
}