forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Проектная работа №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
morechiba
wants to merge
7
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
7 commits
Select commit
Hold shift + click to select a range
18175b1
первая рабочая версия без форматирования
morechiba eaa43a2
убран неиспользуемый сканер
morechiba d120208
обработана ошибка - отрицательная сумма товара
morechiba 6e0ee53
класс Calculator вынесен в отдельный файл
morechiba 3552ae0
классы Good, Formater вынесены в отдельные файлы
morechiba 2189db8
добавлено форматирование итоговой суммы на человека
morechiba c6792c8
в форматировании итоговой суммы добавлена обработка числа больше 100
morechiba 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,35 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
|
|
||
| public class Calculator { | ||
| ArrayList<Good> goodList = new ArrayList<>(); | ||
|
|
||
| float sum = 0; | ||
| int quantityPersons; | ||
|
|
||
| float getSum(ArrayList<Good> goodList) { | ||
|
|
||
| this.goodList = goodList; | ||
| Iterator iterator = goodList.iterator(); | ||
|
|
||
| while(iterator.hasNext()){ | ||
| Good good = (Good) iterator.next(); | ||
| sum = sum + good.price; | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| float getSumForPerson(int quantityPersons, float sum) { | ||
| this.sum = sum; | ||
| this.quantityPersons = quantityPersons; | ||
| float sumForPerson = sum / quantityPersons; | ||
| return sumForPerson; | ||
| } | ||
|
|
||
| /* | ||
|
|
||
| System.out.println(String.format("Каждый должен заплатить " + f%.2f + " р.", sumForPerson)); | ||
| */ | ||
|
|
||
|
|
||
| } |
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,26 @@ | ||
| public class Formater { | ||
| float sumForPerson = 0; | ||
| String formatItog(float sumForPerson) { | ||
| this.sumForPerson = sumForPerson; | ||
| int formaterBase = (int)sumForPerson; | ||
| String rubl = ""; | ||
|
|
||
| if(formaterBase >= 100) { | ||
| formaterBase = formaterBase % 100; | ||
| } | ||
| if(formaterBase > 20){ | ||
| formaterBase = formaterBase % 10; | ||
| } | ||
|
|
||
| if (formaterBase == 1) { | ||
| rubl = "рубль"; | ||
| } else if (formaterBase > 1 && formaterBase < 5){ | ||
| rubl = "рубля"; | ||
| } else { | ||
| rubl = "рублей"; | ||
| } | ||
| String itog = String.format("%.2f %s", sumForPerson, rubl); | ||
| return itog; | ||
| } | ||
|
|
||
| } | ||
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,8 @@ | ||
| public class Good { | ||
| String name; | ||
| float price; | ||
| Good(String name, float price){ | ||
| this.name = name; | ||
| this.price = 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 |
|---|---|---|
| @@ -1,6 +1,108 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| int quantityPersons = getQuantityPersons(); | ||
|
|
||
| ArrayList<Good> goodList = getGoodList(); | ||
|
|
||
| Calculator calculator = new Calculator(); | ||
| Formater formater = new Formater(); | ||
| float sum = calculator.getSum(goodList); | ||
| float sumForPerson = calculator.getSumForPerson(quantityPersons, sum); | ||
| String goodListString = getGoodListString(goodList); | ||
| String formatString = formater.formatItog(sumForPerson); | ||
|
|
||
| System.out.println(goodListString); | ||
| System.out.println("Сумма на человека: " + formatString + "."); | ||
|
|
||
| } | ||
|
|
||
| public static int getQuantityPersons() { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| int quantityPersons; | ||
|
|
||
| while(true) { | ||
| System.out.println("На сколько человек разделить счет?"); | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| quantityPersons = scanner.nextInt(); | ||
| if(quantityPersons <= 0) { | ||
| System.out.println("Некорректное значение."); | ||
| } else if (quantityPersons == 1) { | ||
| System.out.println("Нечего делить!"); | ||
| } else { | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Введите число."); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| return quantityPersons; | ||
| } | ||
| } | ||
| public static ArrayList<Good> getGoodList() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| ArrayList<Good> goodList = new ArrayList<>(); | ||
| int position = 0; | ||
| while(true) { | ||
| if(position == 0) { | ||
| System.out.println("Введите наименование товара."); | ||
| } else { | ||
| System.out.println("Введите наименование товара или команду Завершить."); | ||
| } | ||
|
|
||
| String goodName = scanner.next(); | ||
| if(goodName.equalsIgnoreCase("Завершить")){ | ||
| break; | ||
| } else { | ||
| System.out.println("Введите стоимость товара."); | ||
| while(scanner.hasNext()){ | ||
| if(scanner.hasNextFloat()){ | ||
| float goodPrice = scanner.nextFloat(); | ||
| if(goodPrice <= 0){ | ||
| System.out.println("Стоимость товара не может быть отрицательной или равна нулю."); | ||
| } else { | ||
| Good good = new Good(goodName, goodPrice); | ||
| goodList.add(good); | ||
| System.out.println("Товар " + goodName + " успешно добавлен."); | ||
| position++; | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Стоимость - целое число или число с запятой."); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| float sum = 0; | ||
| Iterator iterator = goodList.iterator(); | ||
|
|
||
| while(iterator.hasNext()){ | ||
| Good good = (Good) iterator.next(); | ||
| sum = sum + good.price; | ||
| } | ||
| return goodList; | ||
| } | ||
|
|
||
| public static String getGoodListString(ArrayList<Good> goodL) { | ||
| ArrayList<Good> goodList = goodL; | ||
| String goodListString = new String(""); | ||
| Iterator iterator = goodList.iterator(); | ||
|
|
||
| while(iterator.hasNext()){ | ||
| Good good = (Good) iterator.next(); | ||
| goodListString = goodListString + "\n" +good.name; | ||
| } | ||
|
|
||
| goodListString = "Добавленные товары:" + goodListString; | ||
| return goodListString; | ||
| } | ||
| } | ||
|
|
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.
Строки можно вынести в константы