调试Java银行账户程序(1个错误)

3

主线程中出现异常: java.lang.Error: 未解决的编译问题: 在BankAccount.TestBankAccount.main(TestBankAccount.java:92)处,类型Database中的方法add(BankAccount)对参数(void)不适用

第92行是在case 3下面的db.add语句(.add被标记为错误)。

package BankAccount;

import java.text.DateFormat;
import java.util.Date;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.util.ArrayList;



public class TestBankAccount 
{
    public static void main(String args[])
    {
        Database db = new Database();  // Creating database for active accounts
        Database close = new Database();   // Creating database for inactive accounts  
        DateFormat Df = DateFormat.getDateInstance(DateFormat.LONG);
        Date now = new Date();
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        boolean done = false;

        while (!done)
        {
            int menu = GetData.getInt("\tUnited Bank of Java\n" + "\t" + Df.format(now) + "\n"
                    + "\nPlease Choose From the Following:" + "\n1. Create New Account\n2. Update Existing Account Account "+ "\n3. Close an Account\n4. View Account Information\n5. Exit");
            switch(menu)
            {
                case 1: //Creating a BankAccount object and storing it in the database
                    // Creating Name object
                    String f = GetData.getString("Enter First Name") ;
                    String l = GetData.getString("Enter Last Name") ;
                    Name n = new Name(f,l);

                    // Creating Address object
                    String str = GetData.getString("Enter Street Name") ;
                    String city = GetData.getString("Enter City") ;
                    String st = GetData.getString("Enter State") ;
                    String zip = GetData.getString("Enter Zip") ;

                    // Creating Customer object
                    Address addr = new Address(str,city,st,zip);
                    String accNo = GetData.getString("Enter Account Number") ;
                    Customer c = new Customer(n,accNo,addr);    

                    // Creating BankAccount object
                    double amount = GetData.getDouble("Enter First Deposit") ;
                    BankAccount ba = new BankAccount(c, amount);

                    // Add BankAccount object to the database
                    db.add(ba);
                    break;
                case 2: //Update Account
                    accNo = GetData.getString("Enter Account Number of Account you'd like to update")  ;
                    db.search(accNo);
                    if (!db.inList())
                        JOptionPane.showMessageDialog(null, "Account not found.");
                    else
                    {
                        int option = GetData.getInt("Would you like to (1) Deposit, (2) Withdraw");
                        switch(option)
                        {
                        case 1:
                            double amt = GetData.getDouble("Enter amount you'd like to deposit");
                            BankAccount b = db.getAccount();
                            b.deposit(amt);
                            break;
                        case 2:
                            double amnt = GetData.getDouble("Enter amount you'd like to withdraw") ;
                            BankAccount bnk = db.getAccount();
                            if (!bnk.isSufficient(amnt))
                                JOptionPane.showMessageDialog(null, "Insufficient funds, withdrawal cannot be done.");
                            else
                                bnk.withdraw(amnt);
                            break;
                        default:             
                            JOptionPane.showMessageDialog(null, "Invalid selection. To return to main menu, please deposit or withdraw $0");
                            break;
                        }
                    }
                    break;
                case 3: //Close Account
                    accNo = GetData.getString("Cose account - Please enter Account No.)");
                    db.search(accNo);
                    if (!db.inList())
                        JOptionPane.showMessageDialog(null, "Account not found.");
                    else
                    {
                        BankAccount b = db.getAccount();
                        int index = db.getIndex();
                        db.add( db.delete(index) );
                        JOptionPane.showMessageDialog(null, "The Account " + accNo + " has been closed.");
                    }
                    break;
                case 4: //View Account
                    int view = GetData.getInt("What information would you like to view?\n1. Single account\n2. All active accounts\n3. All inactive accounts\n");

                    switch(view)
                    {
                    case 1:  // View a single account
                        accNo = GetData.getString("View – account. Please enter Account No.");
                        db.search(accNo);                   
                        if(!db.inList())
                            JOptionPane.showMessageDialog(null, "Account not found.");
                        else
                        {
                            BankAccount bb = db.getAccount();
                            String s = "Customer\t" + bb.getCustomer().getName().getFirst() + "\t" + bb.getAmount() ;
                            JOptionPane.showMessageDialog(null, s, "Bank Account " + bb.getCustomer().getAccountNumber(), JOptionPane.INFORMATION_MESSAGE); 
                        }
                        break;
                    case 2: // View all active accounts
                        ArrayList list = db.getList();
                        if(list.isEmpty())
                            JOptionPane.showMessageDialog(null, "List is empty");
                        else
                        {
                            int i = 0, length = db.getSize();
                            String s = "";
                            while(i < length)
                            {
                                BankAccount b = (BankAccount)list.get(i);
                                s = s + "Customer Name: " + b.getCustomer().getName().getFirst() + "  " + b.getCustomer().getName().getLast() + "\nAccount number: " + b.getCustomer().getAccountNumber() + "\n"
                                        + b.getCustomer().getAddress().getStreet() + ", " + b.getCustomer().getAddress().getCity() + ", " +  b.getCustomer().getAddress().getState() + ", " 
                                        + b.getCustomer().getAddress().getZip() + "\n" + nf.format(b.getAmount()) + 
                                        "\n";
                                i++;
                            }
                            display(s, "Active Accounts", JOptionPane.INFORMATION_MESSAGE);
                        }
                        break;
                    case 3: // View all closed accounts
                        ArrayList closed = db.getList();

                        if(closed.isEmpty())
                            JOptionPane.showMessageDialog(null, "List is empty");
                        else
                        {
                            int i = 0, length = db.getSize();
                            String s = "";
                            while(i < length)
                            {
                                BankAccount b = (BankAccount)closed.get(i);
                                s = s + "Name " + b.getCustomer().getName().getFirst() + "  " + b.getCustomer().getName().getLast() + "\tAccount number: " + b.getCustomer().getAccountNumber() + "\n";
                                i++;
                            }
                            display(s, "Closed Accounts", JOptionPane.INFORMATION_MESSAGE);
                        }
                        break;
                    default:
                        JOptionPane.showMessageDialog(null, "Invalid option.");
                        break;
                    }// End view
                    break;
                case 5: //Exit 
                    done = true;    
                    break;
                default:    
                    JOptionPane.showMessageDialog(null, "Account not found.");
                    break;   
            }
        }
    }
    static void display(String s, String heading, int MESSAGE_TYPE)
    {
        JTextArea text = new JTextArea(s, 20, 30);
        JScrollPane pane = new  JScrollPane(text);
        JOptionPane.showMessageDialog(null, pane, heading, MESSAGE_TYPE);
    }
}

