Hallo! Ich brauche ein bisschen hilfe mit diesem Programm hier.Also zuerst habe ich diese Account Klasse wo ich Exceptions werfven soll die ich gemacht habe.
Und jetzt muss ich auch eine "NoSuchAccountException" werfen, wenn eine Kontonummer nicht existiert.Welche Funktion bzw. Methode soll diese Exception werfen? Hier ist die Exception
Java:
package banking;
import io.In;
import io.Out;
import java.io.IllegalArgumentException;
class Account {
final int accountNo;
final Customer owner;
final double overdraft;
double balance = 0;
private Account(int accountNo, Customer owner, double overdraft) {
this.accountNo = accountNo;
this.owner = owner;
this.overdraft = overdraft;
}
void print() {
Out.println("Kontonummer: " + accountNo);
owner.print();
Out.format("Kontostand: %.2f%nÜberziehungsrahmen: %.2f%n",
balance, overdraft);
}
public boolean deposit(double amount) throws IllegalArgumentException {
if (amount <= 0) {
throws new IllegalArgumentException("Cannot deposit negative amounts!");
break;
} else {
balance += amount;
return true;
}
public boolean withdraw(double amount) throws OverdraftLimitReachedException {
if (amount <= 0 || !isCovered(amount)) {
throw new OverdraftLimitReachedException("Overdraft limit has been reached",accountNo);
break;
} else {
balance -= amount;
return true;
}
boolean isCovered(double amount) {
return amount - overdraft <= balance;
}
boolean transfer(Account target, double amount) {
boolean success = withdraw(amount);
if (!success) return false;
target.deposit(amount);
return true;
}
}
Und jetzt muss ich auch eine "NoSuchAccountException" werfen, wenn eine Kontonummer nicht existiert.Welche Funktion bzw. Methode soll diese Exception werfen? Hier ist die Exception
Java:
package banking;
public class NoSuchAccountException extends AccountException {
public NoSuchAccountException(int accountNo) {
super(message,accountNo);
}
}