Repository navigation
TASK-1 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
nst-personal
wants to merge
6
commits into
dev
Choose a base branch
from
task/sprint-2-1
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
Show all changes
6 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,25 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Calculator { | ||
| private List<Product> productList = new ArrayList<>(); | ||
| private double currentSum; | ||
|
|
||
| public void add(Product product) { | ||
| productList.add(product); | ||
| addToSum(product.getPrice()); | ||
| } | ||
|
|
||
| private void addToSum(double price) { | ||
| currentSum += price; | ||
| } | ||
|
|
||
| public double getSum() { | ||
| return currentSum; | ||
| } | ||
|
|
||
| public List<Product> getProducts() { | ||
| return productList; | ||
| } | ||
|
|
||
| } |
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,83 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Calculator calculator = new Calculator(); | ||
| Scanner scanner = new Scanner(System.in); | ||
| int numberOfPeople = getNumberOfPeople(scanner); | ||
| fillProducts(calculator, scanner); | ||
| displayProducts(calculator); | ||
| calculateSumPerPerson(calculator, numberOfPeople); | ||
| scanner.close(); | ||
| } | ||
| private static int getNumberOfPeople(Scanner scanner) { | ||
| int numberOfPeople = 0; | ||
| while (numberOfPeople <= 1) { | ||
| System.out.println("Enter number of people to split the bill"); | ||
| if (scanner.hasNextInt()) { | ||
| numberOfPeople = scanner.nextInt(); | ||
| if (numberOfPeople <= 0) { | ||
| System.out.println("The number of people must be positive"); | ||
| } else if (numberOfPeople == 1) { | ||
| System.out.println("There is no point in counting"); | ||
| } | ||
| } else { | ||
| System.out.println("Invalid input. Please enter value"); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| return numberOfPeople; | ||
| } | ||
|
|
||
| private static void fillProducts(Calculator calculator, Scanner scanner) { | ||
| System.out.println("Add product"); | ||
| String action = null; | ||
| while (action == null || !action.equalsIgnoreCase("done")) { | ||
| addProduct(scanner, calculator); | ||
| System.out.println("Do you want to add more? Fill 'done' to complete"); | ||
| action = scanner.next(); | ||
| } | ||
| } | ||
|
|
||
| private static void addProduct(Scanner scanner, Calculator calculator) { | ||
| String name = null; | ||
| while (name == null || name.trim().isEmpty()) { | ||
| System.out.println("Enter name of product"); | ||
| name = scanner.next(); | ||
| } | ||
| double price = 0.0; | ||
| while (price <= 0 || isValueHas2Digits(price)) { | ||
| System.out.println("Enter price of product"); | ||
| if (scanner.hasNextDouble()) { | ||
| price = scanner.nextDouble(); | ||
| if (price <= 0 || isValueHas2Digits(price)) { | ||
| System.out.println("The number of people must be positive and has 2 digits after dot"); | ||
| } | ||
| } else { | ||
| System.out.println("Invalid input. Please enter value"); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| calculator.add(new Product(name, price)); | ||
| System.out.println("Product added"); | ||
| } | ||
|
|
||
| private static boolean isValueHas2Digits(double price) { | ||
| return Math.abs(price * 100 - Math.round(price * 100)) > 0.001; | ||
| } | ||
|
|
||
| private static void displayProducts(Calculator calculator) { | ||
| System.out.println("Added products:"); | ||
| RubleFormatter rubleFormatter = new RubleFormatter(); | ||
| for (Product product: calculator.getProducts()) { | ||
| System.out.printf("%s || %.2f %s\n", product.getName(), product.getPrice(), rubleFormatter.format(product.getPrice())); | ||
| } | ||
| } | ||
|
|
||
| private static void calculateSumPerPerson(Calculator calculator, int numberOfPeople) { | ||
| double sumPerPerson = calculator.getSum() / numberOfPeople; | ||
| RubleFormatter rubleFormatter = new RubleFormatter(); | ||
| System.out.printf("Sum per person ===> %.2f %s\n", sumPerPerson, rubleFormatter.format(sumPerPerson)); | ||
| } | ||
|
|
||
| } | ||
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,17 @@ | ||
| public class Product { | ||
| private String name; | ||
| private double price; | ||
| public Product(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public double getPrice() { | ||
| return 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,16 @@ | ||
| public class RubleFormatter { | ||
| public String format(double number) { | ||
| int floorNumber = (int) Math.floor(number); | ||
| int lastDigit = floorNumber % 10; | ||
| int lastTwoDigits = floorNumber % 100; | ||
| if (lastTwoDigits >= 11 && lastTwoDigits <= 19) { | ||
| return "rubley"; | ||
| } else if (lastDigit == 1) { | ||
| return "ruble"; | ||
| } else if (lastDigit >= 2 && lastDigit <= 4) { | ||
| return "rublya"; | ||
| } else { | ||
| return "rubley"; | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.