Repository navigation
Creatint a Calculator program #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
KiMate34
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
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,89 @@ | ||
| import java.util.InputMismatchException; | ||
| import java.util.LinkedList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| int persons; | ||
| float price; | ||
| String name; | ||
|
|
||
| int countingPersons(){ | ||
| try { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите количество человек:"); | ||
| persons = scanner.nextInt(); | ||
|
|
||
| if (persons < 1) { | ||
| System.out.println("Вы ввели некорректное число человек"); | ||
| } else { | ||
| return persons; | ||
| } | ||
| } | ||
| } | ||
| catch (InputMismatchException e){ | ||
| System.out.println("Вы ввели некорректный символ вместо числа. Введите данные заново"); | ||
| return countingPersons(); | ||
| } | ||
| } | ||
| void countingProducts(){ | ||
| LinkedList<Product> list = new LinkedList<>(); | ||
| Scanner scanner = new Scanner(System.in); | ||
| String exit = "ЗАВЕРШИТЬ"; | ||
| int persons = countingPersons(); | ||
| float sum = 0; | ||
|
|
||
| System.out.println("Введите название товара:" + | ||
| "\nВведите 'Завершить' для того, чтобы завершить процесс добавления товаров."); | ||
|
|
||
| while (true) { | ||
| name = scanner.next(); | ||
|
|
||
| if (name.equalsIgnoreCase(exit)) { | ||
| sum = sum / persons; | ||
| String sumProduct = "%.2f"; | ||
| Converter converter = new Converter(sum); | ||
|
|
||
| System.out.println("Добавленные товары:"); | ||
| for (int i = 0; i < list.size(); i++) { | ||
| System.out.println(list.get(i).name + " " + list.get(i).price); | ||
| } | ||
| System.out.println("Каждый должен заплатить: " + String.format(sumProduct, sum) | ||
| + " " + converter.check()); | ||
| break; | ||
| } | ||
|
|
||
| System.out.println("Введите стоимость товара в формате: [руб,коп]:"); | ||
|
|
||
| while (true) { | ||
| try { | ||
| price = scanner.nextFloat(); | ||
| if (price <= 0) { | ||
| System.out.println("Вы ввели некорректную сумму товара! Попробуйте ввести сумму товара снова."); | ||
| } | ||
| else{ | ||
| break; | ||
| } | ||
| } | ||
| catch (InputMismatchException e){ | ||
| if ((Integer.toString((int)price).equalsIgnoreCase(exit))){ | ||
| break; | ||
| } | ||
| System.out.println("Вы ввели некорректный символ вместо числа или ввели некоректное число."); | ||
| countingProducts(); | ||
| } | ||
| } | ||
| Product product = new Product(name, price); | ||
|
|
||
| list.add(product); | ||
|
|
||
| sum = (sum + product.price); | ||
|
|
||
| System.out.println("Товар успешно добавлен! Хотите добавить ещё один товар?" + | ||
| "\nИли введите 'Завершить', чтобы завершить процесс добавления товаров."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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 @@ | ||
| public class Converter { | ||
| float count; | ||
|
|
||
| Converter(float count){ | ||
| this.count = count; | ||
| } | ||
|
|
||
| String check(){ | ||
| float remainTen = count % 10; | ||
| float remainHundred = count % 100; | ||
|
|
||
| if (remainTen > 10 && remainTen < 20 || remainHundred > 10 && remainHundred < 20){ | ||
| return "рублей"; | ||
| } | ||
| else if(remainTen >= 1 && remainTen < 2 || remainHundred >= 1 && remainHundred < 2){ | ||
| return "рубль"; | ||
| } | ||
| else if (remainTen > 1 && remainTen < 5 || remainHundred > 1 && remainHundred < 5){ | ||
| return "рубля"; | ||
| } | ||
| else{ | ||
| return "рублей"; | ||
| } | ||
| } | ||
| } |
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,8 @@ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Calculator calculator = new Calculator(); | ||
| calculator.countingProducts(); | ||
| } | ||
| } | ||
| } | ||
|
|
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; | ||
| float price; | ||
|
|
||
| Product(String name, float price){ | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } |
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.
🍏 При печати в памяти будут создаваться промежуточные строки из-за использования оператора "+". Можно использовать StringBuilder, который сформирует только результирующую строку.
https://habr.com/ru/articles/260767/