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
47 changes: 47 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
public class Calculator {
String allNames = "";
double allPrices = 0;

String declension(double priceD) {
int priceI = (int) Math.floor(priceD);

String rubl = " рубль";
String rublya = " рубля";
String rubley = " рублей";

if (priceI > 100) {
priceI %= 100;
}

if (priceI > 20) {
priceI %= 10;
}

switch (priceI) {
case 1:
return rubl;
case 2:
case 3:
case 4:
return rublya;
default:
return rubley;
}
}

void add(String name, double price) {
allNames = allNames + "\n" + name + " " + String.format("%.2f", price) + declension(price);
allPrices += price;
System.out.println("Товар успешно добавлен!");
}

void out() {
System.out.println("Добавленные товары:" + allNames);
System.out.println("Общая стоимость: " + allPrices + declension(allPrices));
}

void calculate(int people) {
double one = allPrices / people;
System.out.println("Каждый должен заплатить: " + String.format("%.2f", one) + declension(one));
}
}
53 changes: 53 additions & 0 deletions src/main/java/CheckInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.Scanner;

public class CheckInput {
Scanner scanner = new Scanner(System.in);

int CheckCountPeople() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

в Java принято называть методы camelCase'ом, т.е. с маленькой буквы

while (true) {
System.out.println("На скольких человек разделить счёт?");
String s = scanner.nextLine();
try {
int countPeople = Integer.parseInt(s);
if (countPeople > 1) {
return countPeople;
} else {
System.out.println("Ошибка. Попробуйте ещё раз. Введите число > 1");
}

} catch (Exception e) {
System.out.println("Ошибка. Попробуйте ещё раз. Введите число > 1");
}
}
}

String CheckNameGood() {
while (true) {
System.out.println("Укажите название товара");
String name = scanner.nextLine();
if (name.length()> 2) {
return name;
} else {
System.out.println("Ошибка. Попробуйте ещё раз. Название должно содержать минимум 3 символ.");
}
}
}

double CheckPriceGood() {
while (true) {
System.out.println("Укажите цену товара");
String s = scanner.nextLine();
try {
double price = Double.parseDouble(s);
if (price > 0.01) {
return price;
} else {
System.out.println("Ошибка. Попробуйте ещё раз. Введите число > 0.01");
}

} catch (Exception e) {
System.out.println("Ошибка. Попробуйте ещё раз. Введите число > 0.01");
}
}
}
}
28 changes: 25 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import com.sun.tools.javac.util.Convert;

import java.util.Scanner;

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

private static final Scanner scanner = new Scanner(System.in);

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

while (true) {
String name = checkInput.CheckNameGood();
double price = checkInput.CheckPriceGood();

calculator.add(name, price); //добавление к списку и стоимости

System.out.println("Хотите добавить ещё один товар? (Для добавления нового товара - введите любой символ. Для того чтобы закончить - введите \"Завершить\".)");
if (scanner.next().equalsIgnoreCase("завершить")) {
calculator.out(); //вывод списка и общей стоимости
break;
}
scanner.skip(".*\n");
}
calculator.calculate(countPeople); //расчёт на одного человека
}
}