W
W
White_Wolf_DD2020-11-15 21:52:17
Java
White_Wolf_DD, 2020-11-15 21:52:17

How to add balance in Java?

In general, I strongly disliked Java, as a programming language, roughly speaking, I have little understanding of it.
There is a code that I redid for myself, but I can’t finish it the way I want.
I made such a choice in the program that if the user does not have enough balance on the account, he is offered to replenish this balance. Not a more difficult task, I made it so that the user can reset his account to his given number, but not replenish it. How can I make it so that he can not reset his account, but only replenish a specific amount?

double residual;
        residual = customer.getBalance() - product.getPrice();
        if (residual < 0){
            System.out.println("----------------------------------");
            System.out.println("Недостаточно средств для покупки");
            System.out.println("----------------------------------");
            System.out.println("Баланс: "+customer.getBalance()+"€");
            System.out.println("----------------------------------");
            System.out.println("Хотите пополнить свой баланс?");
            System.out.println("1. Пополнить баланс");
            System.out.println("2. Не пополнять баланс");
            String var = scan.nextLine();
            switch (var) {
                case "1":
                    System.out.println("--- ПОПОЛНИТЬ БАЛАНС ---");
                    double numBalance;
                    do {
                        System.out.printf("Введите новый баланс счета: ");
                        String strBalance = scan.nextLine();
                        try {
                            numBalance = Double.parseDouble(strBalance);
                            System.out.println("Вы успешно обнулили свой счёт на "+strBalance+"€");
                            break;
                        } catch (Exception e) {
                            System.out.println("Нужно использовать только цифры.");
                        }
                    } while (true);
                    customer.setBalance(numBalance);
                    System.out.println("Попробуйте заново купить товар.");
                    break;
                case "2":
                    System.out.println(" --- НЕ ПОПОЛНЯТЬ БАЛАНС --- ");
                    break;
            }
                return;
        }else{
            customer.setBalance(residual);
            Purchase purchase =  new Purchase(customer, product, calendar.getTime());
            this.addPurchaseToArray(purchase, listPurchases);
       }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question