2017-09-30 44 views
1

我遇到問題,將類型Account的變量c1和c2添加到類型爲CustomerAccount的變量名稱爲Array的ArrayList中。問題說明將c1和c2分配給Account類型,並將ArrayList排序設置爲CustomerAccount類型。 CustomerAccount和Transaction都是Account類的子類。將父類的對象添加到子類ArrayList

下方爲主要方法循環,​​我試圖做:

sort.add(c1); 

但收到以下錯誤。

The method add(CustomerAccount) in the type ArrayList<CustomerAccount> is not applicable for the arguments (Account) 

CustomerAccount.java:

package questionOne; 

import java.util.ArrayList; 
//import java.util.Arrays.sort; 
import java.util.Date; 

public class CustomerAccount extends Account { 

public static String name; 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    ArrayList<Transaction> transactions = new ArrayList<Transaction>(); 
    CustomerAccount george = new CustomerAccount("George", 1122, 1000.0); 
    george.setannualInterest(1.5); 
    Account c1 = george; 

    george.deposit(30); 
    george.deposit(40); 
    george.deposit(50); 

    george.withdraw(5); 
    george.withdraw(4); 
    george.withdraw(2); 

    System.out.println(c1.toString()); 

    transactions = george.getTransactions(); 
    for (int i=0; i < transactions.size(); i++) { 
     System.out.print("type: " + (transactions.get(i).getType())); 
     System.out.print(" amount: " + (transactions.get(i).getAmount())); 
     System.out.print(" balance: " + (transactions.get(i).getBalance())); 
     System.out.print((transactions.get(i).getDescription())); 
     System.out.println(""); 
    } 

    CustomerAccount john = new CustomerAccount("John", 1123, 500); 
    john.setannualInterest(2.5); 
    Account c2 = john; 

    ArrayList<CustomerAccount> sort = new ArrayList<CustomerAccount>(); 
    sort.add((CustomerAccount) c1); 
    sort.add(c2); 

} 

CustomerAccount(String name, int id, double balance) { 
    super(id, balance); 
    this.name = name; 
} 

} 

class Transaction extends Account { 
    private java.util.Date dateCreated; 
    private char type; 
    private double amount; 
    private double balance; 
    private String description; 
    private double transaction; 

Transaction() { 

} 

Transaction(char type, double amount, double balance, String description) { 
    dateCreated = new java.util.Date(); 
    this.type = type; 
    this.amount = amount; 
    this.balance = balance; 
    this.description = description; 
} 

public String getDateCreated() { 
    return dateCreated.toString(); 
} 

public char getType() { 
    return type; 
} 

public double getAmount() { 
    return amount; 
} 

public double getBalance() { 
    return balance; 
} 

public String getDescription() { 
    return description; 
} 

public double getTransaction() { 
    return this.transaction; 
} 

public void setType(char type) { 
    this.type = type; 
} 

public void setAmount(double amount) { 
    this.amount = amount; 
} 

public void setBalance(double balance) { 
    this.balance = balance; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public void setTransaction(double amount) { 
    this.transaction = amount; 
} 
} 

Account類:

class Account { 
    protected int id = 0; 
    protected double balance = 0.0; 
    protected double annualInterestRate = 0.0; 
    private java.util.Date dateCreated; 
    ArrayList<Transaction> transactions = new ArrayList<Transaction>(); 

// set dateCreated for the time and date the account was created 
Account() { 
    dateCreated = new java.util.Date(); 
} 

// Set accounts id and balance equal to this objects 
Account(int id, double balance){ 
    this(); 
    this.id = id; 
    this.balance = balance; 
} 

// Getters and Setters manipulating variables to be set to the created account 
// Setters have no return value and set itself equal to the variable and getters 
// grab the variables and return them. 
public int getId() { 
    return this.id; 
} 

public double getBalance() { 
    return this.balance; 
} 

public double getannualInterestRate() { 
    return this.annualInterestRate; 
} 

public String getDateCreated() { 
    return this.dateCreated.toString(); 
} 

public void setId(int id) { 
    this.id = id; 
} 

public void setBalance(double balance) { 
    this.balance = balance; 
} 

public void setannualInterest(double annualInterestRate) { 
    this.annualInterestRate = annualInterestRate; 
} 

public double getMonthlyInterestRate() { 
    return (annualInterestRate/100)/12; 
} 

public double getMonthlyInterest() { 
    return balance * getMonthlyInterestRate(); 
} 

// set balance of withdraw to balance - amount = balance 
public void withdraw (double amount) { 
    if(amount < balance) { 
     balance -= amount; 
     transactions.add(new Transaction('W', amount, balance, " withdrawal ")); 
    } 
} 

// set balance of deposit to balance + amount = balance 
public void deposit(double amount) { 
    balance += amount; 
    transactions.add(new Transaction('D', amount, balance, " deposit ")); 
} 

public ArrayList<Transaction> getTransactions() { 
    return transactions; 
} 

@Override 
public String toString() { 
    return "Account holder name: " + CustomerAccount.name + 
      "\nAccount id: " + id + 
      "\nAnnual interest: " + this.getannualInterestRate() + 
      "\nMonthly Interest Rate: " + this.getMonthlyInterestRate() + 
      "\nBalance " + this.getBalance() + 
      "\nTransaction: "; 
} 
} 
+0

爲什麼你認爲你必須把你的'CustomerAccount's成'Account's,以便將它們添加到一個'ArrayList '? –

+0

它只是在問題中說明。它還指出,c1和c2需要是Account類型。 「將兩個賬戶--C1和C2存儲在CustomerAccount類型的數組中,並根據其餘額對 數組進行排序」 – Devin

+0

那麼,您只需將'c1'和'c2'輸入回'CustomerAccount'然後,像這樣:'sort.add((CustomerAccount)c1);'也許這就是練習的要點,爲了證明更一般的'Account'與'CustomerAccount'不兼容,但是你可以使用暴力它與一個你知道會成功的類型轉換。 –

回答

0

ArrayListCustomerAccountc1Account類型,因此無法添加到列表中。

如果是其他方式,它會工作,因爲CustomerAccount延伸Account

CustomerAccount george = new CustomerAccount("George", 1122, 1000.0); 
ArrayList<Account> sort = new ArrayList<>(); 
sort.add(george); 
+0

所以沒有辦法解決這個問題?除了改變ArrayList類型?因爲該問題指出:「將兩個帳戶 - c1和c2存儲在CustomerAccount類型的數組中」 – Devin

+0

如果您將'c1'強制轉換回'CustomerAccount',它將起作用。在你的代碼中,你已經完成了:'sort.add((CustomerAccount)c1);' – Mark