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"
34 changes: 34 additions & 0 deletions src/main/java/Bill.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.ArrayList;

public class Bill {
private ArrayList<Product> products = new ArrayList<>();
private double totalSum = 0;

public void addProduct(){

String productName = Product.initName();
double productPrice = Product.initPrice();

Product product = new Product(productName, productPrice);

this.products.add(product);
this.totalSum = this.totalSum + productPrice;

System.out.printf("В счет добавили позицию \"%s\" по стоимости %s\n", product.getName(), Money.getRurPrice(product.getPrice()));
}

public void printBill(){
System.out.println("Добавленные товары:");

for(Product product : products){
System.out.printf("\"%s\", стоимость %s\n", product.getName(), Money.getRurPrice(product.getPrice()));
}

}

public void printSplitSum(int persons){
double splitSum = this.totalSum/persons;

System.out.printf("Каждый человек должен заплатить %s.", Money.getRurPrice(splitSum));
}
}
72 changes: 70 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
import java.util.Scanner;

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

public static void main(String[] args) {
// ваш код начнется здесь
System.out.println("Привет, username! Это калькулятор счета.\n");

//Заполним количество человек на которых будем делить счет
int persons = getPersonCount();

//Заполним счет позициями
Bill bill = fillBill();

//Напечатаем счет
bill.printBill();

//Напечатаем информацию о сумме на человека
bill.printSplitSum(persons);
}
}

private static int getPersonCount() {
System.out.println("На сколько человек нужно разделить счет?");

int persons = 0;
String inputText;
boolean finishFlag = false;

while (persons <= 1 && !finishFlag) {
inputText = scanner.nextLine().trim();

if (Messages.Bye(inputText)) {
finishFlag = true;
} else {
try {
persons = Integer.parseInt(inputText);

if (persons <= 0) {
System.out.println("Нет людей - нет проблем! Нельзя разделить счет на 0 или меньше человек! Попробуйте еще раз.");
} else if (persons == 1) {
System.out.println("Forever alone! Найди друзей, нельзя разделить счет на одного! Попробуй еще раз.");
}
} catch (NumberFormatException e) {
System.out.println("Мы точно в этой вселенной? Используй арабские цифры. Попробуй еще раз. ");
}
}
}

return persons;
}

private static Bill fillBill(){
Bill bill = new Bill();
System.out.println("Введите позиции из счета.\n");

String inputText;
boolean finishFlag = false;

while (!finishFlag) {
bill.addProduct();

System.out.println("Добавить еще один продукт? Для завершения введите \"" + Messages.FINISH_PHRASE + "\"");
inputText = scanner.nextLine();

if (Messages.Bye(inputText)) {
finishFlag = true;
}

}

return bill;
}

}
11 changes: 11 additions & 0 deletions src/main/java/Messages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Messages {
public static final String FINISH_PHRASE = "Завершить";

public static boolean Bye(String text){
if (FINISH_PHRASE.equalsIgnoreCase(text.trim())) {
System.out.println("Bye!");
return true;
}
return false;
}
}
26 changes: 26 additions & 0 deletions src/main/java/Money.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Money {
public static String getRurPrice(double price){

String result;
int roundedPrice = (int)Math.floor(price);

if (roundedPrice%100 >= 11 && roundedPrice%100 <= 14){
result = String.format("%.2f рублей", price);
} else {
switch (roundedPrice%10) {
case 1:
result = String.format("%.2f рубль", price);
break;
case 2:
case 3:
case 4:
result = String.format("%.2f рубля", price);
break;
default:
result = String.format("%.2f рублей", price);
break;
}
}
return result;
}
}
62 changes: 62 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.util.Scanner;

public class Product {
private static final Scanner scanner = new Scanner(System.in);
private final String name;
private final double price;

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

//Запрос наименования позиции
public static String initName(){
System.out.println("Введите наименовние позиции из счете");

String inputText = "";

while(inputText.isEmpty()) {
inputText = scanner.nextLine();

if (inputText.isEmpty()){
System.out.println("Наименованое не может быть пустым. Попробуйте еще раз.");
}
}

return inputText;
}

//Запрос цены по позиции
public static double initPrice(){
System.out.println("Введите стоимость позиции из счете в формате XX.XX");

String inputText;
double price = 0;

while (price <= 0) {
inputText = scanner.nextLine().trim();
try {
price = Double.parseDouble(inputText.replace(",", "."));

if(price < 0) {
System.out.println("Стоимость не может быть отрицательной. Попробуйте еще раз.");
} else if (price == 0){
System.out.println("Стоимость не может быть равна 0. Попробуйте еще раз.");
}
} catch (NumberFormatException e) {
System.out.println("Введите стоимость позиции из счете в формате XX.XX. Попробуйте еще раз.");
}
}

return price;
}

public double getPrice(){
return this.price;
}

public String getName(){
return this.name;
}
}