forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
первый коммит #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
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| Первая java программа из андроид студии для практикума. | ||
| добавлен второй коммит и второй пулреквест |
57 changes: 57 additions & 0 deletions
src/main/java/BillCalc.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class BillCalc { | ||
| private final List<String> items; | ||
| private BigDecimal totalCost; | ||
| private BigDecimal totalCostPerPerson; | ||
| private final BigDecimal persons; | ||
|
|
||
| public BillCalc(int persons) { | ||
| this.items = new ArrayList<>(); | ||
| this.totalCost = BigDecimal.valueOf(0.00); | ||
| this.totalCostPerPerson = BigDecimal.valueOf(0.00); | ||
| this.persons = BigDecimal.valueOf(persons); | ||
| } | ||
|
|
||
| public void addItem(String itemName, String price) { | ||
| BigDecimal itemPrice = new BigDecimal(price.trim()).setScale(2, RoundingMode.HALF_UP); | ||
| String item = itemName + ", " + itemPrice; | ||
| items.add(item); | ||
| totalCost = totalCost.add(itemPrice); | ||
| totalCostPerPerson = totalCost.divide(persons, RoundingMode.HALF_UP); | ||
| } | ||
|
|
||
| public void printTotal() { | ||
| System.out.println("Количество посетителей: " + persons.setScale(0, RoundingMode.DOWN)); | ||
| System.out.println("Общая сумма счета: " + totalEnding(totalCost)); | ||
| System.out.println("Каждый посетитель должен заплатить: " + totalEnding(totalCostPerPerson)); | ||
| System.out.println("\nВсе позиции меню: "); | ||
| for (int i = 0; i < items.size(); i++) { | ||
| String name = items.get(i).split(",")[0]; | ||
| BigDecimal price = new BigDecimal(items.get(i).split(", ")[1]); | ||
| System.out.println((i+1) + ". " + name + " - " + totalEnding(price)); | ||
| } | ||
| } | ||
|
|
||
| private String totalEnding(BigDecimal val) { | ||
| double rub = val.setScale(0, RoundingMode.DOWN).doubleValue(); | ||
| double kop = val.multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.DOWN).doubleValue() % 100; | ||
|
|
||
| String endRub; | ||
| if (rub % 100 > 10 && rub % 100 < 15) endRub = "рублей"; | ||
| else if (rub % 10 > 1 && rub % 10 < 5) endRub = "рубля"; | ||
| else if (rub % 10 == 1) endRub = "рубль"; | ||
| else endRub = "рублей"; | ||
|
|
||
| String endKop; | ||
| if (kop > 10 && kop < 15) endKop = "копеек"; | ||
| else if (kop % 10 > 1 && kop % 10 < 5) endKop = "копейки"; | ||
| else if (kop % 10 == 1) endKop = "копейка"; | ||
| else endKop = "копеек"; | ||
|
|
||
| return String.format("%.0f %s %.0f %s", rub, endRub, kop, endKop); | ||
| } | ||
| } |
80 changes: 79 additions & 1 deletion
src/main/java/Main.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,84 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static int customers = 0; | ||
| public static String[] strings = { | ||
| "Привет. Это калькулятор счета.", | ||
| "Введите КОЛ-ВО человек (или 'завершить' для выхода):", | ||
| "Неверно: Должно быть целое число. 1 < ЧИСЛО < 1000", | ||
| "Товар успешно добавлен в счет", | ||
| "Введите НАЗВАНИЕ товара (или 'завершить' для выхода): ", | ||
| "Неверно: Название должно содержать текст (буквы, цифры, некоторые знаки препинания, кроме запятой)", | ||
| "Введите СТОИМОСТЬ товара (или 'завершить' для выхода): ", | ||
| "Неверно: 0.00 <= ЦЕНА < 150000000.00,\n Цена, это число не более 2х знаков после запятой."}; | ||
|
|
||
| static BillCalc calculator; | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| printString(0); | ||
|
|
||
| if (customersCount()) return; | ||
| calculator = new BillCalc(customers); | ||
| addItems(); | ||
| calculator.printTotal(); | ||
| } | ||
|
|
||
| public static boolean customersCount() { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| while (true) { | ||
| printString(1); | ||
| String input = scanner.nextLine(); | ||
| if (input.trim().equalsIgnoreCase("завершить")) return true; | ||
| try { | ||
| customers = Integer.parseInt(input); | ||
| if (customers > 1 && customers < 1000) break; | ||
| } catch (NumberFormatException ignored) {} | ||
| printString(2); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static void addItems() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| boolean flag = true; | ||
| while (flag) { | ||
| printString(4); | ||
| String itemName = scanner.nextLine(); | ||
| if (itemName.trim().equalsIgnoreCase("завершить")) flag = false; | ||
| else if (!isText(itemName)) { | ||
| printString(5); | ||
| continue; | ||
| } | ||
| while(flag) { | ||
| printString(6); | ||
| String itemPrice = scanner.nextLine(); | ||
| if (itemPrice.trim().equalsIgnoreCase("завершить")) flag = false; | ||
| else if (isPrice(itemPrice)) { | ||
| calculator.addItem(itemName, itemPrice); | ||
| printString(3); | ||
| break; | ||
| } | ||
| else printString(7); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean isText(String value) { | ||
| return value.matches("[a-zA-Zа-яА-Я\\d\\p{Punct} &&[^,]]+"); | ||
| } | ||
|
|
||
| private static boolean isPrice(String value) { | ||
| try { | ||
| double number = Double.parseDouble(value); | ||
| int decimalPlaces = value.indexOf('.') >= 0 ? value.length() - value.indexOf('.') - 1 : 0; | ||
| return number >= 0 && number < 150000000.00 && decimalPlaces <= 2; | ||
| } catch (NumberFormatException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static void printString(int arg) { | ||
| System.out.println(strings[arg]); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.