0
    import java.text.DecimalFormat;
        import java.util.ArrayList;

        public class AccountOwner {
            private Account account;
            private String firstname;
            private String lastname;
            private String address;
            private double currentBalance;
            private ArrayList<Integer> withdrawAmount = new ArrayList<Integer>(5);
            private ArrayList<Double> deposits = new ArrayList<Double>();
            private ArrayList<Double> purchases = new ArrayList<Double>(5);
            private DecimalFormat formatter = new DecimalFormat("##0.00"); 

            public AccountOwner(String firstname, String lastname, String address) {
                this.firstname = firstname;
                this.lastname = lastname;
                this.address = address;
            }

            public String getFirstName() {
                return firstname;
            }

            public String getLatName() {
                return lastname;
            }

            public String getAddress() {
                return address;
            }


            public String checkBalance() {

                for (Double deposit : deposits) {
                    this.currentBalance += deposit;
                }

                return formatter.format(currentBalance);
            }

            public void makeDeposit(double amount) {
                deposits.add(amount);
            }

            public void viewAllDeposits() {

                double allDeposits = 0.0;
                System.out.println("All deposits till today:");
                for (int i = 0; i < deposits.size(); i++) {
                    allDeposits = deposits.get(i);

                    System.out.print("\t"+"$"+allDeposits);
                }
                System.out.println();
            }

            public void withdrawMoney(int amount) {
                withdrawAmount.add(amount);
                currentBalance -= amount;
            }

            public String getActualBalance() {
                return formatter.format(currentBalance);
            }

            public void withdrawAmounts(){
                System.out.println("All Withdrawls till today");
                for(int i = 0; i < withdrawAmount.size(); i++){
                    System.out.print("\t"+"$"+withdrawAmount.get(i));
                }
                System.out.println();
            }

            public void makePurchase(double itemPrice){
                purchases.add(itemPrice);
                this.currentBalance -= itemPrice;
            }

            public void viewAllmadePurchases() {

                double purchase = 0.0;
                System.out.println("All purchases made till today:");
                for (int i = 0; i < purchases.size(); i++) {
                    purchase = purchases.get(i);

                    System.out.print("\t"+"$"+purchase);
                }

            }

            public void viewMyPersonalInformation(){
                System.out.println("Firstname:" + this.firstname);
                System.out.println("LastName:" +this.lastname);
                System.out.println("Address:" +this.address);
                System.out.println("Balance:" +this.checkBalance());
                viewAllDeposits();
                withdrawAmounts();
                viewAllmadePurchases();

            }



        public class Account {
            private AccountOwner customer;
            private int accountNumber;


            public Account(){
                customer = null;
                accountNumber = 0000000;


            }

            public Account(int accountNumber, AccountOwner owner){
                       this.accountNumber = accountNumber;
                       customer = owner;


            }

            public int accountNumberIs(){
                return accountNumber;
            }

        public class BankManager {
            private Account account;
            private AccountOwner accountOwner;
            private int accountNumber;


                public void createNewAccount(int accountNumber, AccountOwner owner){
                account = new Account(accountNumber, owner);
                this.accountNumber = accountNumber;
                this.accountOwner = owner;
                }



            public int getaccountNumber(){

                return accountNumber;
            }


            public void setAccountNumber(int newaccountnumber){
                accountNumber = newaccountnumber;
            }

            public void customerInformation(){
                accountOwner.viewMyPersonalInformation();
            }
public class MainProgram {

    /**
     * @param args
     */
    public static void main(String[] args) {
        BankManager manager = new BankManager();
        AccountOwner owner = new AccountOwner("Tom", "Smith", "208 W 119th St");
        manager.createNewAccount(389745, owner);
        Account acc = new Account();





        owner.makeDeposit(550);
        owner.makeDeposit(700);
        owner.makeDeposit(122.93);
        owner.makeDeposit(195.93);
        owner.withdrawMoney(200);
        owner.makePurchase(200);
        owner.makeDeposit(100);
        owner.makePurchase(1220);
        owner.withdrawMoney(30);



        owner.viewMyPersonalInformation();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println(acc.accountNumberIs());

私が抱えている問題は、bankmanager クラスへの参照を使用せずに、口座所有者から口座番号にアクセスしようとしていることです。どうすれば機能させることができますか。コンストラクターを作成し、それらのパラメーターをアカウント クラスのフィールドに割り当てるため、アカウント クラス自体を使用しようとしましたが、機能しないようです

4

3 に答える 3

1

このコードにはバグが含まれています:

public Account(int accountNumber, AccountOwner owner){
    AccountOwner cstomer = owner;
    int acctNumber = accountNumber;
    accountNumber = acctNumber;
    //System.out.println(accountNumber);
}

コンストラクターに渡すパラメーターは、クラスのフィールドaccountNumberよりも優先されます。accountNumberあなたAccountaccountNumberフィールドが実際に設定されることはありません。

これは次と同等です。

public Account(int accountNumber, AccountOwner owner){
    AccountOwner cstomer = owner;
    accountNumber = accountNumber;
}

フィールドにアクセスしていることを確認するには、次のようにthisキーワードを使用します。this.accountNumber

public Account(int accountNumber, AccountOwner owner){
    AccountOwner cstomer = owner;
    this.accountNumber = accountNumber;
    //System.out.println(accountNumber);
}

別のバグがあり、Account.customer割り当てられていません:

public Account(int accountNumber, AccountOwner owner){
    customer = owner;
    this.accountNumber = accountNumber;
}

デバッガーの使用方法を学習するか、単体テストをより適切に作成することをお勧めします (最初に)。

于 2013-03-15T14:43:04.927 に答える
0

accountNumber型をintに設定し、それを0000000(int型の場合は0に等しい)に設定したため、getAccountNumber()メソッドを呼び出すと0になります。

代わりに、accountNumberタイプをStringに変更し、「0000000」に初期化します。その場合、0000000が印刷されます。

于 2013-03-15T14:44:16.433 に答える
0
class AccountOwner {
//...
    private Account account;
//...
    public Integer getAccountNumber() {
        return this.account != null ? this.account.accountNumberIs() : null;
    }
}
于 2013-03-15T14:41:27.407 に答える