forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Dev #2
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
Dev #2
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
53 changes: 53 additions & 0 deletions
src/main/java/Calculator.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,53 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Calculator { | ||
| public Calculator(int personCount, ArrayList<Dish> dishesList) { | ||
| calculate(personCount, dishesList); | ||
| } | ||
|
|
||
| private void calculate(int personCount, ArrayList<Dish> dishesList){ | ||
| showDishList(dishesList); | ||
| totalPriceForPerson(personCount, dishesList); | ||
| } | ||
|
|
||
| private String getRightEnding(double price){ | ||
| String ending = ""; | ||
| String x = "рубль"; | ||
| String y = "рубля"; | ||
| String z = "рублей"; | ||
| if(price%100 >= 11 && price%100 <= 14){ | ||
| ending = z; | ||
| return ending; | ||
| } | ||
| if(price%10 == 1){ | ||
| ending = x; | ||
| return ending; | ||
| } | ||
| if(price%10 == 2 || price%10 == 3 || price%10 == 4){ | ||
| ending = y; | ||
| return ending; | ||
| } | ||
| return z; | ||
| } | ||
|
|
||
| private void showDishList(ArrayList<Dish> dishesList){ | ||
| System.out.println("Добавленные товары:"); | ||
| double price; | ||
| for(int i = 0; i < dishesList.size(); i++){ | ||
| price = dishesList.get(i).getPrice(); | ||
| String ending = getRightEnding(price); | ||
| System.out.println("Позиция: " + dishesList.get(i).getName() + " Цена: " + String.format("%.2f", dishesList.get(i).getPrice()) + " " + ending); | ||
| } | ||
| } | ||
|
|
||
| private void totalPriceForPerson(int personCount, ArrayList<Dish> dishesList){ | ||
| Double totalPrice = 0.0; | ||
| for(int i = 0; i < dishesList.size(); i++){ | ||
| totalPrice = totalPrice + dishesList.get(i).getPrice(); | ||
| } | ||
| totalPrice = totalPrice / personCount; | ||
| double price = totalPrice; | ||
| String ending = getRightEnding(price); | ||
| System.out.println("Итоговая цена для каждой персоны: " + String.format("%.2f", totalPrice) + " " + ending); | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
src/main/java/Dish.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,57 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Dish { | ||
|
|
||
| private Double price; | ||
| private String name; | ||
|
|
||
| public Dish() { | ||
| setName(); | ||
| setPrice(); | ||
| System.out.println("Позиция успешно добавлена!"); | ||
| } | ||
|
|
||
| public Double getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName() { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
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(true){ | ||
| System.out.println("Введите название блюда."); | ||
| String line = scanner.nextLine(); | ||
| if(line.trim().isEmpty()){ | ||
| System.out.println("Ничего не введено. Попробуйте снова"); | ||
| continue; | ||
| } | ||
| else{ | ||
| name = line; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void setPrice() { | ||
| Scanner scanner; | ||
| while(true){ | ||
| System.out.println("Укажите стоимость " + name); | ||
| scanner = new Scanner(System.in);; | ||
| if(!scanner.hasNextDouble()){ | ||
| System.out.println("Нужно число без букв. Попробуйте снова"); | ||
| continue; | ||
| } | ||
| price = scanner.nextDouble(); | ||
| if(price <= 0.0){ | ||
| System.out.println("Цена должна быть положительной. Попробуйте снова"); | ||
| continue; | ||
| } | ||
| else{ | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
49 changes: 45 additions & 4 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.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| // dev branch for Y.Practicum | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Double price; | ||
| String name; | ||
|
|
||
| ArrayList<Dish> dishesList = new ArrayList<>(); | ||
|
|
||
| int personCount = getPersonCount(); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
| String line = ""; | ||
| while(true){ | ||
| dishesList.add(new Dish()); | ||
| System.out.println("Для добавления нового товара введите что угодно. Для завершения введите 'завершить'"); | ||
| line = scanner.nextLine(); | ||
| if(line.equalsIgnoreCase("завершить")){ | ||
| System.out.println("Составление списка успешно завершино"); | ||
| break; | ||
| } | ||
| } | ||
| Calculator calculator = new Calculator(personCount, dishesList); | ||
|
|
||
| } | ||
| private static int getPersonCount(){ | ||
| Scanner scanner; | ||
| while(true){ | ||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
| scanner = new Scanner(System.in); | ||
| if(!scanner.hasNextInt()){ | ||
| System.out.println("Нужно целое число без букв. Попробуйте снова"); | ||
| continue; | ||
| } | ||
| int personCount = scanner.nextInt(); | ||
| if(personCount < 2){ | ||
| System.out.println("Число персон должно быть не меньше двух. Попробуйте снова"); | ||
| continue; | ||
| } | ||
| else{ | ||
| System.out.println("Счёт будет разделён на " + personCount + " персоны"); | ||
| return personCount; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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.