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


public void cheque(int number, double totalPrice){
//Вычисляем, сколько должен заплать каждый человек
double result = (float) totalPrice / (float) number;
int money = (int) Math.floor(result);

String str = String.format("%.2f", result);
//Вывод суммы, которую должен заплатить каждый поровну
if ((money % 100) / 10 == 1){
System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рублей");
}else if (money % 100 == 1){

Choose a reason for hiding this comment

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

Здесь лучше прописать money % 10 == 1, чтобы попадало 21, 51 и т.д.

System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рубль");
}else if (money % 10 >= 2 && money % 10 <= 4){

Choose a reason for hiding this comment

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

Выражение money % 10 считается несколько раз, можно посчитать его один раз, записать в переменную и использовать ее

System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рубля");
}else {
System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рублей");

Choose a reason for hiding this comment

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

Код System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рублей"); повторяется в каждой ветке, кроме слова рубль, лучше код вынести после if-else, оставив в нем только определение слова

}
}
}
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 @@
public class Main {

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

People people = new People();
//Ввод количества человек, на которых требуется разделить счет
System.out.println("На скольких человек необходимо разделить счёт?");
people.numberOfPeople();

//Добавление продуктов
Product product = new Product();
product.addProduct();

//Вывод суммы, которую должен заплатить каждый человек
Calculator calculator = new Calculator();
calculator.cheque(people.number, product.totalPrice);



}
}
30 changes: 30 additions & 0 deletions src/main/java/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.Scanner;
public class People {
int number = 0;

// Добавление количества человек, на которых необходимо разделить всю сумму
public void numberOfPeople(){

while (true){
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()){
int count = scanner.nextInt();
if (count > 1){
number = count;
break;
}else if (count == 1){
System.out.println("Количество человек, введенное вами, равно 1. Пожалуйста, введите значение больше 1.");
}else{
System.out.println("Вы ввели некорректное значение. Пожалуйста, введите значение больше 1.");
}

}else{
System.out.println("Вы ввели некорректное значение. Пожалуйста, введите значение больше 1.");

Choose a reason for hiding this comment

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

Можно текст "Пожалуйста, введите значение больше 1." записать в константу, он повторяется 3 раза



}


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

public class Product {

String productList = "";
String spacing = "\n";
String str = "Добавленные товары: ";
double totalPrice;

//Добавление товаров и их стоимости
public void addProduct(){
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название товара");
String name = scanner.nextLine();
productList += str + name + spacing;
System.out.println("Введите стоимость товара");
while (true){
if (scanner.hasNextDouble()){
double price = scanner.nextDouble();
totalPrice+=price;
System.out.println("Товар успешно добавлен");
anotherProduct();
break;
}else {
System.out.println("Вы ввели некорректное значение. Пожалуйста, введите корректное значение стоимости");
scanner.nextLine();
}
}
}



//Спрашиваем у пользователя - хочет ли он добавить еще один товар?
public void anotherProduct(){
Scanner scanner = new Scanner(System.in);
System.out.println("Хотите добавить ещё один товар?");
String fork = scanner.nextLine();

/*Если пользователь вводит "Да", то повторно вызываем метод addProduct
Иначе завершаем добавление продуктов и выводим список добавленных продуктов*/
if (fork.equalsIgnoreCase("Да")){
addProduct();

}else {
addedProducts(productList);
}

}

//Вывод добавленных продуктов
public void addedProducts (String lists){

System.out.println(lists);
}

}