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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
55 changes: 55 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.HashMap;

public class Calculator {
private double productSum;

Choose a reason for hiding this comment

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

🍏 Лишнее поле, ты можешь вызывать calculate при вызове getProductsum или реализовать это логику сразу в этой функции

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 "рублей";
}
}


}
69 changes: 67 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,71 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
int peopleCount = getpeopleCount();
calculateProduct(peopleCount);
}
}

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

private static void calculateProduct(int peopleCount) {
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);
System.out.println("Продукт добавлен");
}
}
calculator.showProduct();
double perconСheck = calculator.getProductsum()/peopleCount;
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;
}
}