Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/main/java/AddTovarAndSave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

class AddTovarAndSave {
List<TovarAndPrice> 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<TovarAndPrice> getAddedTovars() {
return spisokTovarov;
}
}
41 changes: 40 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -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<TovarAndPrice> 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 + " рублей";
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/People.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/TovarAndPrice.java
Original file line number Diff line number Diff line change
@@ -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;
}
}