変数の名前sortを使用して、タイプAccountの変数c1とc2をタイプCustomerAccountのArrayListに追加する際に問題があります。この問題は、アカウントタイプにc1とc2を割り当て、ArrayListソートをタイプCustomerAccountに設定することを示しています。 CustomerAccountとTransactionはどちらもAccountクラスのサブクラスです。 mainメソッド内のループについては、以下のサブクラス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: ";
}
}
なぜあなたは、あなたがそれらを追加するためにAccount's 'にあなたの' CustomerAccount'sを有効にする必要がありだと思います。それは他の方法で回避した場合
CustomerAccount
がAccount
が伸びるのでは、それが働くだろう'ArrayList'? –
それは、問題にそれだけ述べています。また、c1とc2がAccountタイプである必要があることも記載されています。 "c1とc2の両方のアカウントをCustomerAccount型の配列に格納し、残高量に応じて配列を に並べ替えます。 – Devin
次に、' c1'と 'c2'を' CustomerAccount'にタイプキャストするだけです。 'sort.add((CustomerAccount)c1);'より一般的な 'Account'が' CustomerAccount'と互換性がないことを実証するために、それは練習の要点かもしれませんが、あなたはブルートフォースそれはあなたが成功することを知っているタイプキャストとそれ。 –