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
42 changes: 28 additions & 14 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
public class Calculator {
Scanner scanner = new Scanner(System.in);
double sum;
String goodsList = "";
String goods = "";
String currency;

void addGoods() {
while (true) {
System.out.println("Введите название товара: ");
String addProduct = scanner.next();
goodsList = goodsList + "\n" + addProduct;

System.out.println("Введите цену товара:");
double productPrice = scanner.nextDouble();
sum += productPrice;

System.out.println("Товар успешно добавлен!");
goods = goods + addProduct + "\n" ;
getPrice();
System.out.println("Хотите добавить еще один товар? Если нет, введите \"Завершить\"");
String answer = scanner.next();
if (answer.equalsIgnoreCase("Завершить")) {
Expand All @@ -24,16 +19,35 @@ void addGoods() {
}
}

void getPrice() {
while (true) {
System.out.println("Введите цену товара:");
if (scanner.hasNextDouble()) {
double productPrice = scanner.nextDouble();
if (productPrice > 0) {
sum += productPrice;
System.out.println("Товар успешно добавлен!");
break;
} else {
System.out.println("Ошибка! Цена товара может быть только положительной!");
}
} else {
System.out.println("Ошибка! Введите корректную цену товара!");
scanner.next();
}
}
}

void calculatePayment(int numberOfPersons) {
System.out.println("Добавленные товары:\n" + goodsList);
System.out.println("Добавленные товары:\n" + goods);
double sumPerPerson = sum / numberOfPersons;
String sumToPay = String.format("Сумма к оплате на человека: %.2f", sumPerPerson);
int lastNumber = (int)Math.floor(sumPerPerson) % 10;
if ((int)Math.floor(sumPerPerson) > 10 && (int)Math.floor(sumPerPerson) < 21) {
currency = " рублей.";
} else if (sumPerPerson == 1) {
int sumToInt = (int)Math.floor(sumPerPerson);
int secondLastDigit = (sumToInt % 100) / 10;
int lastDigit = sumToInt % 10;
if (lastDigit == 1 && secondLastDigit != 1) {
currency = " рубль.";
} else if (lastNumber <= 4 && lastNumber > 1) {
} else if (lastDigit <= 4 && lastDigit > 1 && secondLastDigit != 1) {
currency = " рубля.";
}else {
currency = " рублей.";
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import com.sun.source.tree.WhileLoopTree;
import java.util.Scanner;


public class Main {
public static void main(String[] args) {
System.out.println("Добрый день! На сколько гостей желаете разделить счет?");
Scanner scanner = new Scanner(System.in);
int guestNumber;
while (true) {
guestNumber = scanner.nextInt();
if (guestNumber > 1) {
System.out.println("Спасибо! Сейчас рассчитаем ваше меню.");
break;
if (scanner.hasNextInt()) {
guestNumber = scanner.nextInt();
if (guestNumber > 1) {
System.out.println("Спасибо! Сейчас рассчитаем ваше меню.");
break;
} else {
System.out.println("Ошибка! Вы ввели некорректное количество гостей! Пожалуйста, повторите попытку.");
}
} else {
System.out.println("Ошибка! Вы ввели некорректное количество гостей! Пожалуйста, повторите попытку.");
System.out.println("Ошибка! Вы не ввели число! Пожалуйста, повторите попытку.");
scanner.next();
}
}
Calculator calculator = new Calculator();
Expand Down