package banking;
import java.lang.IllegalArgumentException;
import io.In;
import io.Out;
class Banking {
static final int CAPACITY = 100;
final Account[] accounts = new Account[CAPACITY];
int nOfAccounts = 0;
int createAccount(String firstName, String lastName, String phone, double overdraft) throws BankServiceException {
if (nOfAccounts == CAPACITY) {
throw new BankServiceException("The maximum capacity has been reached");
}
Customer owner = new Customer(firstName, lastName, phone);
Account account = new Account(nOfAccounts, owner, overdraft);
accounts[nOfAccounts] = account;
nOfAccounts++;
return account.accountNo;
}
private Acount getAccount(accountNo) throws NoSuchAccountException {
if (accountNo >= accounts.length || accounts[accountNo] == null) {
throw new NoSuchAccountException();
}
return account;
}
boolean deposit(int accountNo, double amount) {
Account account = getAccount(accountNo);
return account.deposit(amount);
}
public boolean withdraw(int accountNo, double amount) throws OverdraftLimitReachedException {
if (amount > overdraft ) {
throw new OverdraftLimitReachedException("The overdraft limit has been excedeed");
Account account = accounts[accountNo];
return account.withdraw(amount);
}
}
public boolean transfer(int fromNo, int toNo, double amount) {
if (fromNo < 0 || toNo < 0 ||
fromNo >= nOfAccounts || toNo >= nOfAccounts) return false;
Account from = accounts[fromNo];
Account to = accounts[toNo];
return from.transfer(to, amount);
}
public double getBalance(int accountNo) {
if (accountNo < 0 || accountNo >= nOfAccounts) return 0;
Account account = accounts[accountNo];
return account.balance;
}
double getBalance() {
double sum = 0;
for (int i = 0; i < nOfAccounts; i++) {
sum += accounts[i].balance;
}
return sum;
}
void print() {
Out.println("---------- Bankauszug ----------");
for (int i = 0; i < nOfAccounts; i++) {
accounts[i].print();
Out.println("--------------------------------");
}
Out.format("Bilanzsumme: %.2f%n", getBalance());
Out.println("--------------------------------");
}
// --------------------- Optionaler Teil ---------------------
public static void main(String[] args) {
Banking banking = new Banking();
char op;
do {
printMenu();
op = readOperation();
switch (op) {
case 'a': {
printTitle("Konto anlegen");
String firstName = getString("Vorname");
String lastName = getString("Nachname");
String phone = getString("Telefonnummer");
double overdraft = getDouble("Überziehungsrahmen");
int accountNo = banking.createAccount(
firstName, lastName, phone, overdraft);
printMessage("Anlegen von Konto " + accountNo, accountNo != -1);
break;
}
case 'e': {
printTitle("Einzahlen");
int accountNo = getInt("Kontonummer");
double amount = getDouble("Einzahlungsbetrag");
boolean success = banking.deposit(accountNo, amount);
printMessage("Einzahlen", success);
break;
}
case 'b': {
printTitle("Abheben");
int accountNo = getInt("Kontonummer");
double amount = getDouble("Abhebungsbetrag");
boolean success = banking.withdraw(accountNo, amount);
printMessage("Abheben", success);
break;
}
case 't': {
printTitle("Überweisen");
int fromNo = getInt("Von Kontonummer");
int toNo = getInt("Auf Kontonummer");
double amount = getDouble("Betrag");
boolean success = banking.transfer(fromNo, toNo, amount);
printMessage("Überweisen", success);
break;
}
case 'd':
banking.print();
break;
case 'q':
Out.println("Beenden");
break;
default:
Out.println("Ungültige Operation");
break;
}
} while(op != 'q');
}
static void printMenu() {
Out.println();
Out.println("*********** Bankverwaltung ********");
Out.println("Konto anlegen ................... a");
Out.println("Einzahlen ....................... e");
Out.println("Beheben ......................... b");
Out.println("Überweisen ...................... t");
Out.println("Übersicht drucken ............... d");
Out.println("Beenden ......................... q");
Out.print("Welche Menuoption? [a|e|b|t|d|q]: ");
}
static char readOperation() {
char ch = Character.toLowerCase(In.readChar());
In.readLine();
return ch;
}
static void printTitle(String text) {
Out.println("*** " + text + " ***");
}
static String getString(String text) {
Out.print(text + ": ");
return In.readLine();
}
static double getDouble(String text) {
Out.print(text + ": ");
return In.readDouble();
}
static int getInt(String text) {
Out.print(text + ": ");
return In.readInt();
}
static void printMessage(String operation, boolean success) {
String message = success ?
operation + " erfolgreich durchgeführt" :
"Ungültige Operation";
Out.println(message);
}
}