Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
Saharok1209 wants to merge 4 commits into Yandex-Practicum:master
base: master
Choose a base branch
Loading
from Saharok1209:master
Open
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
16 changes: 16 additions & 0 deletions src/main/java/Calculation.java
View file Open in desktop
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;
Copy link

@MagicUnderHood MagicUnderHood Oct 25, 2022

Choose a reason for hiding this comment

The 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;
}
}
81 changes: 81 additions & 0 deletions src/main/java/Main.java
View file Open in desktop
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(){
Copy link

@MagicUnderHood MagicUnderHood Oct 25, 2022

Choose a reason for hiding this comment

The 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();
Copy link

@MagicUnderHood MagicUnderHood Oct 25, 2022

Choose a reason for hiding this comment

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

Здесь нужен просто scan.next();, иначе приложение упадет, поскольку мы проверили, что !scan.hasNextInt()

}
}
return people;
}
public static Product addProduct(){
Copy link

@MagicUnderHood MagicUnderHood Oct 25, 2022

Choose a reason for hiding this comment

The 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){
Copy link

@MagicUnderHood MagicUnderHood Oct 25, 2022

Choose a reason for hiding this comment

The 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";
}
}
22 changes: 22 additions & 0 deletions src/main/java/Product.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.ArrayList;

public class Product {
Copy link

@MagicUnderHood MagicUnderHood Oct 25, 2022

Choose a reason for hiding this comment

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

Класс содержит информацию не о продукте, а обо всех продуктах, так что можно назвать его Menu или хотя бы Products

static ArrayList<String> nameProduct = new ArrayList<>();
Copy link

@MagicUnderHood MagicUnderHood Oct 25, 2022

Choose a reason for hiding this comment

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

Поскольку в этой переменной хранится не имя одного продукта, а все продукты, лучше ее назвать productNames

static ArrayList<Double> priceProduct = new ArrayList<>();
Copy link

@MagicUnderHood MagicUnderHood Oct 25, 2022

Choose a reason for hiding this comment

The 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() {
Copy link

@MagicUnderHood MagicUnderHood Oct 25, 2022

Choose a reason for hiding this comment

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

Методы возвращают список имен продуктов, поэтому тоже лучше переименовать понятнее

return nameProduct;
}

public ArrayList<Double> getPriceProduct() {
return priceProduct;
}
}

AltStyle によって変換されたページ (->オリジナル) /