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
58 changes: 58 additions & 0 deletions src/main/java/Calculation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
public class Calculation {

String productList = "Добавленные товары:";
int numberPersons;
double totalPrice;

Calculation(int numberPersons) {
this.numberPersons = numberPersons;
}

public void addProduct(String product, double price) {
productList = String.format("%s\n%s стоимость: %.2f %s", productList, product, price, getEnding(price));
totalPrice = totalPrice + price;
}

public String divideCheck() {

if (numberPersons == 0) {
return "Ошибка: нет персон";
}

double result = totalPrice / numberPersons;

return String.format("С одного человека: %.2f %s", result, getEnding(result));
}

public String getEnding(double number){

int lastNumber = (int) Math.floor(number);

if (lastNumber > 100) {
lastNumber = lastNumber % 100;
}

if (lastNumber > 20) {
lastNumber = lastNumber % 10;
}

String ending;

switch (lastNumber) {
case 1:
ending = "рубль";
break;
case 2:
case 3:
case 4:
ending = "рубля";
break;
default:
ending = "рублей";
break;
}

return ending;

}
}
60 changes: 58 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🍏 Немного поехало форматирование. Для форматирования кода и удаления неиспользуемых импортов применяй следующие сочетания клавиш:
Windows: Control+Alt+L
MacOS: Command+Option+L
Больше горячих клавиш можно найти здесь: https://developer.android.com/studio/intro/keyboard-shortcuts


int numberPersons;

while (true) {
if (scanner.hasNextInt()) {
numberPersons = scanner.nextInt();
if (numberPersons > 1) {
break;
} else {
System.out.println("Ошибка! Введено меньше 2. Введите корректное количество гостей. Для закрытия программы напишите \"выход\"");
}
} else {
String exit = scanner.next();
if (exit.equalsIgnoreCase("выход")) {
System.out.println("Завершена работы программы!");
return;
} else {
System.out.println("Ошибка! Введено не число. Введите корректное количество гостей. Для закрытия программы напишите \"выход\"");
scanner.nextLine();
}
}
}

Calculation calculation = new Calculation(numberPersons);

while (true) {

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.println("Введите название товара. Для закрытия программы напишите \"завершить\""); // можно сделать через useDelimeter(). думаю пока как обработать несколько пробелов
String product = scanner.next();
if (product.equalsIgnoreCase("завершить")) {
break;
} else {
while (true) {
System.out.println("Введите стоимость товара (руб,коп).");
try {
double price = scanner.nextDouble();
if (price > 0) {
calculation.addProduct(product, price);
break;
} else {
System.out.println("Ошибка! Введено отрицательное значение!");
}
} catch (Exception err){
System.out.println("Ошибка! Введено не число");
scanner.nextLine();
}
}
}
}

System.out.println(calculation.productList);
System.out.println(calculation.divideCheck());
}
}
}