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

public class Count {
int peoples;
ArrayList<Good> goods = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

public void count() {
getPeoples();
getGoods();
calculate();
}

public void getPeoples(){

System.out.println("На сколько человек разделить счет?");
while (true) {
if (scanner.hasNextInt()) {
int number = scanner.nextInt();
if (number == 1) {
System.out.println("Расчет для одного человека не производится, введите корректное число человек");
scanner.nextLine();
} else if (number < 1) {
System.out.println("Введите корректное число человек");
scanner.nextLine();
} else {
peoples = number;
scanner.nextLine();
break;
}
}
else
{
System.out.println("Необходимо ввести целое число");
scanner.nextLine();
}
}
}

public void getGoods() {
while (true) {
String name = "";
double price;
while (name.length() == 0) {
System.out.println("Введите название товара");
name = scanner.nextLine().toUpperCase().trim();
}
System.out.println("Введите цену товара");
while (true) {
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price <= 0) {
System.out.println("Цена должна быть больше 0, введите корректную цену");
scanner.nextLine();
} else {
break;
}
} else {
System.out.println("Необходимо ввести корректную цену");
scanner.nextLine();
}
}
goods.add(new Good(name, price));
System.out.format("Вы добавили товар %s с ценой %s \nВведите любой символ, чтобы продолжить, или \"завершить\", чтобы перейти к подсчету", name, Formatter.complexFormat(price));
System.out.println();
scanner.nextLine();
if(scanner.nextLine().equalsIgnoreCase("завершить")) break;
}
}

public void calculate(){
double sum = 0.00;
System.out.println("У вас " + goods.size() + Formatter.declension(goods.size()) + ":");
for(int i = 0; i < goods.size(); i++) {
Good good = goods.get(i);
sum = sum + good.price;
System.out.println((i+1) + ". " + good.name);
}
System.out.println("Общая сумма составляет " + Formatter.complexFormat(sum));
System.out.println("Каждый человек должен заплатить " + Formatter.complexFormat(sum / peoples) + "\nВведите любой символ, чтобы завершить программу");
while (true) {
scanner.nextLine();
break;
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Formatter {

public static String complexFormat(double price) {
return round(price) + declension(price);
}
public static String declension(double price) {
int roundPrice = (int) price;
if (roundPrice % 100 < 20 && roundPrice % 100 > 4)
return " рублей";
else if (roundPrice % 10 == 1)
return " рубль";
else if (roundPrice % 10 < 5 && roundPrice % 10 > 1)
return " рубля";
else
return " рублей";
}

public static String declension(int goods) {
if (goods % 100 < 20 && goods % 100 > 4)
return " товаров";
else if (goods % 10 == 1)
return " товар";
else if (goods % 10 < 5 && goods % 10 > 1)
return " товара";
else
return " товаров";
}

public static String round(double price) {
return String.format("%.2f", price);
}
}
9 changes: 9 additions & 0 deletions src/main/java/Good.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Good {
String name;
double price;

Good (String goodName, double goodPrice){
name = goodName;
price = goodPrice;
}
}
5 changes: 4 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

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

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Count count = new Count();
count.count();
}
}