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

Не бейте сильно пишу первый раз в жизни :( #1

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
nim0y wants to merge 3 commits into main
base: main
Choose a base branch
Loading
from dev
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
23 changes: 23 additions & 0 deletions src/main/java/Calc.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Calc {
String productNameC = "";
Copy link

@AleksandrIlinskii AleksandrIlinskii Apr 19, 2023

Choose a reason for hiding this comment

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

Названия productName вполне достаточно для понимания зачем оно нужно

static double finalCost = 0;
static double finalCostEach = 0;

public void productCalc(String name, double price) {
if (price <= 0) {
System.out.println("Вы ошиблись.\nПопробуйте ещё!");
} else {
finalCost = finalCost + price;
productNameC = productNameC + name + "\n";
System.out.println("Добавили! Едем дальше!");
}
}

public void finCountDown(int guestNumber) {
System.out.println("Добавлено:\n" + productNameC);
finalCostEach = finalCost / guestNumber;
System.out.println("С каждого:\n" + String.format("%.2f", finalCostEach) + FormRub.priceForm2());
System.out.println("Всего:\n" + String.format("%.2f", finalCost) + FormRub.priceForm1());

}
}
26 changes: 26 additions & 0 deletions src/main/java/FormRub.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class FormRub {

public static String priceForm1() {
if ((int)(Calc.finalCost % 100 / 10) == 1){
return " рублей.";
}
if ((int) (Calc.finalCost % 10) == 1) {
return " рубль.";
} else if ((int) (Calc.finalCost % 10) == 2 || (int) (Calc.finalCost % 10) == 3 || (int) (Calc.finalCost % 10) == 4) {
Copy link

@AleksandrIlinskii AleksandrIlinskii Apr 19, 2023

Choose a reason for hiding this comment

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

Как совет, ты можешь парой строчек выше завести переменную типа
int lastDigit = (int) (Calc.finalCost % 10) и ее подставлять во все проверки твоих условий, а то сейчас получается, что ты считаешь одно и то же 4 раза.
Это называется boilerplate code (код, который повторяется в нескольких местах). Очень легко так ошибиться

return " рубля.";
}
return " рублей.";
}

public static String priceForm2() {
if ((int)(Calc.finalCostEach % 100 / 10) == 1){
return " рублей.";
}
if ((int) (Calc.finalCostEach % 10) == 1) {
return " рубль.";
} else if ((int) (Calc.finalCostEach % 10) == 2 || (int) (Calc.finalCostEach % 10) == 3 || (int) (Calc.finalCostEach % 10) == 4) {
return " рубля.";
}
return " рублей.";
}
}
23 changes: 23 additions & 0 deletions src/main/java/Gests.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.Scanner;

public class Gests {
Copy link

@AleksandrIlinskii AleksandrIlinskii Apr 19, 2023

Choose a reason for hiding this comment

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

Если предполагалось, что название класса переводится как "Гость", то тут опечатка

Copy link
Owner Author

Choose a reason for hiding this comment

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

Да нет , я пока писал неслолько раз создавал похожие классы.
Крутил разные куски кода , в итоге что бы не путаться в блокноте получилось так.
Понимаю, буду внимательнее.

public static int guestNumber;
public static void Gest() {
Scanner scanner = new Scanner(System.in);
System.out.println("На какое количество человек разделить счет?");
guestNumber = 0;
while(true)
if (scanner.hasNextInt()) {
guestNumber = scanner.nextInt();
if (guestNumber <= 1) {
System.out.println("Слишком маленькое число попробуйте еще");
} else {
return;
}
}else{
System.out.println("Это не число");
scanner.nextLine();
}
}

}
6 changes: 3 additions & 3 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Gests.Gest();
Talk.Talking();
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/Talk.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Scanner;

public class Talk {
public static void Talking() {
Scanner scanner = new Scanner(System.in);
Calc calculator = new Calc();
while (true) {
System.out.println("Введите название товара");
String name = scanner.next();
System.out.println("Введите стоимость");
if (scanner.hasNextDouble()) {
double price = scanner.nextDouble();
calculator.productCalc(name, price);
System.out.println("Добавим еще? Введите любой символ.\nЕсли нет напишите \"Завершить\"");
} else {
System.out.println("Ошибка попробуй еще!");
}
String end = scanner.next();
if (end.trim().equalsIgnoreCase("завершить")) {
calculator.finCountDown(Gests.guestNumber);
System.out.println("Кониек");
break;
}

}

}
}

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