-
Notifications
You must be signed in to change notification settings - Fork 0
Purchase List App #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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
| } |
| 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 + " успешно сохранен"); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
| public void printPurchaseList() { | ||
| System.out.println(purchaseList); | ||
| } | ||
| } | ||
| 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 ? " рубль" : " рублей"; | ||
| } | ||
| } |
| 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); | ||
| } | ||
|
|
||
| } |
| 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; | ||
| } | ||
|
|
||
|
|
||
| } |
| 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("Не тот формат ввода. Введите целое число"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а тут как раз не хватает получения новой строки (scanner.nextLine();) ты все время проверяешь |
||
| 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.