This repository was archived by the owner on May 15, 2023. It is now read-only.
Repository navigation
Calculator and related methods created #1
Open
Kileren
wants to merge
1
commit 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
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,73 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
|
|
||
| ArrayList<Product> products = new ArrayList<>(); | ||
|
|
||
| void start(int numberOfPeople, Scanner scanner) { | ||
| do { | ||
| String productName = getProductName(scanner); | ||
| double productPrice = getProductPrice(scanner); | ||
| products.add(new Product(productName, productPrice)); | ||
| successfullyAddedMessage(); | ||
| } while (shouldContinueAddingProducts(scanner)); | ||
|
|
||
| printResults(numberOfPeople); | ||
| } | ||
|
|
||
| String getProductName(Scanner scanner) { | ||
| System.out.println("Введите название товара:"); | ||
| String productName = ""; | ||
| while (productName.isEmpty()) { | ||
| productName = scanner.next(); | ||
| if (productName.isEmpty()) { | ||
| System.out.println("ОШИБКА. Введенное значение не должно быть пустым. Введите еще раз:"); | ||
| } | ||
| } | ||
| return productName; | ||
| } | ||
|
|
||
| double getProductPrice(Scanner scanner) { | ||
| double price = -1; | ||
| System.out.println("Введите стоимость товара:"); | ||
| while (price < 0) { | ||
| String priceStr = scanner.next(); | ||
| try { | ||
| price = Double.parseDouble(priceStr); | ||
| if (price < 0) { | ||
| System.out.println("ОШИБКА. Стоимость товара не может быть меньше 0, введите снова:"); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("ОШИБКА. Введеное значение не является числом, введите снова:"); | ||
| } | ||
| } | ||
| return price; | ||
| } | ||
|
|
||
| void successfullyAddedMessage() { | ||
| System.out.println("Товар успешно добавлен.\n"); | ||
| } | ||
|
|
||
| boolean shouldContinueAddingProducts(Scanner scanner) { | ||
| System.out.println("Хотите добавить еще один товар?"); | ||
| System.out.println("Введите \"Завершить\", чтобы прекратить ввод товаров или любой символ, чтобы продолжить добавления товаров."); | ||
|
|
||
| String userInput = scanner.next(); | ||
| return !userInput.equalsIgnoreCase("Завершить"); | ||
| } | ||
|
|
||
| void printResults(int numberOfPeople) { | ||
| double totalPrice = 0; | ||
|
|
||
| System.out.println("Добавленные товары:"); | ||
| for (int i = 0; i < products.size(); i++) { | ||
| Product product = products.get(i); | ||
| System.out.println(product.name); | ||
| totalPrice += product.price; | ||
| } | ||
|
|
||
| String amountPerPerson = PriceFormatter.format(totalPrice / numberOfPeople); | ||
| System.out.printf("\nКаждый человек должен заплатить - %s", amountPerPerson); | ||
| } | ||
| } | ||
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,34 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in).useDelimiter("\n"); | ||
| int numberOfPeople = getNumberOfPeople(scanner); | ||
|
|
||
| Calculator calculator = new Calculator(); | ||
| calculator.start(numberOfPeople, scanner); | ||
|
|
||
| scanner.close(); | ||
|
Kileren marked this conversation as resolved.
|
||
| } | ||
|
|
||
| static int getNumberOfPeople(Scanner scanner) { | ||
| int numberOfPeople = 0; | ||
| System.out.println("На скольких человек необходимо разделить счет?"); | ||
| while (numberOfPeople <= 1) { | ||
| String userInput = scanner.next(); | ||
| try { | ||
| numberOfPeople = Integer.parseInt(userInput); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("ОШИБКА. Ожидается целочисленное значение. Введите корректное число:"); | ||
| continue; | ||
| } | ||
|
|
||
| if (numberOfPeople == 1) { | ||
| System.out.println("ОШИБКА. Некорректное число. На одного человека нет смысла ничего считать и делить. Введите корректное число."); | ||
| } else if (numberOfPeople < 1) { | ||
| System.out.println("ОШИБКА. Некорректное число. Количество людей должно быть больше 1. Введите корректное число"); | ||
| } | ||
| } | ||
| return numberOfPeople; | ||
| } | ||
| } | ||
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,15 @@ | ||
| public class PriceFormatter { | ||
|
|
||
| static String format(double amount) { | ||
| int mainPart = (int) Math.floor(amount); | ||
| String rub; | ||
| if (mainPart % 10 == 1 && mainPart % 100 != 11) { | ||
| rub = "рубль"; | ||
| } else if (mainPart % 10 >= 2 && mainPart % 10 <= 4 && (mainPart % 100 < 10 || mainPart % 100 >= 20)) { | ||
| rub = "рубля"; | ||
| } else { | ||
| rub = "рублей"; | ||
| } | ||
| return String.format("%.2f %s", amount, rub); | ||
| } | ||
| } |
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; | ||
| } | ||
| } | ||
|
Kileren marked this conversation as resolved.
|
||
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.