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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
Курсовая работа Шаймарданов Марсель
54 changes: 54 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.Scanner;

public class Calculator {
static int persons = 0;
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 String menuSum = "";
static double priceSum = 0;
public static void calculator() {
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 scanner = new Scanner(System.in);
System.out.println("Введите количество человек:");
while (true) {
if (scanner.hasNextInt()) {
persons = scanner.nextInt();
if (persons < 2) {
System.out.println("Введите число больше 1");
} else {
break;
}
} else {
System.out.println("Введите целое число ");
scanner.next();
}
}
while (true) {
System.out.println("Введите название товара или введите 'Завершить', что бы выйти");
scanner.nextLine();
String menu = scanner.nextLine();
if (menu.equalsIgnoreCase("Завершить")) {
System.out.println("Товар успешно добавлен.");
break;
}
menuSum += menu + "\n";
while (true) {
System.out.println("Введите цену товара:");
if (scanner.hasNextDouble()) {
double price = scanner.nextDouble();
if (price > 0) {
priceSum += price;
break;
} else {
System.out.println("Введите число больше 0");
}
} else {
System.out.println("Введите число");
scanner.next();
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.close() после того, как сканнер больше не используется. Это необходимо для того, что бы этот объект не потреблял ресурсы, тогда когда это уже не требуется.

}
}
}
}
}





19 changes: 19 additions & 0 deletions src/main/java/FormatRub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class FormatRub {

static double rubAverage = Calculator.priceSum / Calculator.persons;

public static String formatRub(){
int priceRemainder = (int)Math.floor(rubAverage);
if (priceRemainder % 10==1)
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-3 условий лучше использовать оператор switch-case - это улучшит читабельность кода и его легче будет расширить или изменить в дальнейшем

return " рубль";
else if (priceRemainder % 10==2 || priceRemainder % 10==3 || priceRemainder % 10==4)
return " рубля";
else
return " рублей";
}





}
9 changes: 6 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Calculator.calculator();
String formatText = "Добавленные товары:\n%sКаждый человек должен заплатить: %.2f %s\n";
System.out.printf(formatText, Calculator.menuSum, FormatRub.rubAverage, FormatRub.formatRub());

}
}
}