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
ilyaPetr
wants to merge
2
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
2 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 |
|---|---|---|
| @@ -1,8 +1,128 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| int numberOfFriends;//количество друзей | ||
| String stopInput;//завершение ввода | ||
| String foodName;//название товара | ||
| float foodPrice;//цена товара | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
| Calculator calculator = new Calculator(); | ||
|
|
||
| System.out.println("на скольких человек необходимо разделить счёт?"); | ||
|
|
||
| while (true) { | ||
| if (scanner.hasNextInt()) { | ||
| numberOfFriends = scanner.nextInt(); | ||
| if (numberOfFriends > 1) { | ||
| break; | ||
| } else { | ||
| System.out.println("Друзей должно быть больше 1! Введите количество друзей!"); | ||
| } | ||
| } else { | ||
| System.out.println("Друзей должно быть больше 1!Введите количество друзей!"); | ||
| scanner.next(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| while (true) { | ||
| System.out.println( "Введите название товара!"); //название может быть любым в том числе и числовым | ||
| foodName = scanner.next(); | ||
| calculator.AddTovarName(foodName); | ||
|
|
||
| while (true) { | ||
| System.out.println(" Введите цену за " + foodName + ". Стоимость должна быть в формате рубли.копейки, например 10.45 или 11.40.");//цена может быть только числом больше 0 | ||
|
|
||
| if (scanner.hasNextFloat())//это число | ||
| { | ||
| foodPrice = scanner.nextFloat(); | ||
| if (foodPrice > 0) //это число больше нуля | ||
| { | ||
| break; | ||
| } | ||
| else { | ||
| System.out.println("Цена должна быть любым числом больше 0!"); | ||
| } | ||
| } else { | ||
| System.out.println("Цена должна быть любым числом больше 0!"); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| calculator.AddTovarPrice(foodPrice); | ||
| System.out.println("Товар добавлен!"); | ||
| System.out.println( " Введите что угодно если хотите продолжить или \"завершить\" если вы добавили все товары!"); | ||
| stopInput = scanner.next(); | ||
|
|
||
| if (stopInput.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| calculator.totalOutput(numberOfFriends); | ||
| } | ||
| } | ||
| class Calculator //получает на вход название и цену товара, сохраняет названия и общую сумму товаров, выводит в нужном формате | ||
|
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. Классы лучше вынести в отдельные файлы |
||
|
|
||
| { | ||
|
|
||
| private String tovarName = "Добавленные товары:"; | ||
| private float totalPrice = 0f; | ||
|
|
||
| public void AddTovarName(String foodName)//добавить товар | ||
| { | ||
| tovarName = "\n" + tovarName + "\n" + foodName; | ||
| } | ||
|
|
||
| public void AddTovarPrice(float foodPrice)//добавить цену на товар | ||
| { | ||
| totalPrice = totalPrice + foodPrice; | ||
| } | ||
|
|
||
| public void totalOutput(int numberOfFriends)//вывод результата | ||
| { | ||
|
|
||
| System.out.println(tovarName + "\n"); //выводим список всех товаров | ||
|
|
||
| System.out.println("Общая сумма к оплате: " + String.format("%.2f", totalPrice) + rubFormat(Math.floor(totalPrice))); | ||
|
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. На самом деле Math.floor не обязательно, потому что приведение double к int точно также сделает округление |
||
| System.out.println("Каждый друг должен " + String.format("%.2f", (totalPrice / numberOfFriends)) + rubFormat(Math.floor(totalPrice / numberOfFriends))); | ||
|
|
||
| } | ||
|
|
||
| private String rubFormat(double totalSum) { | ||
|
|
||
| String rubleView; | ||
|
|
||
| int totalIntBaseSum = (int) totalSum;//базовое число, не меняется | ||
| int totalIntSum = totalIntBaseSum; | ||
|
|
||
| if (totalIntBaseSum >= 100) { | ||
| totalIntSum = totalIntBaseSum % 100; | ||
| if (totalIntSum > 20) { | ||
| totalIntSum = totalIntSum % 10; | ||
| } | ||
| } else if (totalIntBaseSum > 20) { | ||
| totalIntSum = totalIntBaseSum % 10; | ||
|
|
||
| } | ||
|
|
||
| if (totalIntSum < 1) { | ||
| rubleView = " рублей!"; | ||
| } else if (totalIntSum == 1) { | ||
| rubleView = " рубль!"; | ||
| } else if (totalIntSum < 5) { | ||
| rubleView = " рубля!"; | ||
| } else { | ||
| rubleView = " рублей!"; | ||
| } | ||
|
|
||
| return (rubleView); | ||
|
|
||
| } | ||
| } | ||
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.
Методы по код стайлу нужно называть в camelCase