added all past projects

This commit is contained in:
Hannes
2017-11-10 00:13:57 +01:00
parent 5f63f0c599
commit 8c94608805
1391 changed files with 109456 additions and 0 deletions

View File

@@ -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_Assignment05_WS_2015" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
</fileset-config>

View File

@@ -0,0 +1,6 @@
<?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="output" path="bin"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Assignment5B_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>

View File

@@ -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

View File

@@ -0,0 +1,12 @@
<?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" />
</component>
</module>

View File

@@ -0,0 +1,108 @@
package edu.kit.informatik;
/**
* The Class Account.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class Account {
/** The account number. */
private final int accountNumber;
/** The bank code. */
private final int bankCode;
/** The balance. */
private int balance;
/**
* Instantiates a new account (balance is initialized with 0).
*
* @param bankCode
* the bank code
* @param accountNumber
* the account number
*/
public Account(final int bankCode, final int accountNumber) {
if (bankCode < 0 || accountNumber < 0) {
throw new IllegalArgumentException("Account number and bankCode have to be positive integers.");
}
this.bankCode = bankCode;
this.accountNumber = accountNumber;
balance = 0;
}
/**
* Tries to withdraw money from the account (Fails if this would make
* balance negative).
*
* @param amount
* the amount that should be withdrawn
* @return true, if successful
*/
public boolean withdraw(final int amount) {
if (balance - amount < 0 || amount < 0) {
return false;
}
balance -= amount;
return true;
}
/**
* Increases the balance of the account by 'amount'.
*
* @param amount
* the amount
* @throws OverflowException
*/
public void deposit(final int amount) {
if (amount < 0) {
throw new IllegalArgumentException("amount must be positive");
}
balance += amount;
}
/**
* 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;
}
/**
* Gets the balance.
*
* @return the balance
*/
public int getBalance() {
return balance;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj) {
return (obj instanceof Account && ((Account) obj).accountNumber == this.accountNumber);
}
@Override
public String toString() {
return bankCode + "," + accountNumber + "," + balance;
}
}

View File

@@ -0,0 +1,62 @@
package edu.kit.informatik;
/**
* The Class AccountHolder.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class AccountHolder {
/** The first name. */
private final String firstName;
/** The last name. */
private final String lastName;
/** The personnel number. */
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;
}
}

View File

