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



public class Calculator {
public static final Scanner scanner = new Scanner(System.in);
String productNames;
double sumMoney;
Calculator(String productNames, double sumMoney){
this.productNames = productNames;
this.sumMoney = sumMoney;
}
void addProduct(){
System.out.println("Введите название нового товара ");
String nameOfProduct = scanner.next();


System.out.println("Введите цену товара");
double priceOfProduct = checkInputPrice();



System.out.println("\nТовар успешно добавлен! \n");
Product prod = new Product(nameOfProduct, priceOfProduct);
productNames += nameOfProduct + "\n";
sumMoney += priceOfProduct;

}

//Проверка ввода корректной цены
public static double checkInputPrice(){
double userInputDouble;
while (true) {

String userInput = scanner.next();
if (isDouble(userInput)) {
userInputDouble = Double.parseDouble(userInput);
if (userInputDouble > 0)
break;
else
System.out.println("Введите корректную цену товара больше нуля!");
} else
System.out.println("Введите корректную цену товара!");

}
return userInputDouble;
}


//Функция проверки на ввод дробного числа
public static boolean isDouble(String x) throws NumberFormatException
{
try {
Double.parseDouble(x);
return true;
} catch(Exception e) {
return false;
}
}

}
86 changes: 84 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,88 @@
import java.util.*;

public class Main {
public static final Scanner scanner = new Scanner(System.in);



public static void main(String[] args) {
System.out.println("Hello world!");

//Запросили число гостей
System.out.println("Сколько у нас гостей?");
int peopleNumber = checkInputGuests();


Product prod = new Product("", 0);
Calculator calc = new Calculator("", 0);

//Бесконечный ввод товаров до завершения
String askToContinue;

while (true){
calc.addProduct();
System.out.println("Хотите продолжить ввод товаров?\nДля продолжения введите любой символ\nДля завершения введите \"Завершить\" в любом регистре\n");
askToContinue = scanner.next();
if (askToContinue.equalsIgnoreCase("Завершить"))
break;
}

//Подсчёт и вывод результатов
System.out.println("Добавленные товары:\n" + calc.productNames);

double oneGuestSum = calc.sumMoney / peopleNumber;
int OGS = (int) Math.floor(oneGuestSum);
String letter = checkCorrectLetter(OGS);
System.out.println(String.format("Каждый должен заплатить: %.2f рубл%s", oneGuestSum, letter));


}

//Поиск правильного окончания слова рубль
public static String checkCorrectLetter(int OGS){
int temp = OGS % 100;
if (temp % 10 == 1 && temp != 11)
return "ь";
else if ((temp % 10 == 2 || temp % 10 == 3 || temp % 10 == 4) && (temp != 12 && temp != 13 && temp!= 14))
return "я";
else
return "ей";
}
}


//Проверка ввода корректного числа гостей
public static int checkInputGuests(){
int userInputInt;
while (true) {

String userInput = scanner.next();
if (isInt(userInput)) {
userInputInt = Integer.parseInt(userInput);
if (userInputInt> 1)
break;
else
System.out.println("Введите корректное число гостей больше 1!");
} else
System.out.println("Введите корректное число гостей!");

}
return userInputInt;
}

//Функция проверки на ввод целого числа
public static boolean isInt(String x) throws NumberFormatException
{
try {
Integer.parseInt(x);
return true;
} catch(Exception e) {
return false;
}
}






}

9 changes: 9 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//Класс товаров
public class Product {
String name;
double price;
Product (String name, double price){
this.name = name;
this.price = price;
}
}