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
6 changes: 0 additions & 6 deletions src/main/java/Main.java

This file was deleted.

95 changes: 95 additions & 0 deletions src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.example;
import static org.example.NormFormat.returnRub;

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("На сколько человек необходимо разделить счет?");
int numPeople;
while (true) {
if (scanner.hasNextInt()) {
numPeople = scanner.nextInt();
if (numPeople == 1) {
System.out.println("На одного человека нет смысла делить счет. Попробуйте еще раз.");
} else if (numPeople < 1) {
System.out.println("Некорректное значение, попробуйте снова.");
} else {
break;
}
} else {
System.out.println("Некорректное значение, нужна цифра.");
scanner.next();
}
}

ArrayList<String> items = new ArrayList<>();
ArrayList<Double> cost = new ArrayList<>();
double sum = 0;

while (true) {
System.out.println("Введите название товара: ");
String itemName = scanner.next();
items.add(itemName);
if (itemName.equalsIgnoreCase("Завершить")) {
System.out.println("Итого: " + returnRub(sum));

double perPerson = sum / numPeople;
System.out.println("Каждый человек должен заплатить: " + returnRub(perPerson));

System.out.println("Список товаров:");
for (int i = 0; i < items.size(); i++) {
if (i < cost.size()) {
System.out.println(items.get(i) + " - " + returnRub(cost.get(i)));
}
}
scanner.close();
break;

}

System.out.println("Введите стоимость товара. Стоимость должна быть в формате рубли,копейки: ");
while (true) {
if (scanner.hasNextDouble()) {
double costItem = scanner.nextDouble();
if (costItem <= 0) {
System.out.println("Введите число больше нуля: ");
} else {
sum += costItem;
cost.add(costItem);
System.out.println("Товар " + itemName + " добавлен в корзину");
System.out.println("Общая стоимость: " + returnRub(sum));
break;
}
} else {
System.out.println("Введите корректное значение (цифры): ");
scanner.next();
}
}



System.out.println("Желаете добавить еще один товар? Введите \"да\"(или любой другой символ, слово) или \"Завершить\" чтобы завершить добавление товаров.");
String answer = scanner.next();

if (answer.equalsIgnoreCase("завершить")) {
System.out.println("Итого: " + returnRub(sum));

double perPerson = sum / numPeople;
System.out.println("Каждый человек должен заплатить: " + returnRub(perPerson));

System.out.println("Список товаров:");
for (int i = 0; i < items.size(); i++) {
if (i < cost.size()) {
System.out.println(items.get(i) + " - " + returnRub(cost.get(i)));
}
}
break;
}


}
scanner.close();
}
}
24 changes: 24 additions & 0 deletions src/main/java/org/example/NormFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example;
public class NormFormat {
public static String returnRub(double sum) {
int value = (int) sum % 100;
String rub = "";
if (value >= 10 & value <= 20) {
rub = "Рублей";
} else {
rub = switch (value % 10) {
case 1 -> "Рубль";
case 2, 3, 4 -> "Рубля";
case 5, 6, 7, 8, 9, 0 -> "Рублей";
default -> rub;
};
}
return String.format("%.2f",sum) + " " + rub;
}






}