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

public class Calculator {
int person; //переменная количества персон;
double total = 0.00; //переменная для подсчета общей стоимости товара;
Input input = new Input();
Output output = new Output();

Scanner scanner = new Scanner(System.in);
ArrayList<Item> itemList = new ArrayList<>(); //список товара и его стоимости;
Formater format = new Formater(); //создание объекта опредления склонения рубля;

public void calculator() { //метод расчета товара
String answer; //переменная ответа на предложение добавить товар;
double cost = 0.00;
do {
output.print("Введите название товара");
String item = scanner.next();
cost = input.getInput();
createItem(item, cost); //создали списочный массив;
total += cost;
output.print("Хотите ли добавить еще один товар? В противном случае напишите 'завершить'");
answer = scanner.next();
} while (!answer.equalsIgnoreCase("Завершить"));
System.out.println("Добавленные товары:");
for (int n = 0; n < itemList.size(); n++) {
output.print(String.format("%s, стоимостью %.2f %s", itemList.get(n).item, itemList.get(n).cost, format.formater(itemList.get(n).cost)));
}
output.print(String.format("Итоговая сумма: %.2f %s", total, format.formater(total)));
double totalForEach = (total / person);
output.print(String.format("Каждый человек должен заплатить %.2f %s", totalForEach, format.formater(totalForEach)));
}

public void start() {
output.print("Введите количество персон");

while (true) {
output.print("На скольких человек необходимо разделить счёт?");

if (scanner.hasNextInt()) {
person = scanner.nextInt();
if (person > 1) {
calculator();
break;
} else if (person == 1) {
output.print("Придется вам платить одному.");
break;
}
} else {
output.print("Это некорректное значение для подсчёта. Введите целочисленное значение.");
scanner.next();
}
}
}

public void createItem(String name, double cost) {
Item items = new Item(name, cost);
itemList.add(items);
output.print("Товар " + items.item + " успешно добавлен");

}
}
14 changes: 14 additions & 0 deletions src/main/java/Formater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Formater {

public String formater (double cost) { //метод склонения слова рубль
if ((cost % 10) == 1) {
return "рубль";
} else if ((cost % 100) >= 11 && (cost % 100) <= 19) {
return "рублей";
} else if ((cost % 10) >= 2 && (cost % 10) <= 4) {
return "рубля";
} else {
return "рублей";
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Scanner;

public class Input {
private Scanner scanner = new Scanner(System.in);

double getInput() {
Output output = new Output();
output.print("Введите стоимость товара");
double result;

while (true) {
if (scanner.hasNextDouble()) {
double input = scanner.nextDouble();
if (input > 0) {
result = input;
break;
} else {
output.print("Стоимость не должна быть отрицательной. Введите корректное значение.");
}

} else {
output.print("Введите корректное значение");
scanner.next();
}

}
return result;
}
}
13 changes: 13 additions & 0 deletions src/main/java/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.ArrayList;

public class Item {
final String item;
final double cost;

public Item(String item, double cost) {
this.item = item;
this.cost = cost;
}


}
6 changes: 5 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

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



}
}
6 changes: 6 additions & 0 deletions src/main/java/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Output {
void print(String result) {
System.out.println(result);
}

}