-
Notifications
You must be signed in to change notification settings - Fork 220
First project YP #22
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
First project YP #22
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
38 changes: 38 additions & 0 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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| String addedGoods ="";// Строка, хранящая перечень всех товаров | ||
| double sumAllAddedGoods = 0.00;// Строка, хранящая сумму всех товаров | ||
|
|
||
|
|
||
| public void getTheName(String position) {// Добавить товар | ||
| addedGoods = addedGoods+position+"\n"; | ||
| }// функция добавления товара | ||
| public void addition (double cost) {// Добавить стоимость | ||
| sumAllAddedGoods += cost; | ||
| }// функция прибавки цены товара | ||
| public void addedGoods(){// функция вывода перечня товаров | ||
| System.out.println("Добавленные товары:\n"); | ||
| System.out.println(addedGoods); | ||
| } | ||
| public void totalSum (int persons){// функция вычисления, сколько должен каждый | ||
| double sumRound = 0.00;// Вводим переменную для округления суммы товаров | ||
| String message = "Сумма которую должен заплатить каждый: %.2f "; | ||
| String message0 = ""; | ||
| String message1 ="рубль"; | ||
| String message2 ="рубля"; | ||
| String message3 ="рублей"; | ||
| sumRound = Math.floor(sumAllAddedGoods/persons);// Округление числа в меньшую сторону для определения окончания | ||
| double checkNumber = sumRound%10; | ||
| if (checkNumber>=2&&checkNumber<=4) { | ||
| message0 = message2; // окончание"рубля" | ||
| } | ||
| else if (checkNumber ==1){ | ||
| message0 = message1; | ||
| } | ||
| else { | ||
| message0 = message3; | ||
| } | ||
| System.out.println(String.format(message,sumAllAddedGoods/persons) + message0); | ||
| } | ||
|
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. Видно, что местами не хватает отступов или пробелов (в условиях выше, например), ты можешь применить в студии автоформатирование (в выбранном файле, сверху вкладка Code - Reformat Code, либо Ctrl+Alt+L), тогда автоматически код выправится. |
||
| } | ||
56 changes: 55 additions & 1 deletion
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,8 +1,62 @@ | ||
| import java.util.Locale; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| int friendsNumber; | ||
| while (true) { //Цикл ввода количества персон, на которое делится счет | ||
| System.out.println("На сколько человек нужно разделить счёт?\nВведите целым числом"); | ||
| Scanner scanner = new Scanner(System.in).useLocale(Locale.US); // Ввод числа людей | ||
| if (scanner.hasNextInt()) { | ||
| friendsNumber = scanner.nextInt();// Запоминаем число | ||
|
|
||
| //System.out.println(friendsNumber);//проверка 1 - вывод числа | ||
| if (friendsNumber < 1) { | ||
| System.out.println("Круто, но платить некому\nВведите реальное количество людей"); // Проверка на 0 и отрицательность | ||
| } else if (friendsNumber == 1) { | ||
| System.out.println("Придется оплачивать самостоятельно\nВведите реальное количество людей"); // Проверка на 1 | ||
| } else { | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Неправильный ввод"); | ||
| } | ||
| } | ||
| Calculator calc = new Calculator(); | ||
| while (true) { | ||
| System.out.println("Введите название товара или напишите \"завершить\""); | ||
| Scanner scanner = new Scanner(System.in);// Ввод | ||
| String pos = scanner.next();//Считывание названия товара | ||
| if (!pos.equalsIgnoreCase("завершить")) {// проверяем, что "не завершить" | ||
| calc.getTheName(pos);//Добавляем товар в наш перечень | ||
| System.out.println("товар добавлен"); | ||
| while (true) { | ||
| System.out.println("введите стоимость товара в формате рубли.копейки [10.45, 11.40]"); | ||
| double price;//Считывание цены товара | ||
| Scanner scanner1 = new Scanner(System.in).useLocale(Locale.US);// Ввод | ||
| if (scanner1.hasNextDouble()) { | ||
| price = scanner1.nextDouble(); | ||
| if (price > 0) { //Проверяем, что число положительное | ||
| calc.addition(price); | ||
| break; | ||
| } else { | ||
| System.out.println("введено неправильное значение"); | ||
| } | ||
| // если отрицательное или 0, ввод ещё раз | ||
| } | ||
| System.out.println("Неправильный ввод"); | ||
| } | ||
| } else { | ||
| break; | ||
| }// пользователь ввел "завершить" | ||
| } | ||
| calc.addedGoods();// Выводим перечень товаров | ||
| calc.totalSum(friendsNumber); // Вывод суммы, которую каждый должен заплатить | ||
|
|
||
| } | ||
| } | ||
|
|
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.