Skip to content
Open

HW #66

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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

public class Main {

public static void main(String[] args) {
// ваш код начнется здесь

// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Scanner scanner = new Scanner(System.in);
//считываем кол-во гостей
int visitor = 0;
System.out.println ("Введите колличество гостей");
while (true) {
String str = scanner.next();
while (!isNumeric(str)) {
System.out.println("Введите колличество гостей");

@MagicUnderHood MagicUnderHood Oct 5, 2022 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно перенести эту строчку наверх, сразу после while, тогда отсюда и перед while её можно удалить

str = scanner.next();
}
visitor = Integer.parseInt(str);
if ((visitor <= 1) | (visitor == 0)) {
System.out.println("Некорректное кол-во гостей");
System.out.println("Введите колличество гостей");
} else if (visitor > 1) {
break;
}
}
//считываем кол-во товаров
boolean enough = true;

Product list = new Product();
Calculate menu_list = new Calculate();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Для именования переменных лучше использовать верблюжий регистр, то есть menuList

while (enough) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно просто прописать while(true)

System.out.println("Введите название ");
list.name = scanner.next();
System.out.println("Введите цену");
String str = scanner.next();
while (!isNumeric(str)) {
System.out.println("Введите цену с разделителем \".\" ");
str = scanner.next();
}
list.price = Double.parseDouble(str);
menu_list.getAddList(list.name, list.price);
menu_list.getListPrice(list.price);
System.out.println("Хотите ввести ещё товар?");
System.out.println("Eсли вы хотите ввести ещё товар, введите ДА");
System.out.println("Eсли вы хотите ввести ещё товар, введите Завершить ");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Наверное, тут должно быть "Если вы не хотите больше вводить товары, введите Завершить"

String answer = scanner.next();

if (answer.equalsIgnoreCase("завершить")) {
enough = false;
break;
}
}

if (!enough){

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 можно убрать, и переменную enough тоже. Из цикла while может быть выход, только если пользователь введет Завершить, и тогда должен будет выполниться код здесь

System.out.println("Добавленные товары:");
System.out.println(menu_list.receipt);
float pay = (float) (menu_list.pay/visitor);
String rubString = "";
int rub = (int) (pay % 10);
int rubTeen = (int) (pay % 100);
if ((rub == 0 ) || (rub == 5) || (rub == 6) || (rub == 7 ) || (rub == 8 ) || (rub == 9 ) || ((rubTeen >= 11) && (rubTeen <= 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.

Можно упростить условие - rub == 0 || (rub >= 5 && rub <= 9) || (rubTeen >= 11 && rubTeen <= 14)

rubString = "рублей";
} else if ((rub == 2) || (rub == 4) || (rub == 3) ){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно упростить - rub >= 2 && rub <= 4

rubString = "рубля";
} else if (rub == 1) {
rubString = "рубль";
}

String message = "Каждый должен заплатить %.2f %s";
System.out.println(String.format(message, pay, rubString));
}
}
public static class Calculate{
String receipt = "";
double pay = 0;

public void getAddList (String name, double price) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Этот и getListPrice методы называются "get", то есть "получить", но ничего не возвращают. Лучше назвать "addProduct" и "addPrice". Ещё можно передавать в параметре этого метода сразу объект класса Product, и из него брать имя и цену

receipt = receipt + String.format("%s %.2f \n",
name,
price);
System.out.println("Товар успешно добавлен");
}

public void getListPrice(double price){
pay = pay + price;
}
}

public static class Product {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Классы лучше в отдельный файл располагать, и тогда не надо будет делать их статическими

String name;
double price;
}

public static boolean isNumeric(String string) {
double value;

if(string == null || string.equals("")) {
return false;
}
try {
value = Double.parseDouble(string);
return true;
} catch (NumberFormatException e) {

}
return false;
}
}