Repository navigation
Pull request для проектной работы №1 #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
dreygoff
wants to merge
2
commits into
main
Choose a base branch
from
dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
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
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 @@ | ||
|
|
||
|
|
||
| public class Calculator { | ||
|
|
||
| String list = ""; | ||
| float totalPrice = 0; | ||
|
|
||
| RubleFormatter formatter = new RubleFormatter(); | ||
|
|
||
| void add(float price) { | ||
| totalPrice += price; | ||
| } | ||
|
|
||
| void writeToTheList(String nameOfProduct, float priceOfProduct) { | ||
| list += nameOfProduct.trim().toLowerCase(); | ||
| list += " "; | ||
| list += priceOfProduct; | ||
| list += "\n"; | ||
| } | ||
|
|
||
| void showCheck(int numberOfUsers) { | ||
| System.out.println("Добавленные товары:"); | ||
| System.out.println(list); | ||
| String messageTemplateTotalPrice = "Итоговая сумма: %.2f %s."; | ||
| String messageTemplateResult = "Каждый должен заплатить %.2f %s."; | ||
|
|
||
| String formattedUnit = formatter.formatUnit(totalPrice); | ||
| System.out.println(String.format(messageTemplateTotalPrice, totalPrice, formattedUnit)); | ||
|
|
||
| formattedUnit = formatter.formatUnit(totalPrice / numberOfUsers); | ||
| System.out.println(String.format(messageTemplateResult, totalPrice / numberOfUsers, formattedUnit)); | ||
| } | ||
| } |
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,100 @@ | ||
|
|
||
|
|
||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| int numberOfUsers = -1; | ||
|
|
||
| Scanner scanner = new Scanner(System.in); // Создаем экземпляр класса Scanner. | ||
| Calculator calculator = new Calculator(); | ||
|
|
||
| numberOfUsers = getNumberOfPeople(scanner); | ||
| getProductsInfo(scanner, calculator); | ||
| calculator.showCheck(numberOfUsers); | ||
|
|
||
| scanner.close(); // Закрываем Scanner по завершении работы. | ||
| } | ||
|
|
||
|
|
||
| public static int getNumberOfPeople(Scanner scan) { | ||
| int numb = -1; | ||
| while (true) { | ||
| System.out.println("Введите, на скольких человек необходимо разделить счёт"); | ||
|
|
||
| numb = getInt(scan); | ||
| if (numb > 1) { | ||
| break; | ||
| } else { | ||
| System.err.println("Некорректное число"); | ||
| } | ||
| } | ||
| return numb; | ||
| } | ||
|
|
||
|
|
||
| public static void getProductsInfo(Scanner scan, Calculator calculator) { | ||
| while (true) { | ||
| String nameOfProduct = ""; | ||
| float priceOfProduct = 0; | ||
|
|
||
| System.out.println("Введите название товара"); | ||
| nameOfProduct = scan.next(); | ||
|
|
||
|
|
||
| while (priceOfProduct <= 0) { | ||
|
|
||
| System.out.println("Введите цену товара"); | ||
|
|
||
| priceOfProduct = getFloat(scan); | ||
|
|
||
| if (priceOfProduct > 0) { | ||
| calculator.add(priceOfProduct); | ||
| calculator.writeToTheList(nameOfProduct, priceOfProduct); | ||
| System.out.println("Товар успешно добавлен"); | ||
| } else { | ||
| System.err.println("Некорректная цена!"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| System.out.println("Продолжить ввод товаров? Введите \"Завершить\"," + | ||
| " если хотите окончить ввод товаров, или любой символ, если хотите продолжить ввод"); | ||
| String command = scan.next(); | ||
|
|
||
| if (command.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public static float getFloat(Scanner scan) { | ||
| try { | ||
| float number = scan.nextFloat(); | ||
| return number; | ||
| } catch (InputMismatchException e) { | ||
| scan.nextLine(); | ||
| return 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static int getInt(Scanner scan) { | ||
| try { | ||
| int number = scan.nextInt(); | ||
|
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 number; | ||
| } catch (InputMismatchException e) { | ||
| scan.nextLine(); | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
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. Лишние отступы |
||
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,28 @@ | ||
| public class RubleFormatter { | ||
|
|
||
|
|
||
| String formatUnit(float price) { | ||
| /* | ||
| Здесь форматируем окончание валюты, в зависимости от цены. | ||
| Если предпоследняя цифра "1", то возвращаем рублей | ||
| Иначе если последняя 1 - рубль, 2,3,4 - рубля, 5,6,7,8,9,0 - рублей | ||
| */ | ||
|
|
||
| int formattedPrice = (int) Math.floor(price); | ||
|
|
||
| if (formattedPrice % 100 / 10 == 1) { | ||
| return "рублей"; | ||
| } | ||
|
|
||
| switch (formattedPrice % 10) { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| 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.
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.
Тоже самое только hasNextFloat