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
21 changes: 21 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Formatter {
public static String calc(double result) {
String rubleForm;
int sum = (int) Math.floor(result) % 100;
if (sum >= 11 && sum <= 14) {
rubleForm = "рублей";
} else {
sum %= 10;
if (sum == 1) {
rubleForm = "рубль";
}
else if (sum>1 && sum<5) {
rubleForm = "рубля";
}
else {
rubleForm = "рублей";
}
}
return rubleForm;
}
}
60 changes: 57 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,63 @@
import java.text.DecimalFormat;
import java.util.Scanner;

// dev branch for Y.Practicum
public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Scanner scanner = new Scanner(System.in);
double totalPrice = 0;
DecimalFormat two = new DecimalFormat("#.##");
int quantity;
while (true) {
System.out.println("На сколько человек разделить счет?");
while (!scanner.hasNextInt()) {
System.out.println("Введи целое число больше 1");
scanner.next();
}
quantity = scanner.nextInt();
scanner.nextLine();
if (quantity == 1) {
System.out.println("Нет смысла делить счет. Введи новое число");
}
if (quantity < 1) {
System.out.println("Как может прийти меньше 1 человека?! Дурак) Введи снова");
}
if (quantity > 1) {
System.out.println("Принимай заказ. Будем делить чек на " + quantity + "-х");
break;
}
}
StringBuilder totalName = new StringBuilder("Добавленные товары:\n");
while (true) {
System.out.println("Как называется блюдо?");
String name = scanner.nextLine();
double price = 0;
System.out.println("Сколько стоит блюдо (формат ХХ.ХХ)?");
while (price <= 0) {
while (!scanner.hasNextDouble()) {
System.out.println("Введи число!");
scanner.next();
}
price = scanner.nextDouble();
if (price <= 0) {
System.out.println("Введи нормальную стоимость");
}
}
scanner.nextLine();
totalPrice += price;
totalName.append(name + "\n");
System.out.println("Добавлено " + name + ". Его стоимость " + price + ". Хочешь добавить еще блюдо? Если нет - напиши завершить");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

В таких случаях для вывода лучше использовать System.out.printf, ему можно передать параметры, которые будут подставлены в строку.

String input = scanner.nextLine();
if (input.equalsIgnoreCase("завершить")) {
System.out.println(totalName);
break;
}
}
double result = totalPrice / quantity;
Formatter ruble = new Formatter();
System.out.println("По счету каждый платит: " + two.format(result) + " " + Formatter.calc(result));
}
}

// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости