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
8 changes: 8 additions & 0 deletions src/main/java/Guest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class Guest {

int guests;

Guest(int guests) {
this.guests = guests;
}
}
89 changes: 86 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,91 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

static Scanner scanner = new Scanner(System.in);
static ArrayList<Product> products = new ArrayList<Product>();
static Guest guests;
static ProdCalc prodCalc;

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

public static void addGuests() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно методы расчета вынести в отдельный класс Калькулятор, сделать не static, а обычными, и в main() создавать объект калькулятора и вызывать методы от него

int person;
do {
System.out.println("Сколько вас человек?");
while (!scanner.hasNextInt()) {
System.out.println("Введите корректное значение: ");
scanner.next();
}
person = scanner.nextInt();
} while (person <= 1);
guests = new Guest(person);
}

public static void addProd() {
String food;
double price;
while (true) {
System.out.println("Ведите название товара или 'Завершить'");
food = scanner.next();
if (food.equalsIgnoreCase("завершить")) {
break;
} else {
do {
System.out.println("Стоимость товара в формате 'рубли.копейки' (XX,XX)");
while (!scanner.hasNextDouble()) {
System.out.println("Введите корректное значение!");
scanner.next();
}
price = scanner.nextDouble();
}
while (price <= 0);
}
products.add(new Product(food, price));
System.out.println("Добавлено");
}
}


public static double runSum() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Возвращаемое значение метода не используется. Можно сделать метод void или использовать значение

for (int i = 0; i < products.size(); i++) {
prodCalc.sum(products.get(i).coast);
}
return prodCalc.summaryCoast;
}

public static double runDivide() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Возвращаемое значение метода не используется. Можно сделать метод void или использовать значение

return prodCalc.divide(guests.guests);
}

public static void printResult() {
String template = "Каждый должен заплатить %.2f %s";
System.out.println("Добавленные товары: ");


for (int i = 0; i < products.size(); i++) {
System.out.println(products.get(i));
}
System.out.println("Общая сумма: " + prodCalc.summaryCoast);
System.out.println(String.format(template, prodCalc.moneyForPerson, getRightWord(prodCalc.moneyForPerson)));

}

private static String getRightWord(double n) {
n = (int) Math.floor(n);
n = Math.abs(n) % 100;
int n1 = (int) (n % 10);
if (n > 10 && n < 20) return "рублей";
if (n1 > 1 && n1 < 5) return "рубля";
if (n1 == 1) return "рубль";
return "рублей";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Хорошо написанный метод, маленькая вложенность, из-за этого легко читать

}
}
17 changes: 17 additions & 0 deletions src/main/java/ProdCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class ProdCalc {

public double summaryCoast;
public double moneyForPerson;

public double sum(double coast) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Возвращаемое значение метода не используется. Можно сделать метод void или использовать значение

return summaryCoast += coast;
}

public double divide(int guests) {
moneyForPerson = summaryCoast / guests;
return moneyForPerson;
}


}

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 {

public String name;
public double coast;

Product(String name, double coast) {
this.name = name;
this.coast = coast;
}

public String toString() {
return String.format("%s:\t%.2f", name, coast);

}
}