Skip to content
60 changes: 60 additions & 0 deletions src/main/java/Calc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.LinkedList;
import java.util.Locale;
import java.util.Scanner;

public class Calc {
private double goodsSum;
private final int numPeople;
private String goodsStr = "";
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 с полями name и price. Экземпляры этого класса можно сохранить в список. Это может быть удобно, если тебе в дальнейшем потребуется цена каждого товара.

private final Scanner scan;
private final LinkedList<Product> list;

Calc(int nPeople, Scanner scan) {
numPeople = nPeople;
this.scan = scan;
list = new LinkedList<>();
}


void goodsAdd() {
while (true) {
double priceProduct = -1;
System.out.println("Введите название товара:");
String nameProduct = scan.nextLine();
if (nameProduct.length() > 0) {
System.out.println("Введите цену товара в формате рубли.копейки:");
if (scan.hasNextDouble()) {
priceProduct = scan.nextDouble();
} else {
while (true) {
if (!(scan.nextLine().equals(""))) break;
}
}
}
if (priceProduct > 0) {
System.out.println("Товар " + nameProduct + " по цене " + String.format(Locale.ENGLISH, "%.2f", priceProduct) + " успешно добавлен!");
list.add(new Product(nameProduct, priceProduct));
goodsStr = goodsStr.concat("\n" + nameProduct);
goodsSum += priceProduct;
System.out.println("Добавить следующий товар? (Для выхода введите \"завершить\")");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👏 Круто, что подсказываешь пользователю, как выйти из сценария!

scan.nextLine();
String sel = scan.nextLine();
sel = sel.toLowerCase();
if (sel.equals("завершить")) {

System.out.println("Добавленные товары:");
for (Product p : list) {
System.out.println(p.name);
}
goodsSum /= numPeople;
StringBuilder strB = new StringBuilder("Сумма на одного человека: ").append(String.format(Locale.ENGLISH, "%.2f ", goodsSum)).append(Formatter.get(goodsSum));
System.out.println(strB);
break;
}
} else {
System.out.println("Ошибка ввода данных");
}
}
}
}

10 changes: 10 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Formatter {
public static String get(double sum) {
int i = (int) sum % 100;
int i1 = i % 10;
int i2 = i / 10;
if ((i1 == 1) && (i2 != 1)) return "рубль";
else if ((i1 > 1) && (i1 < 5) && (i2 != 1)) return "рубля";
else return "рублей";
}
}
6 changes: 0 additions & 6 deletions src/main/java/Main.java

This file was deleted.

28 changes: 28 additions & 0 deletions src/main/java/MainCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import java.util.Locale;
import java.util.Scanner;

public class MainCalc {
private static int getInt(Scanner scanner) {
System.out.println("Введите количество человек");
while (true) {
if (scanner.hasNextInt()) {
int numPeople = scanner.nextInt();
if (numPeople > 1) return numPeople;
} else {
scanner.next();
}
System.out.println("Ошибка.Введите количество человек еще раз");
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.useLocale(Locale.ENGLISH);
int numPeople = getInt(scanner);
System.out.println("количество человек:" + numPeople);
scanner.nextLine();
Calc clc = new Calc(numPeople, scanner);
clc.goodsAdd();
}
}
9 changes: 9 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Product {
public String name;
public double price;

Product(String name, double price) {
this.name = name;
this.price = price;
}
}