-
Notifications
You must be signed in to change notification settings - Fork 974
Ольга Блохина. Практическая работа #2
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
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,46 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| double sum = 0; | ||
| String listProduct = "Добавленные товары:\n"; | ||
|
|
||
| public void sumProduct() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| Product product = new Product(); | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите название товара"); | ||
|
|
||
| product.name = scanner.next(); | ||
| listProduct = listProduct + product.name + "\n"; | ||
| boolean isRightPrice = false; | ||
| while (!isRightPrice) { | ||
| System.out.println("Введите цену товара в формате рубли.копейки"); | ||
|
|
||
| if (scanner.hasNextDouble()) { | ||
| product.price = scanner.nextDouble(); | ||
|
|
||
| if (product.price < 0) { | ||
| System.out.println("Цена не может быть отрицательной"); | ||
| } else { | ||
| isRightPrice = true; | ||
| } | ||
| } else { | ||
| System.out.println("Вы ввели не число!"); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| sum = sum + product.price; | ||
|
|
||
| System.out.println("Товар добавлен"); | ||
| System.out.println("Хотите ли вы добавить еще товар?"); | ||
| System.out.println("Для завершения добавления товаров напишите Завершить"); | ||
|
|
||
| String answer = scanner.next(); | ||
|
|
||
| if (answer.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,38 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| int persons = 5; | ||
| boolean isNumber = false; | ||
|
|
||
| while (!isNumber) { | ||
| System.out.println("На скольких человек необходимо разделить счёт?");// ваш код начнется здесь | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| persons = scanner.nextInt(); | ||
| if (persons == 1) { | ||
| System.out.println("Нет смысла ничего считать и делить"); | ||
| } else if (persons < 1) { | ||
| System.out.println("Это некорректное значение для подсчёта"); | ||
| } else { | ||
| isNumber = true; | ||
| } | ||
| } else { | ||
| System.out.println("Это неправильное число!"); | ||
| scanner.next(); | ||
| } | ||
| } | ||
|
|
||
| Calculator calculator = new Calculator(); | ||
| calculator.sumProduct(); | ||
| System.out.println(calculator.listProduct); | ||
|
|
||
| double total = calculator.sum / persons; | ||
|
|
||
| Ruble ruble = new Ruble(); | ||
| ruble.writeRuble(total); | ||
| System.out.println(ruble.amountOfRubles); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| public class Product { | ||
| String name; | ||
| double price; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| public class Ruble { | ||
| String amountOfRubles; | ||
|
|
||
| public void writeRuble(double floatAmount) { | ||
| int amount = (int) Math.floor(floatAmount); | ||
| int a = amount % 100; | ||
| int b = a % 10; | ||
|
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. Лучше дать названия более понятные, чтобы другим программистам с первого взгляда было ясно, что они значат |
||
|
|
||
| if (a >= 11 & a <= 19) { | ||
| amountOfRubles = String.format("%.2f", floatAmount) + " рублей"; | ||
| } else if (b == 1) { | ||
| amountOfRubles = String.format("%.2f", floatAmount) + " рубль"; | ||
| } else if (b == 2 | b == 3 | b == 4) { | ||
|
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. Можно писать |
||
| amountOfRubles = String.format("%.2f", floatAmount) + " рубля"; | ||
| } else { | ||
| amountOfRubles = String.format("%.2f", floatAmount) + " рублей"; | ||
|
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. Код |
||
| } | ||
| } | ||
|
|
||
| } | ||
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.
Можно обойтись без переменной, тут писать while (true), а в месте, где нужен выход из цикла - писать break