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
62 changes: 62 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.util.Scanner;

public class Calculator {
double totalPrice;
double priceForPerson;
double price;
int numberOfPeople = 1;
String productList = "Добавленные товары:";


void priceForPerson() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Имена функций должны содержать в себе действие. Функция делает что-либо. Хорошо помогает практика переводить имена функций дословно, если после перевода понятно чем занимается функция, то название хорошее

priceForPerson = totalPrice / numberOfPeople;
Formatter formatter = new Formatter();
formatter.formatRub(priceForPerson);
System.out.println(String.format("Каждый человек должен заплатить %.2f %s", priceForPerson, formatter.rub));
}

void addProduct() {
System.out.println("Введите наименование товара");
Scanner scanner = new Scanner(System.in);
String input="";
while (true) {
if(input.isEmpty()) {
input = scanner.next();
}
productList = productList + "\n" + input;
System.out.println("Введите цену товара в формате рубли,копейки");
price = scanner.nextFloat();
totalPrice = totalPrice + price;
System.out.println("Товар добавлен");
System.out.println("Для продолжения введите наименование следующего товара. Для окончания подсчёта введите \"Завершить\"");
input = scanner.next();
if (input.equalsIgnoreCase("Завершить")) {
System.out.println(productList);
break;
}


}

}
void getNumberOfPeople () {
System.out.println("Введите число человек, на которых необходимо разделить счёт");
Scanner scanner = new Scanner(System.in);
while (true) {
if (scanner.hasNextInt()) {
numberOfPeople = scanner.nextInt();
if (numberOfPeople > 1) {
break;
} else {
System.out.println("Некорректное число. Порпробуйте снова");
}
} else {
System.out.println("Неоходимо ввести число");
scanner.next();
}


}
}

}
18 changes: 18 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Formatter {
String rub ="";
public void formatRub(double price) {
int form = (int) Math.floor(price);
if (form % 100 >= 11 && form % 100 <= 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.

Здесь гораздо лучше подойдет оператор switch/case, так как количество веток if-else больше 2

rub = "рублей";
} else if (form % 10 == 1 && form >11 || form==1) {
rub = "рубль";
} else if (form % 10 >= 2 && form % 10 <= 4 || form >= 2 && form <= 4) {
rub = "рубля";
} else if (form % 10 >= 5 && form % 10 <= 9 || form >= 5 && form <= 9) {
rub = "рублей";
} else if (form % 10 == 0) {
rub = "рублей";
}

}
}
8 changes: 7 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calculator = new Calculator();
calculator.getNumberOfPeople();
calculator.addProduct();
calculator.priceForPerson();
}


}