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

public class Calc {


public static int howManyPersons() {
Scanner scanner = new Scanner(System.in);
System.out.println("На скольких людей разделить счет?");
while(true){
int numberOfPersons = 0;
try {
numberOfPersons = scanner.nextInt();
} catch (InputMismatchException e){
System.out.println("Некорректный ввод. Введите количество персон");
scanner.nextLine();
}
if(numberOfPersons > 1) {
return numberOfPersons;
}
else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

этот блок нужно поднять в try, иначе он выполняется после catch и сообщение "Некорректный ввод. Введите количество персон" выводится несколько раз, попробуй текст ввести.
Try - блок с основным кодом - выполняется по плану программы, если произошла ошибка выполнится то, что в catch. Затем выполняется программа дальше, как если бы не было ошибки.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

а по логику твоей программы после try catch всегда выполняется проверка на количество персон вне зависимости он того была ошибка или нет.
Надеюсь стало яснее)

System.out.println("Некорректный ввод. Введите количество персон");
}
}
}



public static String getProduct() {
String priceList;
priceList = "Добавленные товары:\n";
String product = "";
float price = 0f;
float sum = 0f;
int quantityPerson = howManyPersons();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Введите название товара");
while(true) {
product = scanner.nextLine();
if(product.trim().length() == 0) {
System.out.println("Кажется вы забыли ввести название товара, попробуйте ещё раз");
}
else {
break;
}
}
System.out.println("Введите стоимость товара");
while(true) {
try {
price = scanner.nextFloat();
} catch (InputMismatchException e) {
System.out.println("Некорректный ввод. Введите стоимость товара");
scanner.nextLine();
continue;
}
if(price > 0) {
break;
}
else {
System.out.println("Некорректный ввод. Введите стоимость товара");
}
}
scanner.nextLine();
priceList += String.format("%s - %.2f %s\n", product, price, wordRuble(price));
sum += price;
System.out.println("Товар успешно добавлен, если хотите завершить введите \"завершить\"");
if(scanner.nextLine().toLowerCase().equals("завершить")){
String ruble = wordRuble(sum);
sum = (sum / quantityPerson);
priceList += String.format("Каждый должен заплатить %.2f %s\n ", sum, wordRuble(sum));
break;
}
}
return priceList;
}



public static String wordRuble(float ruble){
int intRuble = (int) ruble;
if (intRuble >= 11 && intRuble <= 19)
return "рублей";
if (intRuble % 10 >= 2 && intRuble % 10 <= 4)
return "рубля";
else if (intRuble % 10 == 1)
return "рубль";
else if (intRuble % 10 >= 5 && intRuble % 10 <= 9 || intRuble % 10 == 0)
return"рублей";
return null;
}
}
8 changes: 8 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
public class Main {
public static void main(String[] args) {
String priceList = "";
priceList += Calc.getProduct();
System.out.println(priceList);
}
}