Skip to content
Closed
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
41 changes: 41 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Calculator {
public List<Product> calculateBill() {
Scanner scanner = new Scanner(System.in);

List<Product> products = new ArrayList<>();
String name;
while (true) {
System.out.println("Введите наименование блюда");
name = scanner.next();

System.out.println("Введите цену ");
if (!scanner.hasNextDouble()) {
System.out.println("Введите цену в формате 00.00, пробуем заново");
scanner.next();
continue;
}

double price = scanner.nextDouble();

if (Double.compare(0.01, price) > 0) {
System.out.println("Только 2 знака после запятой, вот так → 00,00 пробуем заново");
continue;
}

products.add(new Product(price, name));

System.out.println("Завершить подсчет?");
String answer = scanner.next();
if (answer.equalsIgnoreCase("Завершить"))
break;

}
return products;
}


}
54 changes: 51 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
// dev branch for Y.Practicum

import java.util.List;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
firstStep();
int persons = checkInput();
Calculator calculator = new Calculator();
List<Product> products = calculator.calculateBill();
System.out.println("------------------------");
Product.calculation(persons, sumTotal(products));

}

public static void firstStep() {
System.out.println("Добрый день! Это консольное приложение пожет вам разделить счет");
System.out.println("Введите количество персон");
}

public static int checkInput() {
Scanner scanner = new Scanner(System.in);

int input;
while (true) {

if (!scanner.hasNextInt()) {
System.out.println("Введите число виде числа персон!");
scanner.next();
continue;
} else input = scanner.nextInt();

if (input <= 1) {
System.out.println("Ошибка! Введите количество персон больше 1 человека");
continue;
}

break;
}
return input;
}

public static double sumTotal(List<Product> productList) {
double total = 0;
for (Product product : productList) {
total = total + product.price;
String text = "Позиция - " + product.name + " по цене " + product.price;
System.out.println(text);
}
return total;
}


}
22 changes: 22 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Product {
public double price;
public String name;

public Product(double price, String name) {
this.price = price;
this.name = name;
}

public static void calculation(int persons, double total) {
String text = "Итого счет на одного человека будет %.2f %s";
double result = total / persons;
String rub = "рубль";
if (Math.floor(result) >= 5 ) {
rub = "рублей";
} else if (Math.floor(result) < 5 && Math.floor(result) > 1){
rub = "рубля";
}
text = String.format(text, result,rub);
System.out.println(text);
}
}