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

public class Calculator {

public double splitSum(int peopleAmount, ItemsList itemsList) {
double sum = 0;
int s = itemsList.names.size();
for (int i = 0; i < s; i = i + 1) {
sum += itemsList.names.get(i).price;
//System.out.println(sum);
}
return sum / peopleAmount;
}

}
24 changes: 24 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Formatter {

public String makeRightCase(double price) {
String rightCase="руб.";
int p = (int) price;

if (p > 10 && p < 15) {
rightCase = "рублей";
return rightCase;
}

String p1 = Integer.toString(p);
int p2 = Integer.parseInt("" + p1.charAt(p1.length() - 1));
if (p2 == 1) {
rightCase = "рубль";
} else if (1 < p2 && p2 < 5) {
rightCase = "рубля";
} else if (4 < p2 || p2 == 0) {
rightCase = "рублей";
}
return rightCase;
}

}
4 changes: 4 additions & 0 deletions src/main/java/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class Item {
String name;
double price;
}
49 changes: 49 additions & 0 deletions src/main/java/ItemsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ItemsList {
ArrayList<Item> names = new ArrayList<Item>();

public void inputItemsList() {
System.out.println("Давайте добавим позиции из счета!");
while (true) {
Item item = new Item();
System.out.println("Введите текстовое наименование позиции");
while (true) {
String regex = "^[a-zA-Zа-яА-Я]+$";
Pattern pattern = Pattern.compile(regex);
item.name = MyScanner.scan.next();
Matcher matcher = pattern.matcher(item.name);
if (matcher.matches()) {
break;
} else {
System.out.println("Некорректный ввод. Название позиции может содержать только буквы");
}
}
System.out.println("Введите стоимость позиции в рублях в формате \"00.00\"");
while (true) {
try {
item.price = Double.parseDouble(MyScanner.scan.next());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Рекомендация:
Касаемо разделителей "." и ",":
На вход ты получаешь String у этого типа есть большое количество методов для преобразования строки.
Одни из которых replace(char oldChar, char newChar). С его помощью, ты можешь заменить один символ на другой, а в данном случае "," на "."

Получится вот так:

    String input = MyScanner.scan.next().replace(',', '.');
    item.price = Double.parseDouble(input);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Илья, спасибо за рекомендацию!

if (item.price <= 0) {
System.out.println("Некорректный ввод. Нужно ввести десятичное число больше нуля, с разделителем \".\"");
} else {
break;
}
} catch (NumberFormatException ignore) {
System.out.println("Некорректный ввод. Нужно ввести десятичное число больше нуля");
}
}
names.add(item);
System.out.println("Позиция " + item.name + " с ценой " + item.price + " руб. добавлена.");

System.out.println("Чтобы добавить еще одну позицию - введите любой символ.");
System.out.println("Чтобы закончить добавление - напишите \"Завершить\"");
if (MyScanner.scan.next().equalsIgnoreCase("Завершить")) {
MyScanner.scan.close();
return;
}
}
}
}

11 changes: 10 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@


public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
PersonsCount personsCount = new PersonsCount();
ItemsList itemsList = new ItemsList();
Calculator calculator = new Calculator();
ResultViewer resultViewer = new ResultViewer();

int peopleAmount = personsCount.getPeopleAmount();
itemsList.inputItemsList();
double moneyPerPerson = calculator.splitSum(peopleAmount, itemsList);
resultViewer.viewResult(itemsList, moneyPerPerson, peopleAmount);
}
}
6 changes: 6 additions & 0 deletions src/main/java/MyScanner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.util.Locale;
import java.util.Scanner;

public class MyScanner {
public static Scanner scan = new Scanner(System.in).useLocale(Locale.US);
}
22 changes: 22 additions & 0 deletions src/main/java/PersonsCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class PersonsCount {
public int getPeopleAmount() {
while (true) {

System.out.println("Пожалуйста, введите цифру, на сколько человек делим счет");
try {
int peopleAmount = MyScanner.scan.nextInt();
if (peopleAmount > 1 && peopleAmount % 1 == 0) {
//MyScanner.scan.close();
return peopleAmount;
} else {System.out.println("Должно быть целое число больше 1 ;)");}

}catch (Exception e)
{
System.out.println("Должно быть целое число больше 1 ;)");
MyScanner.scan.next();
}


}
}
}
15 changes: 15 additions & 0 deletions src/main/java/ResultViewer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class ResultViewer {

public void viewResult(ItemsList itemsList, double moneyPerPerson, int peopleAmount) {
Formatter formatter = new Formatter();
double sum = moneyPerPerson * peopleAmount;
System.out.println("Добавленные товары:");
for (int i = 0; i < itemsList.names.size(); i = i + 1) {

System.out.println(itemsList.names.get(i).name + " " + itemsList.names.get(i).price + " " + formatter.makeRightCase(itemsList.names.get(i).price));
}
System.out.println("Общая сумма счета: " + String.format("%.2f", sum) + " " + formatter.makeRightCase(sum));
System.out.println("Делим на " + peopleAmount + "-х.");
System.out.println("Итого с каждого по " + String.format("%.2f", moneyPerPerson) + " " + formatter.makeRightCase(moneyPerPerson));
}
}