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

int friendsCount;
public class Calculator {
public static double calcul(){
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название товара");
String nameOfProduct = scanner.nextLine();
double costOfProduct = checkPrice();
double sum = 0.0;
sum += costOfProduct;
String outMess = String.format("\nДобавленные товары: \n" + nameOfProduct + " %.2f", costOfProduct)+ " " + CorrectWriting.writingRub((int)Math.floor(costOfProduct));
System.out.println("Все добавлено. Введите Завершить для окончания сессии либо любой другой символ для продолжения");
while(true){
if (scanner.next().equalsIgnoreCase("Завершить")) {//игнор регистра
System.out.println(outMess);
break;
}
else{
System.out.println("Введите название товара");
scanner.nextLine();
nameOfProduct = scanner.nextLine();
costOfProduct = checkPrice();
sum += costOfProduct;
outMess = String.format("\nДобавленные товары: \n" + nameOfProduct + " %.2f", costOfProduct) + " " + CorrectWriting.writingRub((int)Math.floor(costOfProduct));
System.out.println("Все добавлено. Введите Завершить для окончания сессии либо любой другой символ для продолжения");
}

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

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

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

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

double divideSum() {
return totalPrice / friendsCount;
public static double checkPrice(){
Scanner scanner = new Scanner(System.in);
System.out.println("Введите стоимость товара в формате 00,00 руб, коп");
double costOfProduct;
while(scanner.hasNextDouble() == false){
System.out.println("Введите стоимость товара в формате 00,00 руб, коп, попробуйте еще раз");
scanner.next();
}
costOfProduct = scanner.nextDouble();
while(costOfProduct <= 0){
System.out.println("Ожидается стоимость товара положительная");
costOfProduct = scanner.nextDouble();
}
return costOfProduct;
}
}
14 changes: 14 additions & 0 deletions src/main/java/CorrectWriting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class CorrectWriting {
public static String writingRub(int countOfRub){
String str;
if (countOfRub % 10 > 1 && countOfRub % 10 <=4)
str = "рубля";
else if (countOfRub % 10 == 1)
str = "рубль";
else if (countOfRub % 100 > 4 && countOfRub % 100 <= 20)
str = "рублей";
else
str = "руюлей";
return str;
}
}
17 changes: 0 additions & 17 deletions src/main/java/Formatter.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/java/InputValidation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.Scanner;

public class InputValidation {
public static int askCount(){
Scanner scanner = new Scanner(System.in);
System.out.println("На сколько человек нужно разделить счет?");
int countOfPeople;
while(true){//бесконечный цикл для того, чтобы программа попросила ввести снова количество людей
if(scanner.hasNextInt()==false){//проверка на целое
System.out.println("Ожидалось число. Try again!");
scanner.next();
}
else{
countOfPeople = scanner.nextInt();
if(countOfPeople <= 1)
System.out.println("Ожидалось больше 1 человека. Попробуйте еще раз!");
else
break;
}
}
return countOfPeople;
}
}
10 changes: 0 additions & 10 deletions src/main/java/Item.java

This file was deleted.

51 changes: 8 additions & 43 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
import java.util.Scanner;

public class Main {
private static final Scanner scanner = new Scanner(System.in);

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

Calculator calculator = new Calculator(friendCount);

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;
}
}

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

System.out.println(calculator.cart);
System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result));
System.out.println("Добро пожаловать в калькулятор!");
int count = InputValidation.askCount();
System.out.println("Количество людей: " + count + "штук");
double sum = Calculator.calcul();
System.out.println(String.format("Сумма = " + "%.2f", sum)+ " " + CorrectWriting.writingRub((int)Math.floor(sum)));
System.out.println(String.format("Сумма на одну персону = " + "%.2f", (sum/count))+ " " +CorrectWriting.writingRub((int)Math.floor(sum/count)));
}

}