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
16 changes: 16 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.Scanner;
class Calculator {
void calculatingCheckPerGuest(double total, int numberOfGuests) {
double checkPerGuest = total / numberOfGuests;
double roundingCheck = Math.floor(checkPerGuest);
double roundingCheck2 = roundingCheck - Math.floor(roundingCheck / 100) * 100;
double roundingCheck3 = roundingCheck - Math.floor(roundingCheck / 10) * 10;
if(roundingCheck3 ==1&&roundingCheck2 !=11) {
System.out.println(String.format("Каждый гость должен заплатить %.2f рубль", checkPerGuest));
} else if(roundingCheck3 >1&&roundingCheck3<5&roundingCheck2 !=12&roundingCheck2 !=13&roundingCheck2 !=14) {
System.out.println(String.format("Каждый гость должен заплатить %.2f рубля", checkPerGuest));
} else {
System.out.println(String.format("Каждый гость должен заплатить %.2f рублей", checkPerGuest));
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/Guests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Scanner;

class Guests {
public static int gettingToKnowNumberOfGuests() {
Scanner scanner = new Scanner(System.in);
int number = 0;

while (true) {
System.out.println("Введите кол-во гостей, на которых нужно разделить счет");
while (!scanner.hasNextInt()) {
System.out.println("Ошибка! Вы ввели текст! Вам нужно ввести целое число.");
scanner.next();
}

number = scanner.nextInt();
if (number > 1) {
break;
} else if (number < 0) {
System.out.println("Кол-во гостей не может быть отрицательынм");
} else {
System.out.println("Кол-во гостей не может быть меньше 1");
}
}
return number;
}
}
19 changes: 16 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import java.util.Scanner;

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

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

Guests guests = new Guests();
int numberOfGuests = guests.gettingToKnowNumberOfGuests();

Products products = new Products();
double total = products.calculatingTotalExpense();

Calculator calc = new Calculator();
calc.calculatingCheckPerGuest(total,numberOfGuests);
}
}




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

class Products {

Scanner scan = new Scanner(System.in);
boolean ifWantFinish = true;
String[] dish = new String[100];
int i =0;
double[] expense = new double[100];
double price = 0;
double total = 0;
double calculatingTotalExpense() {
while (ifWantFinish) {
System.out.println("Введите название товара");
dish[i] = scan.next();

expense[i] = checkingIfValueIsNumber();
total = total + expense[i];

System.out.println("Товар " + dish[i] + " стоимостью " + expense[i] + " руб. успешно добавлен!");
i++;
System.out.println("\nВы хотите ввести еще товары? Если да, то введите любой текст. Если нет, введите 'Завершить'");
ifWantFinish = !scan.next().equalsIgnoreCase("Завершить");
}
System.out.println("Добавленные товары:");
for (int l = 0; l < i; l++) {
System.out.println((l + 1) + ". " + dish[l] + " Цена " + expense[l] + " руб.");
}
return total;
}

double checkingIfValueIsNumber () {
while(true) {
System.out.println("Введите цену товара в формате рубли,копейки");
while (true) {
if (scan.hasNextDouble()) {
break;
Comment on lines +36 to +37
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠ Также лучше добавить проверку, что стоимость товара не может быть отрицательной - в прошлой итерации я об этом упомянул в PR. Если ввести -1, то программа продолжит работу и выведет в консоль добавленный товар с стоимостью -1.0 руб, что лучше поправить

} else {
System.out.println("Ошибка! Введите число!");
scan.next();
}
}
price = scan.nextDouble();
if (price <= 0) {
System.out.println("Цена блюда не может быть отрицательной и не может быть равна нулю");
} else {
break;
}
}
return price;
}


}