Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
Проектная работа №1 "Калькулятор счёта"
60 changes: 60 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Calculator {

static String nameGood;
static String normalNames = "";
static double price;
static double sumOfPrices;
static double sumForOne;
static short plugForCycle;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

тут много где private


static ArrayList<Good> goods = new ArrayList<>();

public static void count() {

while (true) {

if (plugForCycle == 0) {
System.out.println("\nВведи название товара");
} else {
System.out.println("\nДобавим ещё товар? Введи его название или команду 'Завершить'");
}

nameGood = Main.scanner.nextLine();

if (nameGood.equalsIgnoreCase("Завершить")) {

System.out.println("\nДобавление товаров завершено!\n\nДобавленные товары:" + normalNames);
break;

} else {

do {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Много вложенности усложняет читаемость кода. Рекомендую разбить всю логику на отдельные не очень большие методы

System.out.println("\nВведи цену этого товара в формате 'рубли,копейки'");
while (!Main.scanner.hasNextDouble()) {
System.out.println("\nОшибка: некорректное значение! Введи цену товара ещё раз в формате 'рубли,копейки'");
Main.scanner.next();
}
price = Main.scanner.nextDouble();
Main.scanner.nextLine();
if (price <= 0) {
System.out.println("\nОшибка: цена не может быть нулевой или отрицательной!");
}
} while (price <= 0);

Good good = new Good(nameGood, 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.

Непонятно, зачем каждый раз создавать экземпляр класса Good, если у Good все поля static

goods.add(good);
System.out.println("\nТовар '" + nameGood + "' добавлен");
normalNames = normalNames + "\n- " + good.name;
sumOfPrices = sumOfPrices + price;
sumForOne = sumOfPrices / Main.numberPersons;
plugForCycle++;
}
}

System.out.println(String.format("\nКаждый гость должен заплатить: %s %s", Formating.sumForMan, Formating.getNormalRubles()));
}
}
19 changes: 19 additions & 0 deletions src/main/java/Formating.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Iterator;

public class Formating {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Formatting


static String sumForMan = String.format("%.2f", Calculator.sumForOne);
static int roundedSumForOne = (int) Math.floor(Calculator.sumForOne);

public static String getNormalRubles() {

if (roundedSumForOne % 100 > 4 && roundedSumForOne % 100 < 21) {
return "рублей";
} else if (roundedSumForOne % 10 == 1) {
return "рубль";
} else if ((roundedSumForOne % 10) > 1 && (roundedSumForOne % 10) < 5) {
return "рубля";
}
return "рублей";
}
}
10 changes: 10 additions & 0 deletions src/main/java/Good.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Good {

static String name;
static double price;

Good(String name, double price) {
this.name = name;
this.price = price;
}
}
26 changes: 25 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import java.util.Scanner;

public class Main {

static Scanner scanner = new Scanner(System.in);
static int numberPersons;

public static void main(String[] args) {
System.out.println("Hello world!");

while (true) {
System.out.println("Введи количество гостей, на которых нужно разделить счёт");

if (scanner.hasNextInt()) {
numberPersons = scanner.nextInt();
scanner.nextLine();
if (numberPersons <= 1) {
System.out.println("Ошибка! Количество гостей должно быть больше 1.");
} else {
Calculator.count();
break;
}

} else {
System.out.println("Ошибка! Некорректное значение");
scanner.nextLine();
}
}
scanner.close();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

В Main надо по хорошему только старотовать программу, а остальное выносить в отдельные сущности

}
}