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
50 changes: 50 additions & 0 deletions src/main/java/BillCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.Scanner;
public class BillCalculator {
public void startBillCalculator() {
int people = enterNumberOfPeople();
Calculator calculator = new Calculator();
calculator.enterListOfItems();
System.out.println("Добавленные товары:");
calculator.printNamesItems();
double amountPerPerson = calculator.calculate(people);
printAmountPerPerson(amountPerPerson);
}

private int enterNumberOfPeople() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Введите количество людей");
if (scanner.hasNextInt()) {
int n = scanner.nextInt();
if (n > 1) {
return n;
}
}
scanner.nextLine();
System.out.println("Вы ввели некорректное количество людей");
}
}

private void printAmountPerPerson(double amountPerPerson) {
String ruble;
int intAmountPerPerson = (int) amountPerPerson;
if (intAmountPerPerson % 100 >= 10 && intAmountPerPerson % 100 <= 20) {
ruble = "рублей";
} else {
switch (intAmountPerPerson % 10) {
case 1:
ruble = "рубль";
break;
case 2:
case 3:
case 4:
ruble = "рубля";
break;
default:
ruble = "рублей";
}
}
System.out.printf("С каждого человека %.2f %s", amountPerPerson, ruble);
}

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


public class Calculator {
private final List<Item> list = new ArrayList<>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно более очевидно назвать переменную, например, products

public void enterListOfItems() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Введите название товара");
String name = scanner.nextLine();
if (name.equals("")) {
System.out.println("Вы не ввели название товара, попробуйте снова");
continue;
}
System.out.println("Введите цену товара");
double cost;
if (scanner.hasNextDouble()) {
cost = scanner.nextDouble();
scanner.nextLine();
if (cost < 0) {
System.out.println("Цена не может быть отрицательной, попробуйте снова");
continue;
}
} else {
System.out.println("Вы некорректно ввели цену, попробуйте снова");
scanner.nextLine();
continue;
}
list.add(new Item(name, cost));
System.out.println("Товар добавлен");
System.out.println("Для завершения ввода товаров напишите \"Завершить\". Если хотите " +
"продолжить, нажмите Enter");
String endPhrase = scanner.nextLine();
if (endPhrase.equalsIgnoreCase("завершить")) {
break;
}
}
}

public double calculate(int people) {
if (list.isEmpty()) {
return 0;
} else {
double summa = 0;
for (Item item : list) {
summa += item.getCost();
}
return summa / people;
}
}

public void printNamesItems() {
if (list.isEmpty()) {
System.out.println("Нет товаров");
} else {
for (Item item : list) {
System.out.println(item.getName());
}
}
}
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно более очевидно назвать, например, Product или Good

private final String name;
private final double cost;

public Item(String name, double cost) {
this.name = name;
this.cost = cost;
}

public String getName() {
return name;
}

public double getCost() {
return cost;
}
}
10 changes: 7 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import java.util.Scanner;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Этот импорт уже неактуален, его можно удалить. Чтобы студия сама оптимизировала импорт в файле, можно нажать Ctrl+Alt+O, или в студии Code - Optimize Imports


public class Main {

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

System.out.println("Алоха Гаваи");

new BillCalculator().startBillCalculator();
}
}