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

标签 java debugging

线程“main”中的异常 java.lang.Error: Unresolved 编译问题: 类型数据库中的方法 add(BankAccount) 不适用于 BankAccount.TestBankAccount.main(TestBankAccount.java:92) 的参数(void)

第 92 行是第 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;
    }
}

最佳答案

嗯,这是你的Database

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) );

没有任何意义,您实际上是在尝试将 void 作为参数传递给等待 BankAccount 类型的方法

关于java - 调试java银行账户程序(1个错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554558/

相关文章:

java - IntelliJ 查找用法不再提示搜索基本方法

delphi - 为什么调试器在 for-in 循环中单步执行析构函数?

debugging - 您可以从Visual Studio 2012直接调试到Microsoft Surface(或任何Windows 8 RT平板电脑)吗?

java - Eclipse - 在 Android 应用程序上引发未处理的异常时中断用户代码

java - 为 JPanel 创建构造函数来创建圆圈?

java - 如何从 XML 文件中读取数据并将其存储到数据库 (MySQL)?

java - 使用Java接口(interface)

java - 为什么对象没有更新

android - 如何调试proguard

iphone - 应该如何使用dispatch_debug?