package BankAccount;
import java.util.ArrayList;
import java.util.Scanner;
class Database 
{
    ArrayList<BankAccount> list;
    BankAccount ba;
    int index;
    boolean found;

    Database()
    {
        list = new ArrayList<BankAccount>();
    }
    void search(String key)  
    {
        found = false;
        int i = 0;

        while(!found && i < list.size())
        {
            BankAccount b = list.get(i);
            if(b.getCustomer().getAccountNumber().equalsIgnoreCase(key))
            {
                ba = b;
                found = true;
                index =i;
            }
            else 
                i++;
        }
    }
    void add(BankAccount b) { list.add(b); }
    void delete(int i) { list.remove(i); }
    int getIndex(){ return index; }
    boolean inList(){  return found; }
    BankAccount getAccount() { return ba; }
    boolean isInList() { return found; }
    int size()  { return list.size(); }
    boolean isEmpty()  { return list.isEmpty(); }
    public ArrayList getList() {
        Scanner readBoard = new Scanner(System.in);
        return null;
    }
    public int getSize() {
        Scanner readBoard = new Scanner(System.in);
        return 0;
    }
}

db.delete(index) 的返回类型是 void - Michael
@Danny 如果我的回答有帮助到你,请随意将其标记为正确的答案。 - Luigi Cortese
1个回答

3

好的,这是你的数据库类。

class Database {

    ...

    void add(BankAccount b) { list.add(b); }

    void delete(int i) { list.remove(i); }

    ...
}

显然,要做到这一点。
Database db = new Database();
...
db.add( db.delete(index) );

这样做没有任何意义,你实际上正在尝试将一个空类型作为参数传递给一个需要 BankAccount 类型的方法


如果您不想向您的“数据库”对象添加任何内容,只需将“db.add(db.delete(index));”更改为“db.delete(index)”。 - Luigi Cortese

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接