Skip to content
Closed

PR01 #146

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"
22 changes: 0 additions & 22 deletions src/main/java/Calculator.java

This file was deleted.

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

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/java/Good.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Good {
public String Name;
public double Price;

public Good(String goodName, double goodPrice) {
Name = goodName;
Price = goodPrice;
}
}
10 changes: 10 additions & 0 deletions src/main/java/InputValueController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class InputValueController {
public static boolean IsNumberOfPeopleCorrect(int countPeople) {
return countPeople > 1;
}

public static boolean IsPriceCorrect(double price) {
return price >= 0;
}
}

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

This file was deleted.

77 changes: 46 additions & 31 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,62 @@
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();
int countPeople;
TotalAmountOutput currencyNameConverter = new TotalAmountOutput();

if (friendCount > 1) {
break;
} else if (friendCount == 1) {
System.out.println(
"Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы.");
} else {
System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз.");
Scanner scanner = new Scanner(System.in);
boolean countPeopleIsCorrect;
do
{
System.out.println("На какое количество человек разделить счёт?");
countPeople = scanner.nextInt();
// проверка введенного количества людей
countPeopleIsCorrect = InputValueController.IsNumberOfPeopleCorrect(countPeople);
if (!countPeopleIsCorrect){
System.out.println("Количество должно быть больше 1");
}
}
} while (!countPeopleIsCorrect);


Calculator calculator = new Calculator(friendCount);
SplitBillCalculator calculator = new SplitBillCalculator(countPeople);

while (true) {
System.out.println("Введите название товара");
String name = scanner.next();
System.out.println("Введите наименование товара или команду 'Завершить' для окончания ввода товаров");
String goodsName = scanner.next();
if (goodsName.equalsIgnoreCase("завершить"))
break;

System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]");
double price = scanner.nextDouble();
double price;
System.out.println("Введите стоимость товара в формате 0,00");
while (true){
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
boolean isCorrect = InputValueController.IsPriceCorrect(price);
if (isCorrect)
break;
}
System.out.println("Ошибка в формате числа. Повторите ввод");
scanner.next();
}

calculator.addItem(new Item(name, price));
Good good = new Good(goodsName, price);
calculator.AddGoods(good);
String rubString = currencyNameConverter.GetCorrectRubString(calculator.TotalAmount);
System.out.printf("Товар добавлен. Текущая сумма счёта %.2f %s\n", calculator.TotalAmount, rubString);
}

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

if (answer.equalsIgnoreCase("Завершить")) {
break;
}
}
String rubString = currencyNameConverter.GetCorrectRubString(calculator.TotalAmount);
System.out.printf("Общая сумма: %.2f %s\n", calculator.TotalAmount, rubString);

double result = calculator.divideSum();
Formatter formatter = new Formatter();
double splitTotalAmount = calculator.SplitTotalAmount();
String splitRubString = currencyNameConverter.GetCorrectRubString(splitTotalAmount);
System.out.printf("Сумма с человека: %.2f %s\n", splitTotalAmount, splitRubString);

System.out.println(calculator.cart);
System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result));
System.out.println("Для завершения нажмите любую клавишу");
scanner.next();
}
}

20 changes: 20 additions & 0 deletions src/main/java/SplitBillCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class SplitBillCalculator {

int countPeople;

public String ListOfGoods = "";
public double TotalAmount = 0;

public SplitBillCalculator(int countPeople) {
this.countPeople = countPeople;
}

public void AddGoods(Good good) {
ListOfGoods = ListOfGoods +"\n"+ good.Name;
TotalAmount = TotalAmount + good.Price;
}

public double SplitTotalAmount() {
return TotalAmount / countPeople;
}
}
26 changes: 26 additions & 0 deletions src/main/java/TotalAmountOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class TotalAmountOutput {
public String GetCorrectRubString(double totalAmount) {
int lastDigit = GetLastIntDigit(totalAmount);

switch (lastDigit) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}

//получить последнюю цифру из числа для определения падежа слова рубль
private int GetLastIntDigit (double totalAmount) {
int intTotalAmount = (int) totalAmount;
String stringTotalAmount = String.valueOf(intTotalAmount);
int length = stringTotalAmount.length();
char lastDigit = stringTotalAmount.charAt(length-1);
return Character.getNumericValue(lastDigit);
}
}