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

public void setTotalCoast(double totalCoast) {
this.totalCoast = totalCoast;
}

public double getTotalCoast() {
return totalCoast;
}

public void calculateTotalCost() {
Scanner scanner = new Scanner(System.in);
ArrayList<String> products = new ArrayList<>();
double totalBill = 0.0;
boolean continueAdding = true;

while (continueAdding) {
System.out.print("Введите название товара (или 'Завершить' для завершения): ");
String productName = scanner.nextLine();

if (productName.equalsIgnoreCase("Завершить")) {
break;
}

while (true) {
System.out.print("Введите стоимость товара в формате рубли.копейки: ");
String input = scanner.nextLine();

try {
double price = Double.parseDouble(input);
if (price <= 0) {
System.out.println("Продукта успешно добавлен в калькулятор.");
continue;
}
totalBill += price;
products.add(productName);
System.out.println("Продукта успешно добавлен в калькулятор.");
break;
} catch (NumberFormatException e) {
System.out.println("Ошибка. Пожалуйста, введите число в формате рубли.копейки.");
}
}

boolean validInput;
do {
System.out.print("Хотите добавить ещё один товар? (Да/Нет): ");
String continueInput = scanner.nextLine().toLowerCase();

if (continueInput.equals("да")) {
continueAdding = true;
validInput = true;
} else if (continueInput.equals("нет")) {
continueAdding = false;
validInput = true;
} else {
System.out.println("Ошибка. Пожалуйста, введите \"Да\" или \"Нет\".");
validInput = false;
}
} while (!validInput);

String rublesSuffix = RubleUtil.getRublesFormattedString(totalBill);

System.out.println("Общая стоимость всех товаров: " + totalBill + " " + rublesSuffix);

System.out.println("Добавленные товары:");
for (String product : products) {
System.out.println(product);
}

totalCoast = totalBill;
}
}
}
41 changes: 38 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@

public class Main {

import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);

int numberOfPersons = 0;

do {
System.out.println("Введите количество персон:");
while (!scanner.hasNextInt()) {
System.out.println("Ошибка, введите пожалуста число.");
scanner.next();
}
numberOfPersons = scanner.nextInt();

if (numberOfPersons <= 0) {
System.out.println("Введите, введите пожалуйста число больше 0");
} else if (numberOfPersons == 1) {
System.out.println("Нет смысла в дальнейших вычислениях.");
}

} while (numberOfPersons <= 0 || numberOfPersons == 1);

Calculator calculator = new Calculator();
calculator.calculateTotalCost();

RubleUtil rubleUtil = new RubleUtil();
String rublesSuffix = RubleUtil.getRublesFormattedString(calculator.getTotalCoast());

NamberPerson nPerson = new NamberPerson();
nPerson.setPerson(numberOfPersons);

double individualPayment = calculator.getTotalCoast() / numberOfPersons;
String individualPaymentRubles = rubleUtil.getRublesFormattedString(individualPayment);

System.out.println("Общая сумма: " + String.format("%.2f", calculator.getTotalCoast()) + " " + rublesSuffix);
System.out.println("Каждый человек должен заплатить: " + String.format("%.2f", individualPayment) + " " + individualPaymentRubles);
}
}
}// Работу сдал Максим Грузинцев
15 changes: 15 additions & 0 deletions src/main/java/NamberPerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class NamberPerson {
private int person;

public void setPerson(int numberOfPersons) {
person = numberOfPersons;
}

public int getPerson() {
return person;
}

public void isPerson() {
System.out.println(person);
}
} // в этом классе сохраняется ввод количества персон
13 changes: 13 additions & 0 deletions src/main/java/RubleUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class RubleUtil {
public static String getRublesFormattedString(double amount) {
int rubles = (int) amount % 10;

if (rubles == 1 && ((int) amount) % 100 != 11) {
return "рубль";
} else if (rubles >= 2 && rubles <= 4 && ((int) amount) % 100 < 10 || ((int) amount) % 100 >= 20) {
return "рубля";
} else {
return "рублей";
}
}
}