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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
apply plugin: 'java'

sourceCompatibility = 1.8
sourceCompatibility = 11
version = '1.0'

repositories {
}

dependencies {
}
targetCompatibility = JavaVersion.VERSION_11
10 changes: 7 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import calculator.Calculator;

public class Main {

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

System.out.println("Добро пожаловать в приложение Калькулятор счета");
calculator.calculate();
System.out.println("Спасибо что выбрали наше приложение");
}
}
111 changes: 111 additions & 0 deletions src/main/java/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package calculator;

import java.util.*;

public class Calculator {
private final Scanner scanner = new Scanner(System.in);
private final List<Product> products = new ArrayList<>();

public void calculate() {
int numberOfPersons = addNumberOfPersons();
double sum = addProducts();
double result = sum / numberOfPersons;

printAllProducts();
printResult(result);
}

private int addNumberOfPersons() {
int numberOfPersons = 0;

System.out.println("Введите количество персон");

while (numberOfPersons < 2) {
if (!scanner.hasNextInt()) {
System.out.println("Вы ввели некорректное значение \nПопробуйте еще раз");
scanner.next();
} else {
numberOfPersons = scanner.nextInt();
if (numberOfPersons < 2) {
System.out.println("Количество персон должно быть более 1 \nПопробуйте еще раз");
}
}
}

return numberOfPersons;
}

private double addProducts() {
String exit = "Завершить";
double sumOfProducts = 0.0;

do {
String productTitle = addProductTitle();
double productPrice = addProductPrice();
Product product = new Product(productTitle, productPrice);
sumOfProducts += productPrice;

products.add(product);
System.out.println("Товар успешно добавлен");
System.out.println("Хотите добавить еще товар? " +
"\nЕсли да, то введите любой символ " +
"\nЕсли хотите завершить, то введите 'Завершить'");

} while (!scanner.next().equalsIgnoreCase(exit));

return sumOfProducts;
}

private String addProductTitle() {
System.out.println("Введите название товара");

return scanner.next();
}

private double addProductPrice() {
double price = 0.0;

System.out.println("Введите стоимость товара в формате 'рубли,копейки' [10,45, 11,40]");

while (price <= 0.01) {
if (!scanner.hasNextDouble()) {
System.out.println("Вы ввели некорректное значение " +
"\nВведите стоимость товара в формате 'рубли,копейки' [10,45, 11,40] " +
"\nПопробуйте еще раз");
scanner.next();
} else {
price = scanner.nextDouble();
if (price <= 0.0) {
System.out.println("Стоимость товара должна быть больше 0 \nПопробуйте еще раз");
}
}
}

return price;
}

private void printAllProducts() {
System.out.println("Добавленные товары:");

for (Product product : products) {
System.out.println(product.getTitle() + " : " + String.format("%.2f", product.getPrice()));
}
}

private void printResult(double price) {
String format = String.valueOf((int) price);
String result;

if (format.endsWith("11")) {
result = "рублей";
} else if (format.endsWith("1")) {
result = "рубль";
} else if (format.endsWith("2") || format.endsWith("3") || format.endsWith("4")) {
result = "рубля";
} else {
result = "рублей";
}

System.out.println("Сумма к оплате: " + String.format("%.2f", price) + " " + result);
}
}
19 changes: 19 additions & 0 deletions src/main/java/calculator/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package calculator;

class Product {
private final String title;
private final double price;

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

String getTitle() {
return title;
}

double getPrice() {
return price;
}
}