Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1cac854
Домашнее задание, спринт 2
Xaxaido Feb 3, 2024
d9372de
Домашнее задание, спринт 2
Xaxaido Feb 4, 2024
a482d5d
Домашнее задание, спринт 2
Xaxaido Feb 5, 2024
2f9cc82
Домашнее задание, спринт 2
Xaxaido Feb 5, 2024
bc6b75a
Домашнее задание, спринт 2
Xaxaido Feb 5, 2024
bb15c62
Домашнее задание, спринт 2
Xaxaido Feb 5, 2024
541d0ac
Домашнее задание, спринт 2
Xaxaido Feb 5, 2024
b3db92c
Домашнее задание, спринт 2
Xaxaido Feb 5, 2024
6c87bb3
Домашнее задание, спринт 2
Xaxaido Feb 6, 2024
5ff59f0
Домашнее задание, спринт 2
Xaxaido Feb 6, 2024
5b76db0
Домашнее задание, спринт 2
Xaxaido Feb 6, 2024
9f14240
Домашнее задание, спринт 2
Xaxaido Feb 6, 2024
c4022a4
Домашнее задание, спринт 2
Xaxaido Feb 6, 2024
94dd026
Домашнее задание, спринт 2
Xaxaido Feb 8, 2024
a0e688f
Домашнее задание, спринт 2
Xaxaido Feb 8, 2024
b254edb
Домашнее задание, спринт 2
Xaxaido Feb 8, 2024
4798156
Домашнее задание, спринт 2
Xaxaido Feb 8, 2024
9da675a
Домашнее задание, спринт 2
Xaxaido Feb 9, 2024
64c6f89
Домашнее задание, спринт 2
Xaxaido Feb 9, 2024
c467b2e
Домашнее задание, спринт 2
Xaxaido Feb 11, 2024
c48b71b
Домашнее задание, спринт 2
Xaxaido Feb 16, 2024
215640f
Домашнее задание, спринт 2
Xaxaido Feb 17, 2024
a666803
Домашнее задание, спринт 2
Xaxaido Feb 18, 2024
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
67 changes: 67 additions & 0 deletions src/main/java/Calc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.ArrayList;

public final class Calc {

private final int personsCount;
private double total;
private final ArrayList<Product> order;
final static String[] valueToFormat = {"рубль", "рубля", "рублей"};
private int maxLength = 0;

public static Calc getInstance(final MyScanner scanner) {

Calc calc = null;

while (calc == null) {

System.out.println("На скольких человек необходимо разделить счёт?");

int personsCount = scanner.nextInt();
if (personsCount >= 2) {
calc = new Calc(personsCount, new ArrayList<Product>());
} else {
MyScanner.errorMessage();
}

}

return calc;

}

public void addProduct(final String name, final double price) {

Product newProduct = new Product(name, price);
order.add(newProduct);
total += price;
maxLength = Math.max(maxLength, name.length());

}

public void printOrder() {

System.out.println("Добавленные товары:");

for (Product product : order) {
String name = product.getName();
double price = product.getPrice();
System.out.println(String.format(name + ".".repeat(maxLength - name.length() + 5) + "%.2f" +
Formatter.formatValue(price, valueToFormat), price));
}

double eachToPay = total / personsCount;
String total = String.format("Сумма к оплате для каждого человека: %.2f %s",
eachToPay, Formatter.formatValue(eachToPay, valueToFormat));
System.out.println("_".repeat(total.length()));
System.out.println(total);

}

Calc(int personsCount, ArrayList<Product> order) {

this.personsCount = personsCount;
this.order = order;

}

}
21 changes: 21 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public final class Formatter {

public static String formatValue(final double value, final String[] valueNameToFormat) {

int roundedValue = (int) Math.floor(value);
int preLastNumber = roundedValue % 100 / 10;
int lastNumber = roundedValue % 10;

if (preLastNumber == 1) {
return " " + valueNameToFormat[2];
}

return switch (lastNumber) {
case 1 -> " " + valueNameToFormat[0];
case 2, 3, 4 -> " " + valueNameToFormat[1];
default -> " " + valueNameToFormat[2];
};

}

}
48 changes: 46 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@

public class Main {

private final static String STOP_WORD = "Завершить";

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

MyScanner scanner = new MyScanner(System.in);
String productName;
double price;
boolean isStop;
Calc calc = Calc.getInstance(scanner);

do {

System.out.println("Введите название товара");
productName = scanner.next();

while (true) {

System.out.println("Введите стоимость товара");
price = scanner.nextDouble();

if (price < 0) {
MyScanner.errorMessage();
} else {
isStop = newProductOrStop(scanner, calc, productName, price);
break;
}

}

} while (!isStop);

calc.printOrder();
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.

Слишком длинный метод. Его сложно поддерживать и лучше разбить на логические составляющие

}

private static boolean newProductOrStop(final MyScanner scanner, final Calc calc, final String productName, final double price) {

calc.addProduct(productName, price);
System.out.println(String.format("Товар '%s' стоимостью %.2f %s успешно добавлен",
productName, price, Formatter.formatValue(price, Calc.valueToFormat)));
System.out.println("Хотите добавить ещё товар?\nДля продолжения введите любой символ\nДля отображения счёта введите '" + STOP_WORD + "'");

return scanner.next().equalsIgnoreCase(STOP_WORD);

}

}
40 changes: 40 additions & 0 deletions src/main/java/MyScanner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.io.InputStream;
import java.util.Scanner;

public final class MyScanner {

private final Scanner scanner;

public String next() { return scanner.next(); }

public int nextInt() { return (int) nextDouble(); }

public double nextDouble() {
return readValue(scanner.next());
}

private double readValue(final String value) {

double newValue;

try {
newValue = Double.parseDouble(value);
} catch(NumberFormatException e) {
newValue = -1;
}

return newValue;

}

public void close() {
scanner.close();
}

public static void errorMessage() { System.out.println("Недопустимый ввод, попробуйте ещё раз"); }

public MyScanner(InputStream in) {
this.scanner = new Scanner(in);
}

}
17 changes: 17 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public final class Product {

private final String name;
private final double price;

public String getName() { return this.name; }

public double getPrice() { return this.price; }

Product(String name, double price) {

this.name = name;
this.price = price;

}

}