Skip to content
Open
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Пустой репозиторий для работы с Java кодом в Android Studio


113 changes: 113 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {

private ArrayList<Products> productList = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);

private double sum;
private int peopleCount;
private String template;

public void calcStart() {
while (!scanner.hasNextInt()) {
System.out.println("Введите, пожалуйста число");
scanner.next();
}
peopleCount = scanner.nextInt();
while (true) {
if (peopleCount == 1) {
System.out.println("Все оплатить придется самому");
break;
}
if (peopleCount > 1) {
prodCount();
break;
}
else {
System.out.println("Вы ввели неправильное значение. Попробуйте снова!");
while (!scanner.hasNextInt()) {
System.out.println("Введите, пожалуйста число");
scanner.next();
}
peopleCount = scanner.nextInt();
}
}
}

public void prodCount() {
while (true) {

System.out.println("Уточните название продукта: ");
String name = scanner.next();

System.out.println("Уточните стоимость продукта: ");
while (!scanner.hasNextDouble()) {
System.out.println("Введите, пожалуйста, число формате 1,45");
scanner.next();
}
double price = scanner.nextDouble();
while (price < 0) {
System.out.println("Введите положительное число!");
while (!scanner.hasNextDouble()) {
System.out.println("Введите, пожалуйста, число формате 1,45");
scanner.next();
}
price = scanner.nextDouble();
}
Products productNew = new Products(name, price);
productList.add(productNew);
sum += productNew.price;

template = "Продукт %s стоимостью %.2f %s добавлен в список." +
"\nОбщая сумма составляет: %.2f %s";
System.out.println(String.format(template, productNew.name, productNew.price,
formatter(productNew.price), sum, formatter(sum)));

System.out.println("\nЧтобы ввести новый товар введите любой символ.\n" +
"Чтобы закончить, введите 'Завершить'");

if (scanner.next().equalsIgnoreCase("Завершить")) {
finalSum();
break;
}
}
}

public void finalSum(){

System.out.println("\nДобавленные товары: ");
for (Products product : productList) {
template = "%s %.2f %s";
System.out.println(String.format(template, product.name,
product.price, formatter(product.price)));
}
template = "Итоговая сумма: %.2f %s";
System.out.println(String.format(template, sum, formatter(sum)));
Comment on lines +86 to +87
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template лучше не писать, а сразу всё в одном вызове писать, иначе потом запутаешься с этой переменной сам


int name = peopleCount;
double finalSum = sum / peopleCount;
template = "Каждый должен оплатить %.2f %s";
System.out.println(String.format(template, finalSum, formatter(finalSum)));
}

public String formatter (double number) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В этой функции дополнительно нужно учесть, что для чисел, которые оканчиваются на 11-19 включительно, необходимо выводить слово "рублей"

if ((number % 100) > 10 && (number % 100) < 20)
return "рублей";

else if ((number % 10) >= 5 && (number % 10) < 10)
return "рублей";

else if ((number % 10) >= 1 && (number % 10) < 2)
return "рубль";

else if ((number % 10) > 1 && (number % 10) < 5)
return "рубля";

else if ((number % 10) >= 0 && (number % 10) < 1)
return "рублей";

return null;
}
}
8 changes: 6 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");

Calculator newCalc = new Calculator();
System.out.println("Добрый день! На сколько человек будем делить счет?");

newCalc.calcStart();
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/Products.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Products {
final String name;
final double price;

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