Skip to content
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
# Калькулятор счёта» — консольное приложение, которое будет задавать вопросы в консоли, считывать пользовательский ввод и в результате показывать список товаров и сумму для каждого из участников.
52 changes: 52 additions & 0 deletions src/main/java/Calc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.ArrayList;
import java.util.Locale;

public class Calc {
double sum = 0;
int visitors = 0;
ArrayList<Product> products = new ArrayList<>();

public void addProduct(Product product) {
products.add(product);
this.sum = product.price;
System.out.println("Товар успешно добавлен.\n");
}

public void getProductList() {
System.out.println("Добавлены товары: ");
for (Product product : products) {
System.out.println(product.name);
}
}

public void getTax(int quantityFinal) {
double count = 0;
double finalSum = 0;
for (Product product : products) {
count = count + product.price;
}
finalSum = count / quantityFinal;
String messageTemplate = "Сумма для каждого: %.2f %s";
System.out.printf(Locale.ROOT, (messageTemplate) + "%n", finalSum, getWord(finalSum));
}

private String getWord(double money) {
Double strNumber = Math.floor(money);
Integer integerNumber = strNumber.intValue();
int x = integerNumber % 10;
if (integerNumber % 100 >= 11 && integerNumber % 100 <= 14) {
return "рублей";
} else {
switch (x) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
}
}
80 changes: 79 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,84 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
int quantityFinal = 0;
boolean result = false;
while (!result) {
System.out.println("На скольких человек необходимо разделить счёт?\n");
String quantity = scanner.nextLine();
quantity = quantity.trim();
boolean isInteger = quantity.matches("-?\\d+");
if (isInteger) {
quantityFinal = Integer.parseInt(quantity);
if (quantityFinal == 1 || quantityFinal < 1) {
System.out.println("Неверное количество человек.\n");
} else {
result = true;
}
} else System.out.println("Неверное количество человек, повторите ввод.\n");
}

Calc calc = new Calc();

while (true) {
String name = null;
boolean chekName = false;
while (!chekName) {
System.out.println("Введите название товара.\n");
name = scanner.nextLine();
name = name.trim();
if (name.trim().isEmpty() || name.isBlank()) {
System.out.println("Неверное наименование товара, повторите ввод.\n");
} else chekName = true;
}
String checkPrice = null;
boolean status = true;
boolean onlyDigits = false;
while (status) {
System.out.println("Введите стоимость товара.\n");
checkPrice = scanner.nextLine();
checkPrice = checkPrice.trim();
onlyDigits = checkPrice.matches("^[0-9]*[.,]?[0-9]+$");
char[] charArray = checkPrice.toCharArray();
if (!checkPrice.isEmpty()) {
if (charArray[0] == ',' || charArray[0] == '.') {
System.out.println("Неверное значение стоимости товара, повторите ввод.\n");
} else if (onlyDigits) {
for (int a = 0; a < charArray.length; a++) {
if (charArray[a] == ',') {
charArray[a] = '.';
checkPrice = new String(charArray);
}
}
status = false;
} else {
System.out.println("Неверное значение стоимости товара, повторите ввод.\n");
}
} else {
System.out.println("Неверное значение стоимости товара, повторите ввод.\n");
}

}
if (onlyDigits) {
double price = Double.parseDouble(checkPrice);
Product product = new Product(name, price);
calc.addProduct(product);
System.out.println("Добавить еще один товар?\n");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("Завершить")) {
calc.getProductList();
calc.getTax(quantityFinal);
break;
} else if (!answer.equalsIgnoreCase("Завершить")) {

}
}
}


}


}
9 changes: 9 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Product {
String name;
double price;

public Product(String name, double price) {
this.name = name;
this.price = price;
}
}