Repository navigation
Первая итерация Проектной работы №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
andrei-urich
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,14 @@ | ||
|
|
||
| public class Calculator { | ||
|
|
||
| public double splitSum(int peopleAmount, ItemsList itemsList) { | ||
| double sum = 0; | ||
| int s = itemsList.names.size(); | ||
| for (int i = 0; i < s; i = i + 1) { | ||
| sum += itemsList.names.get(i).price; | ||
| //System.out.println(sum); | ||
| } | ||
| return sum / peopleAmount; | ||
| } | ||
|
|
||
| } |
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,24 @@ | ||
| public class Formatter { | ||
|
|
||
| public String makeRightCase(double price) { | ||
| String rightCase="руб."; | ||
| int p = (int) price; | ||
|
|
||
| if (p > 10 && p < 15) { | ||
| rightCase = "рублей"; | ||
| return rightCase; | ||
| } | ||
|
|
||
| String p1 = Integer.toString(p); | ||
| int p2 = Integer.parseInt("" + p1.charAt(p1.length() - 1)); | ||
| if (p2 == 1) { | ||
| rightCase = "рубль"; | ||
| } else if (1 < p2 && p2 < 5) { | ||
| rightCase = "рубля"; | ||
| } else if (4 < p2 || p2 == 0) { | ||
| rightCase = "рублей"; | ||
| } | ||
| return rightCase; | ||
| } | ||
|
|
||
| } |
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,4 @@ | ||
| public class Item { | ||
| String name; | ||
| double price; | ||
| } |
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,49 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class ItemsList { | ||
| ArrayList<Item> names = new ArrayList<Item>(); | ||
|
|
||
| public void inputItemsList() { | ||
| System.out.println("Давайте добавим позиции из счета!"); | ||
| while (true) { | ||
| Item item = new Item(); | ||
| System.out.println("Введите текстовое наименование позиции"); | ||
| while (true) { | ||
| String regex = "^[a-zA-Zа-яА-Я]+$"; | ||
| Pattern pattern = Pattern.compile(regex); | ||
| item.name = MyScanner.scan.next(); | ||
| Matcher matcher = pattern.matcher(item.name); | ||
| if (matcher.matches()) { | ||
| break; | ||
| } else { | ||
| System.out.println("Некорректный ввод. Название позиции может содержать только буквы"); | ||
| } | ||
| } | ||
| System.out.println("Введите стоимость позиции в рублях в формате \"00.00\""); | ||
| while (true) { | ||
| try { | ||
| item.price = Double.parseDouble(MyScanner.scan.next()); | ||
| if (item.price <= 0) { | ||
| System.out.println("Некорректный ввод. Нужно ввести десятичное число больше нуля, с разделителем \".\""); | ||
| } else { | ||
| break; | ||
| } | ||
| } catch (NumberFormatException ignore) { | ||
| System.out.println("Некорректный ввод. Нужно ввести десятичное число больше нуля"); | ||
| } | ||
| } | ||
| names.add(item); | ||
| System.out.println("Позиция " + item.name + " с ценой " + item.price + " руб. добавлена."); | ||
|
|
||
| System.out.println("Чтобы добавить еще одну позицию - введите любой символ."); | ||
| System.out.println("Чтобы закончить добавление - напишите \"Завершить\""); | ||
| if (MyScanner.scan.next().equalsIgnoreCase("Завершить")) { | ||
| MyScanner.scan.close(); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
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,15 @@ | ||
|
|
||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| PersonsCount personsCount = new PersonsCount(); | ||
| ItemsList itemsList = new ItemsList(); | ||
| Calculator calculator = new Calculator(); | ||
| ResultViewer resultViewer = new ResultViewer(); | ||
|
|
||
| int peopleAmount = personsCount.getPeopleAmount(); | ||
| itemsList.inputItemsList(); | ||
| double moneyPerPerson = calculator.splitSum(peopleAmount, itemsList); | ||
| resultViewer.viewResult(itemsList, moneyPerPerson, peopleAmount); | ||
| } | ||
| } |
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,6 @@ | ||
| import java.util.Locale; | ||
| import java.util.Scanner; | ||
|
|
||
| public class MyScanner { | ||
| public static Scanner scan = new Scanner(System.in).useLocale(Locale.US); | ||
| } |
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,22 @@ | ||
| public class PersonsCount { | ||
| public int getPeopleAmount() { | ||
| while (true) { | ||
|
|
||
| System.out.println("Пожалуйста, введите цифру, на сколько человек делим счет"); | ||
| try { | ||
| int peopleAmount = MyScanner.scan.nextInt(); | ||
| if (peopleAmount > 1 && peopleAmount % 1 == 0) { | ||
| //MyScanner.scan.close(); | ||
| return peopleAmount; | ||
| } else {System.out.println("Должно быть целое число больше 1 ;)");} | ||
|
|
||
| }catch (Exception e) | ||
| { | ||
| System.out.println("Должно быть целое число больше 1 ;)"); | ||
| MyScanner.scan.next(); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
| } |
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,15 @@ | ||
| public class ResultViewer { | ||
|
|
||
| public void viewResult(ItemsList itemsList, double moneyPerPerson, int peopleAmount) { | ||
| Formatter formatter = new Formatter(); | ||
| double sum = moneyPerPerson * peopleAmount; | ||
| System.out.println("Добавленные товары:"); | ||
| for (int i = 0; i < itemsList.names.size(); i = i + 1) { | ||
|
|
||
| System.out.println(itemsList.names.get(i).name + " " + itemsList.names.get(i).price + " " + formatter.makeRightCase(itemsList.names.get(i).price)); | ||
| } | ||
| System.out.println("Общая сумма счета: " + String.format("%.2f", sum) + " " + formatter.makeRightCase(sum)); | ||
| System.out.println("Делим на " + peopleAmount + "-х."); | ||
| System.out.println("Итого с каждого по " + String.format("%.2f", moneyPerPerson) + " " + formatter.makeRightCase(moneyPerPerson)); | ||
| } | ||
| } |
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.
Рекомендация:
Касаемо разделителей "." и ",":
На вход ты получаешь
Stringу этого типа есть большое количество методов для преобразования строки.Одни из которых
replace(char oldChar, char newChar). С его помощью, ты можешь заменить один символ на другой, а в данном случае "," на "."Получится вот так:
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.
Илья, спасибо за рекомендацию!