Если оператор else не вызывает метод isServiceCharge ()? - PullRequest
0 голосов
/ 26 сентября 2018

Я почти закончил с этой программой банковского счета.У меня есть вызов метода calculateSavings() или calculateCheckings().Если currentBalance меньше требуемого minSavings или minChecking, у меня есть оператор else для вызова метода isServiceCharge() для вычета комиссии.Программа не выдаёт мне ошибок, но если пользователь вводит currentBalance, который меньше требуемого значения для minChecking или minSavings, JOptionPane не показывает результат.Входы по-прежнему работают, но выход не появляется.Все остальное работает нормально, например, добавление процентов, но вычет платы за обслуживание - нет.Как я могу это исправить?

Основной класс

package com.company;

import javax.swing.*;

public class Main
{
    public static void main(String[] args)
    {
        {
            BankAccount myBank = new BankAccount();

            myBank.calculateNewBalance();
        }
    }
}

БанкСчетный класс

package com.company;

import javax.swing.*;

public class BankAccount
{
    private int accountNumber;
    private String accountType;
    private double minSavings = 2500.00;
    private double minChecking = 1000.00;
    private double currentBalance;

    public BankAccount()
    {
        accountNumber = Integer.parseInt(JOptionPane.showInputDialog("Please enter your account number"));
        accountType = JOptionPane.showInputDialog("Please enter your account type");
        currentBalance = Double.parseDouble(JOptionPane.showInputDialog("Please enter your current balance."));
    }

    public int getAccountNumber()
    {
        return accountNumber;
    }

    public void setAccountNumber(int accountNumber)
    {
        this.accountNumber = accountNumber;
    }

    public String getAccountType()
    {
        return accountType;
    }

    public void setAccountType(String accountType)
    {
        this.accountType = accountType;
    }

    public double getMinSavings()
    {
        return minSavings;
    }

    public void setMinSavings(double minSavings)
    {
        this.minSavings = minSavings;
    }

    public double getMinChecking()
    {
        return minChecking;
    }

    public void setMinChecking(double minChecking)
    {
        this.minChecking = minChecking;
    }

    public double getCurrentBalance()
    {
        return currentBalance;
    }

    public void setCurrentBalance(double currentBalance)
    {
        this.currentBalance = currentBalance;
    }

    public void calculateNewBalance()
    {
        if (accountType.equals("S") || accountType.equals("s"))
        {
            accountType = "Savings";
            calculateSavingsBalance();
        } else if (accountType.equals("C") || accountType.equals("c"))
        {
            accountType = "Checking";
            calculateCheckingBalance();
        }
    }

    private void calculateSavingsBalance()
    {
        if (currentBalance >= minSavings)
        {
            double newBalance = currentBalance + (currentBalance * .04 / 12);

            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if(currentBalance < minSavings)
        {
            isServiceCharge();
        }
    }

    private void calculateCheckingBalance()
    {
        if (currentBalance > 6000)
        {
            double newBalance = currentBalance + (currentBalance * .03 / 12);
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if (currentBalance >= minChecking)
        {
            double newBalance = currentBalance + (currentBalance * .05 / 12);
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if(currentBalance < minChecking)
        {
            isServiceCharge();
        }
    }

    public void isServiceCharge()
    {
        if(accountType.equals("s") || accountType.equals("S"))
        {
            double newBalance = currentBalance - 10.0;
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if(accountType.equals("c") || accountType.equals("C"))
        {
            double newBalance = currentBalance - 25.0;
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
    }
}

Ответы [ 2 ]

0 голосов
/ 26 сентября 2018

Это небольшая проблема

Если вы видите эту функцию, вы меняете тип учетной записи на «Экономия» или «Проверка» .

public void calculateNewBalance()
   {
    if (accountType.equals("S") || accountType.equals("s"))
    {
        accountType = "Savings";
        calculateSavingsBalance();

    } else if (accountType.equals("C") || accountType.equals("c"))
    {
        accountType = "Checking";
        calculateCheckingBalance();
    }


}

Покавы сравниваете accountType с 'c' или 's' в

public void isServiceCharge()
{
    if(accountType.equals("s") || accountType.equals("S"))
    {
        double newBalance = currentBalance - 10.0;
        JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
    }
    else if(accountType.equals("c") || accountType.equals("C"))
    {
        double newBalance = currentBalance - 25.0;
        JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
    }

}

По этой причине он не входит ни в один из блоков, поэтому, если вы измените условия на следующиезаявления

if(accountType.equals("Savings"))

else if(accountType.equals("Checking"))

Это будет работать, как вы ожидали

0 голосов
/ 26 сентября 2018

что происходит на самом деле в вашем коде,

public void calculateNewBalance()
{
    if (accountType.equals("S") || accountType.equals("s"))
    {
        accountType = "Savings";
        calculateSavingsBalance();

    } else if (accountType.equals("C") || accountType.equals("c"))
    {
        accountType = "Checking";
        calculateCheckingBalance();
    }


}

для s или S accountType = "Savings";

для c или C accountType = "Проверка";

, но интересно в isServiceCharge () метод

public void isServiceCharge()
{
    if(accountType.equals("s") || accountType.equals("S"))
    {
        double newBalance = currentBalance - 10.0;
        JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
    }
    else if(accountType.equals("c") || accountType.equals("C"))
    {
        double newBalance = currentBalance - 25.0;
        JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
    }

}

вы проверили

accountType.equals("s") || accountType.equals("S") //for savings
accountType.equals("C") || accountType.equals("c")// for checking

, поэтому вышеуказанное условие никогда не будет соответствовать.

, поэтому решение:

public void isServiceCharge()
{
    if(accountType.equals("Savings"))
    {
        double newBalance = currentBalance - 10.0;
        JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
    }
    else if(accountType.equals("Checking"))
    {
        double newBalance = currentBalance - 25.0;
        JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
    }

}
...