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

public class Counter {
public static int count() {
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 предварительно создав экземпляр класса Counter counter = Counter()

System.out.println("Сколько человек?");
int persons = 0;
while (true) {
Scanner sc = new Scanner(System.in);
if (sc.hasNextInt()) {
int number = sc.nextInt();
if (number < 2) {
System.out.println("Разделить можно только на несколько человек.");
} else {
persons = number; //количество человек
break;
}
} else {
System.out.println("Введите целое положительное число.");
}
}
return persons;
}
}
65 changes: 62 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
// dev branch for Y.Practicum
import java.util.Scanner;

public class Main {

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

System.out.println("Заполняем список заказанных товаров. После ввода всех товаров наберите 'Завершить'");
Scanner sc = new Scanner(System.in);
String a = ""; //сюда записываются товары
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Лучше давать более осмысленные имена переменным

String bill = ""; //здесь записываем счет
float b = 0f; //сюда записываем цену
float total = 0f; //здесь считаем стоимость счета
while (true) {
System.out.println("Введите название товара.");
a = sc.nextLine();
if (a.equalsIgnoreCase("завершить")) {
break;
}
System.out.println("Введите цену товара.");
while (true) {
sc = new Scanner(System.in);
if (sc.hasNextFloat()) {
b = sc.nextFloat();
sc.nextLine();
if (b < 0) {
System.out.println("Цена не может быть отрицательной. Введите корректную цену.");
} else {
break;
}
} else {
System.out.println("Введите цену в целочисленном виде.");
}
}
Comment on lines +23 to +36
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Это можно вынести в отдельную функцию

bill = bill + String.format("%s цена %.2f р\n", a, b);
total = total + b;
}
float pricePersonal = total / (i); //здесь считаем цену для каждого человека
System.out.println("Добавленные товары: \n");
System.out.println(bill);

String ruble = "";
int e = ((int) (pricePersonal)) % 10; //сюда записываем последнюю цифру
int f = (int) ((pricePersonal / 10) % 10); //здесь записываем предпоследнюю цифру
if (f == 1) {
ruble = "рублей";
} else {
switch (e) {
case (1):
ruble = "рубль";
break;
case (2):
case (3):
case (4):
ruble = "рубля";
break;
default:
ruble = "рублей";
break;
}
}
Comment on lines +44 to +63
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

это тоже можно было вынести в отдельную функцию и тогда бы код было читать проще

String itog = "";
itog = String.format("На каждого человека приходится %.2f %s", pricePersonal, ruble);
System.out.println(itog);
}
}
}