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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
# «Калькулятор счёта» - проектная работа 1
93 changes: 92 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,97 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
int count = 0;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("На сколько человек необходимо разделить счёт?");
String countStr = scanner.next();

if (!countStr.matches("[1-9]+")) {
System.out.println("Не корректный ввод");
} else {
count = Integer.parseInt(countStr);

if (count == 1) {
System.out.println("Подсчет для одного человека");
addProductList(1);

} else if (count < 1) {
System.out.println("Некорректное значение для подсчёта");

} else {
System.out.println("Подсчет для " + count + " человек");
addProductList(count);
}
}
}

while (count < 2);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Этот логический блок также имело смысл вынести в отдельный метод, как это сейчас сделано с другими (ввод и обработка товаров, вывод окончания и тд)

}

private static void addProductList(int count) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Хорошо, что логика разнесена по разным методам, но лучше их была разнести и по разным классам, потому что у них совсем уж разные зоны ответственности



ArrayList<Product> productList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String nameFormat;
boolean active = true;

while (active) {
System.out.println("Введите название товара");
String name = scanner.next();
nameFormat = name.trim().toUpperCase().toLowerCase();
if (nameFormat.contains("завершить"))
break;
if (!nameFormat.chars().allMatch(Character::isLetter)) {
System.out.println("Не корректное название товара");
} else {
do {
System.out.println("Введите цену товара в формате рубли.копейки");
String str = scanner.next();
if (str.trim().toUpperCase().toLowerCase().contains("завершить")) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

есть метод equalsIgnoreCase

active = false;
break;
}
if (!str.matches("[0-9]+\\.[0-9]+")) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Круто, что использованы регулярки!

System.out.println("Не корректная цена товара");
} else {
double price = Double.parseDouble(str);

Product product = new Product(nameFormat, price);

productList.add(product);
System.out.println("Товар " + product.name + " с ценой " + product.price + " успешно добавлен в корзину");
System.out.println("Хотите добавить еще?");
break;
}
} while (true);
}
}
System.out.println("===========================");
sum(count, productList);
}

private static void sum(int count, ArrayList<Product> productList) {
double sum = productList.stream().mapToDouble(product1 -> product1.price).sum();
System.out.println("Список продуктов: ");
productList.forEach((s) -> System.out.println(s.name + " : " + s.price));
System.out.println("Итоговая сумма: " + parseRubCase(sum));
System.out.println("Итоговая сумма на каждого человека: " + parseRubCase(sum / count));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Такие подряд идущие println лучше объединять в один.

System.out.println("===========================");
}

public static String parseRubCase(double price) {
int inInt = (int) price;
String cased;
if (inInt % 100 / 10 == 1 || inInt % 10 >= 5 || inInt % 10 == 0)
cased = "рублей";
else if (inInt % 10 == 1)
cased = "рубль";
else
cased = "рубля";
return String.format("%.2f", price) + " " + cased;
}
}
11 changes: 11 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Product {
String name;
double price;


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

}
}