Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
eaeb79b
добавлен ввод гостей
Feb 3, 2024
7a946f8
добавлен ввод гостей
Feb 3, 2024
90066c4
добавлен ввод гостей
Feb 3, 2024
a5d2071
добавлен ввод гостей
Feb 3, 2024
3849b18
добавлен ввод гостей
Feb 3, 2024
16de333
добавлен ввод гостей
Feb 3, 2024
4531b81
добавлен ввод гостей
Feb 3, 2024
871c611
добавлен ввод гостей
Feb 3, 2024
1c3d64d
добавлен ввод гостей
Feb 3, 2024
216ca84
добавлен ввод гостей
Feb 3, 2024
9b25bab
+ добавление товаров
Feb 3, 2024
82aeb97
+ добавление товаров
Feb 3, 2024
17bce08
+ добавление товаров
Feb 3, 2024
fae22af
+ добавление товаров
Feb 3, 2024
1e33aa9
программа завершена
Feb 5, 2024
d5d1514
Merge remote-tracking branch 'origin/work' into work
Feb 5, 2024
fef4b96
Merge remote-tracking branch 'origin/work' into work
Feb 6, 2024
67b6033
Merge remote-tracking branch 'origin/work' into work
Feb 6, 2024
4331577
Merge remote-tracking branch 'origin/work' into work
Feb 6, 2024
5dfefb3
Merge remote-tracking branch 'origin/work' into work
Feb 6, 2024
131313b
Merge remote-tracking branch 'origin/work' into work
Feb 6, 2024
1d09e4c
Merge remote-tracking branch 'origin/work' into work
Feb 6, 2024
a900a60
Merge remote-tracking branch 'origin/work' into work
Feb 6, 2024
4309f9c
Merge remote-tracking branch 'origin/work' into work
Feb 8, 2024
a196471
Merge remote-tracking branch 'origin/work' into work
Feb 8, 2024
b939533
Merge remote-tracking branch 'origin/work' into work
Feb 8, 2024
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
81 changes: 81 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Locale;

public class Calculator {
Scanner scanner = new Scanner(System.in);
int numGuests;
ArrayList<String> goodsNamesList = new ArrayList<>();
ArrayList<Double> goodsPricesList = new ArrayList<>();
String goodsName;
double goodsPrice;
double sum = 0.00;

public void start() {
System.out.println("Добро пожаловать в программу 'Калькулятор счета'.");
System.out.println("Укажите в числовом виде количество гостей, на которых необходимо разделить счет:");

while (true) {
String textInput = scanner.nextLine();
if (Checking.isInt(textInput)) {
numGuests = Integer.parseInt(textInput);
} else {
continue;
}
if (numGuests == 1) {
System.out.println("Для одного гостя калькулятор не нужен! Пожалуйста введите число больше 1:");
} else if (numGuests < 1) {
System.out.println("Некорректный ввод! Пожалуйста введите число больше 1:");
} else {
break;
}
}

while (true) {
System.out.println("Укажите название товара:");
String textInput = scanner.nextLine();
if (Checking.isNotEmpty(textInput)) {
goodsName = textInput;
} else {
continue;
}

while (true) {
System.out.println("Укажите стоимость товара в формате 'рубли.копейки':");
textInput = scanner.nextLine();
if (Checking.isDouble(textInput)) {
goodsPrice = Double.parseDouble(textInput);
} else {
continue;
}
if (goodsPrice < 0) {
System.out.print("Стоимость товара не может быть меньше 0! ");
} else {
break;
}
}

Goods product = new Goods(goodsName, goodsPrice);
goodsNamesList.add(product.name);
goodsPricesList.add(product.price);
sum += product.price;
System.out.println("Товар '" + product.name + "' успешно добавлен.");

System.out.println("Добавить еще товар? Если нет, введите 'Завершить':");
if (scanner.nextLine().equalsIgnoreCase("завершить")) {
break;
}
}

System.out.println("Добавленные товары:");
for (int i = 0; i < goodsNamesList.size(); i++) {
System.out.printf(Locale.ROOT, "%s %.2f %s%n", goodsNamesList.get(i), goodsPricesList.get(i), Format.formatRubles(goodsPricesList.get(i)));
}

System.out.println("--------------------");
double result = sum / numGuests;
String rubles = Format.formatRubles(result);
String resultOutput = String.format(Locale.ROOT,"Каждый гость должен заплатить: %.2f %s", result, rubles);
System.out.println(resultOutput);
}
}
56 changes: 56 additions & 0 deletions src/main/java/Checking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
public class Checking {
public static boolean isInt(String input) {

if (input == null || input.isEmpty()) {
System.out.println("Нулевая строка! Введите числовое значение:");
return false;
}

try {
Integer.parseInt(input);
return true;
} catch (NumberFormatException e) {
System.out.println("Должно быть указано числовое значение! Пожалуйста повторите ввод:");
}
return false;
}

public static boolean isNotEmpty(String input) {
if (input == null || input.isEmpty()) {
System.out.print("Нулевая строка! ");
return false;
}
return true;
}

public static boolean isDouble(String input) {

if (input == null || input.isEmpty()) {
System.out.print("Нулевая строка! ");
return false;
}

try {
Double.parseDouble(input);
if (input.indexOf('.') != -1) {
String[] splitter = input.split("\\.");
if (splitter[1].length() > 2) {
System.out.print("Количество копеек не может быть больше 100! ");
return false;
} else {
return true;
}
} else {
return true;
}
} catch (NumberFormatException e) {
System.out.print("Должно быть указано числовое значение! ");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.print("Формат введенной стоимости не соответствует 'рубли.копейки'! ");
}
return false;
}
}


13 changes: 13 additions & 0 deletions src/main/java/Format.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Format {
public static String formatRubles(double rubles) {
int intRubles10 = (int) Math.floor(rubles) % 10;
int intRubles100 = (int) Math.floor(rubles) % 100;
if (intRubles10 == 1 && (intRubles100 < 11 || intRubles100 > 19)) {
return "рубль";
} else if (intRubles10 >= 2 && intRubles10 <= 4 && (intRubles100 < 11 || intRubles100 > 19)) {
return "рубля";
} else {
return "рублей";
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/Goods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Goods {
String name;
double price;

public Goods(String name, double price) {
this.name = name;
this.price = price;
}
}
5 changes: 4 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@


public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calculator = new Calculator();
calculator.start();
}
}