Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Проектная работа No1 - Николаев #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
18 changes: 18 additions & 0 deletions src/main/java/Ending.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Ending {

String end(float num) {
Copy link

@faritowich faritowich Nov 12, 2022

Choose a reason for hiding this comment

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

В Java, да и не только, принято называть методы глаголами.
Почитать можно здесь:
https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html

UltravioletDays reacted with heart emoji
int num100 = (int) (Math.floor(num % 100));
if (num100 > 4 && num100 < 21) {
return "Рублей";
} else {
int num10 = num100 % 10;
if (num10 == 1) {
return "Рубль";
} else if (num10 > 1 && num10 < 5) {
return "Рубля";
} else {
return "Рублей";
}
}
}
}
58 changes: 54 additions & 4 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
import java.util.Scanner;

public class Main {

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

byte friends;
String order;
String table = "";
float price;
float total = 0.0f;
String exit;

while (true) {
System.out.println("Введите число участников:");
Scanner who = new Scanner(System.in);
if (who.hasNextByte()) {
friends = who.nextByte();
if (friends <= 1) {
System.out.println("Нет смысла делить счёт. Попробуйте ещё раз.");
} else {
break;
}
} else {
System.out.println("Требуется указать числовое значение!");
}
}
Copy link

@faritowich faritowich Nov 12, 2022

Choose a reason for hiding this comment

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

Большое количество сплошного кода в одном методе очень затрудняет читаемость кода. Лучше разбивать код на логические блоки и выносить в отдельные методы или классы. Например, можно вынести весь код в циклах while(true), который отвечает за ввод количества гостей/ввод продуктов, в отдельные методы inputGuests, inputProducts.

UltravioletDays reacted with heart emoji

while (true) {
System.out.println("Введите название блюда:");
Scanner input = new Scanner(System.in);
order = input.nextLine();
table += order + "\n";
System.out.println("Стоимость в формате 'руб,коп':");
if (input.hasNextFloat()) {
price = input.nextFloat();
if (price > 0) {
total += price;
System.out.println("Товар успешно добавлен!\nПродолжить? Да/Завершить");
exit = input.next();
if (exit.equalsIgnoreCase("Завершить")) {
break;
}
} else {
System.out.println("Отрицательное значение.");
}
} else {
System.out.println("Неверно указана сумма, попробуйте снова.");
}
}

Ending name = new Ending();

System.out.println("Добавленные товары:\n" + table);
System.out.println("Общая стоимость блюд: " + String.format("%.2f", total) + " " + name.end(total));
System.out.println("Сумма к оплате каждым участником: " + String.format("%.2f", total/friends) + " " + name.end(total/friends));
}
}
}

AltStyle によって変換されたページ (->オリジナル) /