Skip to content
Merged
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
18 changes: 18 additions & 0 deletions src/main/java/GetRubleAddition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Rubles {
public static String GetRubleAddition(int num) {
int preLastDigit = num % 100 / 10;
if (preLastDigit == 1) {
return "рублей";
}
switch (num % 10) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
}
57 changes: 53 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
// dev branch for Y.Practicum
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Scanner scanner = new Scanner(System.in);
System.out.println("На сколько персон разделить счет?"); //Количество людей

while (true) {
int people = scanner.nextInt();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ У Scanner есть удобный метод hasNextInt, можно сразу узнать без дополнительных проверок возможно ли интерпретировать строку как int. В данном случае без этой проверки при введение пользователем строки приложение выбросит исключение

if (people == 1) {
System.out.println("Хотите разделить свою еду?\nДобавьте больше людей!");
}
if (people < 1) {
System.out.println("Неправильное количество!\nДобавьте больше людей!");
}
if (people > 1) {
System.out.println("Давайте подсчитаем!");
Double value =(GroceryList.Product()/people);
String result = String.format("%.2f",value);

System.out.println("Сумма с человека: "+result+" "+Rubles.GetRubleAddition((int)Math.floor(value)));
break;
}
}
}
}
class GroceryList {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Лучше вынести в отдельный файл


public static double Product() {

double sum = 0.0;
StringBuilder products = new StringBuilder(("Добавить блюда")); //Обьявить блюда

while (true) {
String name;
Scanner nameProduct = new Scanner(System.in);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Лучшее переиспользовать инстанс Scanner, а не создавать каждый раз новый.

System.out.println("Введите название блюда"); // Ввести название блюда
name = nameProduct.nextLine();

if (name.equalsIgnoreCase("Завершить")) { //Подсчет итога
System.out.println(products);
break;
}

double price;
System.out.println("Введите стоимость блюда"); // Ввести стоимость
price = nameProduct.nextDouble();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ И тут та же проблема


if (price > 0) {
products.append("\n").append(name).append(" по цене ").append(price);
sum += price;
System.out.println("Блюдо добавлено");
}

}
return sum;
}
}