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

public class Calculator {
String products;
double costOfGoods;

public Calculator() {
products = "";
costOfGoods = 0.0;
}


public void addProduct() {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название товара:");
String product = scanner.nextLine();
if (product.equalsIgnoreCase("Завершить")) {
break;
}
products = products + product + "\n";
System.out.println("Введите цену в формате: 'рубли.копейки' [10,45, 11,40]");
double cost = addCost();
costOfGoods += cost;
System.out.println("Товар успешно добавлен. \n Хотите добавить ещё товар? Если нет, до введите: \"Завершить.\"");
}
}

public double addCost() {
while (true) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextDouble()) {
double cost = scanner.nextDouble();
if (cost >= 0) {
return cost;
} else {
System.out.println("Некорректиный ввод. Цена должна быть > 0. Попробуйте еще раз:");
}
} else {
System.out.println("Некорректиный ввод. Введите цену в формате: 'рубли.копейки' [10,45, 11,40]:");
}
}
}

public static String rubleFormat(double cost) {
if ((int) cost % 100 / 10 == 1) {
return "рублей";
}

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

public void addOutput(int friends) {
System.out.println("Добавленные товары: \n" + products);
System.out.printf("Сумма, которую должен заплатить каждый человек: %.2f %s \n ", costOfGoods / friends, rubleFormat(costOfGoods / friends));
System.out.printf("Общий счет: %.2f %s \n ", costOfGoods, rubleFormat(costOfGoods));
}
}
32 changes: 28 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import java.util.Scanner;

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Студия подсказывает, что friends не используется. Можно из метода numberOfPersons вынести код

Calculator calculator = new Calculator();
calculator.addProduct();
calculator.addOutput(friends);

в функцию main, тогда будет логичнее

}

static int numberOfPersons() {
Calculator calculator = new Calculator();
System.out.println("На скольких человек необходимо разделить счёт:");
int friends = 0;
while (true) {
if (scanner.hasNextInt()) {
friends = scanner.nextInt();
} else {
scanner.next();
}
if (friends > 1) {
System.out.println("Делим счет на " + friends + "х");
calculator.addProduct();
calculator.addOutput(friends);
break;
} else if (friends == 1) {
System.out.println("Это некорректное значение для подсчёта. Нужно больше 1:");
} else
System.out.println("Это некорректное значение . Нужно больше число:");
}
return friends;
}
}
}