类之间的初始化错误

时间:2014-10-21 20:28:38

标签: java class variables object

当我放置检查和保存对象并尝试在另一个类中调用它时,它实际上不起作用,并且它给我一个语法错误。我在下面粘贴了我的java代码。无论出于何种原因,帐户都有问题。有什么我应该改变以避免出现这些错误吗?

给出的错误是: 线程“main”中的异常java.lang.Error:未解决的编译问题:     SavingsAccount无法解析为某种类型     SavingsAccount无法解析为某种类型     客户无法解析为某种类型     银行无法解决     accountentered无法解析为变量     accountentered无法解析为变量     transAccount无法解析为变量     transAccount无法解析为变量     SavingsAccount无法解析为某种类型     SavingsAccount无法解析为某种类型     客户无法解析为某种类型     客户无法解析为某种类型     重复的局部变量     CreditAccount无法解析为某种类型     CreditAccount无法解析为某种类型     重复的局部变量fi​​rstName     重复的局部变量lastName     重复的局部变量pnumber     客户无法解析为某种类型     重复的局部变量newCustomer     客户无法解析为

类型
at ProjectBank.main(ProjectBank.java:25)

import java.io.InputStream;
import java.util.Scanner;

public class CustomerDemo
{

    class Customer
    {
        //two objects/ saving and checking
        Account Saving = new Account();
        Account Checking = new Account();
        public Customer(InputStream in)
        {
            // TODO Auto-generated constructor stub
        }
        boolean deposit(double amount, String acc)
        {
            double currentBalanceChecking, currentBalanceSaving;// this currentBalence is subtracted from and account.
            boolean retVal = false;
            switch (acc)
            {
            case "Checking":
                if(amount >=0)
                {
                    currentBalanceChecking = this.Checking.deposit(amount);
                    retVal = true;
                }
                break;
            case "Saving":
                if(amount >=0)
                {
                    currentBalanceSaving = this.Saving.deposit(amount);
                    retVal = true;
                }
                break;
            default:
                System.out.println("You entered an invalid number.");
            }
            return retVal;
        }
        boolean withdraw(double amount, String acc)
        {
            double currentBalanceChecking, currentBalanceSaving;// this currentBalence is subtracted from and account.
            boolean retVal = false;
            switch (acc)
            {
            case "Checking":
                if(amount >=0)
                {
                    currentBalanceChecking = this.Checking.withdraw(amount);
                    retVal = true;
                }
                break;
            case "Saving":
                if(amount >=0)
                {
                    currentBalanceSaving = this.Saving.withdraw(amount);
                    retVal = true;
                }
                break;
            default:
                System.out.println("You entered an invalid number.");
            }
            return retVal;
        }
        boolean transfer(double amount, String acc)
        {
            double currentBalanceChecking, currentBalanceSaving;// this currentBalence is subtracted from and account.
            boolean retVal = false;

            switch (acc)
            {
            case "Checking":
                if(amount >=0)
                {
                    currentBalanceChecking = this.Checking.withdraw(amount);
                    currentBalanceSaving = this.Checking.deposit(amount);
                    retVal = true;
                }
                break;
            case "Saving":
                if(amount >=0)
                {
                    currentBalanceSaving = this.Checking.withdraw(amount);
                    currentBalanceChecking = this.Checking.deposit(amount);
                    retVal = true;
                }
                break;
            default:
                System.out.println("You entered an invalid number.");
            }
            return retVal;
        }
    }
    void printBalance()
    {
    }
}
class Account
{
    double balance;
    //the constructor tells the customer that there are zero dollars in the account.
    Account()
    {
        balance = 0;
    }
    //deposit money
    double deposit( double depAmount )
    {
        balance= balance + depAmount;//balance+ = depAmount
        return balance;
    }
    double withdraw( double withAmount )
    {
        balance= balance - withAmount;//balance- = withAmount
        return balance;
    }
    double transferToSaving(double transAmount)
    {
        Checking.balance = balance - transAmount;
        Saving.balance = balance + transAmount;
    }

    double transferToChecking(double transAmount)
    {
        Checking.balance = balance - transAmount;
        Saving.balance = balance + transAmount;
    }
    double getBalance()
    {
        return balance;
    }
}

