forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Спринт 2/23 → Тема 5/6: Проектная работа №1 #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
Sobdenis
wants to merge
2
commits 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
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
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,129 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| AccountCalculator сalculator = new AccountCalculator(); | ||
|
|
||
| сalculator.start(); | ||
| } | ||
| } | ||
| class AccountCalculator { | ||
| ArrayList<String> сalculatorList = new ArrayList<>(); | ||
| ArrayList<Double> сalculatorDouble = new ArrayList<>(); | ||
|
|
||
| int userInput; | ||
| String productName; | ||
| Double productPrice; | ||
| String productPriceString; | ||
| String finish; | ||
| Double totalСost = 00.00; | ||
|
|
||
| private void сalculator() { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
|
|
||
| System.out.println("Введите название товара:"); | ||
| productName = scanner.next(); | ||
|
|
||
| сalculatorList.add(productName); | ||
| System.out.println("Введите стоимость в формате 00,00 руб.коп.:"); | ||
| productPriceString = scanner.next(); | ||
| productPriceString = productPriceString.replaceAll(",", "."); | ||
| while (!isNumeric(productPriceString)) { | ||
| System.out.print("Вы ввели некорректное значение\n"); | ||
| productPriceString = scanner.next(); | ||
| } | ||
|
|
||
| productPrice = Double.parseDouble(productPriceString); | ||
| сalculatorDouble.add(productPrice); | ||
| totalСost = totalСost + productPrice; | ||
| String result = String.format("%.2f", totalСost); | ||
| System.out.print("\nСписок добавленного товара:\n"); | ||
| for (int i = 0; i < сalculatorList.size(); i++) { | ||
| String name = сalculatorList.get(i); | ||
| double price = сalculatorDouble.get(i); | ||
| String rubName = " рубль"; | ||
| int rub1 = (int) price; | ||
| switch (rub1 % 10){ case 1: | ||
| rubName = " рубль"; break; | ||
| case 2: case 3: case 4: | ||
| rubName = " рубля"; break; | ||
| case 5: case 6: case 7: case 8: case 9: case 0: | ||
| rubName = " рублей"; | ||
| } | ||
| System.out.print(i+1 + " " + name + " " + price + " " + rubName + "\n"); | ||
| } | ||
|
|
||
| System.out.print("Общая стоимость: " + result + "\n \n"); | ||
| System.out.print("Товар добавлен успешно! \nЧтобы продолжить добавление товара введите любой символ \nЧтобы завершить добавления товара введите команду 'Завершить'\n"); | ||
| finish = scanner.next(); | ||
| finish (finish, userInput, totalСost); | ||
| } | ||
| private void finish (String finish, int userInput, double totalСost) { | ||
| String fin = "Завершить"; | ||
| String finСost = String.format("%.2f", (totalСost/userInput)); | ||
| System.out.print("\nСписок добавленного товара:\n"); | ||
| for (int i = 0; i < сalculatorList.size(); i++) { | ||
| String name = сalculatorList.get(i); | ||
| double price = сalculatorDouble.get(i); | ||
| int namb = i; | ||
| String rubName = " рубль"; | ||
| int rub1 = (int) price; | ||
| switch (rub1 % 10){ case 1: | ||
| rubName = " рубль"; break; | ||
| case 2: case 3: case 4: | ||
| rubName = " рубля"; break; | ||
| case 5: case 6: case 7: case 8: case 9: case 0: | ||
| rubName = " рублей"; | ||
| } | ||
| System.out.print(++namb + " " + name + " " + price + " " + rubName + "\n"); | ||
| } | ||
| if (finish.equalsIgnoreCase(fin)) { | ||
| System.out.print("Делим счет\n"); | ||
| System.out.print("Каждый должен заплатить " + finСost); | ||
| int rub = (int) totalСost/userInput; | ||
| switch (rub % 10){ case 1: | ||
| System.out.println(" рубль"); break; | ||
| case 2: case 3: case 4: | ||
| System.out.println(" рубля"); break; | ||
| case 5: case 6: case 7: case 8: case 9: case 0: | ||
| System.out.println(" рублей"); break; | ||
| } | ||
| } else { | ||
| сalculator(); | ||
| } | ||
| } | ||
| public static boolean isNumeric(String productPriceString) { | ||
| try { | ||
| Double.parseDouble(productPriceString); | ||
| return true; | ||
| } catch (NumberFormatException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public void start() { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| do { | ||
| System.out.println("На скольких человек необходимо разделить счёт \n(Введите количество человек более 1):"); | ||
| while (!scanner.hasNextInt()) { | ||
| System.out.println("Это некорректное значение для подсчёта \n\n На скольких человек необходимо разделить счёт \n(Введите количество человек более 1):"); | ||
| scanner.next(); | ||
| } | ||
| userInput = scanner.nextInt(); | ||
| if (userInput <=1) { | ||
| System.out.println("Ошибка"); | ||
| start(); | ||
| } | ||
| } while (userInput <= 1); | ||
| System.out.println("Будем делить счёт на " + userInput + " чел."); | ||
| сalculator(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
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.
не очень хорошее решение - сложно читается. Тут получается рекурсия и ошибка ввода обрабатывается в двух местах. Если бы функция была сложнее можно было бы легко запутаться. Лучше было бы написать что то типа
while (true){ String userInputString = scanner.next(); if (isUserInputStringOk()) { userInput = Integer.getInteger(userInputString); break; } else { System.out.println("Ошибка"); } }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.
Спасибо, что предлагаете более простые и правильные решения. Учимся на ошибках.