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

class Calculator {
String nameProduct;
String result;
double priceProduct;
double sum;
int number;

void countProduct() {
Scanner scanner = new Scanner(System.in);
StringBuilder builder = new StringBuilder();
while (true) {
System.out.println("Введите название товара:");
this.nameProduct = scanner.next();
if (this.nameProduct == null) continue;
if (this.nameProduct.equalsIgnoreCase("Завершить")) break;
System.out.println("Введите стоимость товара:");
while (true) {
if (scanner.hasNextDouble()) {
this.priceProduct = scanner.nextDouble();
if (this.priceProduct < 0) {
System.out.println("Некорректное значение для подсчета");
} else if (this.priceProduct >= 0) {
builder.append(this.nameProduct).append("\n");
this.sum = this.sum + this.priceProduct;
System.out.println("Товар " + this.nameProduct + " добавлен.\nХотите добавить следующий товар?");
break;
}
} else {
System.out.println("Введённое значение не является числом");
scanner.next();
}

}
}
this.result = builder.toString();
}

void numberOfPerson() {
final Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("На скольких человек разделить счёт?");
if (scanner.hasNextInt()) {
number = scanner.nextInt();
if (this.number == 1) {
System.out.println("Нет смысла делить счёт.");
} else if (this.number < 1) {
System.out.println("Некорректное значение для подсчёта.");
} else {
break;
}
} else {
System.out.println("Введенное значениемне не является числом.");
scanner.next();
}
}
}
}

27 changes: 22 additions & 5 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
public class Main {

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

public class Main {
public static void main(final String[] args) {
final Calculator calculator = new Calculator();
calculator.numberOfPerson();
String rub = "";
calculator.countProduct();
double sumResult = calculator.sum / calculator.number;
if ((Math.floor(sumResult) % 100) >= 11 && (Math.floor(sumResult) % 100) <= 14) {
rub = "рублей";
} else {
if ((Math.floor(sumResult) % 10) == 1) {
rub = "рубль";
} else if ((Math.floor(sumResult) % 10) > 1 && (Math.floor(sumResult) % 10) < 5) {
rub = "рубля";
} else if ((Math.floor(sumResult) % 10) > 5) {
rub = "рублей";
}
}
String line = String.format("Добавленные товары:\n%s\nцена, которую должен заплатить каждый %.2f %s", calculator.result, sumResult, rub);
System.out.println(line);
}
}