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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.util.Locale;
import java.util.Scanner;

public class Calculator {
String listOfProducts;
double productPrice;

public Calculator() {
listOfProducts = "";
productPrice = 0.0;
}

public void addProduct() { //метод добавления товаров
while (true) {
Scanner in = new Scanner(System.in).useLocale(Locale.US);
System.out.println("Введите название товара: ");
String productName = in.nextLine();
if (productName.equalsIgnoreCase("Завершить")) {
break;
}
listOfProducts = productName + "\n";

System.out.println("Введите стоимость в формате: 'рубли.копейки' [10.45, 11.40]");
double price = inputPrice();
productPrice += price;
System.out.println("Товар успешно добавлен! Хотите добавить ещё один товар? Если да, то продолжайте писать ваш список. Если нет, то напишите \"Завершить\"");
}
}

public double inputPrice() {
while (true) {
Scanner in = new Scanner(System.in).useLocale(Locale.US);

if (in.hasNextDouble()) {
double price = in.nextDouble();

if (price >= 0) {
return price;
} else {
System.out.println("Стоимость не может быть отрицательная");
}
} else {
System.out.println("Некорректный ввод");
}
}
}

public static String getRubleAddition(double price)
{
int num = (int)Math.floor(price);
int preLastDigit = num % 100 / 10;
if (preLastDigit == 1)
{
return "рублей";
}

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

public void printInfo(int persona) {
System.out.println("Добавленные товары: \n" + listOfProducts);
System.out.printf("Стоимость товаров для каждого по отдельности: %.2f %s \n", productPrice / persona, getRubleAddition(productPrice / persona));
System.out.printf("Общая стоимость товаров: %.2f %s \n" , productPrice, getRubleAddition(productPrice));
}
}
33 changes: 29 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import java.util.Locale;
import java.util.Scanner;

public class Main {
private static final Scanner scanner = new Scanner(System.in).useLocale(Locale.US);

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
int people = addPeople();

}
static int addPeople() {
Calculator calc = new Calculator();
System.out.println("На скольких человек необходимо разделить счёт?");
int people=0;
while (true) {
if (scanner.hasNextInt()) {
people = scanner.nextInt();
} else {
scanner.next();
}
if (people > 1) {
System.out.println("Делим счет на " + people + "х");
calc.addProduct();
calc.printInfo(people);
break;
} else if (people == 1) {
System.out.println("Счет не делится на одного посетителя!");
}else
System.out.println("Некорректное значение, попробуйте снова!");
}
return people;
}
}
}