-
Notifications
You must be signed in to change notification settings - Fork 222
Проектная работа №1 #82
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
|
|
||
| String receipt = ""; | ||
| double summa = 0; | ||
|
|
||
| public void addGood(String title, double price) { | ||
| Good tovar = new Good(title, price); | ||
|
|
||
| receipt = receipt.concat("\n").concat(tovar.title); | ||
| summa = summa + tovar.amount; | ||
| } | ||
|
|
||
| public String getReceipt() { | ||
| return receipt; | ||
| } | ||
|
|
||
| public double getSumma() { | ||
| return summa; | ||
| } | ||
|
|
||
| public String getRubleAddition(int num) | ||
| { | ||
| int preLastDigit = num % 100 / 10; | ||
| if (preLastDigit == 1) | ||
| { | ||
| return "рублей"; | ||
| } | ||
|
|
||
| switch (num % 10) | ||
| { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| return "рублей"; | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| public class Good { | ||
|
|
||
| // поля | ||
| String title; | ||
| double amount; | ||
|
|
||
| Good (String name, double price) { | ||
| title = name; | ||
| amount = price; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,79 @@ | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
| // шаг 1: запрашиваем количество участников | ||
| int numberOfFriends; | ||
|
|
||
| while (true) { | ||
| System.out.println("На сколько человек вы хотели бы разделить счет?"); | ||
| // проверяем наличие целого числа в вводе | ||
| if (!scanner.hasNextInt()) { // если нет целое число, то просим ввести еще раз | ||
| System.out.println("Введите целое число: пожалуйста."); | ||
| scanner.next(); | ||
| continue; | ||
| } | ||
| else { | ||
| numberOfFriends = scanner.nextInt(); | ||
| } | ||
|
|
||
| // проверяем количество человек | ||
| if (numberOfFriends > 1) { | ||
| break; | ||
| } else { | ||
| System.out.println("Вас слишком мало, чтобы делить счет :)"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // шаг 2. добавление товаров и подсчет товаров | ||
| Calculator calculator = new Calculator(); | ||
| while (true) { | ||
| System.out.println("Введите название товара"); | ||
| scanner.nextLine(); | ||
| String name = scanner.nextLine(); | ||
|
|
||
| System.out.println("Введите цену"); | ||
| double price; | ||
|
|
||
| // проверяем, что цена введена корректно | ||
| if (!scanner.hasNextDouble()) { | ||
| System.out.println("Ошибка в цене товара. Попробуйте снова."); | ||
| continue; | ||
| } | ||
| else { | ||
| price = scanner.nextDouble(); | ||
| } | ||
|
|
||
| if (price > 0) { | ||
| calculator.addGood(name, price); // добавляем товар | ||
| System.out.println("Товар успешно добавлен. Если хотите завершить ввод, напишите - завершить."); | ||
| String exit = scanner.next(); | ||
| if (exit.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Вы ввели отрицательную цену. Попробуйте еще раз ввести товар."); | ||
| } | ||
| } | ||
|
|
||
| // шаг 3. вывод результата | ||
| System.out.println("Добавленные товары:"); | ||
| System.out.println(calculator.getReceipt()); | ||
| System.out.println("Сумма к оплате: " + String.format("%.2f",calculator.getSumma())); | ||
|
|
||
| // определяем окончание слова рубль | ||
| double finalAmount = calculator.getSumma() / numberOfFriends; | ||
| String formattedDouble = String.format("%.2f", finalAmount); | ||
| String rubles = calculator.getRubleAddition((int) Math.floor(finalAmount)); | ||
|
|
||
| System.out.println("Каждый должен заплатить = " + formattedDouble + " " + rubles); | ||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
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.
Каждый шаг можно выделить в отдельную функцию, так код можно будет переиспользовать