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
19 changes: 19 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Calculator {
String item = "";
static double sum = 0;

public void productsSum(Items items) {
sum += items.costOfProduct();
System.out.println("Общая сумма товаров: " + sum + " руб.");
}

public void addNewProducts(Items items) {
String result = String.format(items.nameOfProduct() + "\n");
item += result ;
System.out.println("Добавленные товары:" + "\n" + item);
}
public double chequePerPerson() {
return sum;

}
}
21 changes: 21 additions & 0 deletions src/main/java/Items.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Items {
double cost;
String nameProduct;

Items(double price, String name) {
cost = price;
nameProduct = name;
}

public String nameOfProduct() {

return nameProduct;
}
public double costOfProduct() {

return cost;
}


}

81 changes: 78 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,81 @@

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println("На сколько людей необходимо разделить счет?");
int users = getInput();
calculate();
cheque(users);

}




public static int getInput() {
Scanner scanner = new Scanner(System.in);
int users;
while (true) {
if (scanner.hasNextInt()) {
users = scanner.nextInt();
if (users == 1) {
System.out.println("Делить нечего");
break;
}
if (users <= 1) {
System.out.println("Ошибка, введите число повторно");
scanner.nextLine();
}
if (users > 1) {
System.out.println("Делим счет на " + users);
break;
}
}
else {
System.out.println("Введите корректное число");
scanner.nextLine();}
}
return users;
}
}

public static void calculate() {
Scanner scanner = new Scanner(System.in);
Calculator calculator = new Calculator();
while (true) {
System.out.println("Введите название товара или 'Завершить' для завершения работы калькулятора");
String productName = scanner.nextLine();
if (productName.equalsIgnoreCase("Завершить")) {
break;
}
while (true) {
System.out.println("Введите стоимость товара");
if (scanner.hasNextDouble()) {
double productPrice = scanner.nextDouble();
if (productPrice > 0) {
System.out.println("Товар успешно добавлен в калькулятор.");
scanner.nextLine();
Items items = new Items(productPrice, productName);
calculator.productsSum(items);
calculator.addNewProducts(items);
break;
} else {
System.out.println("Некорректный ввод"); // ввод отрицательного числа
scanner.nextLine();
}
} else {
System.out.println("Некорректный ввод,введите числовое значение "); // ввод нечислового значения
scanner.nextLine();
}
}
}
}

public static void cheque(int users) {
Calculator calculator = new Calculator();
Payment payment = new Payment(calculator.chequePerPerson(),users);
payment.paymentPerPerson();

}


}
30 changes: 30 additions & 0 deletions src/main/java/Payment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

public class Payment {
double pay;
int clients;


Payment( double a, int visitors) {
pay = a;
clients = visitors;
}

public void paymentPerPerson() {
double finalSum = pay/clients;
double piss = Math.floor(finalSum);
String sumFinal = String.format("%.2f",finalSum);
String ruble;
if ((piss % 10 == 1) && (piss != 11)) {
ruble = "рубль";
}
else if ((piss % 10 >= 2) && (piss % 10 <= 4) && ((piss < 10) || (piss > 20))) {
ruble = "рубля";
}
else {
ruble = "рублей";
}
System.out.println("Расчет на человека: " + sumFinal + " " + ruble);

}

}