Skip to content
Closed
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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"
52 changes: 37 additions & 15 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
class Calculator {
import java.lang.Math;
public class Calculator {
Product product;
double fullPrice = 0;
String listOfProducts = "";
double fullPriceSharing = 0;
String rubles = "";

int friendsCount;

String cart = "Добавленные товары:";
double totalPrice = 0;

Calculator(int friendsCount) {
this.friendsCount = friendsCount;
Calculator(Product product) {
this.product = product;
}

void addItem(Item item) {
totalPrice += item.price;
cart = cart + "\n" + item.name;
void addProduct (Product product) {
fullPrice += product.getPrice();
listOfProducts += product.getName() + "\n";
}

System.out.println(item.name + " в корзине");
double everyonePays (int persons){
fullPriceSharing = fullPrice / persons;
return fullPriceSharing;
}

double divideSum() {
return totalPrice / friendsCount;
String ending (double fullPriceSharing){
if (Math.floor(fullPriceSharing) >= 11 && Math.floor(fullPriceSharing) <=19){
rubles = "Рублей";
}
else {
switch ((int) Math.floor(fullPriceSharing % 10)) {
case 1:
rubles = "Рубль";
break;
case 2:
case 3:
case 4:
rubles = "Рубля";
break;
default:
rubles = "Рублей";
break;
}
}
return rubles;
}
}
}
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.

85 changes: 54 additions & 31 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,68 @@ public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int friendCount;
int numberOfPersons = 0;
String name = "";
double price = 0.0;
Product random = new Product(name, price);
Calculator calc = new Calculator(random);

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

if (friendCount > 1) {
break;
} else if (friendCount == 1) {
System.out.println(
"Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы.");
} else {
System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз.");
System.out.println("На скольких человек нужно разделить счёт?");
if (scanner.hasNextInt()) {
numberOfPersons = scanner.nextInt();

if (numberOfPersons > 1) {
break;
} else if (numberOfPersons == 1) {
System.out.println("Вы один, счёт делить не нужно)");
} else {
System.out.println("Некорректное значение");
}
Comment on lines +19 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно заменить на switch case, что бы сделать код более читаемым

}
else{
System.out.println("Некорректное значение");
scanner.nextLine();
}
}

Calculator calculator = new Calculator(friendCount);
System.out.println("Введите название продукта");
scanner.nextLine();
name = scanner.nextLine();

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

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

calculator.addItem(new Item(name, price));
System.out.println("Введите стоимость продукта в формате XX,XX");
if(scanner.hasNextDouble()) {
price = scanner.nextDouble();
if(price >= 0) {
Product UserInput = new Product(name, price);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

переменные принято именовать с маленькой буквы

calc.addProduct(UserInput);

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

if (answer.equalsIgnoreCase("Завершить")) {
break;
System.out.println("Продукт успешно добавлен");
System.out.println("Не хотите добавить еще один продукт?");
System.out.println("Введите название продукта или");
System.out.println("Введите \"Завершить\", чтобы завершить процесс добавления товаров");
scanner.nextLine();
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("завершить")) {
break;
} else {
name = answer;
}
}
else{
System.out.println("Некорректное значение");
scanner.nextLine();
}
}
else{
System.out.println("Некорректное значение");
scanner.nextLine();
}
}

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

System.out.println(calculator.cart);
System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result));
calc.everyonePays(numberOfPersons);
calc.ending(calc.fullPriceSharing);
System.out.println("Добавленные товары:\n" + calc.listOfProducts);
System.out.println(String.format("Каждый должен заплатить %.2f" + " %s", calc.fullPriceSharing, calc.rubles));
}
}
16 changes: 16 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Product {
double price;
String name;

Product(String name, double price){
this.name = name;
this.price = price;
}
String getName (){
return this.name;
}
double getPrice (){
return this.price;
}
}