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
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.*;



public class Calculator {
public static final 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.

🍏 scanner не используется за пределами класса, значит можно убрать static и поменять модификатор на private

String productNames;
double sumMoney;
Calculator(String productNames, double sumMoney){
Comment thread
ralvveo marked this conversation as resolved.
this.productNames = productNames;
this.sumMoney = sumMoney;
}
void addProduct(){
System.out.println("Введите название нового товара ");
String nameOfProduct = scanner.next();


Comment on lines +16 to +17

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("Введите цену товара");
double priceOfProduct = checkInputPrice();



System.out.println("\nТовар успешно добавлен! \n");
Product prod = new Product(nameOfProduct, priceOfProduct);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🍏Это поле, как и класс Product больше нигде не используется, значит они не нужны в коде

productNames += nameOfProduct + "\n";
sumMoney += priceOfProduct;

}

//Проверка ввода корректной цены
public static double checkInputPrice(){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🍏static лучше убрать, советую почитать про static дополнительно

double userInputDouble;
while (true) {

String userInput = scanner.next();
if (isDouble(userInput)) {
userInputDouble = Double.parseDouble(userInput);
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.

🍏Ты 2 раза парсишь Double, лучше так не делать, а перестроить код так, чтобы это пришлось делать только 1 раз, например вытащить код из isDouble сюда и при ошибке выводить сообщение System.out.println("Введите корректную цену товара!");

if (userInputDouble > 0)
break;
else
System.out.println("Введите корректную цену товара больше нуля!");
} else
System.out.println("Введите корректную цену товара!");

}
return userInputDouble;
}


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🍏Необходимо оставлять только одну строку между методами

//Функция проверки на ввод дробного числа
public static boolean isDouble(String x) throws NumberFormatException

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🍏 throws NumberFormatException лишнее тут

{
try {
Double.parseDouble(x);
return true;
} catch(Exception e) {
return false;
}
}

}
86 changes: 84 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,88 @@
import java.util.*;

public class Main {
public static final Scanner scanner = new Scanner(System.in);



public static void main(String[] args) {
System.out.println("Hello world!");

//Запросили число гостей
System.out.println("Сколько у нас гостей?");
int peopleNumber = checkInputGuests();


Product prod = new Product("", 0);
Calculator calc = new Calculator("", 0);

//Бесконечный ввод товаров до завершения
String askToContinue;

while (true){
calc.addProduct();
System.out.println("Хотите продолжить ввод товаров?\nДля продолжения введите любой символ\nДля завершения введите \"Завершить\" в любом регистре\n");
askToContinue = scanner.next();
if (askToContinue.equalsIgnoreCase("Завершить"))
break;
}

//Подсчёт и вывод результатов
System.out.println("Добавленные товары:\n" + calc.productNames);

double oneGuestSum = calc.sumMoney / peopleNumber;
int OGS = (int) Math.floor(oneGuestSum);
String letter = checkCorrectLetter(OGS);
System.out.println(String.format("Каждый должен заплатить: %.2f рубл%s", oneGuestSum, letter));


}

//Поиск правильного окончания слова рубль
public static String checkCorrectLetter(int OGS){
int temp = OGS % 100;
if (temp % 10 == 1 && temp != 11)

@nasibullin1412 nasibullin1412 Sep 9, 2023 •

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 лучше использовать не ifы, а switch

return "ь";
else if ((temp % 10 == 2 || temp % 10 == 3 || temp % 10 == 4) && (temp != 12 && temp != 13 && temp!= 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.

🍏(temp % 10 == 2 || temp % 10 == 3 || temp % 10 == 4) эквивалентно (temp % 10 > 0 && temp % 10 < 5), (temp != 12 && temp != 13 && temp!= 14 эквивалентно (temp<12 || temp > 14)

return "я";
else
return "ей";
}
}


//Проверка ввода корректного числа гостей
public static int checkInputGuests(){
int userInputInt;
while (true) {

String userInput = scanner.next();
if (isInt(userInput)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🍏Тоже самое замечание, что и с isDouble

userInputInt = Integer.parseInt(userInput);
if (userInputInt> 1)
break;
else
System.out.println("Введите корректное число гостей больше 1!");
} else
System.out.println("Введите корректное число гостей!");

}
return userInputInt;
}

//Функция проверки на ввод целого числа
public static boolean isInt(String x) throws NumberFormatException
{
try {
Integer.parseInt(x);
return true;
} catch(Exception e) {
return false;
}
}






Comment on lines +81 to +86

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🍏Много лишних строк

}

9 changes: 9 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//Класс товаров
public class Product {
String name;
double price;
Product (String name, double price){
this.name = name;
this.price = price;
}
}