Repository navigation
Практическая работа №1 "Консольное приложение" #87
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
base: master
Are you sure you want to change the base?
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,103 @@ | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculate { | ||
| static Const aConst=new Const(); | ||
| static Scanner scanner = new Scanner(System.in); | ||
| static ArrayList<String> product = new ArrayList<>(); | ||
| static ArrayList<Double> price = new ArrayList<>(); | ||
| static double score = 0; | ||
|
|
||
| public static void calculate() { | ||
|
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. static можно убрать, поскольку ты вызываешь метод от создаваемого объекта класса Calculate |
||
| int count; | ||
| while (true){ | ||
| while (true)//проверка ввода целого числа | ||
| { | ||
| System.out.println("На сколько человек разделить заказ?"); | ||
| System.out.println("Введите целое больше 1"); | ||
| if (scanner.hasNextInt()) | ||
| { | ||
| count = Integer.parseInt(scanner.nextLine()); | ||
| System.out.println("Вы ввели: " + count); | ||
| break; | ||
| } | ||
| else | ||
| { | ||
| System.out.println("Вы ввели не целое число"); | ||
| scanner.nextLine(); | ||
| }} | ||
|
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. Можешь применить в студии автоформатирование (в выбранном файле, сверху вкладка Code - Reformat Code, либо Ctrl+Alt+L), тогда автоматически код выправится. |
||
| if (count<=1) { | ||
| System.out.println("Оглянись! Ты тут один наел на 15 тысяч"); | ||
| } else { | ||
| //блок заполнения листа и подсчета счета | ||
| System.out.println(aConst.ADD_PRODUCT_AND_PRICE); | ||
| String nameProduct = scanner.next();//название продукта | ||
| Double priceProduct = scanner.nextDouble();//цена | ||
|
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. Пользователь может ввести на вопрос про стоимость товаров вместо числа строку. Для количества человек ты обрабатываешь такую ситуацию через hasNextInt, стоит добавить обработку и для стоимости |
||
| addProduct(nameProduct); | ||
| addPrice(priceProduct); | ||
| System.out.println(aConst.ADD_MESSAGE_PRODUCT); | ||
| while (true) { | ||
| System.out.println(aConst.QUESTION_MESSAGE); | ||
| String question = scanner.next(); | ||
| if (question.equalsIgnoreCase("Да")) { | ||
| System.out.println(aConst.ADD_PRODUCT_AND_PRICE); | ||
| String addNameProduct = scanner.next(); | ||
| double addPriceProduct = scanner.nextDouble(); | ||
| addProduct(addNameProduct); | ||
| addPrice(addPriceProduct); | ||
| System.out.println(aConst.ADD_MESSAGE_PRODUCT); | ||
| } else if (question.equalsIgnoreCase("Завершить")) { | ||
| printProductAndPrice(); | ||
| score = score / count; | ||
| printResult(score); | ||
| break; | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| private static void addProduct(String addProduct) { | ||
| product.add(addProduct); | ||
| } | ||
| private static void addPrice(Double addPrice) { | ||
| price.add(addPrice); | ||
| } | ||
|
|
||
| //ввывод товаров и стоимость каждого продукта | ||
| private static void printProductAndPrice() { | ||
| System.out.println("Добавленные товары:"); | ||
| for (double price : price) { | ||
| score = score + Math.abs(price); | ||
| } | ||
| for (int i=0;i<product.size();i++){ | ||
| System.out.println("Продукт: "+ product.get(i)+ " стоимость: "+ Math.abs(price.get(i))); | ||
| } | ||
| } | ||
|
|
||
| //метод для вывода окончания рубля | ||
| private static void printResult(double score1) { | ||
| if ( Math.floor(score1) % 100 >= 11 && Math.floor(score1) % 100 <= 19) { | ||
| System.out.printf("Каждый должен заплатить: %.2f Рублей%n",score1); | ||
| } else { | ||
| switch ((int) (Math.floor(score1) % 10)) { | ||
| case 1: | ||
| System.out.printf("Каждый должен заплатить: %.2f Рубль%n",score1); | ||
| break; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| System.out.printf("Каждый должен заплатить: %.2f Рубля%n",score1); | ||
| break; | ||
| default: | ||
| System.out.printf("Каждый должен заплатить: %.2f Рублей%n",score1); | ||
|
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. Код |
||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| public class Const{ | ||
| final String ADD_MESSAGE_PRODUCT="Товар успешно добавлен"; | ||
| final String QUESTION_MESSAGE="Хотите ли вы добавить еще товар, выберите напишите 'Да' или напишите 'Завершить' для выхода"; | ||
| final String ADD_PRODUCT_AND_PRICE="Укажите название товара и его стоимость"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,30 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| static Calculate calculate=new Calculate(); | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| calculate.calculate(); | ||
| /*Scanner in = new Scanner(System.in); | ||
| int ab; | ||
| while (true) | ||
| { | ||
| System.out.println("Введите число"); | ||
| if (in.hasNextInt()) | ||
| { | ||
| ab = Integer.parseInt(in.nextLine()); | ||
| System.out.println("Вы ввели: " + ab); | ||
| break; | ||
| } | ||
| else | ||
| { | ||
| System.out.println("Вы ввели не число"); | ||
| in.nextLine(); | ||
| }}*/ | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
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.
static лучше убрать у всех полей и методов класса, необходимости в них нет