-
Notifications
You must be signed in to change notification settings - Fork 220
ДЗ с калькулятором после ревью #28
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 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
3 changes: 3 additions & 0 deletions
.idea/.gitignore
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
.idea/codeStyles/Project.xml
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
.idea/codeStyles/codeStyleConfig.xml
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
.idea/compiler.xml
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
.idea/gradle.xml
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
.idea/misc.xml
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
.idea/vcs.xml
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
73 changes: 59 additions & 14 deletions
src/main/java/Calculator.java
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,67 @@ | ||
| class Calculator { | ||
| import java.util.Scanner; | ||
|
|
||
| int friendsCount; | ||
| public class Calculator { | ||
| //класс и метод подсчета общей стоимости товара | ||
| public void calculate(int guests) { | ||
| String food = "Dobavlennie tovari: \n"; | ||
| Double price = 0.0; | ||
| while (true) { | ||
| System.out.println("Vvedite nazvanie produkta ili zavershit: "); | ||
| Scanner scanner = new Scanner(System.in); | ||
| String inputFood = scanner.next(); | ||
| //если ничего не добавлено и введено "завершить" | ||
| if (inputFood.equalsIgnoreCase("Zavershit") && food.equals("Dobavlennie tovari: \n")) { | ||
| break; | ||
| } else if (inputFood.equalsIgnoreCase("Zavershit")) { | ||
| System.out.println(food); | ||
| System.out.println("Kagdiy chelovek zaplatit: " + String.format("%.2f", price / guests) + " " + getRubleAddition(price / guests)); | ||
| break; | ||
|
|
||
| String cart = "Добавленные товары:"; | ||
| double totalPrice = 0; | ||
| } else { | ||
|
|
||
| Calculator(int friendsCount) { | ||
| this.friendsCount = friendsCount; | ||
| System.out.println("Vvedite cenu tovara [rub.cop]"); | ||
| Double inputPrice = 0.0; | ||
| String inputString; | ||
| // программа принимала значение стоимости через точку,но при вводе запятой крашилась, теперь программа принимает как точку, так и запятую, при вводе | ||
| // некорректного значения цены выводится сообщение о том, что значение введено некорректно | ||
| try { | ||
| inputString = scanner.next(); | ||
| inputString = inputString.replace(",", "."); | ||
| inputPrice = Double.valueOf(inputString); | ||
| } catch (Exception e) { | ||
| System.out.println("Vvedite znachenie stoimosti zanovo i korrektno v ukazanom formate!"); | ||
| continue; | ||
| } | ||
| if (inputPrice > 0) { | ||
|
|
||
| food = food + inputFood + "\n"; | ||
| price = price + inputPrice; | ||
| System.out.println("Tovar " + inputFood + " Dobavlen uspeshno, ego cena sostavlaet: " + inputPrice + " " + getRubleAddition(inputPrice)); | ||
| } else { | ||
| //случай когда цена товара меньше 0 | ||
| System.out.println("Cena tovara ne mozhet bit menshe 0!"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void addItem(Item item) { | ||
| totalPrice += item.price; | ||
| cart = cart + "\n" + item.name; | ||
| //метод для написания рублей в правильном падеже | ||
| public String getRubleAddition(Double num) { | ||
| int preLastDigit = (int) (num % 100 / 10); | ||
| if (preLastDigit == 1) { | ||
| return "rubley"; | ||
| } | ||
|
|
||
| System.out.println(item.name + " в корзине"); | ||
| switch ((int) (num % 10)) { | ||
| case 1: | ||
| return "rubl"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "rublya"; | ||
| default: | ||
| return "rubley"; | ||
| } | ||
| } | ||
|
|
||
| double divideSum() { | ||
| return totalPrice / friendsCount; | ||
| } | ||
| } | ||
| } |
17 changes: 0 additions & 17 deletions
src/main/java/Formatter.java
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
src/main/java/Item.java
Oops, something went wrong.
55 changes: 19 additions & 36 deletions
src/main/java/Main.java
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,32 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| //обработка количества гостей | ||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| int friendCount; | ||
| System.out.println("Vvedite kolichestvo gostey: "); | ||
| while (true) { | ||
| System.out.println("На сколько человек необходимо разделить счет?"); | ||
| friendCount = scanner.nextInt(); | ||
|
|
||
| if (friendCount > 1) { | ||
| break; | ||
| } else if (friendCount == 1) { | ||
| System.out.println( | ||
| "Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы."); | ||
| } else { | ||
| System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз."); | ||
| Scanner scanner = new Scanner(System.in); | ||
| int guests = 0; | ||
| // условие проверки ввода текста при запросе количества гостей | ||
| try { | ||
| guests = scanner.nextInt(); | ||
| } catch (Exception ex) { | ||
| System.out.println("Vvedeno nekorektnoe znachenie gostey, vvedite cifru"); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| 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 (guests <= 0) { | ||
| System.out.println("Vvedite nekorrektnoe znachenie,vvedite znachenie bolshe 1: "); | ||
| } else if (guests == 1) { | ||
| System.out.println("A zachem togta schitat? vvedite znachenie bolshe 1: "); | ||
| } else { | ||
| System.out.println("Seyachas pschitaem!"); | ||
| Calculator calculator = new Calculator(); | ||
| calculator.calculate(guests); | ||
|
|
||
| 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)); | ||
| } | ||
| } | ||
|
|
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.