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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Scanner;
public class Calculator {
public void productsLists( Scanner 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 принята конвенция, по которой методы нучше называть глаголами. Например, можно было было назвать createProductsList или как-то так.

String answer = "Добавленные товары:";
String productName;
float price = 0;
float sum=0.00f;
System.out.println("Введите наименование товара");
productName = scanner.next();
System.out.println("Введите стоимость товара в формате 'рубли.копейки'");
while(price<=0){
if (scanner.hasNextFloat()) price = scanner.nextFloat();
else System.out.println("Введите корректное значение больше 0.0");
}
answer+="\n" + productName;
sum += price;
System.out.println("Товар успешно добавлен!\nХотите добавить ещё?");
switch(scanner.next()) {
case "Да":
productsLists(scanner);
case "Завершить":
System.out.println(answer);
System.out.println("Общая сумма"+sum);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Такие подряд идущие println лучше объединять в один, делая перенос строки при помощи \n. Некритично, но для небольшой экономии производительности один вызов лучше, чем два

}
}
}
32 changes: 28 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import java.util.Scanner;
public class Main {

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Так как на протяжении жизни программы у нас используется один экземпляр класса Calculator, можно сделать его метод productsLists() static, чтобы не приходилось создавать единственный экземпляр калькулятора и напрямую вызывать метод Calculator.productsLists(scanner);

for(int i = 0; i <= people; i++) {
calculator.productsLists(scanner);
}
}
}

public static int addPerson(Scanner scanner) {
int people = 0;
boolean hasPeople = false;
while(!hasPeople) {
System.out.println("Введите количество человек:");
if (scanner.hasNextInt()) {
people = scanner.nextInt();
if (people > 1) {
hasPeople = true;
} else {
System.out.println("Введено неверное количество. попробуйте еще раз.");
}
}else {
System.out.println("Введено неверное количество! попробуйте ввести количество гостей в виде числа.");
scanner.nextLine();
}
}
return people;
}
}