@@ -0,0 +1,246 @@
package edu.kit.informatik;
import edu.kit.informatik.exceptions.InvalidCommandException;
import edu.kit.informatik.list.Container;
import edu.kit.informatik.list.MinimaList;
import edu.kit.informatik.list.Pair;
/**
* The Class Bank.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class Bank {
/** The bank code. */
private final int bankCode;
/** The data. */
private final MinimaList<Pair<AccountHolder, MinimaList<Account>>> data;
/**
* Instantiates a new bank.
*
* @param bankCode
* the bank code
*/
public Bank(final int bankCode) {
this.bankCode = bankCode;
data = new MinimaList<Pair<AccountHolder, MinimaList<Account>>>();
}
/**
* Adds a 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, MinimaList<Account>>(accHolder, new MinimaList<Account>()));
}
/**
* Adds an account.
*
* @param accountNumber
* the account number
* @param personnelNumber
* the personnel number
* @return true, if successful
* @throws InvalidCommandException
* the invalid command exception
*/
public boolean addAccount(final int accountNumber, final int personnelNumber) throws InvalidCommandException {
Container<Pair<AccountHolder, MinimaList<Account>>> container = data.getFirstContainer();
while (container != null) {
if (container.getData().getFirst().getPersonnelNumber() == personnelNumber) {
container.getData().getSecond().add(new Account(bankCode, accountNumber));
return true;
}
container = container.getNext();
}
throw new InvalidCommandException(
"the person with this personnel number(" + personnelNumber + ") is not at this bank.");
}
/**
* Removes an account.
*
* @param accountNumber
* the account number
* @return true, if successful
*/
public boolean removeAccount(final int accountNumber) {
if (containsAccount(accountNumber)) {
Container<Pair<AccountHolder, MinimaList<Account>>> container = data.getFirstContainer();
while (container != null) {
final Pair<AccountHolder, MinimaList<Account>> pair = container.getData();
final int index = pair.getSecond().getIndex(new Account(bankCode, accountNumber));
if (pair.getSecond().remove(index)) {
return true;
}
container = container.getNext();
}
}
return false;
}
/**
* Deposits money.
*
* @param accountNumber
* the account number
* @param amount
* the amount
*/
public void deposit(final int accountNumber, final int amount) {
final Account acc = getAccount(accountNumber);
acc.deposit(amount);
}
/**
* Withdraws money.
*
* @param accountNumber
* the account number
* @param amount
* the amount
* @return true, if successful
*/
public boolean withdraw(final int accountNumber, final int amount) {
final Account acc = getAccount(accountNumber);
return acc.withdraw(amount);
}
/**
* Internal bank transfer.
*
* @param fromAccountNumber
* the from account number
* @param toAccountNumber
* the to account number
* @param amount
* the amount
* @throws InvalidCommandException
* the invalid parameter exception
*/
public void internalBankTransfer(final int fromAccountNumber, final int toAccountNumber, final int amount)
throws InvalidCommandException {
final Account from = getAccount(fromAccountNumber);
final Account to = getAccount(toAccountNumber);
if (to == null || from == null) {
throw new InvalidCommandException("either the account (" + fromAccountNumber
+ ") the money shouled be transfered from or the account (" + toAccountNumber
+ ") the money should be transefered to does not exist.");
}
if (!from.withdraw(amount)) {
throw new InvalidCommandException("amount could not be withdrawn from account (" + fromAccountNumber + ")");
}
to.deposit(amount);
}
/**
* Gets the account count of an account holder.
*
* @param personnelNumber
* the personnel number
* @return the account count
*/
public int getAccountCount(final int personnelNumber) {
Container<Pair<AccountHolder, MinimaList<Account>>> container = data.getFirstContainer();
while (container != null) {
final Pair<AccountHolder, MinimaList<Account>> pair = container.getData();
if (pair.getFirst().getPersonnelNumber() == personnelNumber) {
return pair.getSecond().size();
}
container = container.getNext();
}
return 0;
}
/**
* Checks if account is contained.
*
* @param accountNumber
* the account number
* @return true, if contained
*/
public boolean containsAccount(final int accountNumber) {
Container<Pair<AccountHolder, MinimaList<Account>>> container = data.getFirstContainer();
while (container != null) {
final Pair<AccountHolder, MinimaList<Account>> pair = container.getData();
if (pair.getSecond().contains(new Account(bankCode, accountNumber))) {
return true;
}
container = container.getNext();
}
return false;
}
/**
* Gets the account.
*
* @param accountNumber
* the account number
* @return the account
*/
public Account getAccount(final int accountNumber) {
Container<Pair<AccountHolder, MinimaList<Account>>> container = data.getFirstContainer();
while (container != null) {
final Pair<AccountHolder, MinimaList<Account>> pair = container.getData();
final int index = pair.getSecond().getIndex(new Account(bankCode, accountNumber));
if (index >= 0) {
return pair.getSecond().get(index);
}
container = container.getNext();
}
return null;
}
/**
* Balance.
*
* @param accountNumber
* the account number
* @return the int
* @throws InvalidCommandException
* the invalid parameter exception
*/
public int balance(final int accountNumber) throws InvalidCommandException {
final Account acc = getAccount(accountNumber);
if (acc == null) {
throw new InvalidCommandException(
"the bank (" + this.bankCode + ") does not contain the account (" + accountNumber + ")");
} else {
return acc.getBalance();
}
}
/**
* Gets the bank code.
*
* @return the bank code
*/
public int getBankCode() {
return bankCode;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj) {
return (obj instanceof Bank || ((Bank) obj).bankCode == this.bankCode);
}
}

View File

