This question already has an answer here:
Doing an assignment for my university class with a BankAccount class and a BankAccountClient class. One of the things I need to do is "Add a constructor that the client can use to initialize the name and balance. Make sure the balance cannot be initialized with any negative value." I have added the constructor but I am having problems on making sure the balance cannot be initialized with a negative value.
I've thought about trying an if method inside the constructor but can't think of a way of writing it.
public class BankAccount {
private String name;
private double balance;
public BankAccount() {
this.name = null;
this.balance = 0;
}
public BankAccount(String name, double balance) {
this.name = name;
this.balance = balance;
}
}
public class BankAccountClient {
public static void main(String[] args) {
BankAccount ba = new BankAccount("John", 1000);
}
}
Aucun commentaire:
Enregistrer un commentaire