forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Лаба 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
Dan0nK
wants to merge
3
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
Лаба 1 #1
Changes from all commits
Commits
Show all changes
3 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,47 @@ | ||
| public class Calculator { | ||
| String allNames = ""; | ||
| double allPrices = 0; | ||
|
|
||
| String declension(double priceD) { | ||
| int priceI = (int) Math.floor(priceD); | ||
|
|
||
| String rubl = " рубль"; | ||
| String rublya = " рубля"; | ||
| String rubley = " рублей"; | ||
|
|
||
| if (priceI > 100) { | ||
| priceI %= 100; | ||
| } | ||
|
|
||
| if (priceI > 20) { | ||
| priceI %= 10; | ||
| } | ||
|
|
||
| switch (priceI) { | ||
| case 1: | ||
| return rubl; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return rublya; | ||
| default: | ||
| return rubley; | ||
| } | ||
| } | ||
|
|
||
| void add(String name, double price) { | ||
| allNames = allNames + "\n" + name + " " + String.format("%.2f", price) + declension(price); | ||
| allPrices += price; | ||
| System.out.println("Товар успешно добавлен!"); | ||
| } | ||
|
|
||
| void out() { | ||
| System.out.println("Добавленные товары:" + allNames); | ||
| System.out.println("Общая стоимость: " + allPrices + declension(allPrices)); | ||
| } | ||
|
|
||
| void calculate(int people) { | ||
| double one = allPrices / people; | ||
| System.out.println("Каждый должен заплатить: " + String.format("%.2f", one) + declension(one)); | ||
| } | ||
| } |
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,53 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class CheckInput { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| int CheckCountPeople() { | ||
| while (true) { | ||
| System.out.println("На скольких человек разделить счёт?"); | ||
| String s = scanner.nextLine(); | ||
| try { | ||
| int countPeople = Integer.parseInt(s); | ||
| if (countPeople > 1) { | ||
| return countPeople; | ||
| } else { | ||
| System.out.println("Ошибка. Попробуйте ещё раз. Введите число > 1"); | ||
| } | ||
|
|
||
| } catch (Exception e) { | ||
| System.out.println("Ошибка. Попробуйте ещё раз. Введите число > 1"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| String CheckNameGood() { | ||
| while (true) { | ||
| System.out.println("Укажите название товара"); | ||
| String name = scanner.nextLine(); | ||
| if (name.length()> 2) { | ||
| return name; | ||
| } else { | ||
| System.out.println("Ошибка. Попробуйте ещё раз. Название должно содержать минимум 3 символ."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| double CheckPriceGood() { | ||
| while (true) { | ||
| System.out.println("Укажите цену товара"); | ||
| String s = scanner.nextLine(); | ||
| try { | ||
| double price = Double.parseDouble(s); | ||
| if (price > 0.01) { | ||
| return price; | ||
| } else { | ||
| System.out.println("Ошибка. Попробуйте ещё раз. Введите число > 0.01"); | ||
| } | ||
|
|
||
| } catch (Exception e) { | ||
| System.out.println("Ошибка. Попробуйте ещё раз. Введите число > 0.01"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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,30 @@ | ||
| import com.sun.tools.javac.util.Convert; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| // dev branch for Y.Practicum | ||
| public class Main { | ||
|
|
||
| private static final Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| CheckInput checkInput = new CheckInput(); | ||
| Calculator calculator = new Calculator(); | ||
| int countPeople = checkInput.CheckCountPeople(); | ||
|
|
||
| while (true) { | ||
| String name = checkInput.CheckNameGood(); | ||
| double price = checkInput.CheckPriceGood(); | ||
|
|
||
| calculator.add(name, price); //добавление к списку и стоимости | ||
|
|
||
| System.out.println("Хотите добавить ещё один товар? (Для добавления нового товара - введите любой символ. Для того чтобы закончить - введите \"Завершить\".)"); | ||
| if (scanner.next().equalsIgnoreCase("завершить")) { | ||
| calculator.out(); //вывод списка и общей стоимости | ||
| break; | ||
| } | ||
| scanner.skip(".*\n"); | ||
| } | ||
| calculator.calculate(countPeople); //расчёт на одного человека | ||
| } | ||
| } |
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.
в Java принято называть методы camelCase'ом, т.е. с маленькой буквы