class run
{
    public void main(String[] args)
    {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        Customer customer = new Customer(System.in);
        int accountChoice; // show which account needs to be chosen.
        String cusSel; //for customer selection.
        double money;
        do
        {
            System.out.println("main menu to be selected: " );
            System.out.println("1.deposit " );
            System.out.println("2.withdraw " );
            System.out.println("3.transfer " );
            System.out.println("4.print balance " );
            System.out.println("q.quit " );
            cusSel=in.next();
            switch(cusSel.charAt(0))
            {
            case '1':
                System.out.println("please select account: " );
                System.out.println("1. Checking" );
                System.out.println("2. Saving " );
                accountChoice=in.nextInt();
                if((accountChoice==1)||(accountChoice==2))
                {
                    System.out.println("please imput the deposit amount: " );
                    money=in.nextDouble();
                    if(accountChoice==1)
                    {
                        customer.deposit(money, "Checking");
                    }
                    else if(accountChoice==2)
                    {
                        customer.deposit(money, "Saving");
                    }
                }
                else
                {
                    System.out.println("invalid choice. your choice does not exsist");
                    break;
                }
                break;

            case '2':

                System.out.println("Please select account: " );
                System.out.println("1. Checking" );
                System.out.println("2. Saving " );

                accountChoice=in.nextInt();
                if((accountChoice==1)||(accountChoice==2))
                {
                    System.out.println("please input the withdraw amount: " );

                    money=in.nextDouble();


                    if(accountChoice==1)
                    {
                        customer.withdraw(money, "Checking");
                    }
                    else if(accountChoice==2)
                    {
                        customer.withdraw(money, "Saving");
                    }
                }
                else
                {
                    System.out.println("invalid choice. your choice does not exsist");
                    break;
                }
                break;

            case '3':

                System.out.println("please select an account to transfer from: " );
                System.out.println("1. Checking" );
                System.out.println("2. Saving " );

                accountChoice=in.nextInt();
                if((accountChoice==1)||(accountChoice==2))
                {
                    System.out.println("please input the withdraw amount: " );

                    money=in.nextDouble();

                    if(accountChoice==1)
                    {
                        customer.transfer(money, "saving");
                    }
                    else if(accountChoice==2)
                    {
                        customer.transfer(money, "checking");
                    }
                }
                else
                {
                    System.out.println("invalid choice. your choice does not exsist");
                    break;
                }
                break;

            case '4':
                customer.printBalance();
                break;
            case 'q':
                System.out.println("transaction complete, please have a nice day");
                break;
            default:
                System.out.println("invalid choice.");

            }

        } while (cusSel.charAt(0)!= 'q' && cusSel.charAt(0)!= 'Q');

    }
    private void getAccountChoice(int accountChoice) {
        // TODO Auto-generated method stub
    }

}

1 个答案:

答案 0 :(得分:0)

您收到错误是因为您在客户类中声明了对象,并且您尝试从帐户访问它们作为本地变量。

您遇到此错误的原因是您的类结构。类只应具有与您的类的单个实例直接相关的方法和字段。例如,在您的情况下,帐户类具有"余额"这是100%正确,然后它有像" transferToSavings()" (甚至不使用该变量)与CONNECTION(在这种情况下是客户,或通常是交易)相关,在支票账户和储蓄账户之间而不是账户本身。

转移的基本结构应该是这样的:

Customer{
   Account Saving = new Account();
   Account Checking = new Account();

   //you have something similar with input control
   void tranferFunds(Account from, Account to, double amount){

      from.Withdraw(amount);
      to.Deposit(amount);
   }

}

class Account
{
    double balance;
    Account()
    {
        balance = 0;
    }
    void deposit( double amount)
    {
        balance+=  amount;
    }
    void withdraw( double amount)
    {
        balance-=  amount;
    }
    double getBalance()
    {
        return balance;
    }
}

修改:我注意到您已经在Customer中使用了transfer()方法,该方法应该可以正常工作,因此您不必更改您的客户类别。

Edit2:它无法解析客户,因为您的客户类位于CustomerDemo类中。您可以一起删除CustomerDemo类(附带括号)

编辑3:这里调试了您的代码,没有使用account.transferTo / from方法。我使用在线编译器,所以我没有测试输入,但没有更多的语法错误:

import java.io.InputStream;
import java.util.Scanner;

