diff --git a/src/main/java/AddFormat.java b/src/main/java/AddFormat.java new file mode 100644 index 000000000..319720bdf --- /dev/null +++ b/src/main/java/AddFormat.java @@ -0,0 +1,40 @@ +public class AddFormat { + public static String format(int num, String s1, String s2, String s3, boolean print_num){ + + int count = num%100; + + String result; + + if (count>= 5 && count <= 20) { + result = s3; + } else { + count = count % 10; + if (count == 1) { + result = s1; + } else if (count>= 2 && count <= 4) { + result = s2; + } else { + result = s3; + } + } + + if(print_num){ + result = String.format("%d %s", num, result); + } + + return result; + } + + public static String rub(double price){ + + int num = (int)Math.floor(price); + + String result = format(num, "рубль", "рубля", "рублей", false); + + return String.format("%.2f %s", price, result); + } + + public static String rub(float price){ + return rub((double)price); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..d8cec64d3 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,61 @@ +import java.util.Locale; +import java.util.Scanner; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + Locale.setDefault(Locale.US); + + Scanner scanner = new Scanner(System.in); + + System.out.println("На скольких человек необходимо разделить счёт?"); + + int persons; + + while (true){ + try{ + persons = scanner.nextInt(); + + if(!scanner.nextLine().trim().equals("")){ + //100 раз! Уже 100 раз я повторял, что хочу поделить счет на 5 человек + System.out.println("Введите просто число без дополнительных знаков:"); + continue; + } + + if(persons>1){ + break; + } + + System.out.println("Введите число человек больше 1:"); + + }catch (Exception e) { + //System.out.println(e); + //scanner = new Scanner(System.in); + if(scanner.hasNextLine()) scanner.nextLine(); + System.out.println("Введите число, а не строку:"); + } + } + + System.out.println("Будем делить счет на "+AddFormat.format(persons, "человека", "человека","человек", true)); + + MyCalc calc = new MyCalc(persons); + + System.out.println("Теперь давайте добавим товары в счет"); + + while (true){ + + calc.addProduct(); + + System.out.println("Хотите добавить ещё один товар? [да/завершить]"); + + String str = scanner.nextLine(); + + if(str.trim().equalsIgnoreCase("завершить")){ + break; + } + } + + calc.calculateAndPrint(); } } \ No newline at end of file diff --git a/src/main/java/MyCalc.java b/src/main/java/MyCalc.java new file mode 100644 index 000000000..ec6af0bee --- /dev/null +++ b/src/main/java/MyCalc.java @@ -0,0 +1,37 @@ +public class MyCalc { + private int persons; + public String products = ""; + public double total_price = 0; + + private int total = 0; + MyCalc(int persons){ + this.persons = persons; + } + + public void addProduct(){ + + ProductItem item = new ProductItem(); + + item.askProductData(); + + System.out.println("Добавлен товар " + item.name + " стоимостью "+item.getPrice()); + + products += item.getNameAndPrice() + "\n"; + + this.total_price += item.price; + this.total++; + } + + public void calculateAndPrint(){ + System.out.println("\n"); + System.out.println("Заказанные товары:"); + System.out.println(products.trim()); + + //продавец и копейки не простит, так что если поровну не поделится будут чаевые + double price_per_person = Math.ceil(100*total_price/persons)/100; + + System.out.println("Итого "+AddFormat.format(total, "товар", "товара","товаров", true)+" на сумму "+AddFormat.rub(total_price)); + System.out.println("Делим счет на "+ AddFormat.format(persons, "человека", "человек","человек", true)); + System.out.println("Каждый должен заплатить по "+ AddFormat.rub(price_per_person)); + } +} diff --git a/src/main/java/ProductItem.java b/src/main/java/ProductItem.java new file mode 100644 index 000000000..331778065 --- /dev/null +++ b/src/main/java/ProductItem.java @@ -0,0 +1,59 @@ +import java.util.Scanner; + +public class ProductItem { + + public String name; + public double price; + + public void askProductData(){ + + Scanner scanner = new Scanner(System.in); + + while (true){ + System.out.println("Введите название товара:"); + + name = scanner.nextLine(); + + if(!name.trim().equals("")){ + break; + } + + System.out.println("Ошибка! Название не должно быть пустой строкой"); + } + + while(true) { + + System.out.println("Введите стоимость товара:"); + + try { + + price = scanner.nextDouble(); + + if(!scanner.nextLine().trim().equals("")){ + System.out.println("Введите только число в формате 0.00 без дополнительных знаков"); + continue; + } + + if (price> 0) { + break; + } + + } catch (Exception e) { + + if(scanner.hasNextLine()) scanner.nextLine(); + //scanner = new Scanner(System.in); + } + + System.out.println("Введите число в формате 0.00"); + } + } + + + public String getNameAndPrice(){ + return name + " " + AddFormat.rub(price); + } + + public String getPrice(){ + return AddFormat.rub(price); + } +}

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