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

public class Calculator {
private double currentSum = 0;
final private List<Item> itemList = new ArrayList<>();

public double getCurrentSum() {
return currentSum;
}

public boolean addItem(Item item) {

Choose a reason for hiding this comment

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

🍏 Можно вместо boolean добавить void и убрать return, т.к. возвращаемое значение у тебя нигде не используется

if (findItem(item)) {
return false;
} else {
itemList.add(item);
currentSum = currentSum + item.getPrice();
return true;
}
}

private boolean findItem(Item lookingItem) {
for (Item item : itemList) {
if (item.equals(lookingItem)) {
return true;
}
}
return false;
}

public String getItemList() {
String resultString = "";
for (Item item : itemList) {
resultString = resultString + item.getName() + ", "
+ Formatter.formatDoubleToString(item.getPrice()) + "\n";
}
return resultString;
}
}
24 changes: 24 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Formatter {
public static String formatDoubleToString(double inputNumber) {

int roundedNumber = (int) Math.floor(inputNumber);
int preLastDigit = roundedNumber % 100 / 10;
int lastDigit = roundedNumber % 10;

String rublesValue;

if (preLastDigit == 1) {
rublesValue = "рублей";
} else {
if (lastDigit == 1) {
rublesValue = "рубль";
} else if (lastDigit >= 2 && lastDigit <= 4) {
rublesValue = "рубля";
} else {
rublesValue = "рублей";
}
Comment on lines +13 to +19

Choose a reason for hiding this comment

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

🍏 Такого рода блоки, как if-then-else, в Java можно заменить на switch case. Дело вкуса

}

return String.format("%.2f", inputNumber) + " " + rublesValue;
}
}
39 changes: 39 additions & 0 deletions src/main/java/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class Item {

Choose a reason for hiding this comment

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

Круто, что используешь класс, как модель данных для описания товара 👍

final private String name;
final private double price;

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

public String getName() {
return name;
}

public double getPrice() {
return price;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Item item = (Item) o;

if (Double.compare(item.price, price) != 0) return false;
return name != null ? name.equals(item.name) : item.name == null;
}

@Override
public int hashCode() {
int result;
long temp;
result = name != null ? name.hashCode() : 0;
temp = Double.doubleToLongBits(price);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
Comment on lines +18 to +37

Choose a reason for hiding this comment

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

Весьма полезное решение переопределять hashCode и equals 👍


}
57 changes: 54 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,59 @@
import java.util.Scanner;

// dev branch for Y.Practicum
public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Scanner scanner = new Scanner(System.in);
System.out.println("Привет! Введите количество гостей:");
int personCount;
while (true) {

if (scanner.hasNextInt()) {
personCount = scanner.nextInt();
if (personCount <= 1) {
System.out.println("Число гостей должно быть больше 1. Давайте еще раз");
} else {
scanner.nextLine();
break;
}
} else {
System.out.println("Нет, нужно число! Давайте еще раз! ");
scanner.nextLine();
}
}

String userResponce = "";
Calculator calculator = new Calculator();

while (!"Завершить".equalsIgnoreCase(userResponce)) {
System.out.println("Введите название товара");
String itemName = scanner.nextLine();
double itemPrice;
System.out.println("Введите цену товара, в формате хх,хх");
while (true) {
if (scanner.hasNextDouble()) {
itemPrice = scanner.nextDouble();
scanner.nextLine();
break;
} else {
System.out.println("Нет, цена должна быть числом, в формате хх,хх." +
" Давайте еще раз!");
scanner.nextLine();
}
}
Item item = new Item(itemName, itemPrice);
calculator.addItem(item);
System.out.println("Если желаете продолжить добавление товаров - введите любой символ. " +
"Если желаете закончить добавление - введите \"Завершить\". ");
userResponce = scanner.nextLine();
}

System.out.println("Добавленные товары:");
System.out.println(calculator.getItemList());

System.out.println("Общая сумма товаров: " + Formatter.formatDoubleToString(calculator.getCurrentSum()) + "\n");

System.out.println("Каждый должен заплатить: " + Formatter.formatDoubleToString(calculator.getCurrentSum() / personCount));
}
}