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
11 changes: 11 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Calculator {
double totalPrice;
int numberOfPerson;
double priceToPayment;

public void calculate(double totalPrice, int numberOfPerson){
this.numberOfPerson = numberOfPerson;
this.totalPrice = totalPrice;
priceToPayment = totalPrice / numberOfPerson;
}
}
32 changes: 32 additions & 0 deletions src/main/java/Formater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Formater {
String rubl;

public String declination(double num){
double numToFormat = Math.floor(num);
double lastDigit = numToFormat % 10; //отделяем последнюю цифру
numToFormat /= 10; //готовим число для отделения еще одной цифры
double secondToLastDigit = numToFormat % 10; //отделяем предпоследнюю цифру

if (secondToLastDigit == 1){
return rubl = "рублей";
} else {
switch (String.valueOf(lastDigit)) {
case "1":
return rubl = "рубль";
case "2":
return rubl = "рубля";
case "3":
return rubl = "рубля";
case "4":
return rubl = "рубля";
default:
return rubl = "рублей";
}
}
}

public String rounding(double price){
return String.format("%.2f", price);
}
}

13 changes: 13 additions & 0 deletions src/main/java/ListOfProduct.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class ListOfProduct {
String listToByu = "";
String nameOfProduct;
double price;

public String addToProductList(String nameOfProduct, double price){
this.nameOfProduct = nameOfProduct;
this.price = price;
Formater formater = new Formater();
return listToByu = listToByu + (nameOfProduct + " - " + formater.rounding(price) + " "
+ formater.declination(price) + "\n");
}
}
80 changes: 76 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,80 @@
import java.util.Scanner;

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

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


System.out.println("На сколько человек необходимо разделить счет");
int numberOfPerson;
while (!scanner.hasNextInt()){
System.out.println("Некорректный ввод,повторите попытку");
scanner.next();
}
numberOfPerson = scanner.nextInt();

while (true){
if (numberOfPerson <= 1){
System.out.println("Некорректный ввод,повторите попытку");
while (!scanner.hasNextInt()){
System.out.println("Некорректный ввод,повторите попытку");
scanner.next();
}
numberOfPerson = scanner.nextInt();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тут соотвественно тоже самое


} else {
break;
}
}
System.out.println("Счет необходимо разделить на " + numberOfPerson);

Calculator calculator = new Calculator(); //объект для проведения расчетов
calculator.numberOfPerson = numberOfPerson;

ListOfProduct listOfProducts = new ListOfProduct(); //объект для хранения списка покупок
while (true){
System.out.println("Введите название товара или команду завершить");
listOfProducts.nameOfProduct = scanner.next();

String conditionToExit = "завершить";
if (conditionToExit.equalsIgnoreCase(listOfProducts.nameOfProduct)){
break;
}

System.out.println("Введите цену на товар");
while (!scanner.hasNextDouble()){
System.out.println("Некорректный ввод,повторите попытку");
scanner.next();
}
listOfProducts.price = scanner.nextDouble();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

И тут только метод проверки - hasNextDouble

while (listOfProducts.price < 0){
System.out.println("Некорректный ввод, повторите попытку");
while (!scanner.hasNextDouble()){
System.out.println("Некорректный ввод,повторите попытку");
scanner.next();
}
listOfProducts.price = scanner.nextDouble();
}

listOfProducts.listToByu = listOfProducts.addToProductList(listOfProducts.nameOfProduct, listOfProducts.price);

System.out.println("Товар успешно добавлен");
calculator.totalPrice = calculator.totalPrice + listOfProducts.price;
}

if (calculator.totalPrice == 0){
System.out.println("У Вас нет покупок");
} else {
String rubl = formater.declination(calculator.totalPrice);
System.out.println("Вы купили:" + "\n" + listOfProducts.listToByu + "На сумму: "
+ formater.rounding(calculator.totalPrice) + " " + rubl);

calculator.calculate(calculator.totalPrice, numberOfPerson);
System.out.println("Каждому необходимо заплатить по: " + formater.rounding(calculator.priceToPayment) + " "
+ formater.declination(calculator.priceToPayment));
}
}
}
}