forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Add sum distribute #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
2 commits
Select commit
Hold shift + click to select a range
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
45 changes: 45 additions & 0 deletions
src/main/java/Calc.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,45 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Calc { | ||
|
|
||
| private ArrayList<Item> items; | ||
|
|
||
| Calc() { | ||
|
|
||
| items = new ArrayList<Item>(); | ||
|
|
||
| } | ||
|
|
||
| public void addItem(Item item) { | ||
|
|
||
| items.add(item); | ||
|
|
||
| } | ||
|
|
||
| public String getItemsList() { | ||
|
|
||
| StringBuilder builder = new StringBuilder(); | ||
|
|
||
| for (Item item : items) { | ||
| builder.append(item.name); | ||
| builder.append("\n"); | ||
| } | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| public double getSum() { | ||
|
|
||
| double sum = 0; | ||
| for (Item item : items) { | ||
| sum += item.price; | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| public double distributedSum(double sum, int personsAmount) { | ||
|
|
||
| return ((personsAmount > 0) ? sum / personsAmount : 0); | ||
|
|
||
| } | ||
|
|
||
| } |
32 changes: 32 additions & 0 deletions
src/main/java/Formatter.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,32 @@ | ||
| public class Formatter { | ||
|
|
||
| public String getFormatSum(double sum) { | ||
|
|
||
| return String.format("%.2f %s", sum, getFormattedRub(sum)); | ||
|
|
||
| } | ||
|
|
||
| String getFormattedRub(double sum) { | ||
|
|
||
| if(((sum % 100) >= 11) & ((sum % 100) <=19)) { | ||
| return "рублей"; | ||
| } | ||
|
|
||
| Double s = new Double(sum); | ||
| String sumAsString = String.valueOf(s.intValue()); | ||
| char lastNum = sumAsString.charAt(sumAsString.length() - 1); | ||
| String ending = ""; | ||
| switch (lastNum) { | ||
| case ('1'): | ||
| return "рубль"; | ||
| case ('2'): | ||
| case ('3'): | ||
| case ('4'): | ||
| return "рубля"; | ||
| default: | ||
| return "рублей"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
47 changes: 47 additions & 0 deletions
src/main/java/Input.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,47 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Input { | ||
|
|
||
| public int nextInt(Scanner scanner) { | ||
|
|
||
| boolean isContinue = true; | ||
| int val = 0; | ||
| while (isContinue) { | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| val = scanner.nextInt(); | ||
| isContinue = false; | ||
| } else { | ||
| System.out.println("Значение некорректного типа"); | ||
| scanner.next(); | ||
| } | ||
|
|
||
| } | ||
| return val; | ||
| } | ||
|
|
||
| public double nextDouble(Scanner scanner) { | ||
|
|
||
| boolean isContinue = true; | ||
| double val = 0; | ||
| while (isContinue) { | ||
|
|
||
| if (scanner.hasNextDouble()) { | ||
| val = scanner.nextDouble(); | ||
| isContinue = false; | ||
| } else { | ||
| System.out.println("Значение некорректного типа"); | ||
| scanner.next(); | ||
| } | ||
|
|
||
| } | ||
| return val; | ||
| } | ||
|
|
||
| public String next(Scanner scanner) { | ||
|
|
||
| return scanner.next(); | ||
|
|
||
| } | ||
|
|
||
| } |
10 changes: 10 additions & 0 deletions
src/main/java/Item.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,10 @@ | ||
| public class Item { | ||
|
|
||
| public String name; | ||
| public double price; | ||
|
|
||
| Item(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } |
78 changes: 76 additions & 2 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,80 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| System.out.println("На скольких человек делить счет?"); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
| Input input = new Input(); | ||
|
|
||
| int personsAmount = input.nextInt(scanner); | ||
|
|
||
| while (!isCorrectPersonsAmount(personsAmount)) { | ||
| System.out.println("Введенное значение некорректно. Повторите ввод"); | ||
| personsAmount = input.nextInt(scanner); | ||
| } | ||
|
|
||
| if (!isNeedToDistribute(personsAmount)) { | ||
| System.out.println("Для одного человека делить ничего не требуется."); | ||
| return; | ||
| } | ||
|
|
||
| Calc calc = new Calc(); | ||
|
|
||
| boolean isContinue = true; | ||
|
|
||
| while (isContinue) { | ||
|
|
||
| System.out.println("Введите наименование товара"); | ||
| String itemName = input.next(scanner); | ||
|
|
||
| System.out.println("Введите цену для товара " + itemName); | ||
| double itemPrice = input.nextDouble(scanner); | ||
|
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. нужно проверить, что |
||
| while (! isCorrectPrice(itemPrice)) { | ||
| System.out.println("Введенная цена должна быть больше нуля"); | ||
| itemPrice = input.nextDouble(scanner); | ||
| } | ||
|
|
||
| Item item = new Item(itemName, itemPrice); | ||
| calc.addItem(item); | ||
|
|
||
| System.out.println("Вы хотите добавить еще один товар? Если нет, введите \"" + finishAddComand() + "\" Для завершения ввода"); | ||
|
|
||
| String command = scanner.next(); | ||
| isContinue = !isFinishAddCommand(command); | ||
|
|
||
| } | ||
|
|
||
| System.out.println("Добавленные товары:\n" + calc.getItemsList()); | ||
|
|
||
| double sum = calc.getSum(); | ||
| double distributedSum = calc.distributedSum(sum, personsAmount); | ||
| Formatter formatter = new Formatter(); | ||
| System.out.println("Распределенная на каждого сумма: " + formatter.getFormatSum(distributedSum)); | ||
|
|
||
| } | ||
|
|
||
| static boolean isCorrectPersonsAmount(int personsAmount) { | ||
| return personsAmount >= 1; | ||
| } | ||
|
|
||
| static boolean isNeedToDistribute(int personsAmount) { | ||
| return personsAmount > 1; | ||
| } | ||
|
|
||
| static boolean isFinishAddCommand(String command) { | ||
| return command.equalsIgnoreCase(finishAddComand()); | ||
| } | ||
| } | ||
|
|
||
| static boolean isCorrectPrice(double price) { | ||
| return price > 0; | ||
| } | ||
| static String finishAddComand() { | ||
| return "завершить"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
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.