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
49 changes: 49 additions & 0 deletions src/main/java/CountFriends.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.Scanner;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

В java принят определенный стиль форматирования кода. Если не вдаваться в подробности, то отформатировать код можно быстрой комбинаций клавиш Ctrl+Alt+L(Windows) или (⌘+⌥+L)(Mac)


public class CountFriends {

static int countFriends = 0;
static Scanner sc = new Scanner(System.in);

//Функция ввода количества человек
public static int inputCountFriends() {
do {
System.out.println("На скольких человек необходимо разделить счёт?");
try {
if (sc.hasNextLine()) {
String inputLine = sc.nextLine();
inputLine = inputLine.trim();
countFriends = Integer.parseInt(inputLine);
}
} catch (Exception e) {
System.out.println("Ошибка: " + e.getMessage());
}
switch (countFriends) {
case 0:
System.out.println("Введено некорректное количество человек.\n Введите еще раз!)");
break;
case 1:
System.out.println("Нечего делить в счете оплачиваемым одним человеком.\n Введите еще раз!)");
break;
default:
if (countFriends < 0)
System.out.println("Кхм... Попробуем еще раз!");
else return countFriends;
}

}
while (true);
}

public static double calculate(int countFriends, double countMoney) {
return countMoney / countFriends;
}

public static String ruble(double manyForOne) {
int value = (int) manyForOne;
if ((value % 100 / 10) % 10 == 1 || value % 10 == 0 || value % 10 >= 5) return "рублей";
else if (value % 10 == 1) return "рубль";
else if (value % 10 > 1 && value % 10 < 5) return "рубля";
else return "ошибка";
}
}
11 changes: 10 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner sc = new Scanner(System.in);
int countFriends = CountFriends.inputCountFriends();
Products.addProducts();
double countMoney = Products.getSumm();
double manyForOne = CountFriends.calculate(countFriends, countMoney);

System.out.println(Products.getListProducts());
System.out.println("Каждый должен заплатить по: " + String.format("%.2f", manyForOne).replace(",", ".") + " " + CountFriends.ruble(manyForOne));
sc.close();
}
}
59 changes: 59 additions & 0 deletions src/main/java/Products.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

import java.util.Scanner;

public class Products {
static Scanner sc = new Scanner(System.in);
private static String listProducts = "Добавленные товары:";
private static double summ = 0.00;
static String inputName;
static double price;

public static void addProducts() {
do {
System.out.println("Введите название товара");
if (sc.hasNextLine()) {
inputName = sc.nextLine();
inputName = inputName.trim();
}
if (inputName.equalsIgnoreCase("Завершить")) {
break;
} else if (inputName.length() == 0) continue;
else {
listProducts += "\n" + inputName;
}
while (true) {
System.out.println("Введите стоимость товара цифрой");
try {
if (sc.hasNextLine()) {
String inputLine = sc.nextLine().trim();
double price = Double.parseDouble(inputLine);
if (price < 0) {
System.out.println("Неправильная стоимость товара");
} else {
summ += (double) price;
break;
}
}
} catch (Exception e) {
System.out.println("Ошибка: " + e.getMessage());
}

}
System.out.println("Товар успешно добавлен!");
System.out.println("Введите \"Завершить\" для завершения или любой другой символ для продолжения");
inputName = sc.nextLine();
inputName = inputName.trim();
if (inputName.equalsIgnoreCase("Завершить")) break;
} while (true);
}

public static String getListProducts() {
return listProducts;
}

public static double getSumm() {
return summ;
}


}