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

import java.util.Scanner;

public class Calculator {
StringBuilder productList = new StringBuilder("Добавленные товары:");
double productListPrice;
String guess = "";
double productPrice;

private void readProductName(Scanner scanner, Product product) {
System.out.println("Введите название товара:");
product.name = scanner.nextLine();
}

private void readProductPrice(Scanner scanner, Product product) {
System.out.println("Введите стоимость товара:");
while (true) {
if (scanner.hasNextDouble()) {
productPrice = scanner.nextDouble();
if (productPrice > 0) {
product.price = productPrice;
scanner.nextLine();
break;
} else {
System.out.println("Цена не может быть отрицательной или нулевой, введите снова");
}

} else {
System.out.println("Некорректные данные.");
scanner.next();
}
}
}

private void calculateTotalPrice(Product product) {
productListPrice += product.price;
}

void addNewProduct(Scanner scanner) {
while (!guess.equalsIgnoreCase("завершить")) {
Product product = new Product();
readProductName(scanner, product);
readProductPrice(scanner, product);
productList.append("\n").append(product.name);
calculateTotalPrice(product);
System.out.println("Товар \"" + product.name + "\" успешно добавлен.\n" +
"Хотите добавить ещё товар?");
guess = scanner.nextLine();
}
}

void printProductList(int people) {
String roundedPrice = PriceFormat.roundPrice(productListPrice / people);

Choose a reason for hiding this comment

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

Лучше добавить проверку параметра people, чтобы случайно не поделить на 0, если в будушем эту функцию будет кто-то переиспользовать

System.out.println(productList.toString());
System.out.println("С каждого человека " + roundedPrice+ " " + PriceFormat.rubInflection(productListPrice / people));

Choose a reason for hiding this comment

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

productListPrice / people можно посчитать 1 раз, записать результат в переменную и ей пользоваться

}
}

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

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
PeopleAmount peopleAmount = new PeopleAmount();
Calculator calculator = new Calculator();
int people = peopleAmount.readPeopleAmount(scanner);
calculator.addNewProduct(scanner);
calculator.printProductList(people);

}
}
24 changes: 24 additions & 0 deletions src/main/java/PeopleAmount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.Scanner;

public class PeopleAmount {

Choose a reason for hiding this comment

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

Я бы назвал класс PeopleAmountReader, так будет понятнее, за что он отвечает

int readPeopleAmount(Scanner scanner) {
int amount;
System.out.println("На скольких человек делим счёт?");
while(true) {
if (scanner.hasNextInt()) {
amount = scanner.nextInt();
if (amount <= 1) {
System.out.println("Количество человек должно быть больше одного.");
} else {
scanner.nextLine();
return amount;
}
} else {
System.out.println("Количество человек болжно быть введено в виде числа.");
scanner.next();
}
}

}

}
15 changes: 15 additions & 0 deletions src/main/java/PriceFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class PriceFormat {
static String roundPrice(double price) {
return String.format("%.2f", price);
}

static String rubInflection(double price) {
if (price % 10 == 1 && price % 100 != 11) {
return "рубль";
} else if (price % 10 >= 2 && price % 10 <= 4 && (price % 100 < 10 || price % 100 >= 20)) {
return "рубля";
} else {
return "рублей";
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class Product {
String name;
double price;
}