mirror of
https://github.com/13hannes11/archive.git
synced 2024-09-03 21:50:58 +02:00
added all past projects
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
|
||||
<fileset name="all" enabled="true" check-config-name="Programmieren_Assignment06_WS_2015" local="false">
|
||||
<file-match-pattern match-pattern="." include-pattern="true"/>
|
||||
</fileset>
|
||||
</fileset-config>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
Uni/Java/WS1516/Programmieren/Assignment6B_Bank/.project
Normal file
17
Uni/Java/WS1516/Programmieren/Assignment6B_Bank/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Assignment6B_Bank</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
Binary file not shown.
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="false">
|
||||
<output url="file://$MODULE_DIR$/bin" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
|
||||
<orderEntry type="module-library">
|
||||
<library name="junit4">
|
||||
<CLASSES>
|
||||
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package edu.kit.informatik;
|
||||
|
||||
/**
|
||||
* The Class Account.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Account {
|
||||
private final int accountNumber;
|
||||
private final int bankCode;
|
||||
private int balance;
|
||||
|
||||
/**
|
||||
* Instantiates a new account.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
*/
|
||||
public Account(final int accountNumber, final int bankCode) {
|
||||
if (accountNumber < 0 || bankCode < 0) {
|
||||
throw new IllegalArgumentException("Numbers must be > 0!");
|
||||
}
|
||||
this.accountNumber = accountNumber;
|
||||
this.bankCode = bankCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds money to the account
|
||||
*
|
||||
* @param amount
|
||||
* the amount
|
||||
*/
|
||||
public void deposit(final int amount) {
|
||||
if (amount <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
balance += amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Withdraws money from the account
|
||||
*
|
||||
* @param amount
|
||||
* the amount
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean withdraw(final int amount) {
|
||||
if (amount < 0 || balance - amount < 0) {
|
||||
return false;
|
||||
} else {
|
||||
balance -= amount;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the balance.
|
||||
*
|
||||
* @return the balance
|
||||
*/
|
||||
public int getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the account number.
|
||||
*
|
||||
* @return the account number
|
||||
*/
|
||||
public int getAccountNumber() {
|
||||
return accountNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the bank code.
|
||||
*
|
||||
* @return the bank code
|
||||
*/
|
||||
public int getBankCode() {
|
||||
return bankCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
return (obj.getClass() == Account.class && ((Account) obj).accountNumber == this.accountNumber
|
||||
&& ((Account) obj).bankCode == this.bankCode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
/**
|
||||
* The Class AccountHolder.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class AccountHolder {
|
||||
private final String firstName;
|
||||
private final String lastName;
|
||||
private final int personnelNumber;
|
||||
|
||||
/**
|
||||
* Instantiates a new account holder.
|
||||
*
|
||||
* @param firstName
|
||||
* the first name
|
||||
* @param lastName
|
||||
* the last name
|
||||
* @param personnelNumber
|
||||
* the personnel number
|
||||
*/
|
||||
public AccountHolder(final String firstName, final String lastName, final int personnelNumber) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.personnelNumber = personnelNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first name.
|
||||
*
|
||||
* @return the first name
|
||||
*/
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last name.
|
||||
*
|
||||
* @return the last name
|
||||
*/
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the personnel number.
|
||||
*
|
||||
* @return the personnel number
|
||||
*/
|
||||
public int getPersonnelNumber() {
|
||||
return personnelNumber;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import edu.kit.informatik.exceptions.AccountDoesNotExistException;
|
||||
import edu.kit.informatik.exceptions.AccountHolderDoesNotExistException;
|
||||
|
||||
/**
|
||||
* The Class Bank.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Bank {
|
||||
|
||||
/** The bank code. */
|
||||
private final int bankCode;
|
||||
|
||||
/** The data. */
|
||||
private final ArrayList<Pair<AccountHolder, ArrayList<Account>>> data;
|
||||
|
||||
/**
|
||||
* Instantiates a new bank.
|
||||
*
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
*/
|
||||
public Bank(final int bankCode) {
|
||||
this.bankCode = bankCode;
|
||||
this.data = new ArrayList<Pair<AccountHolder, ArrayList<Account>>>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the user.
|
||||
*
|
||||
* @param firstName
|
||||
* the first name
|
||||
* @param lastName
|
||||
* the last name
|
||||
* @param personnelNumber
|
||||
* the personnel number
|
||||
*/
|
||||
public void addUser(final String firstName, final String lastName, final int personnelNumber) {
|
||||
final AccountHolder accHolder = new AccountHolder(firstName, lastName, personnelNumber);
|
||||
data.add(new Pair<AccountHolder, ArrayList<Account>>(accHolder, new ArrayList<Account>()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the account.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param personnelNumber
|
||||
* the personnel number
|
||||
* @return true, if successful
|
||||
* @throws AccountHolderDoesNotExistException
|
||||
* the account holder does not exist exception
|
||||
*/
|
||||
public boolean addAccount(final int accountNumber, final int personnelNumber)
|
||||
throws AccountHolderDoesNotExistException {
|
||||
for (final Pair<AccountHolder, ArrayList<Account>> pair : data) {
|
||||
if (pair.getFirst().getPersonnelNumber() == personnelNumber) {
|
||||
pair.getSecond().add(new Account(accountNumber, bankCode));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
throw new AccountHolderDoesNotExistException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the account.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
*/
|
||||
public void removeAccount(final int accountNumber) throws AccountDoesNotExistException {
|
||||
for (final Pair<AccountHolder, ArrayList<Account>> pair : data) {
|
||||
if (pair.getSecond().remove(new Account(accountNumber, bankCode))) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new AccountDoesNotExistException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Withdraws money from an account.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param amount
|
||||
* the amount
|
||||
* @return true, if successful
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
*/
|
||||
public boolean withdraw(final int accountNumber, final int amount) throws AccountDoesNotExistException {
|
||||
final Account acc = getAccount(accountNumber);
|
||||
if (acc == null) {
|
||||
return false;
|
||||
}
|
||||
return acc.withdraw(amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds money to an account.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param amount
|
||||
* the amount
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
*/
|
||||
public void deposit(final int accountNumber, final int amount) throws AccountDoesNotExistException {
|
||||
final Account acc = getAccount(accountNumber);
|
||||
if (acc == null) {
|
||||
throw new AccountDoesNotExistException();
|
||||
}
|
||||
acc.deposit(amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfers money from one account to another.
|
||||
*
|
||||
* @param fromAccountNumber
|
||||
* the from account number
|
||||
* @param toAccountnumber
|
||||
* the to accountnumber
|
||||
* @param amount
|
||||
* the amount
|
||||
* @return true, if successful
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
*/
|
||||
public boolean transfer(final int fromAccountNumber, final int toAccountnumber, final int amount)
|
||||
throws AccountDoesNotExistException {
|
||||
final Account fromAcc = getAccount(fromAccountNumber);
|
||||
final Account toAccount = getAccount(toAccountnumber);
|
||||
if (!fromAcc.withdraw(amount)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
toAccount.deposit(amount);
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of accounts. Returns 0 if personnelNumber is not known in
|
||||
* this bank.
|
||||
*
|
||||
* @param personnelNumber
|
||||
* the personnel number
|
||||
* @return the number of accounts
|
||||
*/
|
||||
public int getNumberOfAccounts(final int personnelNumber) {
|
||||
for (final Pair<AccountHolder, ArrayList<Account>> pair : data) {
|
||||
if (pair.getFirst().getPersonnelNumber() == personnelNumber) {
|
||||
return pair.getSecond().size();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if account is contained.
|
||||
*
|
||||
* @param account
|
||||
* the account
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean containsAccount(final Account account) {
|
||||
try {
|
||||
final Account comp = getAccount(account.getAccountNumber());
|
||||
return account.equals(comp);
|
||||
} catch (final AccountDoesNotExistException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the balance of the account with the corresponding bank code. If
|
||||
* there is no account with that bankCode -1 is returned.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @return the balance, if unsuccessful -1
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
*/
|
||||
public int balance(final int accountNumber) throws AccountDoesNotExistException {
|
||||
final Account acc = getAccount(accountNumber);
|
||||
return acc.getBalance();
|
||||
}
|
||||
|
||||
private Account getAccount(final int accountNumber) throws AccountDoesNotExistException {
|
||||
final Account comp = new Account(accountNumber, bankCode);
|
||||
for (final Pair<AccountHolder, ArrayList<Account>> pair : data) {
|
||||
if (pair.getSecond().contains(comp)) {
|
||||
return pair.getSecond().get(pair.getSecond().indexOf(comp));
|
||||
}
|
||||
}
|
||||
throw new AccountDoesNotExistException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the bank code.
|
||||
*
|
||||
* @return the bank code
|
||||
*/
|
||||
public int getBankCode() {
|
||||
return bankCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
return (obj.getClass() == Bank.class && ((Bank) obj).getBankCode() == this.bankCode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import edu.kit.informatik.exceptions.AccountDoesNotExistException;
|
||||
import edu.kit.informatik.exceptions.AccountHolderDoesNotExistException;
|
||||
import edu.kit.informatik.exceptions.BankDoesNotExistException;
|
||||
|
||||
/**
|
||||
* The Class BankRegistry.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class BankRegistry {
|
||||
|
||||
/** The banks. */
|
||||
private final ArrayList<Bank> banks;
|
||||
|
||||
/**
|
||||
* Instantiates a new bank registry.
|
||||
*/
|
||||
public BankRegistry() {
|
||||
banks = new ArrayList<Bank>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the bank.
|
||||
*
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
*/
|
||||
public void addBank(final int bankCode) {
|
||||
banks.add(new Bank(bankCode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the account number.
|
||||
*
|
||||
* @param personnelNumber
|
||||
* the personnel number
|
||||
* @return the account number
|
||||
*/
|
||||
public int getAccountNumber(final int personnelNumber) {
|
||||
int counter = 0;
|
||||
for (final Bank bank : banks) {
|
||||
counter += bank.getNumberOfAccounts(personnelNumber);
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the user to the bank.
|
||||
*
|
||||
* @param firstName
|
||||
* the first name
|
||||
* @param lastName
|
||||
* the last name
|
||||
* @param personnelNumber
|
||||
* the personnel number
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
* @throws BankDoesNotExistException
|
||||
* the bank does not exist exception
|
||||
*/
|
||||
public void addUser(final String firstName, final String lastName, final int personnelNumber, final int bankCode)
|
||||
throws BankDoesNotExistException {
|
||||
final Bank bank = getBank(bankCode);
|
||||
bank.addUser(firstName, lastName, personnelNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the account.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
* @throws BankDoesNotExistException
|
||||
* the bank does not exist exception
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
*/
|
||||
public void removeAccount(final int accountNumber, final int bankCode)
|
||||
throws BankDoesNotExistException, AccountDoesNotExistException {
|
||||
final Bank bank = getBank(bankCode);
|
||||
bank.removeAccount(accountNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deposit.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
* @param amount
|
||||
* the amount
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
* @throws BankDoesNotExistException
|
||||
* the bank does not exist exception
|
||||
*/
|
||||
public void deposit(final int accountNumber, final int bankCode, final int amount)
|
||||
throws AccountDoesNotExistException, BankDoesNotExistException {
|
||||
final Bank bank = getBank(bankCode);
|
||||
bank.deposit(accountNumber, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Withdraw.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
* @param amount
|
||||
* the amount
|
||||
* @return true, if successful
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
* @throws BankDoesNotExistException
|
||||
* the bank does not exist exception
|
||||
*/
|
||||
public boolean withdraw(final int accountNumber, final int bankCode, final int amount)
|
||||
throws AccountDoesNotExistException, BankDoesNotExistException {
|
||||
final Bank bank = getBank(bankCode);
|
||||
return bank.withdraw(accountNumber, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer.
|
||||
*
|
||||
* @param fromAccountNumber
|
||||
* the from account number
|
||||
* @param fromBankCode
|
||||
* the from bank code
|
||||
* @param toAccountNumber
|
||||
* the to account number
|
||||
* @param toBankCode
|
||||
* the to bank code
|
||||
* @param amount
|
||||
* the amount
|
||||
* @return true, if successful
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
* @throws BankDoesNotExistException
|
||||
* the bank does not exist exception
|
||||
*/
|
||||
public boolean transfer(final int fromAccountNumber, final int fromBankCode, final int toAccountNumber,
|
||||
final int toBankCode, final int amount) throws AccountDoesNotExistException, BankDoesNotExistException {
|
||||
final Bank fromBank = getBank(fromBankCode);
|
||||
// internal bank transfer
|
||||
if (fromBankCode == toBankCode) {
|
||||
return fromBank.transfer(fromAccountNumber, toAccountNumber, amount);
|
||||
}
|
||||
final Bank toBank = getBank(toBankCode);
|
||||
// transfer between two different banks
|
||||
if (fromBank.containsAccount(new Account(fromAccountNumber, fromBankCode))
|
||||
&& toBank.containsAccount(new Account(toAccountNumber, toBankCode))) {
|
||||
if (fromBank.withdraw(fromAccountNumber, amount)) {
|
||||
fromBank.deposit(toAccountNumber, amount);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
throw new AccountDoesNotExistException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains account.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
* @return true, if successful
|
||||
* @throws BankDoesNotExistException
|
||||
* the bank does not exist exception
|
||||
*/
|
||||
public boolean containsAccount(final int accountNumber, final int bankCode) throws BankDoesNotExistException {
|
||||
final Bank bank = getBank(bankCode);
|
||||
return bank.containsAccount(new Account(accountNumber, bankCode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Balance.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
* @return the int
|
||||
* @throws AccountDoesNotExistException
|
||||
* the account does not exist exception
|
||||
* @throws BankDoesNotExistException
|
||||
* the bank does not exist exception
|
||||
*/
|
||||
public int balance(final int accountNumber, final int bankCode)
|
||||
throws AccountDoesNotExistException, BankDoesNotExistException {
|
||||
return getBank(bankCode).balance(accountNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the account.
|
||||
*
|
||||
* @param accountNumber
|
||||
* the account number
|
||||
* @param personnelNumber
|
||||
* the personnel number
|
||||
* @param bankCode
|
||||
* the bank code
|
||||
* @throws BankDoesNotExistException
|
||||
* the bank does not exist exception
|
||||
* @throws AccountHolderDoesNotExistException
|
||||
* the account holder does not exist exception
|
||||
*/
|
||||
public void addAccount(final int accountNumber, final int personnelNumber, final int bankCode)
|
||||
throws BankDoesNotExistException, AccountHolderDoesNotExistException {
|
||||
final Bank bank = getBank(bankCode);
|
||||
bank.addAccount(accountNumber, personnelNumber);
|
||||
}
|
||||
|
||||
private Bank getBank(final int bankCode) throws BankDoesNotExistException {
|
||||
if (!banks.contains(new Bank(bankCode))) {
|
||||
throw new BankDoesNotExistException();
|
||||
}
|
||||
return banks.get(banks.indexOf(new Bank(bankCode)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
import edu.kit.informatik.commandline.CommandLineParser;
|
||||
|
||||
/**
|
||||
* The Class Main.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public final class Main {
|
||||
|
||||
/**
|
||||
* Instantiates a new main.
|
||||
*/
|
||||
private Main() {
|
||||
}
|
||||
|
||||
/**
|
||||
* The main method.
|
||||
*
|
||||
* @param args
|
||||
* the arguments
|
||||
*/
|
||||
public static void main(final String[] args) {
|
||||
final CommandLineParser cParser = new CommandLineParser();
|
||||
cParser.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
/**
|
||||
* The Class Pair.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*
|
||||
* @param <T>
|
||||
* the generic type
|
||||
* @param <U>
|
||||
* the generic type
|
||||
*/
|
||||
public class Pair<T, U> {
|
||||
|
||||
private final T first;
|
||||
private final U second;
|
||||
|
||||
/**
|
||||
* Instantiates a new pair.
|
||||
*
|
||||
* @param first
|
||||
* the first
|
||||
* @param second
|
||||
* the second
|
||||
*/
|
||||
public Pair(final T first, final U second) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first element of the pair.
|
||||
*
|
||||
* @return the first
|
||||
*/
|
||||
public T getFirst() {
|
||||
return first;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the second element of the pair.
|
||||
*
|
||||
* @return the second
|
||||
*/
|
||||
public U getSecond() {
|
||||
return second;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* This class provides some simple methods for input/output from and to a
|
||||
* terminal.
|
||||
*
|
||||
* Never modify this class, never upload it to Praktomat. This is only for your
|
||||
* local use. If an assignment tells you to
|
||||
* use this class for input and output never use System.out or System.in in the
|
||||
* same assignment.
|
||||
*
|
||||
* @author ITI, VeriAlg Group
|
||||
* @author IPD, SDQ Group
|
||||
* @version 4
|
||||
*/
|
||||
public final class Terminal {
|
||||
|
||||
/**
|
||||
* BufferedReader for reading from standard input line-by-line.
|
||||
*/
|
||||
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
/**
|
||||
* Private constructor to avoid object generation.
|
||||
*/
|
||||
private Terminal() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a String to the standard output.
|
||||
*
|
||||
* The String out must not be null.
|
||||
*
|
||||
* @param out
|
||||
* The string to be printed.
|
||||
*/
|
||||
public static void printLine(final String out) {
|
||||
System.out.println(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a line from standard input.
|
||||
*
|
||||
* Returns null at the end of the standard input.
|
||||
*
|
||||
* Use Ctrl+D to indicate the end of the standard input.
|
||||
*
|
||||
* @return The next line from the standard input or null.
|
||||
*/
|
||||
public static String readLine() {
|
||||
try {
|
||||
return in.readLine();
|
||||
} catch (final IOException e) {
|
||||
/*
|
||||
* rethrow unchecked (!) exception to prevent students from being
|
||||
* forced to use Exceptions before they have
|
||||
* been introduced in the lecture.
|
||||
*/
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package edu.kit.informatik.commandline;
|
||||
|
||||
/**
|
||||
* The Enum Command.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public enum Command {
|
||||
|
||||
/** The addbank. */
|
||||
ADDBANK("addbank", 1),
|
||||
/** The adduser. */
|
||||
ADDUSER("adduser", 4),
|
||||
/** The addaccount. */
|
||||
ADDACCOUNT("addaccount", 3),
|
||||
/** The removeaccount. */
|
||||
REMOVEACCOUNT("removeaccount", 2),
|
||||
/** The deposit. */
|
||||
DEPOSIT("deposit", 3),
|
||||
/** The withdraw. */
|
||||
WITHDRAW("withdraw", 3),
|
||||
/** The transfer. */
|
||||
TRANSFER("transfer", 5),
|
||||
/** The getaccountnumber. */
|
||||
GETACCOUNTNUMBER("getaccountnumber", 1),
|
||||
/** The containsaccount. */
|
||||
CONTAINSACCOUNT("containsaccount", 2),
|
||||
/** The balance. */
|
||||
BALANCE("balance", 2),
|
||||
/** The quit. */
|
||||
QUIT("quit", 0),
|
||||
/** The invalid. */
|
||||
INVALID("", -1);
|
||||
|
||||
private final String command;
|
||||
|
||||
/** The param count. */
|
||||
private final int paramCount;
|
||||
|
||||
/**
|
||||
* Instantiates a new command.
|
||||
*
|
||||
* @param command
|
||||
* the command
|
||||
* @param paramCount
|
||||
* the param count
|
||||
*/
|
||||
private Command(final String command, final int paramCount) {
|
||||
this.paramCount = paramCount;
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the command.
|
||||
*
|
||||
* @return the command
|
||||
*/
|
||||
public String getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the param count.
|
||||
*
|
||||
* @return the param count
|
||||
*/
|
||||
public int getParamCount() {
|
||||
return paramCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* converts string to command.
|
||||
*
|
||||
* @param str
|
||||
* the str
|
||||
* @return the command
|
||||
*/
|
||||
public static Command convertToCommand(final String str) {
|
||||
for (final Command command : Command.values()) {
|
||||
if (command.getCommand().equals(str)) {
|
||||
return command;
|
||||
}
|
||||
}
|
||||
return Command.INVALID;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
package edu.kit.informatik.commandline;
|
||||
|
||||
import edu.kit.informatik.BankRegistry;
|
||||
import edu.kit.informatik.Terminal;
|
||||
import edu.kit.informatik.exceptions.AccountDoesNotExistException;
|
||||
import edu.kit.informatik.exceptions.AccountHolderDoesNotExistException;
|
||||
import edu.kit.informatik.exceptions.BankDoesNotExistException;
|
||||
|
||||
/**
|
||||
* The Class CommandLineParser.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CommandLineParser {
|
||||
private static final String SUCCESSFULL = "OK";
|
||||
private static final String NOT_NATURAL = "parameters have to be natural numbers";
|
||||
private static final String ACCOUNDHODLER_DOES_NOT_EXIST = "the accountholder does not exists within the bank";
|
||||
private static final String BANK_DOES_NOT_EXIST = "the bank does not exist";
|
||||
private static final String ACCOUNT_DOES_NOT_EXIST = "account does not exist in this bank";
|
||||
private static final String COMMAND_NOT_FOUND = "not valid command.'";
|
||||
private static final String WRONG_PARAMETER_COUNT = "the command expects %d parameter";
|
||||
private static final String WRONG_STRING_FORMAT = "strings have to be lowercase";
|
||||
private static final String UNEXPECTED_ERROR = "something unexpected went wront :(";
|
||||
|
||||
private boolean readyToQuit;
|
||||
|
||||
private final BankRegistry bankRegistry;
|
||||
|
||||
/**
|
||||
* Instantiates a new command line parser.
|
||||
*/
|
||||
public CommandLineParser() {
|
||||
bankRegistry = new BankRegistry();
|
||||
readyToQuit = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the command line parser
|
||||
*/
|
||||
public void run() {
|
||||
while (!readyToQuit) {
|
||||
runCommand(Terminal.readLine());
|
||||
}
|
||||
}
|
||||
|
||||
private void runCommand(final String command) {
|
||||
if (command == null) {
|
||||
error(COMMAND_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
final String[] arr = command.split("[\\s]");
|
||||
String[] parameters;
|
||||
if (arr.length > 1) {
|
||||
parameters = arr[1].split(";");
|
||||
} else {
|
||||
parameters = new String[0];
|
||||
}
|
||||
|
||||
final Command c = Command.convertToCommand(arr[0]);
|
||||
// If parameter count does not match command
|
||||
if (parameters.length != c.getParamCount() && !c.equals(Command.INVALID)) {
|
||||
error(String.format(WRONG_PARAMETER_COUNT, c.getParamCount()));
|
||||
return;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
case WITHDRAW:
|
||||
withdraw(parameters);
|
||||
break;
|
||||
case TRANSFER:
|
||||
transfer(parameters);
|
||||
break;
|
||||
case REMOVEACCOUNT:
|
||||
removeAccount(parameters);
|
||||
break;
|
||||
case GETACCOUNTNUMBER:
|
||||
getAccountNumber(parameters);
|
||||
break;
|
||||
case DEPOSIT:
|
||||
deposit(parameters);
|
||||
break;
|
||||
case CONTAINSACCOUNT:
|
||||
containsAccount(parameters);
|
||||
break;
|
||||
case BALANCE:
|
||||
balance(parameters);
|
||||
break;
|
||||
case ADDUSER:
|
||||
addUser(parameters);
|
||||
break;
|
||||
case ADDBANK:
|
||||
addBank(parameters);
|
||||
break;
|
||||
case ADDACCOUNT:
|
||||
addAccount(parameters);
|
||||
break;
|
||||
case QUIT:
|
||||
readyToQuit = true;
|
||||
break;
|
||||
default:
|
||||
error(COMMAND_NOT_FOUND);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void addUser(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[2]) || !isNaturalNumberString(parameters[3])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
if (!parameters[0].matches("[a-z]+") || !parameters[1].matches("[a-z]+")) {
|
||||
error(WRONG_STRING_FORMAT);
|
||||
return;
|
||||
}
|
||||
final String firstName = parameters[0];
|
||||
final String lastName = parameters[1];
|
||||
final int personnelNumber = Integer.parseInt(parameters[2]);
|
||||
final int bankCode = Integer.parseInt(parameters[3]);
|
||||
try {
|
||||
bankRegistry.addUser(firstName, lastName, personnelNumber, bankCode);
|
||||
Terminal.printLine(SUCCESSFULL);
|
||||
} catch (final BankDoesNotExistException e) {
|
||||
error(BANK_DOES_NOT_EXIST);
|
||||
}
|
||||
}
|
||||
|
||||
private void addBank(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[0])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
final int bankCode = Integer.parseInt(parameters[0]);
|
||||
bankRegistry.addBank(bankCode);
|
||||
Terminal.printLine(SUCCESSFULL);
|
||||
}
|
||||
|
||||
private void addAccount(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[0]) || !isNaturalNumberString(parameters[1])
|
||||
|| !isNaturalNumberString(parameters[2])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
final int accountNumber = Integer.parseInt(parameters[0]);
|
||||
final int personnelNumber = Integer.parseInt(parameters[1]);
|
||||
final int bankCode = Integer.parseInt(parameters[2]);
|
||||
|
||||
try {
|
||||
bankRegistry.addAccount(accountNumber, personnelNumber, bankCode);
|
||||
Terminal.printLine(SUCCESSFULL);
|
||||
} catch (final BankDoesNotExistException e) {
|
||||
error(BANK_DOES_NOT_EXIST);
|
||||
} catch (final AccountHolderDoesNotExistException e) {
|
||||
error(ACCOUNDHODLER_DOES_NOT_EXIST);
|
||||
}
|
||||
}
|
||||
|
||||
private void balance(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[0]) || !isNaturalNumberString(parameters[1])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
final int accountNumber = Integer.parseInt(parameters[0]);
|
||||
final int bankCode = Integer.parseInt(parameters[1]);
|
||||
|
||||
try {
|
||||
Terminal.printLine(Integer.toString(bankRegistry.balance(accountNumber, bankCode)));
|
||||
} catch (final AccountDoesNotExistException e) {
|
||||
error(ACCOUNT_DOES_NOT_EXIST);
|
||||
} catch (final BankDoesNotExistException e) {
|
||||
error(BANK_DOES_NOT_EXIST);
|
||||
}
|
||||
}
|
||||
|
||||
private void containsAccount(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[0]) || !isNaturalNumberString(parameters[1])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
final int accountNumber = Integer.parseInt(parameters[0]);
|
||||
final int bankCode = Integer.parseInt(parameters[1]);
|
||||
try {
|
||||
Terminal.printLine(Boolean.toString(bankRegistry.containsAccount(accountNumber, bankCode)));
|
||||
} catch (final BankDoesNotExistException e) {
|
||||
error(BANK_DOES_NOT_EXIST);
|
||||
}
|
||||
}
|
||||
|
||||
private void deposit(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[0]) || !isNaturalNumberString(parameters[1])
|
||||
|| !isNaturalNumberString(parameters[2])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
final int accountNumber = Integer.parseInt(parameters[0]);
|
||||
final int bankCode = Integer.parseInt(parameters[1]);
|
||||
final int amount = Integer.parseInt(parameters[2]);
|
||||
|
||||
try {
|
||||
bankRegistry.deposit(accountNumber, bankCode, amount);
|
||||
Terminal.printLine(SUCCESSFULL);
|
||||
} catch (final AccountDoesNotExistException e) {
|
||||
error(ACCOUNT_DOES_NOT_EXIST);
|
||||
} catch (final BankDoesNotExistException e) {
|
||||
error(BANK_DOES_NOT_EXIST);
|
||||
}
|
||||
}
|
||||
|
||||
private void getAccountNumber(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[0])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
final int personnelNumber = Integer.parseInt(parameters[0]);
|
||||
Terminal.printLine(Integer.toString(bankRegistry.getAccountNumber(personnelNumber)));
|
||||
}
|
||||
|
||||
private void removeAccount(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[0]) || !isNaturalNumberString(parameters[1])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
final int accountNumber = Integer.parseInt(parameters[0]);
|
||||
final int bankCode = Integer.parseInt(parameters[1]);
|
||||
try {
|
||||
bankRegistry.removeAccount(accountNumber, bankCode);
|
||||
Terminal.printLine(SUCCESSFULL);
|
||||
} catch (final AccountDoesNotExistException e) {
|
||||
error(ACCOUNT_DOES_NOT_EXIST);
|
||||
} catch (final BankDoesNotExistException e) {
|
||||
error(BANK_DOES_NOT_EXIST);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void transfer(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[0]) || !isNaturalNumberString(parameters[1])
|
||||
|| !isNaturalNumberString(parameters[2]) || !isNaturalNumberString(parameters[3])
|
||||
|| !isNaturalNumberString(parameters[4])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
final int fromAccountNumber = Integer.parseInt(parameters[0]);
|
||||
final int fromBankCode = Integer.parseInt(parameters[1]);
|
||||
final int toAccountNumber = Integer.parseInt(parameters[2]);
|
||||
final int toBankCode = Integer.parseInt(parameters[3]);
|
||||
final int amount = Integer.parseInt(parameters[4]);
|
||||
|
||||
try {
|
||||
bankRegistry.transfer(fromAccountNumber, fromBankCode, toAccountNumber, toBankCode, amount);
|
||||
Terminal.printLine(SUCCESSFULL);
|
||||
} catch (final AccountDoesNotExistException e) {
|
||||
error(ACCOUNT_DOES_NOT_EXIST);
|
||||
} catch (final BankDoesNotExistException e) {
|
||||
error(BANK_DOES_NOT_EXIST);
|
||||
}
|
||||
}
|
||||
|
||||
private void withdraw(final String[] parameters) {
|
||||
if (!isNaturalNumberString(parameters[0]) || !isNaturalNumberString(parameters[1])
|
||||
|| !isNaturalNumberString(parameters[2])) {
|
||||
error(NOT_NATURAL);
|
||||
return;
|
||||
}
|
||||
final int accountNumber = Integer.parseInt(parameters[0]);
|
||||
final int bankCode = Integer.parseInt(parameters[1]);
|
||||
final int amount = Integer.parseInt(parameters[2]);
|
||||
|
||||
try {
|
||||
if (bankRegistry.withdraw(accountNumber, bankCode, amount)) {
|
||||
Terminal.printLine(SUCCESSFULL);
|
||||
} else {
|
||||
error(UNEXPECTED_ERROR);
|
||||
}
|
||||
} catch (final AccountDoesNotExistException e) {
|
||||
error(ACCOUNT_DOES_NOT_EXIST);
|
||||
} catch (final BankDoesNotExistException e) {
|
||||
error(BANK_DOES_NOT_EXIST);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNaturalNumberString(final String str) {
|
||||
return str.matches("[0-9]+");
|
||||
}
|
||||
|
||||
private void error(final String str) {
|
||||
Terminal.printLine("Error, " + str);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package edu.kit.informatik.exceptions;
|
||||
|
||||
/**
|
||||
* The Class AccountDoesNotExistException.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class AccountDoesNotExistException extends Exception {
|
||||
|
||||
/**
|
||||
* Instantiates a new account does not exist exception.
|
||||
*/
|
||||
public AccountDoesNotExistException() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package edu.kit.informatik.exceptions;
|
||||
|
||||
/**
|
||||
* The Class AccountHolderDoesNotExistException.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class AccountHolderDoesNotExistException extends Exception {
|
||||
|
||||
/**
|
||||
* Instantiates a new account holder does not exist exception.
|
||||
*/
|
||||
public AccountHolderDoesNotExistException() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package edu.kit.informatik.exceptions;
|
||||
|
||||
/**
|
||||
* The Class BankDoesNotExistException.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class BankDoesNotExistException extends Exception {
|
||||
|
||||
/**
|
||||
* Instantiates a new bank does not exist exception.
|
||||
*/
|
||||
public BankDoesNotExistException() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user