-
Notifications
You must be signed in to change notification settings - Fork 0
Практическая работа №1 #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
6d356f8
6b73181
1550570
153e2e6
e73912e
09882f8
81e577d
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| # Уже не пустой репозиторий для работы с Java кодом в Android Studio |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Calculator { | ||
| static float calculate(ArrayList<Good> receipt, int numberOfGuests) { | ||
| float totalPrice = 0; | ||
| for (Good good : receipt) { | ||
| totalPrice += good.price; | ||
| } | ||
| return totalPrice / numberOfGuests; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Formatter { | ||
|
|
||
| static String getFormattedOutput(float price) { | ||
| int resultInt = (int) price; | ||
|
|
||
| String rubles; | ||
| if (resultInt % 100 <= 19 && resultInt % 100 >= 11) { | ||
| rubles = "рублей"; | ||
| } else if (resultInt % 10 >= 2 && resultInt % 10 <= 4) { | ||
| rubles = "рубля"; | ||
| } else if (resultInt % 10 == 1) { | ||
| rubles = "рубль"; | ||
| } else { | ||
| rubles = "рублей"; | ||
| } | ||
| return String.format("С каждого по %.2f %s", price, rubles); | ||
| } | ||
|
|
||
| static String getFormattedGoods(ArrayList<Good> goods) { | ||
| StringBuilder result = new StringBuilder(); | ||
| result.append("Добавленные товары:\n"); | ||
| for (Good good : goods) { | ||
| result.append(good.name).append('\n'); | ||
| } | ||
| return result.toString().trim(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Good { | ||
| float price; | ||
| String name; | ||
|
|
||
| public Good(float price, String name) { | ||
| this.price = price; | ||
| this.name = name; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,108 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| private final static String GREET = "На сколько человек требуется разделить счёт?"; | ||
| private final static String ERROR_INCORRECT_NUMBER = "Введено некорректное число. \nВведите корректное число."; | ||
| private final static String ADD_NEW_GOOD = "Добавление товара. Ведите наименование товара или 'Завершить' для перехода к расчёту чека"; | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| ArrayList<Good> receipt = new ArrayList<>(); | ||
|
|
||
|
|
||
| System.out.println(GREET); | ||
| int number = 1; | ||
| Scanner scanner = new Scanner(System.in); | ||
| //int number = inputNumber(scanner); | ||
|
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 (number <= 1) { | ||
| try { | ||
| number = inputNumber(scanner); | ||
| if (number <= 1) { | ||
| System.out.println(ERROR_INCORRECT_NUMBER); | ||
| } | ||
| } catch (Exception e) { | ||
| System.out.println(ERROR_INCORRECT_NUMBER); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| System.out.println(ADD_NEW_GOOD); | ||
| String name = scanner.nextLine(); | ||
|
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. Здесь есть небольшое повторение. Мы выводим строку перед циклом и в конце цикла. Можно использовать цикл do-while, чтобы этого избежать(Но это совсем не обязательно!). |
||
| while (!name.equalsIgnoreCase("завершить")) { | ||
| System.out.println("Введите цену товара:"); | ||
| float price = -1; | ||
|
|
||
| while (price < 0) { | ||
|
|
||
| try { | ||
| price = inputFloat(scanner); | ||
| } catch (Exception exception) { | ||
| System.out.println(ERROR_INCORRECT_NUMBER); | ||
| } | ||
| } | ||
|
|
||
| Good good = new Good(price, name); | ||
| receipt.add(good); | ||
| System.out.println(ADD_NEW_GOOD); | ||
| name = scanner.nextLine(); | ||
|
|
||
| } | ||
| float result = Calculator.calculate(receipt, number); | ||
| System.out.println(Formatter.getFormattedGoods(receipt)); | ||
| System.out.println(Formatter.getFormattedOutput(result)); | ||
| } | ||
|
|
||
| static boolean checkForInt(String string) { | ||
| boolean isWasMinus = false; | ||
| for (int i = 0; i < string.length(); ++i) { | ||
| if (!Character.isDigit(string.charAt(i))) { | ||
| if (!Character.isDigit(string.charAt(i))) { | ||
|
Comment on lines
+59
to
+60
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 (string.charAt(i) == '-' && !isWasMinus) { | ||
| isWasMinus = true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| static int inputNumber(Scanner scanner) { | ||
|
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. Мне нравится, что логика вынесена в отдельные функции, но кажется, что можно было сделать проще используюя стандартные методы сканера. |
||
| String numberOfGuests = scanner.nextLine(); | ||
| while (!checkForInt(numberOfGuests)) { | ||
| System.out.println(ERROR_INCORRECT_NUMBER); | ||
| numberOfGuests = scanner.nextLine(); | ||
|
|
||
| } | ||
| return Integer.parseInt(numberOfGuests); | ||
| } | ||
|
|
||
| static boolean checkForFloat(String string) { | ||
| boolean isWasDot = false; | ||
| boolean isWasMinus = false; | ||
| for (int i = 0; i < string.length(); ++i) { | ||
| if (!Character.isDigit(string.charAt(i))) { | ||
| if (string.charAt(i) == '-' && !isWasMinus) { | ||
| isWasMinus = true; | ||
| } else if (string.charAt(i) == '.' && !isWasDot) { | ||
| isWasDot = true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| static float inputFloat(Scanner scanner) { | ||
| String numberOfGuests = scanner.nextLine(); | ||
| while (!checkForFloat(numberOfGuests)) { | ||
| System.out.println(ERROR_INCORRECT_NUMBER); | ||
| numberOfGuests = scanner.nextLine(); | ||
|
|
||
| } | ||
| return Float.parseFloat(numberOfGuests); | ||
| } | ||
| } | ||
| } | ||
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.
Здорово, что форматтер вынесен в отдельный класс!