Repository navigation
Проектная работа №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: dev
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| Проектная работа №1 "Калькулятор счёта" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
|
|
||
| static String nameGood; | ||
| static String normalNames = ""; | ||
| static double price; | ||
| static double sumOfPrices; | ||
| static double sumForOne; | ||
| static short plugForCycle; | ||
|
|
||
| static ArrayList<Good> goods = new ArrayList<>(); | ||
|
|
||
| public static void count() { | ||
|
|
||
| while (true) { | ||
|
|
||
| if (plugForCycle == 0) { | ||
| System.out.println("\nВведи название товара"); | ||
| } else { | ||
| System.out.println("\nДобавим ещё товар? Введи его название или команду 'Завершить'"); | ||
| } | ||
|
|
||
| nameGood = Main.scanner.nextLine(); | ||
|
|
||
| if (nameGood.equalsIgnoreCase("Завершить")) { | ||
|
|
||
| System.out.println("\nДобавление товаров завершено!\n\nДобавленные товары:" + normalNames); | ||
| break; | ||
|
|
||
| } else { | ||
|
|
||
| do { | ||
|
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("\nВведи цену этого товара в формате 'рубли,копейки'"); | ||
| while (!Main.scanner.hasNextDouble()) { | ||
| System.out.println("\nОшибка: некорректное значение! Введи цену товара ещё раз в формате 'рубли,копейки'"); | ||
| Main.scanner.next(); | ||
| } | ||
| price = Main.scanner.nextDouble(); | ||
| Main.scanner.nextLine(); | ||
| if (price <= 0) { | ||
| System.out.println("\nОшибка: цена не может быть нулевой или отрицательной!"); | ||
| } | ||
| } while (price <= 0); | ||
|
|
||
| Good good = new Good(nameGood, price); | ||
|
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. Непонятно, зачем каждый раз создавать экземпляр класса Good, если у Good все поля static |
||
| goods.add(good); | ||
| System.out.println("\nТовар '" + nameGood + "' добавлен"); | ||
| normalNames = normalNames + "\n- " + good.name; | ||
| sumOfPrices = sumOfPrices + price; | ||
| sumForOne = sumOfPrices / Main.numberPersons; | ||
| plugForCycle++; | ||
| } | ||
| } | ||
|
|
||
| System.out.println(String.format("\nКаждый гость должен заплатить: %s %s", Formating.sumForMan, Formating.getNormalRubles())); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import java.util.Iterator; | ||
|
|
||
| public class Formating { | ||
|
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.
|
||
|
|
||
| static String sumForMan = String.format("%.2f", Calculator.sumForOne); | ||
| static int roundedSumForOne = (int) Math.floor(Calculator.sumForOne); | ||
|
|
||
| public static String getNormalRubles() { | ||
|
|
||
| if (roundedSumForOne % 100 > 4 && roundedSumForOne % 100 < 21) { | ||
| return "рублей"; | ||
| } else if (roundedSumForOne % 10 == 1) { | ||
| return "рубль"; | ||
| } else if ((roundedSumForOne % 10) > 1 && (roundedSumForOne % 10) < 5) { | ||
| return "рубля"; | ||
| } | ||
| return "рублей"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| public class Good { | ||
|
|
||
| static String name; | ||
| static double price; | ||
|
|
||
| Good(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,30 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| static Scanner scanner = new Scanner(System.in); | ||
| static int numberPersons; | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| while (true) { | ||
| System.out.println("Введи количество гостей, на которых нужно разделить счёт"); | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| numberPersons = scanner.nextInt(); | ||
| scanner.nextLine(); | ||
| if (numberPersons <= 1) { | ||
| System.out.println("Ошибка! Количество гостей должно быть больше 1."); | ||
| } else { | ||
| Calculator.count(); | ||
| break; | ||
| } | ||
|
|
||
| } else { | ||
| System.out.println("Ошибка! Некорректное значение"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| scanner.close(); | ||
|
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. В Main надо по хорошему только старотовать программу, а остальное выносить в отдельные сущности |
||
| } | ||
| } | ||
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.
тут много где private