forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
create Calculator #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
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
56 changes: 56 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,56 @@ | ||
| public class Calculator { | ||
| private int peoples; | ||
| private double finalPrice; | ||
| private String allProductNames = ""; | ||
|
|
||
| public void setPeoples(int peoples) { | ||
| this.peoples = peoples; | ||
| } | ||
|
|
||
| public String addProduct(Product product) { | ||
| finalPrice = finalPrice + product.productPrice; | ||
| if (allProductNames.isEmpty()) { | ||
| allProductNames = product.productName; | ||
| } else { | ||
| allProductNames = allProductNames + ", \n" + product.productName; | ||
| } | ||
|
|
||
| return String.format("Продукт %s по цене %.2f добавлен.", product.productName, product.productPrice); | ||
| } | ||
|
|
||
| public String getAllProductNames() { | ||
| return allProductNames; | ||
| } | ||
|
|
||
| public String getFinalPrice() { | ||
| return ("Итоговая общая цена: " + convertPriceToString(finalPrice)); | ||
| } | ||
|
|
||
| public String getPriceForOnePeople() { | ||
| double priceForOnePeople = finalPrice / peoples; | ||
| return ("Каждый человек должен заплатить: " + convertPriceToString(priceForOnePeople)); | ||
| } | ||
|
|
||
| private String convertPriceToString(double price) { | ||
| String stringPrice = String.format("%.2f", price); | ||
|
|
||
| price = Math.floor(price); | ||
| double remains = price % 100; | ||
|
|
||
| if (11 <= remains && remains <= 14) { | ||
| stringPrice = stringPrice + " рублей"; | ||
| } else { | ||
| remains = remains % 10; | ||
| if (remains == 1) { | ||
| stringPrice = stringPrice + " рубль"; | ||
| } | ||
| else if (2 <= remains && remains <= 4) { | ||
| stringPrice = stringPrice + " рубля"; | ||
| } else { | ||
| stringPrice = stringPrice + " рублей"; | ||
| } | ||
| } | ||
|
|
||
| return stringPrice; | ||
| } | ||
| } |
89 changes: 88 additions & 1 deletion
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,95 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| static Scanner scanner; | ||
| static Calculator calculator; | ||
| static int peoples = 0; | ||
| static boolean isPeoplesSetCorrectly = true; | ||
| static boolean isProductPurchased = false; | ||
| static String stopProductPurchased = "Завершить"; | ||
|
|
||
| public static void main(String[] args) { | ||
| scanner = new Scanner(System.in); | ||
|
|
||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| while (isPeoplesSetCorrectly) { | ||
| System.out.println("На сколько человек делим счет?"); | ||
|
|
||
| try { | ||
| peoples = scanner.nextInt(); | ||
|
|
||
| if (peoples == 1) { | ||
| showErrorMessage("Количество человек должно быть более 1."); | ||
| } | ||
| if (peoples < 1) { | ||
| showErrorMessage("Маловато будет. Это менее 1."); | ||
| } | ||
| if (peoples > 1) { | ||
| isPeoplesSetCorrectly = false; | ||
| isProductPurchased = true; | ||
| } | ||
|
|
||
|
|
||
| } catch (Exception e) { | ||
| showErrorMessage(e.getMessage()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| calculator = new Calculator(); | ||
| calculator.setPeoples(peoples); | ||
|
|
||
| while (isProductPurchased) { | ||
| System.out.println("Ведите название товара."); | ||
| String productName; | ||
| double productPrice; | ||
| productName = scanner.next(); | ||
|
|
||
| boolean isPriceSetCorrectly = false; | ||
| while (!isPriceSetCorrectly) { | ||
|
|
||
| System.out.println("Введите цену товара в формате <рубли.копейки>."); | ||
| String stringProductPrice = scanner.next().trim(); | ||
| if (stringProductPrice.contains(",")) { | ||
| stringProductPrice = stringProductPrice.replace(",", "."); | ||
| } | ||
|
|
||
| try { | ||
| productPrice = Double.parseDouble(stringProductPrice); | ||
| if (productPrice <= 0.0) { | ||
| showErrorMessage("Цена товара слишком мала. " + productPrice); | ||
| break; | ||
| } else { | ||
| Product product = new Product(productName, productPrice); | ||
| System.out.println(calculator.addProduct(product)); | ||
| isPriceSetCorrectly = true; | ||
| } | ||
| } catch (Exception e) { | ||
| System.out.println(e.getMessage()); | ||
| showErrorMessage("Цена должна быть указана в виде <xx.yy>." + e.getMessage()); | ||
| } | ||
| } | ||
| System.out.println("Желаете добавить еще товар? Для завершения наберите <Завершить>."); | ||
|
|
||
| String buyerWantsBuy = scanner.next(); | ||
| if (buyerWantsBuy.trim().equalsIgnoreCase(stopProductPurchased)) { | ||
| isProductPurchased = false; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| System.out.println(calculator.getAllProductNames()); | ||
| System.out.println(calculator.getFinalPrice()); | ||
|
|
||
| System.out.println(calculator.getPriceForOnePeople()); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| static void showErrorMessage(String errorString) { | ||
| System.out.println("Ошибка! " + errorString); | ||
| } | ||
|
|
||
| } |
9 changes: 9 additions & 0 deletions
src/main/java/Product.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,9 @@ | ||
| public class Product { | ||
| String productName; | ||
| double productPrice; | ||
|
|
||
| public Product(String productName, double productPrice) { | ||
| this.productName = productName; | ||
| this.productPrice = productPrice; | ||
| } | ||
| } |
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.