Repository navigation
Проектная работа №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
AGALODEB
wants to merge
4
commits into
main
Choose a base branch
from
master
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
4 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 |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ | |
| .externalNativeBuild | ||
| .cxx | ||
| local.properties | ||
| .idea/ | ||
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
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,91 @@ | ||
| import java.util.Scanner; | ||
| import java.lang.Math; | ||
| public class App { | ||
| Scanner sc = new Scanner(System.in); | ||
| Calculator calculator = new Calculator(); | ||
|
|
||
| public void run() { | ||
| int peopleCount = getPeopleCount(); | ||
| readItems(); | ||
| double sum = calculator.getSum(); | ||
| String info = calculator.getItemsAsStr(); | ||
| System.out.println(info); | ||
| double avg = sum / peopleCount; | ||
| double roundedValue = Math.floor(avg); | ||
| String template; | ||
| if (roundedValue % 100 > 9 && roundedValue % 100 < 21){ | ||
| template = "%.2f рублей"; | ||
| } | ||
| else if (roundedValue % 10 == 1) { | ||
| template = "%.2f рубль"; | ||
| } | ||
| else if ( roundedValue % 10 > 1 && roundedValue % 10 < 5 ) { | ||
| template = "%.2f рубля"; | ||
| } | ||
| else { | ||
| template = "%.2f рублей"; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для множественных вариантов лучше использовать конструкцию switch |
||
| System.out.println(String.format(template, avg)); | ||
|
|
||
|
|
||
| } | ||
| private int getPeopleCount(){ | ||
| while(true){ | ||
| try { | ||
| System.out.println("Укажите колличество человек"); | ||
| String myString = sc.next(); | ||
| int peopleCount = Integer.parseInt(myString); | ||
| if (peopleCount <= 1) { | ||
| System.out.println("Колличество человек должно быть больше 1"); | ||
| continue; | ||
| } | ||
| else { | ||
| return peopleCount; | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Неревный ввод"); | ||
|
|
||
| } | ||
|
|
||
| } | ||
| } | ||
| private void readItems(){ | ||
| while (true) { | ||
| System.out.println("Введите название продукта"); | ||
| String title = sc.next(); | ||
| while (true) { | ||
| try { | ||
| System.out.println("Введите цену продукта"); | ||
| String myString1 = sc.next(); | ||
| double price = Double.parseDouble(myString1); | ||
| if (price > 0) { | ||
| Item item = new Item(title, price); | ||
| calculator.addItem(item); | ||
| System.out.println("Товар успешно добавлен"); | ||
| System.out.println("Если это все продукты, то введите 'exit', иначе введите любой символ"); | ||
| String finish = sc.next(); | ||
| if (finish.equalsIgnoreCase("exit")) { | ||
| break; | ||
| } else { | ||
| continue; | ||
| } | ||
| } else { | ||
| System.out.println("Неревный ввод"); | ||
| continue; | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Неревный ввод"); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
| break; | ||
|
|
||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
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,22 +1,22 @@ | ||
| class Calculator { | ||
|
|
||
| int friendsCount; | ||
|
|
||
| String cart = "Добавленные товары:"; | ||
| double totalPrice = 0; | ||
|
|
||
| Calculator(int friendsCount) { | ||
| this.friendsCount = friendsCount; | ||
| import java.util.ArrayList; | ||
| public class Calculator{ | ||
| ArrayList<Item> items = new ArrayList<Item>(); | ||
| public double getSum(){ | ||
| double sum = 0; | ||
| for (Item item: items){ | ||
| sum += item.price; | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| void addItem(Item item) { | ||
| totalPrice += item.price; | ||
| cart = cart + "\n" + item.name; | ||
|
|
||
| System.out.println(item.name + " в корзине"); | ||
| public void addItem(Item item){ | ||
| items.add(item); | ||
| } | ||
|
|
||
| double divideSum() { | ||
| return totalPrice / friendsCount; | ||
| public String getItemsAsStr(){ | ||
| String info = "Добавленные товары: \n"; | ||
| for (Item item: items){ | ||
| info += item.toString() + "\n"; | ||
| } | ||
| return info; | ||
| } | ||
| } | ||
|
|
||
| } |
This file was deleted.
Oops, something went wrong.
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,10 +1,12 @@ | ||
| class Item { | ||
|
|
||
| String name; | ||
| public class Item { | ||
| String title; | ||
| double price; | ||
|
|
||
| Item(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| Item(String Title, double Price) { | ||
| title = Title; | ||
| price = Price; | ||
| } | ||
| public String toString(){ | ||
| String priceAsStr = Double.toString(price); | ||
| return title + " " + priceAsStr; | ||
| } | ||
| } |
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,49 +1,8 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| int friendCount; | ||
| while (true) { | ||
| System.out.println("На сколько человек необходимо разделить счет?"); | ||
| friendCount = scanner.nextInt(); | ||
|
|
||
| if (friendCount > 1) { | ||
| break; | ||
| } else if (friendCount == 1) { | ||
| System.out.println( | ||
| "Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы."); | ||
| } else { | ||
| System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз."); | ||
| } | ||
| } | ||
|
|
||
| Calculator calculator = new Calculator(friendCount); | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите название товара"); | ||
| String name = scanner.next(); | ||
|
|
||
| System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]"); | ||
| double price = scanner.nextDouble(); | ||
|
|
||
| calculator.addItem(new Item(name, price)); | ||
|
|
||
| System.out.println( | ||
| "Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления"); | ||
| String answer = scanner.next(); | ||
|
|
||
| if (answer.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| double result = calculator.divideSum(); | ||
| Formatter formatter = new Formatter(); | ||
|
|
||
| System.out.println(calculator.cart); | ||
| System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result)); | ||
| App app = new App(); | ||
| app.run(); | ||
| } | ||
| } | ||
|
|
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.
Так как калькулятор один на всю программу, можно сделать его, его методы и поля static,чтобы не нужно было создавать его экземпляр. Тогда можно будет просто вызывать методы напрямую: Calculator:.getSum()