변수 이름이 sort 인 CustomerAccount 유형의 내 ArrayList에 Account 유형의 변수 c1 및 c2를 추가하는 데 문제가 있습니다. 문제는 계정 유형에 c1 및 c2를 지정하고 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: ";
}
}
왜 당신이 그 (것)들을 추가하기 위해 Account's'로'CustomerAccount's을 설정해야 할 생각 : 그것은 다른 방법으로 주위에 있었다면
CustomerAccount
이Account
이 확장하기 때문에, 그것은 작동합니다 'ArrayList'? –
그냥 문제가 있음을 나타냅니다. 또한 c1 및 c2는 계정 유형이어야합니다. "계좌 - c1과 c2를 모두 CustomerAccount 유형의 배열에 저장하고 잔액 금액에 따라 배열을 정렬하십시오." – Devin
그럼, 'c1'과'c2'를 'CustomerAccount'로 다시 입력하면됩니다. 'sort.add ((CustomerAccount) c1);'좀 더 일반적인'Account'가'CustomerAccount'와 호환되지 않는다는 것을 증명하기 위해 연습의 요점이 될 수도 있지만, 당신은 무차별 대항 할 수 있습니다. 당신이 알고있는 타입 캐스트로 성공할 것입니다. –