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
# Репозиторий Татьяны Аникиной
65 changes: 65 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;

public class Calculator {
ArrayList<Product> productsList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Перед коммитом стоит приводить файлы к правильному стилю, так как код будут смотреть другие разработчики. В IDEA это легко делать путём вызова горячей комбинации клавиш (она зависит от ОС), её можно посмотреть в меню Code → Reformat Code


public void input() {
System.out.println("После ввода всех товаров введите команду Завершить");
String name;
double price;
System.out.println("Введите название товара");
while (true) {
name = scanner.nextLine().trim();
if (name.equalsIgnoreCase("Завершить")) {
productPrint();
break;
} else {
System.out.println("Введите его стоимость через точку в формате рубли.копейки (00.00)");
scanner.useLocale(Locale.ENGLISH);
while (true) {
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price >= 0) {
productsList.add(new Product(name, price));
System.out.printf(String.format("Товар %s успешно добавлен, цена %.2f\n", name, price));
System.out.println("Добавьте еще товар. Или введите команду Завершить");
name = scanner.nextLine();
break;
} else {
System.out.println("Вы ввели отрицательную стоимость. Введите в формате рубли.копейки (00.00)");
name = scanner.nextLine(); // Очищаем неправильный ввод
}
} else {
System.out.println("Вы неверно ввели стоимость. Введите в формате рубли.копейки (00.00)");
scanner.nextLine(); // Очищаем неправильный ввод
}
}
}
}
}

public void productPrint() {
System.out.println("Добавленные товары:");
for (Product product : productsList) {
System.out.printf("%s %.2f \n", product.getName(), product.getPrice());
}
}
public void finalCalculation(int people, Formatter formatter) {
System.out.printf("Общий счет: %.2f %s\n", totalSum(), formatter.formatter(totalSum()));
System.out.println("Выводим окончательный расчет");
Double finalCalc = totalSum() / people;
System.out.printf(String.format("Для каждого из " + people + " человек сумма к оплате %.2f %s", finalCalc, formatter.formatter(finalCalc)));
}
public double totalSum() {
Double sum = 0.0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно здесь и примитивный тип использовать

for (Product product : productsList) {
sum += product.getPrice();
}
return sum;
}
}


16 changes: 16 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Formatter {
public String formatter(double sumForOne) {


if (sumForOne % 100 >= 11 && sumForOne % 100 <= 19) {
return "рублей";
} else if (sumForOne % 10 == 1) {
return "рубль";
} else if (sumForOne % 10 >= 2 && sumForOne % 10 <= 4) {
return "рубля";
} else {
return "рублей";
}

}
}
38 changes: 36 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
import java.util.Locale;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println("Добро пожаловать в калькулятор счета");
Calculator calculator = new Calculator();
Formatter formatter = new Formatter();
int people = Counter.getQuantity();
calculator.input();
calculator.finalCalculation(people, formatter);
}
}

static class Counter {

public static int getQuantity() {
Scanner scanner = new Scanner(System.in);
System.out.println("На сколько человек необходимо разделить счет?");
int quantity;

while (true) {
if (scanner.hasNextInt()) {
quantity = scanner.nextInt();
if (quantity < 1) {
System.out.println("Введите корректное значение больше одного");
} else if (quantity == 1) {
System.out.println("На одного человека нельзя разделить счет");
} else {
System.out.println("Счет разделим на " + quantity + "-х человек");
break;
}
} else {
System.out.println("Нужно ввести количество человек");
scanner.next(); // Очищаем неправильный ввод
}
}
return quantity;
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Product {
private final String name;
private final double price;

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