public class run
{
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        Customer customer = new Customer(System.in);
        int accountChoice; // show which account needs to be chosen.
        String cusSel; //for customer selection.
        double money;
        do
        {
            System.out.println("main menu to be selected: " );
            System.out.println("1.deposit " );
            System.out.println("2.withdraw " );
            System.out.println("3.transfer " );
            System.out.println("4.print balance " );
            System.out.println("q.quit " );
            cusSel=in.next();
            switch(cusSel.charAt(0))
            {
            case '1':
                System.out.println("please select account: " );
                System.out.println("1. Checking" );
                System.out.println("2. Saving " );
                accountChoice=in.nextInt();
                if((accountChoice==1)||(accountChoice==2))
                {
                    System.out.println("please imput the deposit amount: " );
                    money=in.nextDouble();
                    if(accountChoice==1)
                    {
                        customer.deposit(money, "Checking");
                    }
                    else if(accountChoice==2)
                    {
                        customer.deposit(money, "Saving");
                    }
                }
                else
                {
                    System.out.println("invalid choice. your choice does not exsist");
                    break;
                }
                break;

            case '2':

                System.out.println("Please select account: " );
                System.out.println("1. Checking" );
                System.out.println("2. Saving " );

                accountChoice=in.nextInt();
                if((accountChoice==1)||(accountChoice==2))
                {
                    System.out.println("please input the withdraw amount: " );

                    money=in.nextDouble();


                    if(accountChoice==1)
                    {
                        customer.withdraw(money, "Checking");
                    }
                    else if(accountChoice==2)
                    {
                        customer.withdraw(money, "Saving");
                    }
                }
                else
                {
                    System.out.println("invalid choice. your choice does not exsist");
                    break;
                }
                break;

            case '3':

                System.out.println("please select an account to transfer from: " );
                System.out.println("1. Checking" );
                System.out.println("2. Saving " );

                accountChoice=in.nextInt();
                if((accountChoice==1)||(accountChoice==2))
                {
                    System.out.println("please input the withdraw amount: " );

                    money=in.nextDouble();

                    if(accountChoice==1)
                    {
                        customer.transfer(money, "saving");
                    }
                    else if(accountChoice==2)
                    {
                        customer.transfer(money, "checking");
                    }
                }
                else
                {
                    System.out.println("invalid choice. your choice does not exsist");
                    break;
                }
                break;

            case '4':
                customer.printBalance();
                break;
            case 'q':
                System.out.println("transaction complete, please have a nice day");
                break;
            default:
                System.out.println("invalid choice.");

            }

        } while (cusSel.charAt(0)!= 'q' && cusSel.charAt(0)!= 'Q');

    }
    private void getAccountChoice(int accountChoice) {
        // TODO Auto-generated method stub
    }

}

class Customer
{
    //two objects/ saving and checking
    Account Saving = new Account();
    Account Checking = new Account();
    public Customer(InputStream in)
    {
        // TODO Auto-generated constructor stub
    }
    boolean deposit(double amount, String acc)
    {
        double currentBalanceChecking, currentBalanceSaving;// this currentBalence is subtracted from and account.
        boolean retVal = false;
        switch (acc)
        {
        case "Checking":
            if(amount >=0)
            {
                currentBalanceChecking = this.Checking.deposit(amount);
                retVal = true;
            }
            break;
        case "Saving":
            if(amount >=0)
            {
                currentBalanceSaving = this.Saving.deposit(amount);
                retVal = true;
            }
            break;
        default:
            System.out.println("You entered an invalid number.");
        }
        return retVal;
    }
    boolean withdraw(double amount, String acc)
    {
        double currentBalanceChecking, currentBalanceSaving;// this currentBalence is subtracted from and account.
        boolean retVal = false;
        switch (acc)
        {
        case "Checking":
            if(amount >=0)
            {
                currentBalanceChecking = this.Checking.withdraw(amount);
                retVal = true;
            }
            break;
        case "Saving":
            if(amount >=0)
            {
                currentBalanceSaving = this.Saving.withdraw(amount);
                retVal = true;
            }
            break;
        default:
            System.out.println("You entered an invalid number.");
        }
        return retVal;
    }
    boolean transfer(double amount, String acc)
    {
        double currentBalanceChecking, currentBalanceSaving;// this currentBalence is subtracted from and account.
        boolean retVal = false;

    switch (acc)
    {
    case "Checking":
        if(amount >=0)
        {
            currentBalanceChecking = this.Checking.withdraw(amount);
            currentBalanceSaving = this.Checking.deposit(amount);
            retVal = true;
        }
        break;
    case "Saving":
        if(amount >=0)
        {
            currentBalanceSaving = this.Checking.withdraw(amount);
            currentBalanceChecking = this.Checking.deposit(amount);
            retVal = true;
        }
        break;
    default:
        System.out.println("You entered an invalid number.");
    }
    return retVal;
    }
    void printBalance()
    {
    }
    }


class Account
{
    double balance;
    //the constructor tells the customer that there are zero dollars in the account.
    Account()
    {
        balance = 0;
    }
    //deposit money
    double deposit( double depAmount )
    {
        balance= balance + depAmount;//balance+ = depAmount
        return balance;
    }
    double withdraw( double withAmount )
    {
        balance= balance - withAmount;//balance- = withAmount
        return balance;
    }

    double getBalance()
    {
        return balance;
    }
}