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

public class Calculator {
private double orderSum;
private final HashMap<Good, Integer> order = new HashMap<>();

public void addGood(Good good, int quantity) {
if (order.containsKey(good)) {
int oldQuantity = order.get(good);
int newQuantity = oldQuantity + quantity;
order.put(good, newQuantity);
} else {
order.put(good, quantity);
}
orderSum = calculate();

}

private double calculate() {
double sum = 0;

for (Good good : order.keySet()) {
int quantity = order.get(good);
sum += good.price * quantity;
}

return sum;
}

public void showOrder() {
System.out.println("Добавленные товары:");
for (Good good : order.keySet()) {
System.out.println(good.name + " " + String.format("%.2f", good.price));
}
}

public double getOrderSum() {
return orderSum;
}

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

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

Good(String name, double price) {
this.name = name;
this.price = price;
}
}
68 changes: 67 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,72 @@
import java.util.Scanner;

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

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

private static void calculateGoods(int visitorCount) {
Calculator calculator = new Calculator();
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Пожалуйста, введите название товара или команду 'завершить' чтобы закончить расчет");
String goodName = scanner.nextLine();
if (goodName.equalsIgnoreCase("завершить")) {
break;
}
System.out.println("Пожалуйста введите цену товара");
double goodPrice;
String goodPriceString = scanner.next();

goodPrice = parseStringIntoDouble(goodPriceString);

if (goodPrice < 0) {
System.out.println("Вы ввели некорректное значение");
} else {
Good good = new Good(goodName, goodPrice);
calculator.addGood(good, 1);
System.out.println("Товар был успешно добавлен");
}
}

calculator.showOrder();
double sumToPayPerPerson = calculator.getOrderSum() / visitorCount;
System.out.println("Каждый должен заплатить: " + String.format("%.2f", sumToPayPerPerson) + " " + calculator.getRubleInRightFormat(sumToPayPerPerson));
}


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