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


public class Calculator {
String product = "";
double totalPrice;
double personalAmount;

public void addProduct(String productName, double costGoods) {

product = product + productName +" "+":"+" "+Formatter.roundingCostProduct(costGoods) +" "+ Formatter.rubleCorrectCase(costGoods) + "\n" ;
}

public void countingGoods(int numberPeople, double costGoods) {

totalPrice = totalPrice + costGoods;
personalAmount = totalPrice / numberPeople;
}
}
33 changes: 33 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Formatter {

public static String roundingCostProduct(double izadadd) {

String format = String.format("%.2f", izadadd);
return format;
}

public static String rubleCorrectCase(double price) {

int count = (int) Math.floor(price);

{
double preLastDigit = count % 100 / 10;
if (preLastDigit == 1)
{
return "рублей";
}

switch (count % 10)
{
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
}
}
101 changes: 100 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,105 @@
import java.util.Scanner;

public class Main {

private static boolean isNumber(String value) {
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException ex) {
return false;
}
}

private static boolean isDouble(String value) {
try {
Double.parseDouble(value);
return true;
} catch (NumberFormatException ex) {
return false;
}
}

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

//После запуска программа должна спрашивать у пользователя, на скольких человек необходимо разделить счёт.


int numberPeople;
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите количество людей (больше или равно 1)");
String value = scanner.next();
boolean isNumber = isNumber(value);

if (isNumber) {
numberPeople = Integer.parseInt(value);

if (numberPeople == 1) {
System.out.println("Нет смысла ничего считать и делить.");
} else if (numberPeople < 1) {
System.out.println("Это некорректное значение для подсчёта.");
} else if (numberPeople > 1) {
break;
}
} else {
System.out.println("Введите корректное количество гостей.");
}
}




//Запросите у пользователя название товара и его стоимость.
Calculator calculator = new Calculator();


double costGoods = 0;
while (true) {

Scanner scannerr = new Scanner(System.in);

System.out.println("Введите название товара");
String productName = scannerr.nextLine();

while (true) {
System.out.println("Введите стоимость товара (в формате руб.копейки) ");
String costG = scannerr.next();

boolean isDouble = isDouble(costG);

if (isDouble) {
costGoods = Double.parseDouble(costG);

if (costGoods < 1) {
System.out.println("Это некорректное значение для подсчёта.");
} else if (costGoods > 0) {
break;
}
} else {
System.out.println("Введите корректное количество стоимость товара.");
}
}


calculator.addProduct(productName, costGoods);
System.out.println("Товар успешно добавлен");

calculator.countingGoods( numberPeople, costGoods);

System.out.println("Хотите добавить еще один товар или Завершить?");
String addOrFinish = scannerr.next();

if (addOrFinish.equalsIgnoreCase("Завершить")) {
System.out.println("Все добавленные товары : " +
"\n " + calculator.product +
"\n" + "Общая сумма счета :" + " " +Formatter.roundingCostProduct(calculator.totalPrice)+" "+Formatter.rubleCorrectCase(calculator.totalPrice) +
"\n" + "Сумма счета на каждого человека :" + " " +Formatter.roundingCostProduct(calculator.personalAmount)+" "+ Formatter.rubleCorrectCase(calculator.personalAmount));
break;

}

}

}
}