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
16 changes: 16 additions & 0 deletions src/main/java/BillCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class BillCalculator {
void startBillCalculator() {
Guests guests = new Guests();
int countGuests = guests.defineCountGuests();

Calculator calculator = new Calculator();
calculator.addProducts();

double priceForPerson = calculator.calcPriceForPerson(countGuests);
String rubleAddition = calculator.getRubleAddition(priceForPerson);
String conclusion = "Каждый должен заплатить по %.2f " + rubleAddition + ".";

System.out.println(calculator.products);
System.out.println(String.format(conclusion, priceForPerson));
}
}
61 changes: 61 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.util.Scanner;

public class Calculator {
double total;
String products = "Добавленные продукты:\n";
Scanner scanner = new Scanner(System.in);

void addProducts() {
while (true) {
Product product = new Product();
System.out.println("Введите наазвание товара:");
product.name = scanner.next();
while (true) {

System.out.println("Введите стоимость товара в формате ('рубли.копейки' [10.45, 11.40]):");

String strPriceProduct = scanner.next();
try {
product.price = Double.parseDouble(strPriceProduct);
} catch (NumberFormatException error) {
System.out.println("Стоимость указана в некорректном формате.");
continue;
}
if (product.price > 0) {
break;
}
System.out.println("Ошибка. Значение стоимости введено некорректно.");
}
System.out.println("Товар уcпешно добавлен. Хотите добавить еще товар?");
total = total + product.price;
products = products + product.name + "\n";
String NoOrYes = scanner.next();
if (NoOrYes.equalsIgnoreCase("Завершить")) {
break;
}
}
}

double calcPriceForPerson(int countGuest) {
return total / (double) countGuest;
}

String getRubleAddition(double moneyValue) {
int preLastDigit = (int)moneyValue % 100 / 10;
if (preLastDigit == 1) {
return "рублей";
}
int LastDigit = (int)moneyValue % 10;
switch (LastDigit) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";

}
}
}
27 changes: 27 additions & 0 deletions src/main/java/Guests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.InputMismatchException;
import java.util.Scanner;

public class Guests {
int countGuest = 0;
Scanner scanner = new Scanner(System.in);

int defineCountGuests() {
while (true) {
System.out.println("На скольких человек необходимо рзделить счет?");
try {
countGuest = scanner.nextInt();
} catch (InputMismatchException er) {
System.out.println("Некорректное значение");
scanner.nextLine();
continue;
}

if (countGuest <= 1) {
System.out.println("Ошибка. Введите корректное число гостей:");
continue;
}
return countGuest;

}
}
}
18 changes: 15 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
BillCalculator billCalculator = new BillCalculator();
billCalculator.startBillCalculator();
}
}

class Product {
String name;
double price;
}