Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

первый коммит #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
harmgehog wants to merge 2 commits into main
base: main
Choose a base branch
Loading
from dev
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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]);
}
}

AltStyle によって変換されたページ (->オリジナル) /