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
22 changes: 22 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Calculator {
private double sumPrice;
private int numberOfPeople;
private String products;

public Calculator(int numberOfPeople) {
this.products="";
this.numberOfPeople = numberOfPeople;
}

public void addProduct(String productName,double price) {
products += productName + "\n";
sumPrice+=price;
}

public String getProducts(){
return products;
}
public double getPersonalBill(){
return sumPrice/numberOfPeople;
}
}
84 changes: 80 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,84 @@
import java.util.Scanner;

public class Main {

private static Calculator calculator;
private static Scanner scanner;

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
scanner = new Scanner(System.in);
int numberOfPeople = askNumberOfPeople();
calculator = new Calculator(numberOfPeople);
askProductAndPrice();
showResult();
}

private static int askNumberOfPeople() {
System.out.println("Много вас?");
int numberOfPeople;
while (true) {
if (scanner.hasNextInt()) {
numberOfPeople = scanner.nextInt();
scanner.nextLine();
if (numberOfPeople > 1) {
return numberOfPeople;
}
System.out.println("Введите корректное количество людей (>1)");
} else {
System.out.println("Введите число >1");
}
}
}

public static void askProductAndPrice() {
while (true) {
System.out.println("Что кушать изволите?");
String product = scanner.nextLine();
System.out.println("Че почем?");
double price = askPrice();

calculator.addProduct(product, price);
System.out.println("Я все запомнил!");
System.out.println("Еще заказывать будете? Если нет, введите \"завершить\" ");
String input = scanner.nextLine();
if ("завершить".equalsIgnoreCase(input)) {
break;
}
}
}

private static double askPrice() {
while (true) {
double price;
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
scanner.nextLine();
if (price <= 0) {
System.out.println("Бесплатный сыр только в мышеловке! Некорректная цена");
} else {
return price;
}
} else {
scanner.nextLine();
System.out.println("Введите цену в формате рубли.копейки");
}
}
}

private static void showResult() {
System.out.println("Добавленные товары:");
System.out.println(calculator.getProducts());
double personalBill = calculator.getPersonalBill();
double first = Math.floor(personalBill / 10) % 10;
double second = Math.floor(personalBill) % 10;
String rubName;
if (second == 1 && first != 1) {
rubName = "рубль";
} else if (second >= 2 && second <= 4 && first != 1) {
rubName = "рубля";
} else {
rubName = "рублей";
}
System.out.println(String.format("С носа : %.2f %s", personalBill, rubName));
}
}
}