Repository navigation
Dev21 02 #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
al2407111
wants to merge
3
commits into
main
Choose a base branch
from
dev21_02
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
Dev21 02 #1
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
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,6 +1,154 @@ | ||
|
|
||
| import java.util.Map; | ||
| import java.util.HashMap; | ||
| import java.util.Scanner; | ||
| import java.util.Iterator; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| Calculate calculate1 = new Calculate(); | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| double people=0.0; | ||
| while (true) { | ||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
|
|
||
| String peopleString = scanner.next(); | ||
| if (calculate1.isNumeric(peopleString)){//проверка на коректный ввод | ||
| people= Double.parseDouble(peopleString); | ||
| } | ||
| if (people % 1!=0){ | ||
| people=0.0; | ||
| } | ||
|
|
||
|
|
||
| if (people < 1) { | ||
| System.out.println("Это некорректное значение для подсчёта"); | ||
|
|
||
| } else if (people == 1) { | ||
| System.out.println("Нет смысла ничего считать и делить"); | ||
|
|
||
| } else if (people > 1) { | ||
|
|
||
| calculate1.calculateProductСost(people); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Calculate { | ||
| Double sumPrice = 0.0; | ||
| Scanner scanner = new Scanner(System.in); | ||
| int i = 0; | ||
| HashMap<Integer, String> inputProduct = new HashMap<>(); | ||
| HashMap<Integer, Double> inputPrice = new HashMap<>(); | ||
|
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. Что бы не использовать 2 мапы можно создать класс в котором будет два поля - цена и название товара и хранить их в листе, это будет гораздо удобнее. |
||
|
|
||
| public void calculateProductСost(double people) { | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите название товара:"); | ||
| i++; | ||
|
|
||
| String name = scanner.next(); | ||
| if ("Завершить".equalsIgnoreCase(name)) { | ||
| break; | ||
| } | ||
| inputProduct.put(i, name); | ||
|
|
||
| Double inpСost = 0.0; | ||
| while (true) { | ||
| System.out.println("Введите стоимость:"); | ||
| String inpСost1 = scanner.next(); | ||
| if (isNumeric(inpСost1)) { | ||
| inpСost = Double.parseDouble(inpСost1); | ||
|
|
||
| if ((inpСost >= 0)&&(((inpСost*100) % 1)==0)) { | ||
| break; | ||
| } | ||
|
|
||
| } | ||
| System.out.println("Стоимость должна быть в формате рубли.копейки, например 10.45 или 11.40!!"); | ||
| } | ||
|
|
||
|
|
||
| inputPrice.put(i, inpСost); | ||
|
|
||
|
|
||
| System.out.println("Товар " + inputProduct.get(i) + " стоимостью " + inputPrice.get(i) + " " +formatAmount(inpСost)+" добавлен успешно."); | ||
|
|
||
|
|
||
|
|
||
| sumPrice = sumPrice + inputPrice.get(i); | ||
|
|
||
|
|
||
| System.out.println("Общая сумма " + (Math.floor(sumPrice * 100.0) / 100.0) +" " +formatAmount(sumPrice)+ "\nДобавить ещё один товар?"); | ||
|
|
||
| String name1 = scanner.next(); | ||
| if ("Завершить".equalsIgnoreCase(name1)) { | ||
|
|
||
| break; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| System.out.println("Список товаров"); | ||
| Collection<String> values = inputProduct.values(); | ||
| for (String value : values) { | ||
| System.out.println(value); | ||
| } | ||
| // String roundSumPrice = String.format("%.02f", (sumPrice) / people); | ||
| System.out.println("Средний чек " +(Math.floor((sumPrice / people)*100)/100) +" " +formatAmount((sumPrice) / people)); | ||
| } | ||
| public static boolean isNumeric(String str) { | ||
| try { | ||
| Double.parseDouble(str); | ||
| return true; | ||
| } catch(NumberFormatException e){ | ||
| return false; | ||
| } | ||
| } | ||
| public static String formatAmount(double formatPrise) {//Сумма считается в рублях. Ваша задача — обработать правильный вывод. Если сумма 1.45, то вы должны вывести "1.45 рубль", а если сумма будет 3.20 или 4.00, вы должны вывести 3.20 рубля или 4.00 рубля. | ||
| int d1=100; | ||
| if(formatPrise<100){ | ||
| d1=10; | ||
| } | ||
|
|
||
| double number = formatPrise; | ||
| double roundedNumberPrice = number /d1; | ||
| double integralPart = Math.round(((formatPrise- (formatPrise - (roundedNumberPrice % 1)))*d1));//на что заканчивается последние 2 цифры | ||
| String currency = ""; | ||
|
|
||
|
|
||
| if (integralPart>=15) { | ||
| double roundedIng=integralPart/10; | ||
| integralPart =Math.round(integralPart-(integralPart-(roundedIng% 1)*10)); | ||
| } | ||
| if (formatPrise<15) { | ||
|
|
||
| integralPart =Math.floor(formatPrise); | ||
| } | ||
|
|
||
| if ((integralPart==0)|| ((integralPart>=5) && (integralPart<=14))) | ||
| {currency = "рублей";} | ||
| else if ((integralPart>1) && (integralPart<=4)) | ||
| {currency = "рубля";} | ||
| else if ((integralPart==1)) | ||
| {currency = "рубль";} | ||
|
|
||
| return (currency); | ||
| } | ||
| } | ||
| } | ||
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.
количество людей - целое число.