forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Моё первое дз #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
PaladinLeonid
wants to merge
1
commit into
dev
Choose a base branch
from
main
base: dev
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
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 |
|---|---|---|
| @@ -1 +0,0 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
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,55 @@ | ||
| import java.util.HashMap; | ||
|
|
||
| public class Calculator { | ||
| private double productSum; | ||
| private final HashMap<Product, Integer> order = new HashMap<>(); | ||
|
|
||
| public void addProduct (Product product, int quantity) { | ||
| if (order.containsKey(product)) { | ||
| int oldQuantity = order.get(product); | ||
| int newQuantity = oldQuantity + quantity; | ||
| order.put(product, newQuantity); | ||
| }else { | ||
| order.put(product, quantity); | ||
| } | ||
| productSum = calculate(); | ||
| } | ||
|
|
||
| private double calculate() { | ||
| double sum = 0; | ||
| for (Product product : order.keySet()){ | ||
| int quantity = order.get(product); | ||
| sum += product.price * quantity; | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| public void showProduct(){ | ||
| System.out.println("Список товаров: "); | ||
| for (Product product : order.keySet()){ | ||
| System.out.println(product.name + " " + String.format("%.2f", product.price)); | ||
| } | ||
| } | ||
|
|
||
| public double getProductsum() { | ||
| return productSum; | ||
| } | ||
|
|
||
| public String rublesFormat(double rubles) { | ||
| int lastDigit = (int) (Math.floor(rubles) % 10); | ||
| if (((int) (Math.floor(rubles) % 100)) <= 19 && ((int)(Math.floor(rubles) % 100)) >= 11) | ||
| return "рублей"; | ||
| switch (lastDigit) { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| 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,71 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| int peopleCount = getpeopleCount(); | ||
| calculateProduct(peopleCount); | ||
| } | ||
| } | ||
|
|
||
| private static int getpeopleCount() { | ||
| int peopleCount; | ||
| while (true){ | ||
| System.out.println("Введите кол-во людей"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| String peopleCountString = scanner.next(); | ||
| peopleCount = parseStringIntoInt(peopleCountString); | ||
| if (peopleCount == 1){ | ||
| System.out.println("Нет смысла высчитывать на 1-го человека"); | ||
| }else if (peopleCount < 1) { | ||
| System.out.println("Введено неверное число"); | ||
| }else { | ||
| break; | ||
| } | ||
| } | ||
| return peopleCount; | ||
| } | ||
|
|
||
| private static void calculateProduct(int peopleCount) { | ||
| Calculator calculator = new Calculator(); | ||
| while (true){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Введите название продукта или \"Завершить\" что бы закончить." ); | ||
| String productName = scanner.next(); | ||
| if (productName.equalsIgnoreCase("Завершить")){ | ||
| break; | ||
| } | ||
| System.out.println("Введите цену в формате \"рубли.копейки\""); | ||
| double productPrice; | ||
| String productPriceString = scanner.next(); | ||
| productPrice = parseStringIntoDouble(productPriceString); | ||
| if (productPrice < 0){ | ||
| System.out.println("Введено неверное значение"); | ||
| }else { | ||
| Product product = new Product(productName, productPrice); | ||
| calculator.addProduct(product,1); | ||
| System.out.println("Продукт добавлен"); | ||
| } | ||
| } | ||
| calculator.showProduct(); | ||
| double perconСheck = calculator.getProductsum()/peopleCount; | ||
| System.out.println("Каждый должен заплатить по: " + String.format("%.2f",perconСheck) + " " + calculator.rublesFormat(perconСheck)); | ||
| } | ||
|
|
||
| private static int parseStringIntoInt(String string){ | ||
| try { | ||
| return Integer.parseInt(string); | ||
| }catch (NumberFormatException e){ | ||
| return -1; | ||
| } | ||
| } | ||
| private static double parseStringIntoDouble(String string) { | ||
| try { | ||
| return Double.parseDouble(string); | ||
| }catch (NumberFormatException e){ | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
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,9 @@ | ||
| public class Product { | ||
| String name; | ||
| double price; | ||
|
|
||
| Product(String name, double price){ | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } |
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.
🍏 Лишнее поле, ты можешь вызывать
calculateпри вызовеgetProductsumили реализовать это логику сразу в этой функции