Skip to content
Closed
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
33 changes: 30 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import java.util.Scanner;

public class Main {

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

System.out.println("Программа \"Калькулятор счёта\"");
numberOfFriends = inputOfFriends();
calc.input(numberOfFriends);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно запустить программу одной строчкой
calc.input(inputOfFriends());

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Точно. Лишний код получился.

}



public static int inputOfFriends() {
Scanner scanner = new Scanner(System.in);
int numberOfFriends;
do {
System.out.println("Введите количество друзей: ");
while (!scanner.hasNextInt()){
System.out.println("Вы ввели не целое число! Повторите ввод числа друзей:");
scanner.next();
}
numberOfFriends = scanner.nextInt();
if (numberOfFriends <= 0) {
System.out.println("Число друзей не может быть равным нулю или быть отрицательным!");
} else if (numberOfFriends == 1) {
System.out.println("Число друзей должно быть больше 1 !");
}
}
while (numberOfFriends <= 1);
return numberOfFriends;
}

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

public class calc {
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 принято называть с большой буквы
Про конвенции названий можно почитать здесь: https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

да, понял, что ошибся только перед pull request'ом :(

public static void input(int numberOfFriends) {
Scanner scanner = new Scanner(System.in);
String roubles;
String[] product = new String[100];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

В данном случае ввод ограничен 100 товарами, после чего программа завершится. Скорее всего до 100 товаров не дойдет, но лучше подстраховаться и использовать ArrayList

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Хотелось выполнить работу соответственно уровню знаний, которые нам дали.
Формально ни массивы, ни arrayList нам неизвестны.
Но со строковой переменной не хотелось, хотя и разобрался, как это можно было.

Ну и, да, в реальности выполнения программы до 100 товаров никто не доберётся. Впрочем, как и в жизни :)

Спасибо за ревью!

int numbersOfProduct = 0;
float sum = 0;
float price;

for (int i = 0; i < product.length; i++) {
System.out.println("Введите название блюда:");
product[i] = scanner.next();

do {
System.out.println("Введите его стоимость с копейками (в формате 0,00): ");
while (!scanner.hasNextFloat()) {
System.out.println("Вы ввели не число! Повторите ввод цены:");
scanner.next();
}
price = scanner.nextFloat();
} while (price <= 0);

sum += price;
numbersOfProduct++;
System.out.println("Добавлено!\n Добавите ещё блюдо? Нет - введите Завершить. Да - введите любой символ");
String answer = scanner.next();
if (answer.equalsIgnoreCase("завершить")) {
break;
}
}

printAllProducts(numbersOfProduct,product);

roubles = getRubleAddition((int) sum / numberOfFriends);

System.out.printf("\nС каждого друга по %.2f %s\n", sum / numberOfFriends, roubles);
}

private static void printAllProducts(int numbersOfProduct, String[] product){
System.out.println("\n\"Добавленные товары:\" ");
for (int j = 0; j < numbersOfProduct; j++) {
if (product[j].isEmpty()) {
break;
} else {
System.out.println(product[j]);
}
}
}

public static String getRubleAddition(int number){
float predPosCifra = number % 100 / 10;
if (predPosCifra == 1)
{
return "рублей";
}
switch (number % 10)
{
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
}