@@ -0,0 +1,335 @@
package edu.kit.informatik;
import edu.kit.informatik.exceptions.InvalidCommandException;
import edu.kit.informatik.list.Container;
import edu.kit.informatik.list.MinimaList;
/**
* The Class BankRegistry.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class BankRegistry {
private boolean readyToQuit;
private final MinimaList<Bank> banks;
/**
* Instantiates a new bank registry.
*/
public BankRegistry() {
readyToQuit = false;
banks = new MinimaList<Bank>();
}
/**
* Run the bank registry
*/
public void run() {
while (!readyToQuit) {
try {
processCommand(Terminal.readLine());
} catch (final InvalidCommandException e) {
Terminal.printLine("Error: " + e.getMessage());
}
}
}
private boolean processBankActions(final String action, final String[] parameters) throws InvalidCommandException {
switch (action) {
case "addbank": {
if (parameters.length != 1) {
throw new InvalidCommandException("addbank expects one parameter: addbank <bankCode>");
}
final int bankCode = Integer.parseInt(parameters[0]);
addBank(bankCode);
Terminal.printLine("OK");
return true;
}
case "adduser": {
if (parameters.length != 4) {
throw new InvalidCommandException("adduser expects four parameters: "
+ "addbank <firsName>;<lastName>;<personnelNumber>;<bankCode>");
}
final String firstName = parameters[0];
final String lastName = parameters[1];
final int personnelNumber = Integer.parseInt(parameters[2]);
final int bankCode = Integer.parseInt(parameters[3]);
addUser(firstName, lastName, personnelNumber, bankCode);
Terminal.printLine("OK");
return true;
}
case "addaccount": {
if (parameters.length != 3) {
throw new InvalidCommandException("addaccount expects three parameters: "
+ "addaccount <accountNumber>;<personnelMumber>;<bankCode>");
}
final int accountNumber = Integer.parseInt(parameters[0]);
final int personnelNumber = Integer.parseInt(parameters[1]);
final int bankCode = Integer.parseInt(parameters[2]);
addAccount(accountNumber, personnelNumber, bankCode);
Terminal.printLine("OK");
return true;
}
case "removeaccount": {
if (parameters.length != 2) {
throw new InvalidCommandException(
"removeaccount expects two parameters: removeaccount <accountNumber>;<bankCode>;");
}
final int accountNumber = Integer.parseInt(parameters[0]);
final int bankCode = Integer.parseInt(parameters[1]);
removeAccount(accountNumber, bankCode);
Terminal.printLine("OK");
return true;
}
case "containsaccount": {
if (parameters.length != 2) {
throw new InvalidCommandException(
"containsaccount expects two parameters: containsaccount <accountNumber>;<bankCode>");
}
final int accountNumber = Integer.parseInt(parameters[0]);
final int bankCode = Integer.parseInt(parameters[1]);
Terminal.printLine(new Boolean(containsAccount(accountNumber, bankCode)).toString());
return true;
}
case "getaccountnumber": {
if (parameters.length != 1) {
throw new InvalidCommandException(
"getaccountnumber expects one parameter: getaccountnumber <personnelNumber>");
}
final int personnelNumber = Integer.parseInt(parameters[0]);
Terminal.printLine(new Integer(getAccountCount(personnelNumber)).toString());
return true;
}
default:
return false;
}
}
private void processCommand(final String command) throws InvalidCommandException {
final String action = command.split(" ")[0];
String[] parameters = new String[0];
if (command.split(" ").length >= 2) {
parameters = (command.split(" ")[1]).split(";");
}
switch (action) {
case "deposit": {
if (parameters.length != 3) {
throw new InvalidCommandException(
"deposit expects three parameters: deposit <accountNumber>;<bankCode>;<amount>");
}
final int accountNumber = Integer.parseInt(parameters[0]);
final int bankCode = Integer.parseInt(parameters[1]);
final int amount = Integer.parseInt(parameters[2]);
deposit(accountNumber, bankCode, amount);
Terminal.printLine("OK");
break;
}
case "withdraw": {
if (parameters.length != 3) {
throw new InvalidCommandException(
"withdraw expects three parameters: withdraw <accountNumber>;<bankCode>;<amount>");
}
final int accountNumber = Integer.parseInt(parameters[0]);
final int bankCode = Integer.parseInt(parameters[1]);
final int amount = Integer.parseInt(parameters[2]);
withdraw(accountNumber, bankCode, amount);
Terminal.printLine("OK");
break;
}
case "transfer": {
if (parameters.length != 5) {
throw new InvalidCommandException("transfer expects five xparameters: "
+ "transfer <fromAccountNumber>;<fromBankCode>;<toAccountNumber>;<toBankCode>;<amount>");
}
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]);
transfer(fromAccountNumber, fromBankCode, toAccountNumber, toBankCode, amount);
Terminal.printLine("OK");
break;
}
case "balance": {
if (parameters.length != 2) {
throw new InvalidCommandException(
"balance expects two parameters: balance <accountNumber>;<bankCode>");
}
final int accountNumber = Integer.parseInt(parameters[0]);
final int bankCode = Integer.parseInt(parameters[1]);
Terminal.printLine(new Integer(balance(accountNumber, bankCode)).toString());
break;
}
case "quit":
if (parameters.length != 0) {
throw new InvalidCommandException("quit expects zero parameters: quit");
}
readyToQuit = true;
break;
default:
if (!processBankActions(action, parameters)) {
throw new InvalidCommandException(
"command not found, supported commands: 'quit', 'balance <accountNumber>;<bankCode>', "
+ "'containsaccount <accountNumber>;<bankCode>', 'getaccountnumber "
+ "<personnelNumber>', 'transfer <fromAccountNumber>;<fromBankCode>;"
+ "<toAccountNumber>;<toBankCode>;<amount>', 'withdraw <accountNumber>;<bankCode>"
+ ";<amount>', 'deposit <accountNumber>;<bankCode>;<amount>', 'removeaccount "
+ "<accountNumber>;<bankCode>;', 'addaccount <accountNumber>;<personnelMumber>;"
+ "<bankCode>', 'addbank <bankCode>'");
}
}
}
private void addBank(final int bankCode) throws InvalidCommandException {
final Bank b = new Bank(bankCode);
if (bankCode <= 0) {
throw new InvalidCommandException("bank code has to be a positive integer");
}
if (banks.contains(b)) {
throw new InvalidCommandException("bank with this bank code already exists");
}
banks.add(b);
}
private void addUser(final String firstName, final String lastName, final int personnelNumber, final int bankCode)
throws InvalidCommandException {
if (!(isValidName(firstName) && isValidName(lastName))) {
throw new InvalidCommandException("names have to be in lowercase and can only contain characters a-z");
}
if (!(isNaturalNumber(personnelNumber) && isNaturalNumber(bankCode))) {
throw new InvalidCommandException("numbers have to be natural numbers");
}
final int index = banks.getIndex(new Bank(bankCode));
if (index < 0) {
throw new InvalidCommandException("bank (" + bankCode + ") does not exist");
}
banks.get(index).addUser(firstName, lastName, personnelNumber);
}
private boolean isNaturalNumber(final int number) {
return (number > 0);
}
private boolean isValidName(final String name) {
if (name == "") {
return false;
}
final char[] chars = name.toCharArray();
for (final char c : chars) {
// If char is not a lower-case letter
if (!(Character.isLowerCase(c) && Character.isLetter(c))) {
return false;
}
}
return true;
}
private void addAccount(final int accountNumber, final int personnelNumber, final int bankCode)
throws InvalidCommandException {
if (!(isNaturalNumber(personnelNumber) && isNaturalNumber(bankCode) && isNaturalNumber(accountNumber))) {
throw new InvalidCommandException("numbers have to be natural numbers");
}
final int index = banks.getIndex(new Bank(bankCode));
if (index < 0) {
throw new InvalidCommandException("bank (" + bankCode + ") does not exist");
}
banks.get(index).addAccount(accountNumber, personnelNumber);
}
private void removeAccount(final int accountNumber, final int bankCode) throws InvalidCommandException {
if (!(isNaturalNumber(accountNumber) && isNaturalNumber(bankCode))) {
throw new InvalidCommandException("numbers have to be natural numbers");
}
final int index = banks.getIndex(new Bank(bankCode));
if (index < 0) {
throw new InvalidCommandException("bank (" + bankCode + ") does not exist");
}
banks.get(index).removeAccount(accountNumber);
}
private void deposit(final int accountNumber, final int bankCode, final int amount) throws InvalidCommandException {
if (!isNaturalNumber(accountNumber) || !isNaturalNumber(bankCode) || !isNaturalNumber(amount)) {
throw new InvalidCommandException("numbers have to be natural numbers");
}
final int index = banks.getIndex(new Bank(bankCode));
if (index < 0) {
throw new InvalidCommandException("bank (" + bankCode + ") does not exist");
}
banks.get(index).deposit(accountNumber, amount);
}
private void withdraw(final int accountNumber, final int bankCode, final int amount)
throws InvalidCommandException {
if (!isNaturalNumber(accountNumber) || !isNaturalNumber(bankCode) || !isNaturalNumber(amount)) {
throw new InvalidCommandException("numbers have to be natural numbers");
}
final int index = banks.getIndex(new Bank(bankCode));
if (index < 0) {
throw new InvalidCommandException("bank (" + bankCode + ") does not exist");
}
banks.get(index).withdraw(accountNumber, amount);
}
private void transfer(final int fromAccountNumber, final int fromBankCode, final int toAccountNumber,
final int toBankCode, final int amount) throws InvalidCommandException {
if (!isNaturalNumber(fromAccountNumber) || !isNaturalNumber(fromBankCode) || !isNaturalNumber(toAccountNumber)
|| !isNaturalNumber(toBankCode) || !isNaturalNumber(amount)) {
throw new InvalidCommandException("numbers have to be natural numbers");
}
if (fromBankCode == toBankCode) {
// Internal bank transfer
final int index = banks.getIndex(new Bank(fromBankCode));
if (index < 0) {
throw new InvalidCommandException("bank (" + fromBankCode + ") does not exist");
}
banks.get(index).internalBankTransfer(fromAccountNumber, toAccountNumber, amount);
} else {
// external bank transfer
withdraw(fromAccountNumber, fromBankCode, amount);
deposit(toAccountNumber, toBankCode, amount);
}
}
private int getAccountCount(final int personnelNumber) {
Container<Bank> container = banks.getFirstContainer();
while (container != null) {
final Bank bank = container.getData();
if (bank.getAccountCount(personnelNumber) > 0) {
return bank.getAccountCount(personnelNumber);
}
container = container.getNext();
}
return 0;
}
private boolean containsAccount(final int accountNumber, final int bankCode) throws InvalidCommandException {
final int index = banks.getIndex(new Bank(bankCode));
if (index == -1) {
throw new InvalidCommandException("bank with this bankcode does not exist");
}
return banks.get(index).containsAccount(accountNumber);
}
private int balance(final int accountNumber, final int bankCode) throws InvalidCommandException {
final int index = banks.getIndex(new Bank(bankCode));
if (index == -1) {
throw new InvalidCommandException("bank with this bankcode does not exist");
}
return banks.get(index).balance(accountNumber);
}
}

