Skip to content

Консольное приложение - #2

Open
purpleratchet wants to merge 5 commits into
Yandex-Practicum:masterfrom
purpleratchet:src_test
Open

purpleratchet wants to merge 5 commits into
Yandex-Practicum:masterfrom
purpleratchet:src_test

Conversation

@purpleratchet

Copy link
Copy Markdown

No description provided.

Comment thread src/main/java/Main.java Outdated
Comment on lines +11 to +19
public static double calculate(ArrayList<Position> positions) {
int i = 0;
double result = 0;
while (i < positions.size()) {
result += positions.get(i).positionPrice;
i++;
}
return result;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Не совсем понятно для чего используется статический метод, если можно обойтись обычным методом

Comment thread src/main/java/Main.java Outdated
Comment on lines +22 to +33
switch ((int) (d % 10)) {
case 1: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рубль"); break;
case 2: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рубля"); break;
case 3: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рубля"); break;
case 4: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рубля"); break;
case 5: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рублей"); break;
case 6: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рублей"); break;
case 7: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рублей"); break;
case 8: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рублей"); break;
case 9: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рублей"); break;
case 0: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рублей"); break;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

учтены не все кейсы. например у суммы 111 должно быть рублей, а в твоем случае будет рубль

Comment thread src/main/java/Main.java Outdated
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
class Position {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

По хорошему каждый класс должен находиться в отдельном файле

Comment thread src/main/java/Main.java Outdated
public class Main {

public static void main(String[] args) {
public static ArrayList<Position> counter() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Еще один статичный метод. В своем коде необходимо стараться по минимуму использовать статические методы, и если есть возможность вместо статического метода использовать обычный, то в 90% случаев надо его использовать.

Comment thread src/main/java/Main.java Outdated
Comment on lines +42 to +65
ArrayList<Position> positions = new ArrayList();
int i = 0;
double price = -1;
String close = "";
String name = "";
final Scanner scanner = new Scanner(System.in);
PrintStream ps = new PrintStream(System.out);

do {
ps.println("Введите название товара и цену через пробел (дробные цены вводите через ,)");
name = scanner.next();
price = scanner.nextDouble();
while (price <= 0) {
ps.println("Введите положительную цену, имя заново вводить не нужно");
price = scanner.nextDouble();
}
Position buf = new Position(name, price);
positions.add(buf);
i++;
ps.println("Товар успешно добавлен");
ps.println("Добавить еще товар или завершить ввод?");
close = scanner.next();
} while (!(close.equalsIgnoreCase("завершить")));
return positions;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

в случае если пользователь введен не цифры, а другие символы, приложение упадет. Это можно поправить, например с помощью конструкции try catch

Comment thread src/main/java/Main.java Outdated
Comment on lines +89 to +100
switch ((int) (result % 10)) {
case 1: ps.println(String.format("%.2f", result)+" рубль"); break;
case 2: ps.println(String.format("%.2f", result)+" рубля"); break;
case 3: ps.println(String.format("%.2f", result)+" рубля"); break;
case 4: ps.println(String.format("%.2f", result)+" рубля"); break;
case 5: ps.println(String.format("%.2f", result)+" рублей"); break;
case 6: ps.println(String.format("%.2f", result)+" рублей"); break;
case 7: ps.println(String.format("%.2f", result)+" рублей"); break;
case 8: ps.println(String.format("%.2f", result)+" рублей"); break;
case 9: ps.println(String.format("%.2f", result)+" рублей"); break;
case 0: ps.println(String.format("%.2f", result)+" рублей"); break;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Повторение кода, что бы избежать повторения большого числа кода его можно выносить в общие методы

Comment thread src/main/java/Main.java Outdated
ps.println("Введите положительную цену, имя заново вводить не нужно");
price = scanner.nextDouble();
}
Position buf = new Position(name, price);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

стоит давать более осмысленные имена переменным, иначе код становится читать труднее

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants