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

public class Calculator {
ArrayList<Item> items = new ArrayList<>();

public void addItems() {
System.out.println("Введите название товара или напишите \"завершить\"");
Scanner scanner = new Scanner(System.in);
scanner.useLocale(Locale.US);
while(true){
String itemName = scanner.nextLine();
if(itemName.equalsIgnoreCase("завершить")){
break;
}
System.out.println(String.format("Введите стоимость товара \"%s\"", itemName));
try{
String inputPrice = scanner.nextLine();
if(inputPrice.equalsIgnoreCase("завершить")){
break;
}
Double itemPrice = Double.parseDouble(inputPrice.replace(",","."));
if(itemPrice > 0){
items.add(new Item(itemName, itemPrice));
System.out.println(String.format("Товар %s успешно добавлен!\n", itemName));
System.out.println("Напишите название следующего товара или напишите \"завершить\"");
} else {
System.out.println("Стоимость товара не может быть отрицательной или равна 0, напишите название товара или напишите \"завершить\"");
}
} catch (NumberFormatException e){
System.out.println("Стоимость введена неверно, напишите название товара или напишите \"завершить\"");
}
}
scanner.close();
}
public void calculate(int visitors){
Double total = 0.0;
for (Item item : items) {
total += item.price;
}
Double separatedPrice = total/visitors;
System.out.println(String.format("Каждый из %s должен заплатить %.2f %s", visitors, separatedPrice, new Formatter().getCase(separatedPrice)));
}

public void showItems() {
System.out.println("Добавленные товары:");
for (Item item: items) {
System.out.println(String.format("- %s: %s %s", item.name, item.price, new Formatter().getCase(item.price)));
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Formatter {
public String getCase(Double number){
// округляем число до целых
double flooredNumber = Math.floor(number);
// находим остаток от деления на 10 чтобы получить крайнюю цифру справа
// по нему определяем падеж, единственное или множественное число
int remainder = (int) flooredNumber % 100;
if(remainder >= 11 && remainder <= 19){
return "рублей";
} else if(remainder % 10 == 1){
return "рубль";
} else if(remainder % 10 >=2 && remainder % 10 <= 4){
return "рубля";
} else return "рублей";
}
}
9 changes: 9 additions & 0 deletions src/main/java/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Item {
String name = "untitled";
double price = 0;

public Item(String name, Double price) {
this.name = name;
this.price = price;
}
}
36 changes: 35 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

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

System.out.println("На сколько человек разделить счет?");

int visitors = 0;

// узнаем сколько на сколько человек делить счет

while(true){
Scanner scanner = new Scanner(System.in);
try{
visitors = scanner.nextInt();
if(visitors > 1){
break;
} else if(visitors == 1){
System.out.println("Одному человеку нечего делить, введите количество человек от 2 и больше");
} else {
System.out.println("Число не может быть нулем или отрицательным");
}
} catch (NoSuchElementException e){
System.out.println("Введите количество человек, число должно быть целым");
}
}



Calculator calculator = new Calculator();
// узнаем у пользователя что было заказано
calculator.addItems();
// показываем список товаров
calculator.showItems();
// считаем
calculator.calculate(visitors);
}
}