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
15 changes: 15 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Calculator {
int howManyPals;

public void addPals(int howManyPals) {
this.howManyPals = howManyPals;
}

public Double split(Double total, int pals) {
return total / pals;
}

public int returnPals() {
return howManyPals;
}
}
52 changes: 52 additions & 0 deletions src/main/java/Checkers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.Scanner;
import java.util.Locale;
import java.util.InputMismatchException;

public class Checkers {
public int isInt() {
Scanner scan = new Scanner(System.in);
int intToCheck;

while (true) {
try {
intToCheck = scan.nextInt();
} catch (InputMismatchException e) {
System.out.println("Не-а, введите корректное число!");
scan.next();
continue;
}

if (intToCheck > 1) {
return intToCheck;
} else if (intToCheck == 1) {
System.out.println("Так Вы же один, чего тут считать? :O");
} else if (intToCheck < 0) {
System.out.println("Введите положительное число!");
} else {
System.out.println("Никто не пришел на ужин? :C");
}
}
}

public Double isCorrectCost() {
Scanner scan = new Scanner(System.in).useLocale(Locale.US);
Formatter formatter = new Formatter();
Double validDouble;

while (true) {
try {
validDouble = scan.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Введите корректную стоимость!");
scan.next();
continue;
}

if (validDouble < 0.01) {
System.out.println("Цена должна быть больше нуля!");
} else {
return formatter.formatDouble(validDouble);
}
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Formatter {
public String correctCost(Double cost) {
int remainder = ((int)(Math.floor(cost) % 10));
String formattedCost;

if ((cost * 10) - (int)(cost * 10) == 0) {
formattedCost = cost + "0";
} else {
formattedCost = cost.toString();
}

if ((cost > 10 && cost < 15) || remainder == 0 || (remainder > 4 && remainder < 10)) {
formattedCost += " рублей";
} else if (remainder == 1) {
formattedCost += " рубль";
} else if (remainder > 1 && remainder < 5) {
formattedCost += " рубля";
}
return formattedCost;
}

public Double formatDouble(Double doubleToFormat) {
String strToDouble = String.format("%.2f", doubleToFormat).replace(",", ".");
return Double.valueOf(strToDouble);
}
}
57 changes: 57 additions & 0 deletions src/main/java/Goods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;

public class Goods {
ArrayList<String> mealList = new ArrayList<>();
ArrayList<Double> costList = new ArrayList<>();
Double totalCost = 0.00;

public void addMeal() {
Scanner scan = new Scanner(System.in).useLocale(Locale.US);
Checkers checker = new Checkers();
boolean inputIsEmpty = false;

for (int i = 0; true; i++) {
if (!inputIsEmpty) {
System.out.print(i == 0 ? "Введите название блюда: " : "Введите название блюда или \"Завершить\": ");
} else {
System.out.print("Введите хоть что-нибудь!: ");
inputIsEmpty = false;
}
String addNextMeal = scan.nextLine();

if (addNextMeal.equalsIgnoreCase("Завершить")) {
break;
} else if (addNextMeal.equals(" ") || addNextMeal.equals("")) {
inputIsEmpty = true;
continue;
} else {
mealList.add(addNextMeal);
}

System.out.print("Введите стоимость блюда: ");

Double addNextCost = checker.isCorrectCost();
costList.add(addNextCost);
totalCost += addNextCost;

System.out.println("\nТовар добавлен! Желаете добавить что-то еще?");
}
}

public void printTotalGoods() {
Formatter formatter = new Formatter();
System.out.println("Добавленные товары:");

for (int i = 0; i < mealList.size(); i++) {
System.out.println(i + 1 + ". " + mealList.get(i) + " " + formatter.correctCost(costList.get(i)));
}

System.out.println("Итог: " + formatter.correctCost(formatter.formatDouble(totalCost)));
}

public Double returnTotalCost() {
return totalCost;
}
}
17 changes: 15 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Checkers checker = new Checkers();
Goods goods = new Goods();
Calculator calculator = new Calculator();
Formatter formatter = new Formatter();

System.out.println("Введите размер Вашей компании :)");

calculator.addPals(checker.isInt());

System.out.println("Окей, давайте Вас рассчитаем :)");

goods.addMeal();
goods.printTotalGoods();
Double total = calculator.split(goods.returnTotalCost(), calculator.returnPals());
System.out.println("С каждого: " + formatter.correctCost(formatter.formatDouble(total)));
}
}