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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

import java.util.Scanner;

public class Calculator {

String receipt = "";
double summa = 0;

public void addGood(String title, double price) {
Good tovar = new Good(title, price);

receipt = receipt.concat("\n").concat(tovar.title);
summa = summa + tovar.amount;
}

public String getReceipt() {
return receipt;
}

public double getSumma() {
return summa;
}

public String getRubleAddition(int num)
{
int preLastDigit = num % 100 / 10;
if (preLastDigit == 1)
{
return "рублей";
}

switch (num % 10)
{
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}

}
12 changes: 12 additions & 0 deletions src/main/java/Good.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Good {

// поля
String title;
double amount;

Good (String name, double price) {
title = name;
amount = price;
}

}
77 changes: 74 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,79 @@

import java.util.Scanner;

public class Main {

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

Scanner scanner = new Scanner(System.in);
// шаг 1: запрашиваем количество участников

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Каждый шаг можно выделить в отдельную функцию, так код можно будет переиспользовать

int numberOfFriends;

while (true) {
System.out.println("На сколько человек вы хотели бы разделить счет?");
// проверяем наличие целого числа в вводе
if (!scanner.hasNextInt()) { // если нет целое число, то просим ввести еще раз
System.out.println("Введите целое число: пожалуйста.");
scanner.next();
continue;
}
else {
numberOfFriends = scanner.nextInt();
}

// проверяем количество человек
if (numberOfFriends > 1) {
break;
} else {
System.out.println("Вас слишком мало, чтобы делить счет :)");
}

}

// шаг 2. добавление товаров и подсчет товаров
Calculator calculator = new Calculator();
while (true) {
System.out.println("Введите название товара");
scanner.nextLine();
String name = scanner.nextLine();

System.out.println("Введите цену");
double price;

// проверяем, что цена введена корректно
if (!scanner.hasNextDouble()) {
System.out.println("Ошибка в цене товара. Попробуйте снова.");
continue;
}
else {
price = scanner.nextDouble();
}

if (price > 0) {
calculator.addGood(name, price); // добавляем товар
System.out.println("Товар успешно добавлен. Если хотите завершить ввод, напишите - завершить.");
String exit = scanner.next();
if (exit.equalsIgnoreCase("завершить")) {
break;
}
} else {
System.out.println("Вы ввели отрицательную цену. Попробуйте еще раз ввести товар.");
}
}

// шаг 3. вывод результата
System.out.println("Добавленные товары:");
System.out.println(calculator.getReceipt());
System.out.println("Сумма к оплате: " + String.format("%.2f",calculator.getSumma()));

// определяем окончание слова рубль
double finalAmount = calculator.getSumma() / numberOfFriends;
String formattedDouble = String.format("%.2f", finalAmount);
String rubles = calculator.getRubleAddition((int) Math.floor(finalAmount));

System.out.println("Каждый должен заплатить = " + formattedDouble + " " + rubles);

}
}