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
26 changes: 26 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Scanner;
public class Calculator {
public static String processProducts(){
Scanner scanner = new Scanner(System.in);

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

return name;

}

public static double receiveTheCostOfTheProduct(){
Scanner scanner = new Scanner(System.in);
String cost;
System.out.println("Введите стоимость товара в формате рубли.копейки.");
while (true) {
cost = scanner.nextLine();
if (Formatter.checksForPrice(cost)) {
break;
}
}

return Double.parseDouble(cost);
}
}
57 changes: 57 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.Scanner;

public class Formatter {
public static void main(String[] args) {
}
public static String GetDeclinationRub(int num) {
var penultimateDigit = num % 100 / 10;
if (penultimateDigit == 1)
{
return "рублей.";
}

switch (num % 10)
{
case 1:
return "рубль.";
case 2:
case 3:
case 4:
return "рубля.";
default:
return "рублей.";
}
}
public static boolean checksForInput(String input) {
boolean proverka = false;
try {
int people = Integer.parseInt(input);
if (people == 1) {
System.out.println("Количество людей должно быть больше одного. Повторите ввод.");
} else if (people < 1) {
System.out.println("Некорректное значение для подсчёта. Повторите ввод.");
}else {
proverka = true;
}
} catch (NumberFormatException e) {
System.out.println("Некорректное значение для подсчёта. Повторите ввод.");
proverka = false;
}
return proverka;
}
public static boolean checksForPrice(String input){
boolean proverka = false;
try {
Double cost = Double.parseDouble(input);
if (cost <= 0) {
System.out.println("Некоректное значение, повторите ввод.");
} else {
proverka = true;
}
} catch (NumberFormatException e) {
System.out.println("Некорректное значение. Повторите ввод.");
proverka = false;
}
return proverka;
}
}
32 changes: 31 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);

double sum = 0.0;
String people;
String allProducts = "";

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

while (true) {
people = scanner.nextLine();

if (Formatter.checksForInput(people)) {
break;
}
}
while (true) {
allProducts += Calculator.processProducts() + "\n";

sum += Calculator.receiveTheCostOfTheProduct() ;

System.out.println("Товар успешно добавлен.\nНапишите \"Да\", чтобы продолжить или \"Завершить\", чтобы закрыть программу. ");
String answer = scanner.next();
if (answer.equalsIgnoreCase("Завершить")) {
break;
}
}
double priceForOnePerson = (int)sum / Integer.parseInt(people);
String format = "Каждый человек должен заплатить %.2f ";
System.out.println("Добавленные товары:" + "\n" + allProducts);
System.out.println(String.format(format, priceForOnePerson) + Formatter.GetDeclinationRub((int) Math.floor(priceForOnePerson)));
}
}