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
12 changes: 12 additions & 0 deletions src/main/java/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Calculate {
static double totalCost;

public void sum(double item) {
totalCost += item;
System.out.println(totalCost);
}

public double getResult(int count) {
return totalCost / count;
}
}
20 changes: 20 additions & 0 deletions src/main/java/Cart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Cart {
static String purchaseList = "";

public void addPurchase(String name, double cost, String str) {
if (purchaseList == "") {
purchaseList = "Добавленные товары: \n\n" + name + ": " + String.format("%.2f",cost) + str + "\n";
System.out.println("Товар " + name + " стоимостью " + String.format("%.2f",cost) + str + " успешно сохранен");
} else {
purchaseList = purchaseList + name + ": " + String.format("%.2f",cost) + str + "\n";
System.out.println("Товар " + name + " стоимостью " + String.format("%.2f",cost) + str + " успешно сохранен");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

У вас есть класс Product, но вы его почему-то не используете. Вы можете сохранять данные в ArrayList purchaseList = new ArrayList();
И при необходимости печати в цикле перебирать элементы листа и подставлять значения в String.format.




}

public void printPurchaseList() {
System.out.println(purchaseList);
}
}
10 changes: 10 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Formatter {

public String formate(int num) {
int res = num;
while (res > 20) {
res = res % 10;
}
return res > 1 && res < 5 ? " рубля" : res == 1 ? " рубль" : " рублей";
}
}
37 changes: 36 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@

import java.util.Scanner;


public class Main {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Hello world!");

Validator validator = new Validator();
Cart cart = new Cart();
Formatter formatter = new Formatter();
Calculate calculate = new Calculate();

System.out.println("Введите количество гостей");

int countPerson = validator.verifyInt();

String event = "";

while (!event.equalsIgnoreCase("Завершить")) {
System.out.println("Введите наименование товара");
String name = validator.verifyString();
System.out.println("Введите стоимость товара");
double cost = validator.verifyDouble();
String currency = formatter.formate((int) Math.floor(cost));

cart.addPurchase(name, cost, currency);
calculate.sum(cost);

System.out.println("Добавить новый товар? Чтобы перейти к расчету введите 'Завершить'");
event = scanner.next();


}
cart.printPurchaseList();
double partPayment = calculate.getResult(countPerson);
String currency = formatter.formate((int) Math.floor(partPayment));
System.out.println("Нужно оплатить по " + String.format("%.2f", partPayment) + currency);
}

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

String name;
double price;

Product(String name, double cost) {
this.name = name;
this.price = cost;
}


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

public class Validator {
private static final Scanner scanner = new Scanner(System.in);

public double verifyDouble() {
boolean verifyDouble = false;
double res = 0;

while (!verifyDouble) {
if (scanner.hasNextDouble()) {
double tmp = scanner.nextDouble();
scanner.nextLine();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

первый раз считываешь строку

if (tmp > 0) {
res = tmp;
verifyDouble = true;
} else {
System.out.println("Стоимость не может быть меньше 0");

}
} else {
System.out.println("Не тот формат ввода. Повторите");
scanner.nextLine();
}
}
return res;
}

public int verifyInt() {
boolean verify = false;
int res = 0;

while (!verify) {
if (!scanner.useDelimiter("\n").hasNextInt()) {
System.out.println("Не тот формат ввода. Введите целое число");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

а тут как раз не хватает получения новой строки (scanner.nextLine();) ты все время проверяешь
в if (!scanner.useDelimiter("\n").hasNextInt()) {
одну и ту же строку, получается вечный цикл

scanner.nextLine();
} else {
int tmp = scanner.useDelimiter("\n").nextInt();
scanner.nextLine();
if (tmp < 2) {
System.out.println("Значение должно быть больше или равно 2");
} else {
res = tmp;
verify = true;
}
}
}
return res;
}

public String verifyString() {
boolean verify = false;
String res = "";

while (!verify) {
String tmp = scanner.nextLine().trim();
if (tmp.isEmpty()) {
System.out.println("Наименование не может быть пустым");

} else {
verify = true;
res = tmp;
}

}
return res;
}
}