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
Aeeiii wants to merge 1 commit into main
base: main
Choose a base branch
Loading
from dev
Open
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
134 changes: 133 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,138 @@
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
SplitBill splitBill = new SplitBill(); // создаем и запускам дележ счета
splitBill.start();
}
}

class SplitBill { // основной класс
Scanner scanner = new Scanner(System.in);
Copy link

@i-masloed i-masloed Feb 11, 2024

Choose a reason for hiding this comment

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

  • хорошей практикой является делать вызов scanner.close() после того, как сканнер больше не используется. Это необходимо для того, что бы этот объект не потреблял ресурсы, тогда когда это уже не требуется.

ArrayList<Product> productList = new ArrayList<>();// создаем список под все заказы

public void start(){
double finalTotalCost = 0; // общая сумма

int numberOfPayers = getNumberOfPayers(); // спрашиваем количество гостей, возвращает количество гостей, если ввели завершить - возвращает 0
if (numberOfPayers != 0) {
finalTotalCost = calculate(); // запускаем подсчет, возвращает общую сумму всех товаров
}
output(numberOfPayers, finalTotalCost); // запускаем вывод
}

private int getNumberOfPayers() { // узнаем количество людей, на которых делим счет, если вводят 'Завершить' - возвращает 0
System.out.println("На скольких человек необходимо разделить счёт?");
while (true){

if (scanner.hasNextInt()) { // проверяем, число ли введено
int enterPayers = scanner.nextInt();
if (enterPayers > 0){ //проверяем, что гостей больше 0
return enterPayers; //если да - возвращаем число гостей и завершаем цикл
} else {
System.out.println("Количество человек должно быть больше 0, пожалуйста введите корректное значение");
}

} else { // если введено не число - проверяем, введено ли 'Завершить'
if (thisIsTheEnd(scanner.next())) {
return 0; // если введено слово 'Завершить' - возвращаем 0
}
System.out.println("Введено некорректное значение, пожалуйста введите корректное значение");
}
}
}

private double calculate() { // Принимает название товара и его цену, добавляет товар в список. Возвращает общую сумму товаров

double totalCost = 0;

while (true) {

System.out.println("Введите название товара или команду 'Завершить'");

String name = scanner.next(); // Считываем строку с названием

if (thisIsTheEnd(name)) { // проверяем, введено ли 'Завершить'
break;
} else {

System.out.println("Введите стоимость товара в формате 'Р.КК' или команду 'Завершить'");

double price = getCost(); //узнаем цену товара, возвращает цену строго больше 0 или -1 в том случае, если ввели 'Завершить'
if (price == -1) { // возвращается в том случае, если ввели 'Завершить'
break;
}
productList.add(new Product(name, price)); // создаем новый товар

System.out.println("Товар " + name + " стоимостью " + price + " " + rublesEnd(price) + " добавлен");
totalCost += price; // добавляем к общей сумме цену товара
}
}
return totalCost;
}

private boolean thisIsTheEnd(String input){ // Проверяет: введено ли 'Завершить'
return input.equalsIgnoreCase("Завершить");
}

private double getCost() { // Получаем цену товара
while (true) {

if (scanner.hasNextDouble()) { // проверяем тип введенных данных

double price = scanner.nextDouble();

if (price > 0) { // если цена больше 0 и формата РР.КК - возвращаем цену
return price;
} else {
System.out.println("Цена должна быть больше 0, в формате РР.КК, пожалуйста введите корректное значение"); //если меньше или 0, просим ввести все заново
}

} else {

if (thisIsTheEnd(scanner.next())) {
return -1;
}
System.out.println("Цена должна быть числом, пожалуйста введите корректное значение");
}
}
}

private String rublesEnd(double rubles) { // процедура возвращает слово рубль с правильным окончанием
if (((int) rubles % 100) > 10 && ((int) rubles % 100) < 21 ) {
Copy link

@i-masloed i-masloed Feb 11, 2024

Choose a reason for hiding this comment

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

rubles % 100 и rubles % 10 используется несколько раз , стоит вынести в переменную

return "рублей";
}
int i = (int) rubles % 10; // выясняем последнюю цифру
if (i == 1) {
return "рубль";
} else if (i == 2 || i == 3 || i == 4) {
Copy link

@i-masloed i-masloed Feb 11, 2024

Choose a reason for hiding this comment

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

if (i >= 2 || i<=4)

return "рубля";
} else {
return "рублей";
Copy link

@i-masloed i-masloed Feb 11, 2024

Choose a reason for hiding this comment

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

строковые значения можно вынести в константы

}
}

private void output(int numberOfPayers, double cost) { //выводим список товаров и цену для одного
if (productList.isEmpty()) {
System.out.println("Как хорошо, что вам не за что платить");
} else {
System.out.println("Добавленные товары:");

for (Product product : productList) {
System.out.println("Товар " + product.name + " стоимостью " + product.price + " " + rublesEnd(product.price)); // для каждого товара выводим название и цену
}
double onePersonPay = cost / numberOfPayers;
System.out.println(String.format("%.2f", onePersonPay) + " " + rublesEnd(onePersonPay) + " c каждого человека"); // вывод сколько должен заплатить каждый
}
}
}

class Product {
Copy link

@i-masloed i-masloed Feb 11, 2024

Choose a reason for hiding this comment

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

Рекомендация: в java принято правило: один класс-один файл.

String name;
double price;
public Product(String name, double price) { //создаем по названию и цене формата РР.КК
this.name = name;
this.price = price;
}
}

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