Add SmartBank

This commit is contained in:
Matti 2024-10-07 16:38:49 +02:00
parent c653aac568
commit 0c43ff5b3e
18 changed files with 225 additions and 5 deletions

Binary file not shown.

1
.idea/misc.xml generated
View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="FrameworkDetectionExcludesConfiguration">

View File

@ -1,9 +1,11 @@
import de.dhbw.parprog.JavaPipe;
import smartBank.TransferSpam;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
JavaPipe.PipeMain();
// JavaPipe.PipeMain();
TransferSpam.main();
}
}

View File

@ -3,7 +3,9 @@ package de.dhbw.parprog;
import de.dhbw.parprog.processemu.Pipe;
import de.dhbw.parprog.processemu.ProcessEmu;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaPipe {
@ -29,11 +31,16 @@ public class JavaPipe {
System.out.println("sup nerd");
int sum = 0;
for (int allResult : allResults) {
sum += allResult;
System.out.println("Got a return of " + allResult);
for (int Result : allResults) {
sum += Result;
System.out.println("Got a return of " + Result);
}
System.out.println("The sum is " + sum);
/*
BufferedReader reader = new BufferedReader(new InputStreamReader(pipes[0].getIn()));
String result = reader.readLine();
*/
}
}

View File

@ -0,0 +1,72 @@
package smartBank;
public class Account {
public static long LOWER_LIMIT = 0;
public static long UPPER_LIMIT = 100000;
private long balance;
private final long accountNumber;
public Account(long AccountNumber){
this(AccountNumber, 0);
}
public Account(long Accountnumber, long initialBalance){
this.balance = initialBalance;
this.accountNumber = Accountnumber;
}
public long getBalance() {
return balance;
}
public void setBalance(long balance) {
if (balance < LOWER_LIMIT){
throw new IllegalAccountStateException();
}
if (balance > UPPER_LIMIT){
throw new IllegalAccountStateException();
}
this.balance = balance;
}
public void withdraw(long amount) throws IllegalAccountStateException{
if (amount > this.balance){
throw new IllegalAccountStateException();
}
this.setBalance( (this.getBalance() - amount) );
}
public void deposit(long amount) throws IllegalAccountStateException{
if((this.getBalance()+amount) > UPPER_LIMIT){
throw new IllegalAccountStateException();
}
this.setBalance( (this.getBalance()+amount) );
}
public static Account getLowerIdAccount(Account a1, Account a2){
if ((a1 != a2) && (a1.accountNumber == a2.accountNumber)){
throw new RuntimeException("2 Accounts with Same ID");
}
if (a1 == a2){
throw new IllegalArgumentException();
}
if (a1.accountNumber < a2.accountNumber){
return a1;
}
return a2;
}
public static Account getHigherIdAccount(Account a1, Account a2){
if (getLowerIdAccount(a1, a2) == a1){
return a2;
}else {
return a1;
}
}
}

View File

@ -0,0 +1,82 @@
package smartBank;
import java.util.Vector;
public class Bank {
static long AccountsInUse = 0;
public Vector<Account> allAccounts = new Vector<Account>();
/**
* Erzeugt ein neues Konto mit initialem Kontostand 0.
* @return das neue Konto
*/
public synchronized Account createAccount() {
Account newAccount = new Account(AccountsInUse++);
allAccounts.add(newAccount);
return newAccount;
}
public void setAllBalances(long value){
for (Account account : this.allAccounts) {
account.setBalance(value);
}
}
/**
* Ruft den aktuellen Kontostand ab
* @param account Konto, dessen Kontostand bestimmt werden soll
* @return der aktuelle Kontostand
* @throws IllegalArgumentException bei ungültigen Parametern
*/
public long getBalance(Account account) throws IllegalArgumentException {
return account.getBalance();
}
/**
* Einzahlen eines bestimmten Betrags
* @param account das Konto, auf den der Betrag eingezahlt werden soll
* @param amount der Betrag (muß >=0 sein)
* @throws IllegalArgumentException bei ungültigen Parametern
* @throws IllegalAccountStateException falls der Kontostand außerhalb des gültigen Wertebereichs geraten würde
*/
public void deposit(Account account, long amount) throws IllegalAccountStateException, IllegalArgumentException {
account.deposit(amount);
}
/**
* Abheben eines bestimmten Betrags
* @param account das Konto, von dem der Betrag abgehoben werden soll
* @param amount der Betrag (muß >=0 sein)
* @throws IllegalArgumentException bei ungültigen Parametern
* @throws IllegalAccountStateException falls der Kontostand außerhalb des gültigen Wertebereichs geraten würde
*/
public void withdraw(Account account, long amount) throws IllegalAccountStateException, IllegalArgumentException {
account.withdraw(amount);
}
/**
* Überweisen eines Betrags von einem Konto auf ein anderes
* @param fromAccount Konto, von dem abgebucht werden soll
* @param toAccount Konto, auf das gutgeschrieben werden soll
* @param amount der zu transferierende Betrag (muß >=0 sein)
* @throws IllegalArgumentException bei ungültigen Parametern
* @throws IllegalAccountStateException falls einer der Kontostände außerhalb des gültigen Wertebereichs geraten würde
*/
public void transfer(Account fromAccount, Account toAccount, long amount) throws IllegalAccountStateException, IllegalArgumentException {
fromAccount.withdraw(amount);
toAccount.deposit(amount);
}
public String toString() {
String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H"};
String result = "";
for (int i = 0; i < this.allAccounts.size(); i++) {
result += (alphabet[i] + " " + allAccounts.get(i).getBalance() + " ");
}
return result;
}
}

View File

@ -0,0 +1,5 @@
package smartBank;
public class IllegalAccountStateException extends RuntimeException {
}

View File

@ -0,0 +1,53 @@
package smartBank;
public class TransferSpam extends Thread{
Bank bank;
Account sender;
Account receiver;
public TransferSpam(Bank bank, Account sender, Account receiver){
this.bank = bank;
this.sender = sender;
this.receiver = receiver;
}
@Override
public void run() {
for (int i = 0; i < 25; i++) {
try {
Thread.sleep(0);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (Account.getLowerIdAccount(this.sender, this.receiver)){
synchronized (Account.getHigherIdAccount(this.sender, this.receiver)){
bank.transfer(this.sender, this.receiver, 1);
System.out.println(bank);
}
}
}
}
public static void main() {
Bank bank = new Bank();
bank.createAccount();
bank.createAccount();
bank.createAccount();
bank.createAccount();
bank.createAccount();
bank.setAllBalances(1000);
TransferSpam t1 = new TransferSpam(bank, bank.allAccounts.get(0), bank.allAccounts.get(1));
TransferSpam t2 = new TransferSpam(bank, bank.allAccounts.get(1), bank.allAccounts.get(0));
TransferSpam t3 = new TransferSpam(bank, bank.allAccounts.get(2), bank.allAccounts.get(4));
TransferSpam t4 = new TransferSpam(bank, bank.allAccounts.get(1), bank.allAccounts.get(3));
TransferSpam t5 = new TransferSpam(bank, bank.allAccounts.get(3), bank.allAccounts.get(2));
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}