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

public class AddProduct {

String product;
float price;

public void addproduct() {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название товара");
this.product = scanner.next();
while (true) {
System.out.println("Введите цену товара");
scanner = new Scanner(System.in);
if (scanner.hasNextFloat()) {
this.price = scanner.nextFloat();
if (this.price > 0) {
System.out.println("Товар успешно добавлен");
break;
} else {
System.out.println("Введите число больше 0");
scanner = new Scanner(System.in);
}

} else {
System.out.println("Введите положительное число");
scanner = new Scanner(System.in);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тут и выше можно не создавать каждый раз новый инстанс класса Scanner

}
}

}
}
72 changes: 72 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.util.Scanner;

public class Calculator {

int number;
String products = "";
float amount = 0;

public int numberPeople() {

while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("На скольки человек необходимо разделить счет?");
if(scanner.hasNextInt()) {
number = scanner.nextInt();
if (number == 1)
System.out.println("Нет смысла ничего делить");
else if (number < 1)
System.out.println("Введено некорректное количество");
else
break;
}
else
System.out.println("Введите целое положительное число:");
}
return number;
}
public void addProduct() {

while (true) {
Scanner scanner = new Scanner(System.in);
AddProduct nomenclature = new AddProduct();
nomenclature.addproduct();
amount += nomenclature.price;
products = products +"\n"+ nomenclature.product;
System.out.println("Вы хотите добавить еще один товар? Если нет, введите завершить");
String complete = scanner.next();
if (complete.equalsIgnoreCase("Завершить"))
break;
}

}

public void totals(){

String rub;

System.out.println("Добавленные товары: \n" + products);
System.out.println("Стоимость товаров: " + amount);

int lastnumber = (int) (Math.floor(amount/number)%10);
int ostatok = (int) ((amount/number)%100);

if (ostatok>=11 && ostatok<=14)
rub = "рублей";
else
{
int ostatok10 = (int) ((amount/number)%10);

if (ostatok10==1)
rub = "рубль";
else if (ostatok10>=2 && ostatok10<=4)
rub = "рубля";
else
rub = "рублей";
}

System.out.println("Каждый должен заплатить: " + String.format("%.2f",amount/number)+" "+rub);

}

}
13 changes: 9 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@


// dev branch for Y.Practicum
public class Main {

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

Calculator calc = new Calculator();
int number = calc.numberPeople();
calc.addProduct();
calc.totals();
}
}
}