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
12 changes: 12 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.util.List;

public class Calculator {
static double divider(List<Food> listOfFood, int numberOfPeople) {
double total = 0;

for ( Food food : listOfFood) {
total += food.getPrice();
}
return total / numberOfPeople;
}
}
21 changes: 21 additions & 0 deletions src/main/java/Food.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Food {
private String name;
private Double price;

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

@Override
public String toString() {
return "{name='" + name + "', price=" + price + "}";
}

}
42 changes: 41 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);


System.out.println("Введите количество людей, на которых нужно разделить счет:");

int peopleCount = Validator.getPeopleCount();


ProductManager manager = new ProductManager();
while (true) {
System.out.println("Введите название товара:");
String foodName = Validator.getFoodName();

System.out.println("Введите стоимость товара в формате рубли,копейки (например 10,45 или 11,40):");
double foodPrice = Validator.getFoodPrice();

Food food = new Food(foodName, foodPrice);
manager.addFood(food);
System.out.println("Вы ввели товар: " + foodName);
System.out.println("Стоимость товара: " + RubFormat.rubFormat(foodPrice));

System.out.println("Если не хотите добавлять товар: " + "'" + "Завершить" + "' " + "или любой символ, чтобы добавить еще");
String exit = scanner.next();
if (Validator.checkOnExit(exit)) {
manager.printFoods();
break;
}
}

double total = Calculator.divider(manager.getListOfFood(), peopleCount);

System.out.println("Каждый должен заплатить по: " + RubFormat.rubFormat(total));
}







}
23 changes: 23 additions & 0 deletions src/main/java/ProductManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.ArrayList;
import java.util.List;

public class ProductManager {
private List<Food> listOfFood;

public ProductManager() {
listOfFood = new ArrayList<>();
}
public List<Food> getListOfFood() {
return listOfFood;
}

public void addFood(Food food) {
listOfFood.add(food);
}
public void printFoods() {
System.out.println("Добавленные товары:");
for (Food food : listOfFood) {
System.out.println(food.toString());
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/RubFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class RubFormat {
static public String rubFormat(double rub) {
String ending;
int rubles = (int) rub;
int lastDigit = rubles % 10;
int lastTwoDigits = rubles % 100;

if (lastDigit == 1 && lastTwoDigits != 11) {
ending = "рубль";
} else if (lastDigit >= 2 && lastDigit <= 4 && (lastTwoDigits < 12 || lastTwoDigits > 14)) {
ending = "рубля";
} else {
ending = "рублей";
}

return String.format("%.2f %s", rub, ending);
}
}

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

public class Validator {

public static int getPeopleCount() {
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
int count;
while (true) {
if (scanner.hasNextInt() && (count = scanner.nextInt()) > 0) {
return count;
} else {
createErrorMessage();
scanner.nextLine();
}
}
}

public static String getFoodName() {
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
String text;
while (true) {
if (isFoodNameValid(text = scanner.nextLine())) {
return text;
} else {
createErrorMessage();
}
}
}

public static double getFoodPrice() {
Scanner scanner = new Scanner(System.in).useDelimiter("\n");

double price;
while (true) {
if (scanner.hasNextDouble() && checkNonNegative(price = scanner.nextDouble())) {
return price;
} else {
createErrorMessage();
scanner.nextLine();
}
}
}

public static boolean checkOnExit(String exit) {
return exit.equalsIgnoreCase("завершить");
}

private static boolean checkNonNegative(double foodPrice) {
return foodPrice > 0;
}

private static boolean isFoodNameValid(String text) {
return !(text.trim().isEmpty());
}

private static void createErrorMessage() {
System.out.println("Некорректное значение. Пожалуйста, введите заново:");
}

}