-
Notifications
You must be signed in to change notification settings - Fork 220
Pull Request приложения «Калькулятор счёта» #100
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
Open
Changes from all commits
Commits
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
35 changes: 17 additions & 18 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,21 @@ | ||
| class Calculator { | ||
| public class Calculator { | ||
|
|
||
| int friendsCount; | ||
|
|
||
| String cart = "Добавленные товары:"; | ||
| double totalPrice = 0; | ||
| public void cheque(int number, double totalPrice){ | ||
| //Вычисляем, сколько должен заплать каждый человек | ||
| double result = (float) totalPrice / (float) number; | ||
| int money = (int) Math.floor(result); | ||
|
|
||
| Calculator(int friendsCount) { | ||
| this.friendsCount = friendsCount; | ||
| String str = String.format("%.2f", result); | ||
| //Вывод суммы, которую должен заплатить каждый поровну | ||
| if ((money % 100) / 10 == 1){ | ||
| System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рублей"); | ||
| }else if (money % 100 == 1){ | ||
| System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рубль"); | ||
| }else if (money % 10 >= 2 && money % 10 <= 4){ | ||
| System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рубля"); | ||
| }else { | ||
| System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рублей"); | ||
| } | ||
| } | ||
|
|
||
| void addItem(Item item) { | ||
| totalPrice += item.price; | ||
| cart = cart + "\n" + item.name; | ||
|
|
||
| System.out.println(item.name + " в корзине"); | ||
| } | ||
|
|
||
| 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.
48 changes: 10 additions & 38 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,21 @@ | ||
| 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(); | ||
| People people = new People(); | ||
| //Ввод количества человек, на которых требуется разделить счет | ||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
| people.numberOfPeople(); | ||
|
|
||
| calculator.addItem(new Item(name, price)); | ||
| //Добавление продуктов | ||
| Product product = new Product(); | ||
| product.addProduct(); | ||
|
|
||
| System.out.println( | ||
| "Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления"); | ||
| String answer = scanner.next(); | ||
| //Вывод суммы, которую должен заплатить каждый человек | ||
| Calculator calculator = new Calculator(); | ||
| calculator.cheque(people.number, product.totalPrice); | ||
|
|
||
| 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)); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
src/main/java/People.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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import java.util.Scanner; | ||
| public class People { | ||
| int number = 0; | ||
|
|
||
| // Добавление количества человек, на которых необходимо разделить всю сумму | ||
| public void numberOfPeople(){ | ||
|
|
||
| while (true){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| if (scanner.hasNextInt()){ | ||
| int count = scanner.nextInt(); | ||
| if (count > 1){ | ||
| number = count; | ||
| break; | ||
| }else if (count == 1){ | ||
| System.out.println("Количество человек, введенное вами, равно 1. Пожалуйста, введите значение больше 1."); | ||
| }else{ | ||
| System.out.println("Вы ввели некорректное значение. Пожалуйста, введите значение больше 1."); | ||
| } | ||
|
|
||
| }else{ | ||
| System.out.println("Вы ввели некорректное значение. Пожалуйста, введите значение больше 1."); | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
src/main/java/Product.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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Product { | ||
|
|
||
| String productList = ""; | ||
| String spacing = "\n"; | ||
| String str = "Добавленные товары: "; | ||
| double totalPrice; | ||
|
|
||
| //Добавление товаров и их стоимости | ||
| public void addProduct(){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Введите название товара"); | ||
| String name = scanner.nextLine(); | ||
| productList += str + name + spacing; | ||
| System.out.println("Введите стоимость товара"); | ||
| while (true){ | ||
| if (scanner.hasNextDouble()){ | ||
| double price = scanner.nextDouble(); | ||
| totalPrice+=price; | ||
| System.out.println("Товар успешно добавлен"); | ||
| anotherProduct(); | ||
| break; | ||
| }else { | ||
| System.out.println("Вы ввели некорректное значение. Пожалуйста, введите корректное значение стоимости"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| //Спрашиваем у пользователя - хочет ли он добавить еще один товар? | ||
| public void anotherProduct(){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Хотите добавить ещё один товар?"); | ||
| String fork = scanner.nextLine(); | ||
|
|
||
| /*Если пользователь вводит "Да", то повторно вызываем метод addProduct | ||
| Иначе завершаем добавление продуктов и выводим список добавленных продуктов*/ | ||
| if (fork.equalsIgnoreCase("Да")){ | ||
| addProduct(); | ||
|
|
||
| }else { | ||
| addedProducts(productList); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| //Вывод добавленных продуктов | ||
| public void addedProducts (String lists){ | ||
|
|
||
| System.out.println(lists); | ||
| } | ||
|
|
||
| } |
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.