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

public class Calculator {

public int getTheQuantity() {
int numberOfPeople;
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("На скольких человек необходимо разделить счёт?");
if (scanner.hasNextInt()) {
numberOfPeople = scanner.nextInt();
if (numberOfPeople == 1) {
System.out.println("Недостаточное количество человек для подсчета. Введите число еще раз");
} else if (numberOfPeople < 1) {
System.out.println("Введите другое число");
} else {
break;
}
}
scanner.next();
}
return numberOfPeople;

}

public String inputName(int numberOfPeople) {

String list = "Добавленные товары:\n";
String productName;
double productCost;
double amount = 0;
String response;
double amountOfPeople;
String an = "Завершить";
Scanner scanner = new Scanner(System.in);


while (true) {

System.out.println("Введите наименование товара");
productName = scanner.nextLine();

while (true) {

System.out.println("Введите стоимость товара в формате 00,00");
if (scanner.hasNextDouble()) {
productCost = scanner.nextDouble();
if (productCost <= 0) {
System.out.println("Некорректный ввод. Стоимость товара не может быть равна 0 и быть отрицательным числом.");
} else {
break;
}
} else if (!scanner.hasNextDouble()) {
System.out.println("Некорректный ввод");
scanner.next();
}
}
list = list + String.format("%s - %.2f\n", productName, productCost);
amount = amount + productCost;


System.out.println("Товар успешно добавлнен. Хотите добавить еще товар? Введите любой символ. Если нет,то введите Завершить");

response = scanner.next();

if (response.equalsIgnoreCase(an)) {
amountOfPeople = amount / ((double) numberOfPeople);
String rubleEnd = getRuble(amountOfPeople);
list = list + String.format("Каждый должен заплатить %.2f %s\n ", amountOfPeople, rubleEnd);
break;
}
scanner.nextLine();
}
return list;
}

public String getRuble(double amountOfPeople) {
double resultEnd = (Math.floor(amountOfPeople)) % 100;
double r = resultEnd % 10;
if (resultEnd == 0) {
return "рублей";
} else if (resultEnd > 10 && resultEnd < 20) {
return "рублей";
} else if (r > 1 && r < 5) {
return "рубля";
} else if (r == 1) {
return "рубль";
}
return "рублей";
}

}


10 changes: 8 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@



public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calculator = new Calculator();
int numberOfPeople = calculator.getTheQuantity();
String list = calculator.inputName(numberOfPeople);
System.out.println("Количество человек: " + numberOfPeople);
System.out.println(list);
}
}
}