diff --git a/src/main/java/AddTovarAndSave.java b/src/main/java/AddTovarAndSave.java new file mode 100644 index 000000000..338c12237 --- /dev/null +++ b/src/main/java/AddTovarAndSave.java @@ -0,0 +1,56 @@ +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.List; +import java.util.Scanner; + +class AddTovarAndSave { + List spisokTovarov; + + public AddTovarAndSave() { + spisokTovarov = new ArrayList(); + } + + public void addBludo (Scanner scanner) { + System.out.println("Добавить блюдо или напиток: "); + while (true) { + System.out.println("Введите название товара (или нажмите 'Завершить'): "); + String bludoName = scanner.next(); + + if (bludoName.equalsIgnoreCase("Завершить")) { + break; + } + + if (!bludoName.matches("[a-zA-Zа-яА-Я]+")) { + System.out.println("У нас в ресторане все названия блюд пишутся буквами, введите еще раз: "); + continue; + } + + double bludoPrice = -1; + boolean noPrice = false; + while (!noPrice) { + System.out.println("Введите цену: "); + try { + bludoPrice = scanner.nextDouble(); + noPrice = bludoPrice>= 0; + } catch (InputMismatchException e) { + System.out.println("Ввод осуществляется только цифрами, попробуйте еще раз"); + scanner.next(); + } + } + + if (bludoPrice < 0) { + System.out.println("Похоже вы ошиблись с вводом, попробуйте еще раз" + + "а то вы хотите, чтоб за товар заплатили ВАМ:-) "); + continue; + } + spisokTovarov.add(new TovarAndPrice(bludoName, bludoPrice)); + System.out.println("Товар добавлен"); + } + } + public double getAllSumma() { + return spisokTovarov.stream().mapToDouble(TovarAndPrice::getTovarPrice).sum(); + } + public List getAddedTovars() { + return spisokTovarov; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..2d135ab3c 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,45 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + Scanner scanner = new Scanner(System.in); + People people = new People(); + int numberPeople = people.reqCountOfPeople(scanner); + + if (numberPeople < 1){ + System.out.println("Видимо никто не пришел, счет не на кого не поделить"); + } + AddTovarAndSave addTovarAndSave = new AddTovarAndSave(); + addTovarAndSave.addBludo(scanner); + + List addedTovars = addTovarAndSave.getAddedTovars(); + System.out.println("Добавленные товары:"); + for (TovarAndPrice tovar : addedTovars) { + System.out.println(tovar.getTovarName() + ": " + formatCurrency(tovar.getTovarPrice())); + } + + double allSumma = addTovarAndSave.getAllSumma(); + double onePersonSumma = allSumma / numberPeople; + System.out.println("Общий счет: " + formatCurrency (allSumma)); + System.out.println("Сумма на каждого: " + formatCurrency (onePersonSumma)); + } + + + + private static String formatCurrency(double amount) { + String formattedAmount = String.format("%.2f", amount); + formattedAmount = formattedAmount.replace(',', '.'); // Заменить запятую на точку + String[] parts = formattedAmount.split("\\."); + int integerPart = (int) Double.parseDouble(parts[0]); + + if (integerPart % 10 == 1 && integerPart % 100 != 11) { + return formattedAmount + " рубль"; + } else if ((integerPart % 10>= 2 && integerPart % 10 <= 4) && (integerPart % 100 < 12 || integerPart % 100> 14)) { + return formattedAmount + " рубля"; + } else { + return formattedAmount + " рублей"; + } } } \ No newline at end of file diff --git a/src/main/java/People.java b/src/main/java/People.java new file mode 100644 index 000000000..3802841bb --- /dev/null +++ b/src/main/java/People.java @@ -0,0 +1,26 @@ +import java.util.InputMismatchException; +import java.util.Scanner; + +public class People { + public int reqCountOfPeople (Scanner scanner) { + while (true){ + try { + System.out.println("Сколько человек будут платить? "); + int countOfPeople = scanner.nextInt(); + if (countOfPeople < 1) { + System.out.println("Введите еще раз! А то судя по этим данным вы вообще не пришли"); + } + else if (countOfPeople == 1) { + System.out.println("Попробуйте ввести еще раз, а то платить будете один! "); + } + else { + return countOfPeople; + } + } + catch (InputMismatchException e) { + System.out.println("Похоже, что вы ввели не цифры. Попробуйте еще раз "); + scanner.next(); + } + } + } +} diff --git a/src/main/java/TovarAndPrice.java b/src/main/java/TovarAndPrice.java new file mode 100644 index 000000000..612b768f3 --- /dev/null +++ b/src/main/java/TovarAndPrice.java @@ -0,0 +1,15 @@ +public class TovarAndPrice { + String tovarName; + double tovarPrice; + + public TovarAndPrice (String tovarName, double tovarPrice){ + this.tovarName = tovarName; + this.tovarPrice = tovarPrice; + } + public String getTovarName(){ + return tovarName; + } + public double getTovarPrice(){ + return tovarPrice; + } +}

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