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

public class DivisionCheck {
public static String rub (float sum) {
if (sum % 10 == 1 && sum % 100 != 11) {
return "рубль";
} else if ((sum % 10 == 2 || sum % 10 == 3 || sum % 10 == 4)&& sum % 100 != 12 && sum % 100 != 13 && sum % 100 != 14) {
return "рубля";
} else {
return "рублей";
}
}

public static void divCheck (int numberPeople) {

Scanner scanner = new Scanner(System.in);
int i = 0;
String name;
float price;
float sum = 0;
ArrayList<Product> productList = new ArrayList<>();

while (true) {
i = i + 1;
while(true) {
System.out.printf("Введите название товара №%d: ", i);
name = scanner.next();
break;
}

while(true) {
System.out.printf("Введите стоимоть товара №%d: ", i);
if (scanner.hasNextFloat()){
price = scanner.nextFloat();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тоже самое, что и с hasNextInt только тут надо hasNextFloat

if (price > 0) {
break;
}
} else scanner.next();
System.out.println("Ошибка ввода. Необходимо вводить числовое значение.\n" + "Цена не может быть отрицательной или нулевой.");
}

productList.add(new Product(name, price));
sum = sum + price;
System.out.println("Товар добавлен в список. Продолжаем?");
System.out.println("Для продолжения введите любой символ, для завершения введите \"завершить\".");
String reply = scanner.next();
if (reply.equalsIgnoreCase("завершить"))
{break;}
}
System.out.println("Список добавленных товаров");
System.out.println("|Наименование товара |Стоимость |");
for (i = 0; i < productList.size(); i = i + 1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно воспользоваться циклом foreach, было бы удобнее, подробнее можно почитать тут
https://javarush.com/groups/posts/for-each-java

System.out.println("|" + String.format("%-20s", productList.get(i).name) + "|" + String.format("%-10.2f", productList.get(i).price) + "|");
}
System.out.printf("Итоговая сумма: %.2f %s\t\n", sum, rub(sum));
System.out.printf("Количество гостей: %d\t\t\n", numberPeople);
System.out.printf("Итого на человека: %.2f %s\t\t\n", sum / numberPeople, rub(sum / numberPeople));
}
}

class Product {
String name;
float price;
Product (String name, float price) {
this.name = name;
this.price = price;
}
}
20 changes: 19 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
int numberPeople;

System.out.print("Приветствую пользователь!" + "\n" + "На сколько человек разделить счёт?");

while (true) {
if (scanner.hasNextInt()){
numberPeople = scanner.nextInt();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Если передать в этот момент произвольную строку программа завершит работу с исключением. Перед тем, как прочитать инт из сканнера, нужно сначала проверить возможно ли это при помощи соответствующего метода hasNextInt

if (numberPeople > 1) {
System.out.println("\nЗамечательно, колличество гостей - " + numberPeople + "!\n" + "А теперь займёмся вашим меню!\n");
break;
}
} else
scanner.next();
System.out.println("Значение некорректное.\n" + "Нельзя вводить спец символы или буквы.");
System.out.println("Колличество гостей не может быть равно 1 или 0, а так же не может быть отрицательным.\n");
}
DivisionCheck.divCheck(numberPeople);
}
}