View File

@@ -0,0 +1,22 @@
package edu.kit.informatik;
/**
* The Class Main.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public final class Main {
private Main() {
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(final String[] args) {
final BankRegistry bankRegistry = new BankRegistry();
bankRegistry.run();
}
}

View File

@@ -0,0 +1,67 @@
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);
}
}
}

View File

@@ -0,0 +1,20 @@
package edu.kit.informatik.exceptions;
/**
* The Class InvalidCommandException.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class InvalidCommandException extends Exception {
/**
* Instantiates a new invalid command exception.
*
* @param message
* the message
*/
public InvalidCommandException(final String message) {
super(message);
}
}

View File

@@ -0,0 +1,58 @@
package edu.kit.informatik.list;
/**
* The Class Container.
*
* @author Hannes Kuchelmeister
* @version 1.0
* @param <T>
* the generic type
*/
public class Container<T> {
/** The data. */
private final T data;
/** The next. */
private Container<T> next;
/**
* Instantiates a new container.
*
* @param data
* the data
*/
public Container(final T data) {
this.data = data;
this.next = null;
}
/**
* Sets the next.
*
* @param next
* the new next
*/
public void setNext(final Container<T> next) {
this.next = next;
}
/**
* Gets the next.
*
* @return the next
*/
public Container<T> getNext() {
return next;
}
/**
* Gets the data.
*
* @return the data
*/
public T getData() {
return data;
}
}

