Skip to content
Merged
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
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 "рублей";
}
}
}
}
142 changes: 48 additions & 94 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,130 +1,84 @@
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;

int quantityFinal = 0;
boolean result = false;
while (!result) {
System.out.println("На скольких человек необходимо разделить счёт?");
int quantity = scanner.nextInt();
if (quantity == 1 || quantity < 1) {
result = false;
System.out.println("Неверное количество человек");

} else result = true;
count = quantity;

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");
}
if (count > 1) {
Calc calc = new Calc();

while (true) {
System.out.println("Введите название товара");
String name = scanner.next();
String checkPrice = null;
boolean status = true;

while (status) {
System.out.println("Введите стоимость товара");
checkPrice = scanner.next();
char[] charArray = checkPrice.toCharArray();
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("Неверное значение стоимости товара, повторите ввод.");
} else {
for (int i = 1; i < charArray.length; i++) {
if (!Character.isDigit(charArray[i]) && charArray[i] != ',' && charArray[i] != '.') {
System.out.println("Неверное значение стоимости товара, повторите ввод.");
} else {
status = false;
}
}
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("Добавить еще один товар?");
String answer = scanner.next();
System.out.println("Добавить еще один товар?\n");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("Завершить")) {
calc.getProductList();
calc.getTax();
calc.getTax(quantityFinal);
break;
} else if (!answer.equalsIgnoreCase("Завершить")) {

}

}
}

}


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

private void addVisitors(int a) {
this.visitors = this.visitors + a;
}

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

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

private void getTax() {
double finalSum = 0;
for (Product product : products) {
finalSum = finalSum + product.price;
}
finalSum = finalSum / products.size();

String messageTemplate = "Каждый должен заплатить по: %.2f %s";
System.out.println(String.format(Locale.ROOT, messageTemplate, finalSum, getWord(finalSum)));
}

private String getWord(double pay) {
double strNumber = Math.floor(pay);
if (strNumber == 1) {
return "рубль";
} else if (strNumber > 1 && strNumber < 5) {
return "рубля";
} else if (strNumber > 4) {
return "рублей";
}
return null;
}
}


static class Product {
String name;
double price;

public Product(String name, double price) {
this.name = name;
this.price = price;
}
}
}
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;
}
}