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
53 changes: 53 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.HashMap;
public class Calculator {
private double productSum;
private final HashMap<Product, Integer> order = new HashMap<>();

public void addProduct (Product product, int quantity) {
if (order.containsKey(product)) {
int oldQuantity = order.get(product);
int newQuantity = oldQuantity + quantity;
order.put(product, newQuantity);
}else {
order.put(product, quantity);
}
productSum = calculate();
}

private double calculate() {
double sum = 0;
for (Product product : order.keySet()){
int quantity = order.get(product);
sum += product.price * quantity;
}
return sum;
}

public void showProduct(){
System.out.println("Список товаров: ");
for (Product product : order.keySet()){
System.out.println(product.name + " " + String.format("%.2f", product.price));
}
}

public double getProductsum(){return productSum;}

public String rublesFormat(double rubles) {
int lastDigit = (int) (Math.floor(rubles) % 10);
if (((int) (Math.floor(rubles) % 100)) <= 19 && ((int)(Math.floor(rubles) % 100)) >= 11)
return "рублей";
switch (lastDigit) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}



}
66 changes: 64 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
int countPeople = getCountpeople();
calculateProduct(countPeople);
}

private static int getCountpeople() {
int countPeople;
while (true){
System.out.println("Введите количество людей");
Scanner scanner = new Scanner(System.in);
String countPeopleString = scanner.next();
countPeople = parseStringIntoInt(countPeopleString);
if (countPeople == 1){
System.out.println("Нет смысла рассчитывать на 1-го человека");
}else if (countPeople < 1) {
System.out.println("Введено некоректное число");
}else {
break;
}
}
return countPeople;
}

private static void calculateProduct(int countPeople) {
Calculator calculator = new Calculator();
while (true){
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название продукта или 'Завершить' что бы закончить." );
String productName = scanner.next();
if (productName.equalsIgnoreCase("Завершить")){
break;
}
System.out.println("Введите цену");
double productPrice;
String productPriceString = scanner.next();
productPrice = parseStringIntoDouble(productPriceString);
if (productPrice < 0){
System.out.println("Введено некорректное значение");
}else {
Product product = new Product(productName, productPrice);
calculator.addProduct(product,1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно также не создавать переменную, а создавать объект при вызове метода:
calculator.addProduct(new Product(productName, productPrice),1);

System.out.println("Продукт добавлен");
}
}
calculator.showProduct();
double perconСheck = calculator.getProductsum()/countPeople;
System.out.println("Каждый должен заплатить: " + String.format("%.2f",perconСheck) + " " + calculator.rublesFormat(perconСheck));
}

private static int parseStringIntoInt(String string){
try {
return Integer.parseInt(string);
}catch (NumberFormatException e){
return -1;
}
}
private static double parseStringIntoDouble(String string) {
try {
return Double.parseDouble(string);
}catch (NumberFormatException e){
return -1;
}
}


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

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