Repository navigation
Практическая работа 1 #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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) { | ||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Такие подряд идущие println лучше объединять в один, делая перенос строки при помощи \n. Некритично, но для небольшой экономии производительности один вызов лучше, чем два |
||
| } | ||
| } | ||
| } | ||
| 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так как на протяжении жизни программы у нас используется один экземпляр класса Calculator, можно сделать его метод productsLists() static, чтобы не приходилось создавать единственный экземпляр калькулятора и напрямую вызывать метод |
||
| 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В Java принята конвенция, по которой методы нучше называть глаголами. Например, можно было было назвать createProductsList или как-то так.