diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..7643783 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + xmlns:android + + ^$ + + + + + + + + + xmlns:.* + + ^$ + + + BY_NAME + + + + + + + .*:id + + http://schemas.android.com/apk/res/android + + + + + + + + + .*:name + + http://schemas.android.com/apk/res/android + + + + + + + + + name + + ^$ + + + + + + + + + style + + ^$ + + + + + + + + + .* + + ^$ + + + BY_NAME + + + + + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + + + + + + + .* + + .* + + + BY_NAME + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..6cec569 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5d10be7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/BillCalculator.java b/src/main/java/BillCalculator.java new file mode 100644 index 0000000..2b82e1a --- /dev/null +++ b/src/main/java/BillCalculator.java @@ -0,0 +1,29 @@ +import java.util.ArrayList; + +public class BillCalculator { + int personsNumber; + double sumPrice; + ArrayList products = new ArrayList(); + + public BillCalculator(int personsNumber) { + this.personsNumber = personsNumber; + } + + public void addProduct(String name, double price) { // Добавление продукта в список + products.add(new Product(name, price)); + sumPrice += price; + } + + public void printProducts() { // Вывод списка продуктов + int size = products.size(); + System.out.println("Добавленные товары:"); + for (int i = 0; i < size; i++) { + products.get(i).printProduct(); + } + System.out.println("Итого: " + sumPrice + Helpers.getRuble(sumPrice)); + } + + public double calculateBill() { // Расчет суммы к оплате каждым человеком + return sumPrice/personsNumber; + } +} diff --git a/src/main/java/Helpers.java b/src/main/java/Helpers.java new file mode 100644 index 0000000..af6a2af --- /dev/null +++ b/src/main/java/Helpers.java @@ -0,0 +1,16 @@ +public class Helpers { + public static String getRuble(double number) { // Определение окончания в зависимости от числа + switch ((int)number) { + case 1: + return " рубль"; + case 2: + case 3: + case 4: + return " рубля"; + default: + return " рублей"; + } + } + +} + diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..36a18f5 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,53 @@ +import java.util.Locale; +import java.util.Scanner; + public class Main { + private static final Scanner scanner = new Scanner(System.in).useLocale(Locale.US); + public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + int personsNumber = 0; + while (personsNumber < 2) { // Ввод количества человек для разделения счета + System.out.println("На скольких человек необходимо разделить счет?"); + if (scanner.hasNextInt()) { + personsNumber = scanner.nextInt(); + if (personsNumber < 2) { + System.out.println("Количество человек для разделения счета должно быть больше 1."); + } + } + else { + scanner.next(); + System.out.println("Ошибка ввода! Введите целое число."); + } + } + BillCalculator billCalculator = new BillCalculator(personsNumber); + while (true) { // Цикл ввода товаров + System.out.println("Введите название товара:"); + String name = scanner.next(); + while (true) { // Цикл ввода цены товара с проверкой на валидность + System.out.println("Введите стоимость товара в формате \"рубли.копейки\":"); + if (scanner.hasNextDouble()) { + double price = scanner.nextDouble(); + if (price < 0) { + System.out.println("Цена должна быть неотрицательной."); + } else { + billCalculator.addProduct(name, price); + System.out.println("Товар успешно добавлен!"); + break; + } + } else { + scanner.next(); + System.out.println("Ошибка ввода! Введите стоимость товара в правильном формате."); + } + } + System.out.println("Хотите добавить еще один товар? Введите \"завершить\" для завершения ввода."); + String close = scanner.next(); + if (close.equalsIgnoreCase("завершить")) { + break; + } + } + billCalculator.printProducts(); + double amount = billCalculator.calculateBill(); + System.out.println("Сумма, которую должен заплатить каждый: " + String.format("%.2f", amount) + Helpers.getRuble(amount)); } } diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 0000000..fe14cd7 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,15 @@ +import java.lang.Math; + +public class Product { + String name; + double price; + + public Product(String name, double price) { + this.name = name; + this.price = price; + } + + public void printProduct() { + System.out.println(name + " - " + String.format("%.2f",price) + Helpers.getRuble(price)); + } +} AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル