-
Notifications
You must be signed in to change notification settings - Fork 0
My first job=) #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
base: main
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,21 @@ | ||
| public class Format { | ||
| static String formatRub(double sum) { | ||
| int a = (int) sum; | ||
| if (a > 100) { | ||
| a %= 100; | ||
| } | ||
| if (a > 20) { | ||
| a %= 10; | ||
| } | ||
| switch (a) { | ||
| case 1: | ||
| return " рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return " рубля"; | ||
| default: | ||
| return " рублей"; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,65 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| double sum = 0; | ||
| int quantity = 0; | ||
|
|
||
| while (true) { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
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. Не очень хорошо, что объект сканера создается каждый раз при итерации запроса, лучше создать один( как было до этого, и его использовать
Owner
Author
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("На скольких человек необходимо разделить счёт?"); | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| quantity = scanner.nextInt(); | ||
| if (quantity < 2) { | ||
| System.out.println("Неверное значение"); | ||
| } else { | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Неверное значение"); | ||
| } | ||
| } | ||
|
|
||
| ArrayList<Product> productsList = new ArrayList<>(); | ||
| Scanner sc = new Scanner(System.in); | ||
| while (true) { | ||
| System.out.println("Введите название товара"); | ||
| String name = sc.nextLine(); | ||
| if (name.trim().isEmpty()) { | ||
| System.out.println("Что-нибудь еще, кроме пустоты?"); | ||
| } else { | ||
| if (name.equalsIgnoreCase("Завершить")) { | ||
|
|
||
| for (int i = 0; i < productsList.size(); i++) { | ||
| Product element = productsList.get(i); | ||
| System.out.println(element.name + " " + element.price); | ||
| sum += element.price; | ||
| } | ||
| String rub = Format.formatRub(sum / quantity); | ||
| System.out.printf("Итого с человека: %.2f" + rub, sum / quantity); | ||
| break; | ||
|
|
||
| } else { | ||
| while (true) { | ||
| Scanner sc1 = new Scanner(System.in); | ||
| System.out.println("Введите цену"); | ||
| if (sc1.hasNextDouble()) { | ||
| double price = sc1.nextDouble(); | ||
| Product newProduct = new Product(name, price); | ||
| productsList.add(newProduct); | ||
| System.out.println("Добавленные товары: "); | ||
| for (Product k : productsList) { | ||
| System.out.println(k.name); | ||
| } | ||
| break; | ||
| } else { | ||
| System.out.println("Неверное значение цены"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Product { | ||
| String name; | ||
| double price; | ||
|
|
||
| Product(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } |
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.