View File

@@ -0,0 +1,224 @@
package edu.kit.informatik.list;
/**
* The Class MinimaList.
*
* @author Hannes Kuchelmeister
* @version 1.0
* @param <T>
* the generic type
*/
public class MinimaList<T> {
/** The first container that stores the first element of the list. */
private Container<T> firstContainer;
/** The last container that stores the last element of the list. */
private Container<T> lastContainer;
/** The number of elements of the list. */
private int size;
/**
* Instantiates a new minima list.
*/
public MinimaList() {
size = 0;
firstContainer = null;
lastContainer = null;
}
/**
* Adds an element at the end of the list.
*
* @param element
* the account
*/
public void add(final T element) {
if (firstContainer == null) {
this.firstContainer = new Container<T>(element);
this.lastContainer = this.firstContainer;
} else {
final Container<T> tmp = new Container<T>(element);
this.lastContainer.setNext(tmp);
this.lastContainer = tmp;
}
this.size++;
}
/**
* Gets the index of an element.
*
* @param element
* the account
* @return the index
*/
public int getIndex(final T element) {
final int pos = -1;
int i = 0;
Container<T> pointer = firstContainer;
while (pointer != null) {
if (pointer.getData().equals(element)) {
return i;
}
pointer = pointer.getNext();
i++;
}
return pos;
}
/**
* Adds an element to the list at the given index.
*
* @param element
* the element
* @param index
* the index
*/
public void add(final T element, final int index) {
if (index >= size || index < 0) {
this.add(element);
} else if (index == 0) {
final Container<T> tmpContainer = new Container<T>(element);
tmpContainer.setNext(firstContainer);
firstContainer = tmpContainer;
this.size++;
} else {
final Container<T> pointer = getContainer(index - 1);
final Container<T> tmpContainer = new Container<T>(element);
tmpContainer.setNext(pointer.getNext());
pointer.setNext(tmpContainer);
this.size++;
}
}
/**
* Removes the element at the index from the list.
*
* @param index
* the index
* @return true, if successful
*/
public boolean remove(final int index) {
if (index >= size || index < 0) {
return false;
} else if (index == 0) {
firstContainer = firstContainer.getNext();
this.size--;
return true;
} else if (index == size - 1) {
lastContainer = getContainer(index - 1);
lastContainer.setNext(null);
this.size--;
return true;
} else {
this.size--;
return true;
}
}
/**
* Gets the first element of the list.
*
* @return the first
*/
public T getFirst() {
return firstContainer.getData();
}
/**
* Gets the last element of the list.
*
* @return the last
*/
public T getLast() {
return lastContainer.getData();
}
/**
* Gets the container.
*
* @param index
* the index
* @return the container
*/
private Container<T> getContainer(final int index) {
if (index < 0 || index >= size) {
return null;
}
Container<T> pointer = firstContainer;
int i = 0;
while (i < index) {
pointer = pointer.getNext();
i++;
}
return pointer;
}
/**
* Gets the element at given index.
*
* @param index
* the index
* @return the element
*/
public T get(final int index) {
if (index < 0 || index >= size) {
return null;
}
return this.getContainer(index).getData();
}
/**
* Checks if element is part of the list.
*
* @param element
* the element
* @return true, if element is part of list
*/
public boolean contains(final T element) {
Container<T> pointer = firstContainer;
while (pointer != null) {
if (pointer.getData().equals(element)) {
return true;
}
pointer = pointer.getNext();
}
return false;
}
/**
* Size.
*
* @return the size of the list
*/
public int size() {
return size;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String ret = "";
Container<T> pointer = firstContainer;
while (pointer != null) {
ret += "\t" + pointer.getData().toString() + "\n";
pointer = pointer.getNext();
}
return "\t" + ret.trim();
}
/**
* Gets the first container.
*
* @return the first container
*/
public Container<T> getFirstContainer() {
return firstContainer;
}
}

View File

@@ -0,0 +1,50 @@
package edu.kit.informatik.list;
/**
* 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;
}
}