-
Notifications
You must be signed in to change notification settings - Fork 220
консольное приложение #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
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f5d5fe4
Первый
78de1b9
второй
c713384
Первый кусок
35dc40b
Update Counter.java
alk8 d887d24
release
alk8 cf7e9cc
Update Main.java
alk8 c78d086
Update Main.java
alk8 5a71a69
Merge pull request #1 from alk8/alk8-patch-1
alk8 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
3 changes: 3 additions & 0 deletions
.idea/.gitignore
Oops, something went wrong.
123 changes: 123 additions & 0 deletions
.idea/codeStyles/Project.xml
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
.idea/codeStyles/codeStyleConfig.xml
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
.idea/compiler.xml
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
.idea/gradle.xml
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
.idea/misc.xml
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
.idea/vcs.xml
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ dependencyResolutionManagement { | |
| } | ||
| } | ||
| rootProject.name = "Java-Module-Project" | ||
| include ':JDK' | ||
112 changes: 112 additions & 0 deletions
src/main/java/Counter.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,112 @@ | ||
| import java.text.DecimalFormat; | ||
| import java.util.HashMap; | ||
| import java.util.Scanner; | ||
|
|
||
|
|
||
| public class Counter { | ||
|
|
||
| private final int countPerson; | ||
| private final HashMap<String, Double> goods = new HashMap<>(); | ||
|
|
||
| Counter(int countPerson) { | ||
| this.countPerson = countPerson; | ||
| } | ||
|
|
||
| public void askUser() { | ||
|
|
||
| Scanner sc = new Scanner(System.in); | ||
|
|
||
| System.out.println("Введите товар"); | ||
|
|
||
| // Получение товара | ||
| String product = sc.next(); | ||
|
|
||
| if (endSession(product)) { | ||
| System.out.println("Сессия закончена. Спасибо!"); | ||
| return; | ||
| } | ||
|
|
||
| System.out.println("Введите стоимость"); | ||
|
|
||
| // Стоимость | ||
| String cost = addCost(sc); | ||
|
|
||
| if (endSession(cost)) { | ||
| System.out.println("Сессия закончена. Спасибо!"); | ||
| return; | ||
| } | ||
|
|
||
| // добавляем продукт | ||
| goods.put(product, Double.parseDouble(cost)); | ||
|
|
||
| System.out.println("Добавлено успешно!"); | ||
|
|
||
| // Снова спрашиваем про товар (Рекурсия) | ||
| askUser(); | ||
|
|
||
| } | ||
|
|
||
| private String addCost(Scanner sc) { | ||
|
|
||
| String cost = sc.next(); | ||
|
|
||
| if (!cost.matches("\\d+.\\d{2}")) { | ||
|
|
||
| System.out.println("Значение не соответсвует маске XX.XX Введите стоимость заново"); | ||
| // (Рекурсия) | ||
| cost = addCost(sc); | ||
| } | ||
|
|
||
| return cost; | ||
| } | ||
|
|
||
| private boolean endSession(String str) { | ||
|
|
||
| if (str.equalsIgnoreCase("ЗАВЕРШИТЬ")) { | ||
|
|
||
| DecimalFormat decimalFormat = new DecimalFormat("#.##"); | ||
| // вывести результаты | ||
| System.out.println("Добавленные товары:"); | ||
|
|
||
| goods.forEach((key, value) -> | ||
| System.out.println("*** " + key + " | Стоимость " + value + " " + GetRubleAddition(value))); | ||
|
|
||
| double total = goods.values().stream().mapToDouble(Double::doubleValue).sum(); | ||
|
|
||
| System.out.println("Всего потрачено: " + decimalFormat.format(total) + " " + GetRubleAddition(total)); | ||
|
|
||
| double fromOnePerson = total / countPerson; | ||
|
|
||
| System.out.println("С человека - " + decimalFormat.format(fromOnePerson) + | ||
| " " + GetRubleAddition(fromOnePerson)); | ||
|
|
||
| return true; | ||
|
|
||
| } | ||
|
|
||
| return false; | ||
|
|
||
| } | ||
|
|
||
| private String GetRubleAddition(Double value) { | ||
|
|
||
| int num = value.intValue(); | ||
|
|
||
| int preLastDigit = num % 100 / 10; | ||
| if (preLastDigit == 1) { | ||
| return "рублей"; | ||
| } | ||
|
|
||
| switch (num % 10) { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| return "рублей"; | ||
| } | ||
| } | ||
|
|
||
| } |
47 changes: 44 additions & 3 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,8 +1,49 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| int countPerson = 0; | ||
|
|
||
| System.out.println("Добрый день!"); | ||
| Scanner sc = new Scanner(System.in); | ||
|
|
||
| while (true){ | ||
|
|
||
| System.out.println("Сколько вас?"); | ||
|
|
||
| try { | ||
|
|
||
| countPerson = sc.nextInt(); | ||
|
|
||
| if (countPerson == 1) { | ||
|
|
||
| System.out.println("Для одного человека в приложении нет необходимости"); | ||
|
|
||
| } else if (countPerson < 1) { | ||
|
|
||
| System.out.println("Ошибочное значение. Повторите ввод"); | ||
|
|
||
| } else { | ||
|
|
||
| break; | ||
|
|
||
| } | ||
|
|
||
| } catch (Exception e){ | ||
|
|
||
| System.out.println("Введено неправильное значение повторите ввод"); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| // Пользователь ввел корректное количество. Приступаем к расчетам | ||
| Counter counter = new Counter(countPerson); | ||
| counter.askUser(); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
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.