forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
консольное приложение калькулятор #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
volkovdimaya
wants to merge
5
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
5 commits
Select commit
Hold shift + click to select a range
d40889a
консольное приложение калькулятор
volkovdimaya e3d3f4c
исправлено окончание с рублем
volkovdimaya fa74f29
исправлено окончание с рублем
volkovdimaya 6e67f89
переделано конкатенация строк
volkovdimaya 29f2b99
переделал методы в Product equals и hashCode только по имени
volkovdimaya 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,67 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| private double sum; | ||
| private Scanner scaner = new Scanner(System.in); | ||
| private ArrayList<Product> products = new ArrayList<>(); | ||
| private String iputName() { | ||
| System.out.println("Введите название товара"); | ||
| String name = scaner.next(); | ||
| return name; | ||
| } | ||
|
|
||
| private double inputCost() { | ||
| while (true) { | ||
| System.out.println("Введите стоимость"); | ||
| if (scaner.hasNextDouble()) { | ||
| double cost = scaner.nextDouble(); | ||
| if (cost <= 0) { | ||
| System.out.println("Стоимость не может быть меньше 0"); | ||
| scaner.nextLine(); | ||
| continue; | ||
| } | ||
| return cost; | ||
| } else { | ||
| System.out.println("Ошибка, попробуйте еще раз"); | ||
| scaner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void addProduct() { | ||
| while (true) { | ||
| System.out.println("Добавление товара в калькулятор"); | ||
| Product product = new Product(iputName(), inputCost()); | ||
| products.add(product); | ||
| sum += product.cost; | ||
| System.out.println(String.format("Товар %s успешно добавлен!", product.name)); | ||
| System.out.println("Продолжить добовление товара?"); | ||
| System.out.println("Ввести команду \"Завершить\" для того, чтоб завершить процесс добавления товаров"); | ||
|
|
||
| String comand = scaner.next(); | ||
| if (comand.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void showProducts() { | ||
| FormatterRub formatterRub = new FormatterRub();//для форматирования окончания рубля | ||
| System.out.println("Добавленные товары:"); | ||
| String message = "Товар: %s цена: %.2f %s"; | ||
| for (Product product : products) { | ||
| System.out.println(String.format(message, product.name, product.cost, formatterRub.correctEnding(product.cost))); | ||
| } | ||
| } | ||
|
|
||
| public void showResult(int countHuman) { | ||
| FormatterRub formatterRub = new FormatterRub(); | ||
| System.out.println(String.format("Всего товаров на сумму: %.2f %s", sum, formatterRub.correctEnding(sum))); | ||
| double result = sum / countHuman; | ||
|
|
||
| String text = "Каждый должен заплатить: %.2f %s"; | ||
| System.out.println(String.format(text, result, formatterRub.correctEnding(result))); | ||
| } | ||
|
|
||
| } |
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,20 @@ | ||
| public class FormatterRub { | ||
|
|
||
| public String correctEnding(double value) { | ||
| value = Math.floor(value); | ||
|
|
||
| value = value % 100; | ||
| if (value > 10 && value < 20) { | ||
| return "рублей"; | ||
| } else { | ||
| value = value % 10; | ||
| if (value == 1) { | ||
| return "рубль"; | ||
| } else if (value > 1 && value < 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,32 @@ | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| System.out.println("Добрый день"); | ||
| Scanner scaner = new Scanner(System.in); | ||
| int input = 0; | ||
| while (true) { | ||
| System.out.println("На сколько человек необходимо разделить счёт?"); | ||
| if (scaner.hasNextInt()) { | ||
| input = scaner.nextInt(); | ||
| if (input == 1) { | ||
| System.out.println("Нет смысла ничего считать и делить"); | ||
| } else if (input < 1) { | ||
| System.out.println("Это некорректное значение для подсчёта"); | ||
| } else if (input > 1) { | ||
| Calculator cal = new Calculator(); | ||
| cal.addProduct(); | ||
| cal.showProducts(); | ||
| cal.showResult(input); | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Ошибка, попробуйте еще раз"); | ||
| scaner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
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,23 @@ | ||
| public class Product { | ||
| String name; | ||
| double cost; | ||
|
|
||
| Product(String name, double cost) { | ||
| this.name = name; | ||
| this.cost = cost; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Product product = (Product) o; | ||
| return name.equals(product.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = name == null ? 0 : name.hashCode(); | ||
| return result; | ||
| } | ||
| } |
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.