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
89 changes: 89 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Scanner;

public class Calculator {
int persons;
float price;
String name;

int countingPersons(){
try {
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("Введите количество человек:");
persons = scanner.nextInt();

if (persons < 1) {
System.out.println("Вы ввели некорректное число человек");
} else {
return persons;
}
}
}
catch (InputMismatchException e){
System.out.println("Вы ввели некорректный символ вместо числа. Введите данные заново");
return countingPersons();
}
}
void countingProducts(){
LinkedList<Product> list = new LinkedList<>();
Scanner scanner = new Scanner(System.in);
String exit = "ЗАВЕРШИТЬ";
int persons = countingPersons();
float sum = 0;

System.out.println("Введите название товара:" +
"\nВведите 'Завершить' для того, чтобы завершить процесс добавления товаров.");

while (true) {
name = scanner.next();

if (name.equalsIgnoreCase(exit)) {
sum = sum / persons;
String sumProduct = "%.2f";
Converter converter = new Converter(sum);

System.out.println("Добавленные товары:");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).name + " " + list.get(i).price);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🍏 При печати в памяти будут создаваться промежуточные строки из-за использования оператора "+". Можно использовать StringBuilder, который сформирует только результирующую строку.
https://habr.com/ru/articles/260767/

}
System.out.println("Каждый должен заплатить: " + String.format(sumProduct, sum)
+ " " + converter.check());
break;
}

System.out.println("Введите стоимость товара в формате: [руб,коп]:");

while (true) {
try {
price = scanner.nextFloat();
if (price <= 0) {
System.out.println("Вы ввели некорректную сумму товара! Попробуйте ввести сумму товара снова.");
}
else{
break;
}
}
catch (InputMismatchException e){
if ((Integer.toString((int)price).equalsIgnoreCase(exit))){
break;
}
System.out.println("Вы ввели некорректный символ вместо числа или ввели некоректное число.");
countingProducts();
}
}
Product product = new Product(name, price);

list.add(product);

sum = (sum + product.price);

System.out.println("Товар успешно добавлен! Хотите добавить ещё один товар?" +
"\nИли введите 'Завершить', чтобы завершить процесс добавления товаров.");
}
}
}


25 changes: 25 additions & 0 deletions src/main/java/Converter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Converter {
float count;

Converter(float count){
this.count = count;
}

String check(){
float remainTen = count % 10;
float remainHundred = count % 100;

if (remainTen > 10 && remainTen < 20 || remainHundred > 10 && remainHundred < 20){
return "рублей";
}
else if(remainTen >= 1 && remainTen < 2 || remainHundred >= 1 && remainHundred < 2){
return "рубль";
}
else if (remainTen > 1 && remainTen < 5 || remainHundred > 1 && remainHundred < 5){
return "рубля";
}
else{
return "рублей";
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calculator = new Calculator();
calculator.countingProducts();
}
}
}

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;
float price;

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