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"
62 changes: 49 additions & 13 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
class Calculator {
import java.util.Scanner;

int friendsCount;
public class Calculator {
public static void calculator(int delitel) {

String cart = "Добавленные товары:";
double totalPrice = 0;
Scanner scannerProduct = new Scanner(System.in);
Product productAll = new Product();
while (true) {
System.out.println("Введите название товара");
String product = scannerProduct.next();
String and = "Завершить";
if (product.equalsIgnoreCase(and)) {
break;
}

Calculator(int friendsCount) {
this.friendsCount = friendsCount;
}
System.out.println("Введите стоимость товара (рубли.копейки)");
Float price = cost();
productAll.allProduct = productAll.allProduct + "\n" + product ;
productAll.allPrice = productAll.allPrice + price;
System.out.println("Товар добавлен, хотите добавить еще товар ? \n Если нет, введите команду \"Завершить\"");
String answer = scannerProduct.next();
if (answer.equalsIgnoreCase(and)) {
break;
}

void addItem(Item item) {
totalPrice += item.price;
cart = cart + "\n" + item.name;
}
float personPrice = productAll.allPrice / delitel;
String personPriceString = String.format("%.2f", personPrice);
int personPriceInteger = (int) personPrice;
String rub = PrintRub.getStringRub(personPriceInteger);
System.out.println("Добавленные товары:" + productAll.allProduct + "\n" + "Сумма к оплате для каждого человека - " + personPriceString + " " + rub);

System.out.println(item.name + " в корзине");
}

double divideSum() {
return totalPrice / friendsCount;
//введение цены
public static float cost() {
Scanner scanner = new Scanner(System.in);
float price = 0.0f;
try {
String value = scanner.next().trim();
price = Float.parseFloat(value);
int pointPosition = value.indexOf('.');
if (!(pointPosition == -1 || value.length() - 1 - pointPosition <= 2)) {
System.out.println("Введено некорректное значение, попробуйте еще.");
price = cost();
}
if (price < 0) {
System.out.println("Введено некорректное значение, попробуйте еще.");
price = cost();
}
} catch (Exception e) {
System.out.println("Введено некорректное значение, попробуйте еще.");
price = cost();
}

return price;
}
}
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.

133 changes: 96 additions & 37 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,106 @@
public class Main {

public static void main(String[] args) {
System.out.println("Здравствуйте! На сколько человек разделить счёт?");
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("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз.");
}
}

Calculator calculator = new Calculator(friendCount);

int x = 0;
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(
"Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления");
String answer = scanner.next();

if (answer.equalsIgnoreCase("Завершить")) {
break;
try {
x = scanner.nextInt();
if (x >= 2) { // если пользователь вводит корректное значение
System.out.println();
break;
}
} catch (Exception e) {
scanner.next();
}
System.out.println("Введено некорректное значение, попробуйте еще.");
}
Calculator.calculator(x);
}

double result = calculator.divideSum();
Formatter formatter = new Formatter();
//
//
// public static void calculator(int delitel) {
//
// Scanner scannerProduct = new Scanner(System.in);
// String allProduct = "";
// Float allPrice = 0.0f;
// while (true) {
// System.out.println("Введите название товара");
// String product = scannerProduct.next();
// String and = "Завершить";
// if (product.equalsIgnoreCase(and)) {
// break;
// }
//
// System.out.println("Введите стоимость товара (рубли.копейки)");
// Float price = cost();
// allProduct = allProduct + "\n" + product + ": " + price;
// allPrice = allPrice + price;
// System.out.println("Товар добавлен, хотите добавить еще товар ? \n Если нет, введите команду \"Завершить\"");
// String answer = scannerProduct.next();
// if (answer.equalsIgnoreCase(and)) {
// break;
// }
//
// }
// float personPrice = allPrice / delitel;
// String personPriceString = String.format("%.2f", personPrice);
// int personPriceInteger = (int) personPrice;
// String rub = conjugation(personPriceInteger);
// System.out.println("Добавленные товары:" + "\n" + allProduct + "\n" + "Сумма к оплате для каждого человека - " + personPriceString + rub);
//
// }
//
// //введение цены
// public static float cost() {
// Scanner scanner = new Scanner(System.in);
// float price = 0.0f;
// try {
// String value = scanner.next().trim();
// price = Float.parseFloat(value);
// int pointPosition = value.indexOf('.');
// if (!(pointPosition == -1 || value.length() - 1 - pointPosition <= 2)) {
// System.out.println("Введено некорректное значение, попробуйте еще.");
// price = cost();
// }
// if (price > 0) {
// System.out.println("Введено некорректное значение, попробуйте еще.");
// price = cost();
// }
// } catch (Exception e) {
// System.out.println("Введено некорректное значение, попробуйте еще.");
// price = cost();
// }
//
// return price;
// }

System.out.println(calculator.cart);
System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result));
}
// public static String conjugation(int integer) {
// int remainder = integer % 100;
// if (remainder >= 11 && remainder <= 14) {
// return "рублей";
// }
// remainder = integer % 10;
// switch (remainder) {
// case 1:
// return "рубль";
// case 2:
// case 3:
// case 4:
// return "рубля";
// case 0:
// case 5:
// case 6:
// case 7:
// case 8:
// case 9:
// return "рублей";
// default:
// return "рубль";
// }
//
// }
}

27 changes: 27 additions & 0 deletions src/main/java/PrintRub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class PrintRub {
public static String getStringRub(int integer) {
int remainder = integer % 100;
if (remainder >= 11 && remainder <= 14) {
return "рублей";
}
remainder = integer % 10;
switch (remainder) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
case 0:
case 5:
case 6:
case 7:
case 8:
case 9:
return "рублей";
default:
return "рубль";
}

}
}
5 changes: 5 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Product {
String allProduct = "";
Float allPrice = 0.0f;

}