0

Im tried to passing 2 Objects defined (Transaction with one attribute "amount") to another method that will receive the list of Transaction

This is the main:

public class Test {

public static void main(String[] args) {    
    // Here is when the Transaction was sent twice with differents amounts
    BankAccount saving = new SavingAccount("Raul", 1000d, new Transaction(500d), new Transaction(-500d));
}

}

This is the Object Transaction

class Transaction {

    private Double amount;
    public Transaction(Double amount) {
        super();
        this.amount = amount;
    }
    public Double getAmount() {
        return amount;
    }
    public void setAmount(Double amount) {
        this.amount = amount;
    }

}

and This is the Bank class

class BankAccount {

private String name;
private Double amount;

public BankAccount(String name, Double amount) {
    super();
    this.name = name;
    this.amount = amount;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Double getAmount() {
    return amount;
}
public void setAmount(Double amount) {
    this.amount = amount;
}

}

and finally the SavingAccount will do some operations with the List of transactions

class SavingAccount extends BankAccount{
public SavingAccount(String name, Double amount, List<Transaction> transaction) {
    super(name, amount);
    // Do something with the list of transactions
}

}

My first solution was to create a List of type Transaction and pass to the method and it Works, but my solution was rejected...

public class Test {

public static void main(String[] args) {    

    List<Transaction> transaction = new ArrayList<Transaction>();
    transaction.add(new Transaction(5000d));
    transaction.add(new Transaction(-5000d));
    BankAccount saving = new SavingAccount("Raul", 1000d, transaction);
}

}

How can i do to maintain the send of that objects as parameters without define a ArrayList first?

2
  • What do you mean when you say your solution was rejected? Was there an auto-grader? Try looking up varargs, which let you basically enter as many arguments as you want (although it's really just an array, so it's not much different from you using an ArrayList) Commented Jun 11, 2020 at 21:28
  • 1
    When dealing with money, arbitrary-precision is important. So, use BigDecimal over double in such a scenario. Commented Jun 11, 2020 at 21:40

1 Answer 1

2

You can change the signature of your SavingAccount constructor to use varargs that receive how many Transaction objects that you want:

 public SavingAccount(String name, BigDecimal amount, Transaction ... transactions) {}

Or you can use simply two Transaction objects:

public SavingAccount(String name, BigDecimal amount, Transaction transaction1, Transaction transaction2) {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.