diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..4e72dcd44 --- /dev/null +++ b/src/main/java/Calculator.java @@ -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); + System.out.println(productList.toString()); + System.out.println("С каждого человека " + roundedPrice+ " " + PriceFormat.rubInflection(productListPrice / people)); + } + } + diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..886bf8b18 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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); + } } \ No newline at end of file diff --git a/src/main/java/PeopleAmount.java b/src/main/java/PeopleAmount.java new file mode 100644 index 000000000..345017294 --- /dev/null +++ b/src/main/java/PeopleAmount.java @@ -0,0 +1,24 @@ +import java.util.Scanner; + +public class PeopleAmount { +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(); + } + } + +} + +} diff --git a/src/main/java/PriceFormat.java b/src/main/java/PriceFormat.java new file mode 100644 index 000000000..4397c52ea --- /dev/null +++ b/src/main/java/PriceFormat.java @@ -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 "рублей"; + } + } +} diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 000000000..813cdbb86 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,4 @@ +public class Product { + String name; + double price; +}

AltStyle によって変換されたページ (->オリジナル) /