Repository navigation
Первое практическое задание #74
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| public class Goods { | ||
| String name; | ||
| double price; | ||
| Goods (String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,80 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| int count=0; | ||
| Goods purchases = new Goods ("",0.0); | ||
| System.out.println ("For how many people do you need to split the bill?"); | ||
| count = countOfPeople(); | ||
| System.out.println (count); | ||
| Scanner scanner = new Scanner(System.in); | ||
| boolean trigger = true; | ||
|
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. Это инициализированное значение не используется (в while будет проверяться то, что будет получено от |
||
|
|
||
| do { | ||
| Goods product = new Goods("", 0.0); | ||
| System.out.println("Enter good`s name"); | ||
| product.name = scanner.nextLine(); | ||
| System.out.println("Enter good`s price in format ruble.penny. For example: 0.00"); | ||
| while (true) { | ||
| if (scanner.hasNextDouble()) { | ||
| product.price = scanner.nextDouble(); | ||
| if (product.price > 0) { | ||
| break; | ||
| } else { | ||
| System.out.println("Wrong format, pleas try again"); | ||
| scanner.nextLine(); | ||
| } | ||
| } else { | ||
| System.out.println("Wrong format, pleas try again"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| purchases = Summoning.Calculate(product, purchases); | ||
| System.out.println("Do you want to add next product? If no, write \"complete\", else any phrase."); | ||
| scanner.nextLine(); | ||
| String chek = scanner.nextLine(); | ||
| trigger = !chek.equalsIgnoreCase("complete"); | ||
|
|
||
| } while (trigger); | ||
|
|
||
| System.out.println("Purchased products:\n"+purchases.name); | ||
| double bill = purchases.price/count; | ||
| String finalPhrase1 = "%.2f рубль"; | ||
| String finalPhrase2 = "%.2f рубля"; | ||
| String finalPhrase3 = "%.2f рублей"; | ||
| int ostatok = (int) (Math.floor(bill)%10.0); | ||
|
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. Хорошо, что используешь переменные для подсчета только 1 раз |
||
| if ((bill <=19 && bill>=11) || (ostatok>=5 && ostatok<=9)|| ostatok==0){ | ||
| System.out.println(String.format(finalPhrase3, bill)); | ||
| } else{ | ||
| if (ostatok == 1) { | ||
| System.out.println(String.format(finalPhrase1, bill)); | ||
| } else{ | ||
| System.out.println(String.format(finalPhrase2, bill)); | ||
|
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. Код System.out.println(String.format(finalPhrase2, bill)); повторяется несколько раз, лучше его вынести за if-else, а в нем определять только в переменной, каким будет слово рубль |
||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| public static int countOfPeople (){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| while (true){ | ||
| if (scanner.hasNextInt()) { | ||
| int x = scanner.nextInt(); | ||
| if (x > 1) { | ||
| return x; | ||
| } else { | ||
| System.out.println("Invalid quantity entered, please try again."); | ||
| } | ||
| } else{ | ||
| System.out.println("Invalid quantity entered, please try again."); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Summoning { | ||
| public static Goods Calculate (Goods fullList, Goods product) { | ||
| Goods list = fullList; | ||
| list.name = list.name +'\n' + product.name; | ||
| list.price = list.price+ product.price; | ||
| return list; | ||
|
|
||
| } | ||
| } |
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.
По умолчанию int инициализируется 0, так что здесь инициализировать не обязательно