Skip to content
Closed
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
57 changes: 57 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.Scanner;
public class Calculator {
Scanner scanner = new Scanner(System.in);
double sum;
String goods = "";
String currency;

void addGoods() {
while (true) {
System.out.println("Введите название товара: ");
String addProduct = scanner.next();
goods = goods + addProduct + "\n" ;
getPrice();
System.out.println("Хотите добавить еще один товар? Если нет, введите \"Завершить\"");
String answer = scanner.next();
if (answer.equalsIgnoreCase("Завершить")) {
break;
}
}
}

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" + goods);
double sumPerPerson = sum / numberOfPersons;
String sumToPay = String.format("Сумма к оплате на человека: %.2f", sumPerPerson);
int sumToInt = (int)Math.floor(sumPerPerson);
int secondLastDigit = (sumToInt % 100) / 10;
int lastDigit = sumToInt % 10;
if (lastDigit == 1 && secondLastDigit != 1) {
currency = " рубль.";
} else if (lastDigit <= 4 && lastDigit > 1 && secondLastDigit != 1) {
currency = " рубля.";
}else {
currency = " рублей.";
}
System.out.println(sumToPay + currency);
}
}
25 changes: 24 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import com.sun.source.tree.WhileLoopTree;
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 guestNumber;
while (true) {
if (scanner.hasNextInt()) {
guestNumber = scanner.nextInt();
if (guestNumber > 1) {
System.out.println("Спасибо! Сейчас рассчитаем ваше меню.");
break;
} else {
System.out.println("Ошибка! Вы ввели некорректное количество гостей! Пожалуйста, повторите попытку.");
}
} else {
System.out.println("Ошибка! Вы не ввели число! Пожалуйста, повторите попытку.");
scanner.next();
}
}
Calculator calculator = new Calculator();
calculator.addGoods();
calculator.calculatePayment(guestNumber);

}
}