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
43 changes: 39 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
import java.util.Scanner;

public class Main {

static Scanner scanner = new Scanner(System.in);


public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
int peoples = readCounter();
ProductList products = new ProductList();
products.readProducts(scanner);
printConclusion(products, products.calculate(peoples));
scanner.close();
}

public static int readCounter() {
while (true) {
System.out.println("На скольких человек необходимо разделить счёт");
if (scanner.hasNextInt()) {
int number = scanner.nextInt();
scanner.nextLine();
if (number > 1) {
return number;
}
if (number == 1) {
System.out.println("Нет смысла делить счёт");
} else {
System.out.println("Неверное количество людей");
}
} else {
System.out.println("Введите пожалуйста число");
scanner.next();
}
}
}

public static void printConclusion(ProductList products, float output) {
System.out.println("Добавленные товары:");
products.writeNameList();
System.out.println(String.format("%.2f", output) + Spelling.writeRub(output));
}
}

}
10 changes: 10 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Product {
String name;
float cost;

Product (String productName, float productCoast) {
name = productName;
cost = productCoast;
}

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

public class ProductList {

private ArrayList<Product> productsList = new ArrayList<>();

public void readProducts(Scanner scanner) {
String end = "завершить";
String nameOfProduct;
float costOfProduct;
while(true) {
System.out.println("введите имя товара");
nameOfProduct = scanner.nextLine();
System.out.println("введите стоимость");
if (scanner.hasNextFloat()) {
costOfProduct = scanner.nextFloat();
scanner.nextLine();
if (costOfProduct > 0) {
productsList.add(new Product(nameOfProduct, costOfProduct));
System.out.println("Товар успешно добавлен. Хотите ввести ещё товар? \nЕсли нет, то введите ''Завершить''");
if (end.equalsIgnoreCase(scanner.nextLine())) {
break;
}
} else {
System.out.println("Товар введён неверно");
}
} else {
System.out.println("Товар введён неверно");
scanner.nextLine();
}
}
}


public float calculate(int n) {
float sum = 0;
for (int i = 0; i < productsList.size(); i++) {
sum += productsList.get(i).cost;
}
return sum / n;
}

public void writeNameList() {
for (int i = 0; i < productsList.size(); i++) {
System.out.println(productsList.get(i).name);
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/Spelling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Spelling {
public static String writeRub(float rub) {
int decRubl = (int) rub % 100;
int ones = decRubl % 10;
int tens = decRubl / 10;
if (ones == 1 && tens != 1) {
return " рубль";
} else if (ones >= 2 && ones <= 4 && tens != 1) {
return " рубля";
} else {
return " рублей";
}

}

}