-
Notifications
You must be signed in to change notification settings - Fork 220
Rychkov PW1 #84
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
Rychkov PW1 #84
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public class Calculation { | ||
| String allProducts = " "; | ||
| double totalPrice = 0.0; | ||
| Product product = new Product(); | ||
|
|
||
| public String calculationNameProduct(){ | ||
| for (int i = 0; i < product.getNameProduct().size(); i++) { | ||
| allProducts = allProducts + " " + product.getNameProduct().get(i); | ||
| }return allProducts; | ||
|
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), тогда автоматически код выправится |
||
| } | ||
| public Double calculationPriceProduct(){ | ||
| for (int i = 0; i < product.getPriceProduct().size(); i++) { | ||
| totalPrice = totalPrice + product.getPriceProduct().get(i); | ||
| } return totalPrice; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,89 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| static Scanner scan = new Scanner(System.in); | ||
| static Scanner scan1 = new Scanner(System.in); | ||
| static Scanner scan2 = new Scanner(System.in); | ||
| static Scanner scan3 = new Scanner(System.in); | ||
| static int people; | ||
| static Double productPrice; | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| Calculation calculation = new Calculation(); | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| people(); | ||
|
|
||
| while(true){ | ||
| addProduct(); | ||
| System.out.println("You want add another product? Input 'finish' if - No, else any symbol"); | ||
| String finish = scan3.nextLine(); | ||
| if(finish.equalsIgnoreCase("finish")){ | ||
| break; | ||
| } | ||
| } | ||
| System.out.println("You order: " + calculation.calculationNameProduct()); | ||
| System.out.println("Your price per guest: " + calculation.calculationPriceProduct() / people + " ' " + makeEnding(productPrice) + ".penni'"); | ||
|
|
||
| } | ||
| public static int people(){ | ||
|
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. Студия подсказывает, что возвращаемый тип функции никак не используется, тогда можно его убрать, или всё-таки использовать. Методы лучше называть с глаголом, например, readPeople или getPeople (это если возвращаемый результат будет использоваться) |
||
|
|
||
| while (true) { | ||
| System.out.println("How guest?"); | ||
| if (scan.hasNextInt()) { | ||
| people = scan.nextInt(); | ||
| if (people > 1) { | ||
| break; | ||
| } else if (people == 1) { | ||
| System.out.println("nothing to share"); | ||
| } else { | ||
| System.out.println("invalid input"); | ||
| scan.nextInt(); | ||
| } | ||
| }else{ | ||
| System.out.println("invalid input"); | ||
| scan.nextInt(); | ||
|
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. Здесь нужен просто scan.next();, иначе приложение упадет, поскольку мы проверили, что |
||
| } | ||
| } | ||
| return people; | ||
| } | ||
| public static Product addProduct(){ | ||
|
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. Студия подсказывает, что возвращаемый тип функции никак не используется, тогда можно его убрать, или всё-таки использовать |
||
| Product product = new Product(); | ||
|
|
||
| String productName; | ||
| System.out.println("Input name product"); | ||
| productName = scan1.nextLine(); | ||
| product.setNameProduct(productName); | ||
| System.out.println("you added " + productName); | ||
| while(true){ | ||
| System.out.println("Input price product into format: 'rubles.penni' [10,45]"); | ||
| if(scan2.hasNextDouble()){ | ||
| productPrice = scan2.nextDouble(); | ||
|
|
||
| if(productPrice > 0){ | ||
| System.out.println("Price product: " + productPrice); | ||
| product.setPriceProduct(productPrice); | ||
| break; | ||
| }else { | ||
| System.out.println("invalid input"); | ||
| scan2.next(); | ||
| } | ||
| }else { | ||
| System.out.println("invalid input"); | ||
| scan2.next(); | ||
| } | ||
|
|
||
| }return product; | ||
|
|
||
| } | ||
| public static String makeEnding(Double productPrice){ | ||
|
|
||
| if(Math.round(productPrice)%10 == 1 && Math.round(productPrice)%100 != 11){ | ||
| return "ruble"; | ||
| }else if((Math.round(productPrice)%10) <= 4 && (Math.round(productPrice)%100 >= 2) && (Math.round(productPrice)%100)/10 != 2){ | ||
|
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. Повторяющийся код Math.round(productPrice) и Math.round(productPrice)%10, Math.round(productPrice)%100 можно вынести в переменные |
||
| return "rubles"; | ||
| } else return "rubles"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Product { | ||
|
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. Класс содержит информацию не о продукте, а обо всех продуктах, так что можно назвать его Menu или хотя бы Products |
||
| static ArrayList<String> nameProduct = new ArrayList<>(); | ||
|
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. Поскольку в этой переменной хранится не имя одного продукта, а все продукты, лучше ее назвать productNames |
||
| static ArrayList<Double> priceProduct = new ArrayList<>(); | ||
|
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. Аналогично с nameProduct |
||
|
|
||
| public void setNameProduct(String nameProduct) { | ||
| Product.nameProduct.add(nameProduct); | ||
| } | ||
|
|
||
| public void setPriceProduct(double priceProduct) { | ||
| Product.priceProduct.add(priceProduct); | ||
| } | ||
|
|
||
| public ArrayList<String> getNameProduct() { | ||
|
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. Методы возвращают список имен продуктов, поэтому тоже лучше переименовать понятнее |
||
| return nameProduct; | ||
| } | ||
|
|
||
| public ArrayList<Double> getPriceProduct() { | ||
| return priceProduct; | ||
| } | ||
| } | ||