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

public class Calculator {
private List<Product> productList = new ArrayList<>();
private double currentSum;

public void add(Product product) {
productList.add(product);
addToSum(product.getPrice());
}

private void addToSum(double price) {
currentSum += price;
}

public double getSum() {
return currentSum;
}

public List<Product> getProducts() {
return productList;
}

}
79 changes: 78 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,83 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calculator = new Calculator();
Scanner scanner = new Scanner(System.in);
Comment thread
nst-personal marked this conversation as resolved.
int numberOfPeople = getNumberOfPeople(scanner);
fillProducts(calculator, scanner);
displayProducts(calculator);
calculateSumPerPerson(calculator, numberOfPeople);
scanner.close();
}
private static int getNumberOfPeople(Scanner scanner) {
int numberOfPeople = 0;
while (numberOfPeople <= 1) {
System.out.println("Enter number of people to split the bill");
if (scanner.hasNextInt()) {
numberOfPeople = scanner.nextInt();
if (numberOfPeople <= 0) {
System.out.println("The number of people must be positive");
} else if (numberOfPeople == 1) {
System.out.println("There is no point in counting");
}
} else {
System.out.println("Invalid input. Please enter value");
scanner.next();
}
}
return numberOfPeople;
}

private static void fillProducts(Calculator calculator, Scanner scanner) {
System.out.println("Add product");
String action = null;
while (action == null || !action.equalsIgnoreCase("done")) {
addProduct(scanner, calculator);
System.out.println("Do you want to add more? Fill 'done' to complete");
action = scanner.next();
}
}

private static void addProduct(Scanner scanner, Calculator calculator) {
String name = null;
while (name == null || name.trim().isEmpty()) {
System.out.println("Enter name of product");
name = scanner.next();
}
double price = 0.0;
while (price <= 0 || isValueHas2Digits(price)) {
System.out.println("Enter price of product");
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price <= 0 || isValueHas2Digits(price)) {
System.out.println("The number of people must be positive and has 2 digits after dot");
}
} else {
System.out.println("Invalid input. Please enter value");
scanner.next();
}
}
calculator.add(new Product(name, price));
System.out.println("Product added");
}

private static boolean isValueHas2Digits(double price) {
return Math.abs(price * 100 - Math.round(price * 100)) > 0.001;
}

private static void displayProducts(Calculator calculator) {
System.out.println("Added products:");
RubleFormatter rubleFormatter = new RubleFormatter();
for (Product product: calculator.getProducts()) {
System.out.printf("%s || %.2f %s\n", product.getName(), product.getPrice(), rubleFormatter.format(product.getPrice()));
}
}

private static void calculateSumPerPerson(Calculator calculator, int numberOfPeople) {
double sumPerPerson = calculator.getSum() / numberOfPeople;
RubleFormatter rubleFormatter = new RubleFormatter();
System.out.printf("Sum per person ===> %.2f %s\n", sumPerPerson, rubleFormatter.format(sumPerPerson));
}

}
17 changes: 17 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public double getPrice() {
return price;
}

}
16 changes: 16 additions & 0 deletions src/main/java/RubleFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class RubleFormatter {
public String format(double number) {
int floorNumber = (int) Math.floor(number);
int lastDigit = floorNumber % 10;
int lastTwoDigits = floorNumber % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 19) {
return "rubley";
} else if (lastDigit == 1) {
return "ruble";
} else if (lastDigit >= 2 && lastDigit <= 4) {
return "rublya";
} else {
return "rubley";
}
}
}