Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Внесение полученных замечаний #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
niksshg wants to merge 4 commits into main
base: main
Choose a base branch
Loading
from master
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
View file Open in desktop
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"
48 changes: 35 additions & 13 deletions src/main/java/Calculator.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
class Calculator {
public class Calculator {

int friendsCount;
static double bill;

String cart = "Добавленные товары:";
double totalPrice = 0;
public static void addProduct(Product p) {
bill += p.getPrice();
System.out.println(bill);
}

Calculator(int friendsCount) {
this.friendsCount = friendsCount;
public static String calculateBill(int numberOfCustomers) {
return correctCurrency(Math.floor(bill / numberOfCustomers));
}

void addItem(Item item) {
totalPrice += item.price;
cart = cart + "\n" + item.name;
public static String correctCurrency (double value) {
int twoDigits = (int) value % 100;
int lastValue = (int) value % 10;
String correctFormat = value + " ";

System.out.println(item.name + " в корзине");
if (String.valueOf(Math.abs((long)twoDigits)).charAt(0) == '1' && twoDigits >= 10 && twoDigits < 20) {
correctFormat += "рублей";
} else {
switch (lastValue) {
case 5:
case 6:
case 7:
case 8:
case 9:
case 0:
correctFormat += "рублей";
break;
case 1:
correctFormat += "рубль";
break;
case 2:
case 3:
case 4:
correctFormat += "рубля";
break;
}
}
return correctFormat;
}

double divideSum() {
return totalPrice / friendsCount;
}
}
17 changes: 0 additions & 17 deletions src/main/java/Formatter.java
View file Open in desktop

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/Item.java
View file Open in desktop

This file was deleted.

94 changes: 62 additions & 32 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,49 +1,79 @@
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
static ArrayList<Product> products = new ArrayList<>();

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean correctCustomersNumberEntered = false;
int customers = 0;
Scanner s = new Scanner(System.in);
System.out.println("На скольких человек необходимо разделить счёт?");
do {
try {
customers = s.nextInt();
correctCustomersNumberEntered = true;

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("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз.");

} catch (InputMismatchException e) {
System.out.println ("Это некорректное значение гостей. Введите корректное количество гостей.");
s.nextLine();
}
}
} while (!correctCustomersNumberEntered);

Calculator calculator = new Calculator(friendCount);
while (customers <= 1) {
System.out.println("Это некорректное значение для подсчёта.\nВведите корректное количество гостей. Если гостей меньше двух, то расчет не требуется");
customers = s.nextInt();
}

outer:
while (true) {
System.out.println("Введите название товара");
String name = scanner.next();
System.out.println("Укажите наименование товара:");
String product = s.next();
System.out.println("Укажите стоимость товара в формате 'рубли.копейки' [10.45, 11.40]:");
double price = 0;
boolean correctPriceEntered = false;
do {

System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]");
double price = scanner.nextDouble();
try {
price = s.nextDouble();
if (price <= 0) {
System.out.println("Некорректный ввод.Товар должен быть указан в корректном формате 'рубли.копейки' [10.45, 11.40]");
s.nextLine();
} else {
correctPriceEntered = true;
}
} catch (InputMismatchException e) {
System.out.println("Товар должен быть указан в корректном формате 'рубли.копейки' [10.45, 11.40]");
s.nextLine();
}
} while (!correctPriceEntered);

calculator.addItem(new Item(name, price));
Product p = new Product(product, price);
Calculator.addProduct(p);
products.add(p);

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

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

double result = calculator.divideSum();
Formatter formatter = new Formatter();
String enough;
do {
System.out.println("Добавить еще товаров в заказ (Да/Нет)?");
enough = s.next();
if (enough.equalsIgnoreCase("нет")) {
System.out.println("Добавленные товары:");
for (int i = 0; i < products.size(); i++) {
System.out.println(products.get(i).getLabel());
}
System.out.println("Каждый поситетль должен заплатить" + " " + Calculator.calculateBill(customers));
break outer;
} else if (enough.equalsIgnoreCase("да")) {
break;
} else {
System.out.println("Некорректный ввод");
}

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

19 changes: 19 additions & 0 deletions src/main/java/Product.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Product {
String label;
double price;

Product(String label, double price) {
this.label = label;
this.price = price;

}

public String getLabel() {
return label;
}

public double getPrice() {
return price;
}

}

AltStyle によって変換されたページ (->オリジナル) /