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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.util.Locale;
import java.util.Scanner;

public class Calculator {
private Scanner scanner = new Scanner(System.in);
private String dishes = "Добавленные товары:\n ";
private double summary = 0;

void addDishes() {
while (true) {
System.out.println("Введите название продукта.");
dishes += scanner.next() + " \n";

System.out.println("Теперь введите его стоимость в формате [руб.коп.].");

while (!scanner.hasNextDouble()) {
scanner.next();
System.out.println("Ошибка, введите корректное число.");
}
while (true) {
double i = scanner.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.

Лучше называть переменные так, чтобы при взгляде на них было сразу понятно, что они означают. Например, тут использовать имя price

if (i <= 0) {
System.out.println("Введите корректную стоимость продукта.");
} else {
summary += i;
break;
}
}
System.out.println("Успех! \nХотите ли добавить еще продукт?");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тут лучше дописать "Введи Завершить, чтобы закончить ввод", иначе пользователь не поймёт, что следует ввести для выхода


String answer = scanner.next();
if (answer.equalsIgnoreCase("завершить")) {
break;
}
}
}

void showResult(int guests) {
double result = summary / guests;
String rubleTitle = rubleCase(result);
System.out.println(dishes);
System.out.println(String.format("%.2f", result) + " " + rubleTitle);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно добавить больше информации в вывод, например "Каждый человек должен заплатить ..."

}

private String rubleCase(double money) {
int floorMoney = (int) Math.floor(money);
int lastDigit = floorMoney % 10;
if (lastDigit == 1) {
return "рубль ";
}
else if (lastDigit == 2 || lastDigit == 3 || lastDigit == 4) {
return "рубля";
}
else {
return "рублей ";

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-else можно переписать на switch

}

}
}


38 changes: 35 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
import java.util.Scanner;

public class Main {


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Лучше называть переменные так, чтобы при взгляде на них было сразу понятно, что они означают. Например, тут использовать имя person

Scanner scanner = new Scanner(System.in);
System.out.println("Доброго дня!\nНа скольких человек необходимо разделить счёт?");

while (!scanner.hasNextInt()) {
scanner.next();
System.out.println("Ошибка, введите число.");

}

while (true) {
i = scanner.nextInt();
if (i <= 1) {
System.out.println("Введите корректное количество гостей.");
}

if (i > 1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Это условие можно перенести в виде else к предыдущему if

System.out.println();
break;
}
}


Calculator calculator = new Calculator();
calculator.addDishes();
calculator.showResult(i);


}
}