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

public class Calculator {
double sum = 0;
String listProduct = "Добавленные товары:\n";

public void sumProduct() {
Scanner scanner = new Scanner(System.in);
Product product = new Product();

while (true) {
System.out.println("Введите название товара");

product.name = scanner.next();
listProduct = listProduct + product.name + "\n";
boolean isRightPrice = false;
while (!isRightPrice) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно обойтись без переменной, тут писать while (true), а в месте, где нужен выход из цикла - писать break

System.out.println("Введите цену товара в формате рубли.копейки");

if (scanner.hasNextDouble()) {
product.price = scanner.nextDouble();

if (product.price < 0) {
System.out.println("Цена не может быть отрицательной");
} else {
isRightPrice = true;
}
} else {
System.out.println("Вы ввели не число!");
scanner.next();
}
}
sum = sum + product.price;

System.out.println("Товар добавлен");
System.out.println("Хотите ли вы добавить еще товар?");
System.out.println("Для завершения добавления товаров напишите Завершить");

String answer = scanner.next();

if (answer.equalsIgnoreCase("Завершить")) {
break;
}
}
}
}
38 changes: 34 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import java.util.Scanner;

public class Main {

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

while (!isNumber) {
System.out.println("На скольких человек необходимо разделить счёт?");// ваш код начнется здесь

if (scanner.hasNextInt()) {
persons = scanner.nextInt();
if (persons == 1) {
System.out.println("Нет смысла ничего считать и делить");
} else if (persons < 1) {
System.out.println("Это некорректное значение для подсчёта");
} else {
isNumber = true;
}
} else {
System.out.println("Это неправильное число!");
scanner.next();
}
}

Calculator calculator = new Calculator();
calculator.sumProduct();
System.out.println(calculator.listProduct);

double total = calculator.sum / persons;

Ruble ruble = new Ruble();
ruble.writeRuble(total);
System.out.println(ruble.amountOfRubles);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class Product {
String name;
double price;
}
20 changes: 20 additions & 0 deletions src/main/java/Ruble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Ruble {
String amountOfRubles;

public void writeRuble(double floatAmount) {
int amount = (int) Math.floor(floatAmount);
int a = amount % 100;
int b = a % 10;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Лучше дать названия более понятные, чтобы другим программистам с первого взгляда было ясно, что они значат


if (a >= 11 & a <= 19) {
amountOfRubles = String.format("%.2f", floatAmount) + " рублей";
} else if (b == 1) {
amountOfRubles = String.format("%.2f", floatAmount) + " рубль";
} else if (b == 2 | b == 3 | b == 4) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно писать b >= 2 && b <= 4

amountOfRubles = String.format("%.2f", floatAmount) + " рубля";
} else {
amountOfRubles = String.format("%.2f", floatAmount) + " рублей";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Код amountOfRubles = String.format("%.2f", floatAmount) повторяется в каждой ветке, лучше его вынести после if-else

}
}

}