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

public class Calculator {
double totalPrice;
ArrayList<Product> product;
Scanner scanner = new Scanner(System.in);

public Calculator() {
product = new ArrayList<>();
}

public void add() {
while (true) {
System.out.println("Введите товар:");
String productInput = scanner.next();
while (true) {
System.out.println("Введите цену:");
double priceInput = 0;
if (scanner.hasNextDouble()) {
priceInput = scanner.nextDouble();
if (priceInput < 0) {
System.out.println("Цена не может быть меньше ноля");
} else {
System.out.println("Товар добавлен");
product.add(new Product(productInput, priceInput));
totalPrice += priceInput;
break;
}
} else {
System.out.println("Ошибка ввода, нужны цифры");
scanner.nextLine();
}
}
System.out.println("Хотите добавить еще товар?");
System.out.println("Введите Завершить для подсчёта, или любой символ что-бы добавить еще товар");
String end = scanner.next();
if (end.equalsIgnoreCase("Завершить") || productInput.equalsIgnoreCase("Завершить")) {
System.out.println("Расчёт завершён.");
break;
}
}
}

public void print(int people) {
Formatter formatter = new Formatter();

System.out.println("Добавленные товары");
for (int i = 0; i < product.size(); i++) {
System.out.println(product.get(i).name);
}
double summa = totalPrice / people;
System.out.println("Общая сумма: " + String.format("%.2f", totalPrice) + " " + formatter.formatter(totalPrice));
System.out.println("Каждый должен: " + String.format("%.2f", summa) + " " + formatter.formatter(summa));
}
}


14 changes: 14 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Formatter {
public String formatter(double summa) {
int nums = (int) Math.floor(summa);
String rub = "рублей";
if(nums % 100 >= 11 && nums % 100 <= 19 || nums % 10 == 0) {
rub = "рублей";
} else if (nums % 10 == 1) {
rub = "рубль";
}else if (nums % 10 >= 2 && nums % 10 <= 4){
rub = "рубля";
}
return rub;
}
}
24 changes: 23 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
Calculator calc = new Calculator();
while (true) {
System.out.println("На скольких человек разделить счёт?");
int people = 0;
if (scanner.hasNextInt()) {
people = scanner.nextInt();
if (people == 1) {
System.out.println("Вы один радуйтесь!");
} else if (people < 1) {
System.out.println("Некоррекное значение.");
} else {
System.out.println("Ого сколько у вас друзей, сейчас посчитаю");
calc.add();
calc.print(people);
break;
}
}else {
System.out.println("Некорректное значение, нужна цифра");
scanner.nextLine();
}
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Product {
String name;
double price;

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