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
25 changes: 21 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

public class Main {

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно try-catch разместить внутри бесконечного цикла while, тогда при вводе строки пользователю будет позволено снова ввести число, без перезапуска программы (как сделано с числами меньше 1)

int numPeople = scan.nextInt();
while (1 < 4) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно вместо 1 < 4 писать просто true

if (numPeople > 1) {
break;
}
System.out.println("Ошибка. Невозможно разделить чек на данное колличество людей. Попробуйте снова.");
numPeople = scan.nextInt();
}

double check = calc.calculator() / numPeople;
System.out.println("Каждый человек должен заплатить: " + check + calc.ending(check));
} catch (Exception e){
System.out.println("Ошибка. Попробуйте снова.");
}
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/calc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.util.Scanner;

public class calc {

public static double calculator() {
Scanner scan2 = new Scanner(System.in);
Scanner scan3 = new Scanner(System.in);
Scanner scan4 = new Scanner(System.in);
for (String i = "Добавленные товары:"; 1 != 0; i = 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.

1 != 0 можно заменить на true, а i = i убрать. Может, здесь for можно заменить на цикл while?

for (double j = 0; j >= 0; j = j) {
System.out.println("Пожалуйста, введите название товара.");
String itemName = scan2.nextLine();
System.out.println("Пожалуйста, введите цену товара в формате 'рубли.копейки' [10.45, 11.40]");
double itemCost = scan3.nextDouble();
System.out.println("Товар добавлен в список.");
i = i + "\n" + itemName + " " + itemCost + ending(itemCost);
j = j + itemCost;
System.out.println("Хотите ли бы вы завершить покупки?");
String answer = scan4.nextLine();
String stopWord = "Завершить";
if (answer.equalsIgnoreCase(stopWord)) {
System.out.println(i);
System.out.println("Общая сумма покупок: " + j + ending(j));
return j;
}
}
}
}
public static String ending(double j){
if(Math.floor(j) % 100 == 11 ^ Math.floor(j) % 100 == 12 ^ Math.floor(j) % 100 == 13 ^ Math.floor(j) % 100 == 14){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Math.floor(j) % 100 повторяется. можно посчитать один раз, записать в переменную и использовать её
Вместо ^ чаще используют написание ||
Можно условие заменить на Math.floor(j) % 100 >= 11 && Math.floor(j) % 100 <= 14

String stringe = " рублей";
return stringe;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно убрать переменную, написав просто return " рублей";, и ниже тоже

}
else {
double summ = Math.floor(j) % 10;
int remainder = (int) summ;
switch (remainder) {
case 1:
String string1 = " рубль";
return string1;
case 2:
String string2 = " рубля";
return string2;
case 3:
String string3 = " рубля";
return string3;
case 4:
String string4 = " рубля";
return string4;
default:
String stringdef = " рублей";
return stringdef;
}
}
}
}