Repository navigation
я пытаюсь... #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
я пытаюсь... #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Check { | ||
| public static boolean isNumeric(String str) | ||
| { | ||
| try | ||
| { | ||
| Integer.parseInt(str); | ||
| return true; | ||
| } | ||
| catch(NumberFormatException e) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static boolean isDouble(String str) | ||
| { | ||
| try | ||
| { | ||
| Double.parseDouble(str); | ||
| return true; | ||
| } | ||
| catch(NumberFormatException e) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static String enter() | ||
| { | ||
| Scanner in = new Scanner(System.in); | ||
| return in.next(); | ||
| } | ||
|
|
||
| public static String countPeople(String enter) | ||
| { | ||
| boolean flag = true; | ||
| while(flag) | ||
| { | ||
| if(isNumeric(enter)) | ||
| { | ||
| int countPeople = Integer.parseInt(enter); | ||
|
Comment on lines
+41
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот тут получается два вызова |
||
| if(countPeople == 1) | ||
| { | ||
| System.out.println("Количество человек, должно быть больше единицы. Введите значение больше единицы."); | ||
| enter = enter(); | ||
| } | ||
| else if (countPeople < 1) | ||
| { | ||
| System.out.println("Ошибка, введите число больше единицы."); | ||
| enter = enter(); | ||
| } | ||
| else | ||
| { | ||
| flag = false; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| System.out.println("Некорректный ввод.Введите числовое значение, больше единицы."); | ||
| enter = enter(); | ||
| } | ||
| } | ||
| return enter; | ||
| } | ||
| public static double checkPrice(String enter) | ||
| { | ||
| boolean flag = true; | ||
| while(flag) | ||
| { | ||
| if (isDouble(enter.replace(',','.'))) | ||
| { | ||
| if(Double.parseDouble(enter) <= 0) | ||
| { | ||
| System.out.println("Ошибка, введите числовое значение, больше нуля"); | ||
| enter = enter().replace(',','.'); | ||
| } | ||
| else | ||
| { | ||
| flag = false; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| System.out.println("Ошибка, введите числовое значение"); | ||
| enter = enter().replace(',','.'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Много где повторяется |
||
| } | ||
| } | ||
| return Double.parseDouble(enter.replace(',','.')); | ||
| } | ||
|
|
||
| public static String print(double result) | ||
| { | ||
| String[] a = String.valueOf(result).split("[.]"); | ||
| int integralPart = Integer.parseInt(a[0]); | ||
| int endOne = integralPart%10; | ||
| int endTwo = integralPart%100; | ||
|
Comment on lines
+95
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Чтобы получить последние две цифры числа не обязательно нужно работать со строкой и парсить заново. Можно чисто операциями деления добиться двух последних цифр числа, например: |
||
| if(endOne == 1){ | ||
| return " рубль"; | ||
| } | ||
| else if((endOne > 1 && endOne < 5) && !(endTwo>=11 && endTwo<=14)){ | ||
| return " рубля"; | ||
| } | ||
| else if(endTwo >= 11 && endTwo <= 14){ | ||
| return " рублей"; | ||
| } | ||
| else{ | ||
| return " рублей"; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| public class Main | ||
| { | ||
| public static void main(String[] args) | ||
| { | ||
| System.out.println("Добро пожаловать в приложение калькулятор!"); | ||
| System.out.println("На скольких человек, вы планируете разделить счет?"); | ||
| String enteredValue = Check.enter(); | ||
| int countPeople = Integer.parseInt(Check.countPeople(enteredValue)); | ||
| Products.productAll(countPeople); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import java.text.DecimalFormat; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Products { | ||
| String name; | ||
| double price; | ||
|
|
||
| public Products(String name, double price) | ||
| { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public String getNameOrPrice() { | ||
| String rubs = Check.print(price); | ||
| String formattedDouble = new DecimalFormat("#0.00").format(price); | ||
| formattedDouble = formattedDouble.replace(',','.'); | ||
| return name + " цена: " + formattedDouble + rubs; | ||
| } | ||
|
|
||
| static double onePeople(int countPeople, double allPriceProduct) | ||
| { | ||
| return allPriceProduct/countPeople; | ||
| } | ||
|
|
||
| static void productAll(int countPeople) { | ||
| Scanner in = new Scanner(System.in); | ||
| //boolean flag = true; | ||
| List<Products> products = new ArrayList<>(); | ||
| double allPriceProduct = 0.00f; | ||
| while (true) { | ||
| System.out.println("Введите название продукта:"); | ||
| String nameProduct = in.next(); | ||
| System.out.println("Введите цену на данный товар:"); | ||
| String enterPrice = in.next(); | ||
| enterPrice = enterPrice.replace(',', '.'); | ||
| if (Check.isDouble(enterPrice)) { | ||
| double priceProduct = Double.parseDouble(enterPrice); | ||
| if (priceProduct <= 0) | ||
| { | ||
| priceProduct = Check.checkPrice(enterPrice); | ||
| allPriceProduct += priceProduct; | ||
| Products product = new Products(nameProduct, priceProduct); | ||
| products.add(product); | ||
| } | ||
| else | ||
| { | ||
| allPriceProduct += priceProduct; | ||
| Products product = new Products(nameProduct, priceProduct); | ||
| products.add(product); | ||
| } | ||
| } else { | ||
| double priceProduct = Check.checkPrice(enterPrice); | ||
| allPriceProduct += priceProduct; | ||
| Products product = new Products(nameProduct, priceProduct); | ||
| products.add(product); | ||
| } | ||
|
|
||
| System.out.println("Хотите ли вы ввести еще один продукт? Да - введите любой символ, нет - введите 'завершить'."); | ||
| String answer = in.next(); | ||
| if (answer.equalsIgnoreCase("Завершить")) { | ||
| //flag = false; | ||
| break; | ||
| } | ||
| } | ||
| double priceForOne = onePeople(countPeople, allPriceProduct); | ||
| String formattedDouble = new DecimalFormat("#0.00").format(priceForOne); | ||
| formattedDouble = formattedDouble.replace(',','.'); | ||
| double result = Double.parseDouble(formattedDouble); | ||
| System.out.println("Добавленные товары:"); | ||
| for(Products products1 : products) { | ||
| System.out.println(products1.getNameOrPrice()); | ||
| } | ||
| String formattedDoubleTwo = new DecimalFormat("#0.00").format(allPriceProduct); | ||
| formattedDoubleTwo = formattedDoubleTwo.replace(',','.'); | ||
| String rubsOne = Check.print(allPriceProduct); | ||
| System.out.println("Общая сумма: " + formattedDoubleTwo + " " + rubsOne); | ||
| String rubs = Check.print(result); | ||
| System.out.println("К оплате для одного человека: " + result + rubs); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Если проверять так, то получается, что тебе приходится каждый раз по 2 раза вызывать функцию
parseInt- сначала для проверки, а второй раз чтобы получить число после проверки. Можно обойтись одним вызовом, а у классаScannerесть функцииhasNextIntиhasNextFloat, которые помогут избежать лишних вызовов парсинга