forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Калькулятор счёта #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
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2756b03
1. Входные параметры для счётчика
X4PDK0P 8614cee
2. Добавление товаров в калькулятор
X4PDK0P 75115e4
2. Добавление товаров в калькулятор
X4PDK0P 4d165a5
2. Добавление товаров в калькулятор
X4PDK0P dbc09f1
Добавил класс Formater, который имеет метод peopleCorrectly. Метод во...
X4PDK0P 1c4addd
Готовый проект на проверку
VaySpe cf86dea
Исправление критической ошибки:
X4PDK0P File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| IOException - что это и с чем его едят ? | ||
|
|
||
| try catch - мы это проходили ? | ||
|
|
11 changes: 11 additions & 0 deletions
src/main/java/Calculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public class Calculator { | ||
| double account = 0; | ||
| int people; | ||
|
|
||
| double personalAccount(){ | ||
| return account / people; | ||
| } | ||
| Calculator(int people){ | ||
| this.people = people; | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
src/main/java/Formater.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import java.util.Scanner; | ||
| public class Formater { | ||
|
|
||
| public static int peopleCorrectly (){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| boolean numberOfPeopleCorrectly = false; | ||
| String people = ""; | ||
| while (!numberOfPeopleCorrectly) { // пока не корректное колво людей | ||
| System.out.println("на скольких человек необходимо разделить счёт?"); | ||
| // принимаем не известный тип данных | ||
| people = scanner.nextLine(); | ||
| if (Main.isInt(people)) { // Проверяем на интовость кол-во чел и более 1 | ||
| numberOfPeopleCorrectly = true; // Корректное колво людей | ||
| } | ||
| } | ||
| return Integer.parseInt (people); | ||
| } | ||
|
|
||
| public static String rub (double priceDouble){ | ||
| int price = (int) priceDouble; | ||
| int units = price%10; | ||
| int dozens = price%100/10; | ||
| if (dozens == 1){ | ||
| return "рублей"; | ||
| } else if (units > 4 || units == 0){ | ||
| return "рублей"; | ||
| } else if (units == 1) { | ||
| return "рубль"; | ||
| } else { | ||
| return "рубля"; | ||
| } | ||
| } | ||
| } |
97 changes: 91 additions & 6 deletions
src/main/java/Main.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,91 @@ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| } | ||
| } | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static boolean isInt(String people){ | ||
| try { | ||
| int peopleInt = Integer.parseInt (people); | ||
| if (peopleInt <= 1){ | ||
| System.out.println("Введено не корректное кол-во человек"); | ||
| return false; | ||
| } else { | ||
| return true; | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Введено не корректное значение, используйте целые числа"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static boolean isPriceDouble(String strPrice){ | ||
| try { | ||
| double price = Double.parseDouble(strPrice); | ||
| if (price >= 0.01) { | ||
| return true; | ||
| } else { | ||
| System.out.println("Введено не корректное значение, стоимость должна быть больше 0"); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Введено не корректное значение, стоимость должна быть в формате {рубли.копейки}"); | ||
| System.out.println("Пример:\n Помидор 10.45"); | ||
| return false; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| ArrayList<Product> products = new ArrayList<Product>(); // создаем список товаров | ||
| Scanner scanner = new Scanner(System.in); | ||
| int people = Formater.peopleCorrectly(); // получаем интовое колво людей | ||
|
|
||
| Calculator calculator = new Calculator(people); | ||
| System.out.println("Добавление товаров в калькулятор"); | ||
| System.out.println("Пример:\n Помидор 10.45"); | ||
|
|
||
| boolean isAddProduct = true; | ||
| while (isAddProduct) { // собираем товары | ||
|
|
||
| String product = scanner.next(); | ||
| if (product.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
| // принимаем не известный тип данных | ||
| String strPrice = scanner.next(); | ||
| if (strPrice.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
|
|
||
| if (isPriceDouble(strPrice)){ | ||
| //товар корректный | ||
| double price = Double.parseDouble(strPrice); | ||
| products.add(new Product(product, price)); //добавление товара в товары | ||
| System.out.println(product + " " + Math.floor(price*100)/100 + ". Данный товар был успешно добавлен"); | ||
| calculator.account += Math.floor(price*100)/100; | ||
| System.out.println("Общий чек составляет:" + calculator.account); | ||
| isAddProduct = false; | ||
| } | ||
| System.out.println("Хотите ли Вы добавить ещё один товар?"); | ||
| while (!isAddProduct){ // проверяем хочет ли пользователь добавить еще товар и переспрашиваем при не понятном ответе | ||
| String ans = scanner.nextLine().trim(); | ||
| if (ans.equalsIgnoreCase("нет") || ans.equalsIgnoreCase("завершить")){ | ||
| break; | ||
| } else if (ans.equalsIgnoreCase("да")) { | ||
| System.out.println("Введите название и цену"); | ||
| isAddProduct = true; | ||
| } else { | ||
| System.out.println("Введите да или нет"); | ||
| } | ||
| } | ||
| } | ||
| // вывод всех товаров | ||
| System.out.println("Добавленные товары:\n"); | ||
| for (Product product : products){ | ||
| String output = product.title + " " + product.price + " " + Formater.rub(product.price); | ||
| System.out.println(output); | ||
| } | ||
| // вывод сколько должен заплатить каждый | ||
| System.out.print("Каждый должен заплатить: "); | ||
| System.out.println(Math.floor(calculator.personalAccount()*100)/100 + " " + Formater.rub(Math.floor(calculator.personalAccount()*100)/100)); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
src/main/java/Product.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Product { | ||
| String title; // название товара | ||
| double price; // цена товара | ||
|
|
||
| Product(String title, double price){ | ||
| this.title = title; | ||
| this.price = Math.floor(price*100)/100; // Округляем число в меньшую сторону до двух знаков после запятой | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.