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
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ dependencyResolutionManagement {
mavenCentral()
}
}
rootProject.name = "BillCalculator"
rootProject.name = "Java-Module-Project"
65 changes: 52 additions & 13 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@
class Calculator {
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

int friendsCount;
public class Calculator {

String cart = "Добавленные товары:";
double totalPrice = 0;
List<String> menuList = new LinkedList<>();

Calculator(int friendsCount) {
this.friendsCount = friendsCount;
}
String stopWord = "завершить";
String dishes;
double price;
static double priceSum;

void addItem(Item item) {
totalPrice += item.price;
cart = cart + "\n" + item.name;
public void costCalculator() {

System.out.println(item.name + " в корзине");
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название еды:");
dishes = scanner.nextLine();
menuList.add(dishes);
System.out.println("Введите стоимость в формате: \"'рубли.копейки' [10,45; 11,40]\"");
while (!scanner.hasNextDouble()) {
System.out.println("Вводить нужно только цифры! Попробуйте заново:");
scanner.nextLine();
}
price = scanner.nextDouble();
if (price > 0) {
priceSum = priceSum + price;
System.out.println("Товар успешно добавлен! ");
}
while (price <= 0) {
if (price <= 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Из-за строчки выше всегда будет true, можно убрать

System.out.println("Цена отрицательная, доходы мы еще не считаем! Введите заново: ");
price = scanner.nextDouble();
if (price > 0) {
priceSum = priceSum + price;
System.out.println("Товар успешно добавлен! ");
break;
}
}
}
System.out.println("Если хотите продолжить введите - любой символ,если хотите остановиться введите 'завершить' ");
dishes = scanner.next();
if (dishes.equalsIgnoreCase(stopWord)) {
break;
}
}
}

double divideSum() {
return totalPrice / friendsCount;
static String costRounding(double i) {
i = Math.floor(i);
double modularDivision = i % 10;
if (modularDivision == 1) {
return "рубль";
} else if (modularDivision == 2 || modularDivision == 3 || modularDivision == 4) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно упростить modularDivision >= 2 && modularDivision <= 4

return "рубля";
}
return "рублей";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Не учтена ситуация для чисел 11-19, окончание должно быть "рублей", а сейчас дает рубля для 12-14. Для этого стоит проверять, что i % 100 между 11 и 19 включительно находится

}
}

17 changes: 0 additions & 17 deletions src/main/java/Formatter.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/Item.java

This file was deleted.

60 changes: 28 additions & 32 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,44 @@

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int friendCount;
while (true) {
System.out.println("На сколько человек необходимо разделить счет?");
friendCount = scanner.nextInt();

if (friendCount > 1) {
break;
} else if (friendCount == 1) {
System.out.println(
"Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы.");
} else {
System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз.");
Scanner scanner = new Scanner(System.in);
System.out.println("Введите количество людей:");
while (!scanner.hasNextInt()) {
System.out.println("Вводить нужно только цифры! Попробуйте заново:");
scanner.nextLine();
}
int numberOfPeople = scanner.nextInt();
while (numberOfPeople <= 1) {
if (numberOfPeople < 1) {
System.out.println("Введенно не корректное количество людей! Попробуйте занво:");
numberOfPeople = scanner.nextInt();
}
if (numberOfPeople == 1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно поставить просто else

System.out.println("Вы один и платите только вы. Попробуйте занво: ");
numberOfPeople = scanner.nextInt();
}
}

Calculator calculator = new Calculator(friendCount);
Calculator calculator = new Calculator();
calculator.costCalculator();

while (true) {
System.out.println("Введите название товара");
String name = scanner.next();
double needPay = Calculator.priceSum / numberOfPeople;

System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]");
double price = scanner.nextDouble();
System.out.println("Список выбранных товаров: ");

calculator.addItem(new Item(name, price));
for (String str : calculator.menuList) {
System.out.println(str);
}
System.out.printf("Общая сумма: %.2f %s%n", Calculator.priceSum, Calculator.costRounding(Calculator.priceSum));
System.out.printf("Кол-во человек: %s%n", numberOfPeople);
System.out.printf("Каждый платит по: %.2f %s%n", needPay, Calculator.costRounding(needPay));

System.out.println(
"Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления");
String answer = scanner.next();

if (answer.equalsIgnoreCase("Завершить")) {
break;
}
}
}


double result = calculator.divideSum();
Formatter formatter = new Formatter();

System.out.println(calculator.cart);
System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result));
}
}