diff --git a/.gradle/8.4/executionHistory/executionHistory.bin b/.gradle/8.4/executionHistory/executionHistory.bin index f837c20..953fc87 100644 Binary files a/.gradle/8.4/executionHistory/executionHistory.bin and b/.gradle/8.4/executionHistory/executionHistory.bin differ diff --git a/.gradle/8.4/executionHistory/executionHistory.lock b/.gradle/8.4/executionHistory/executionHistory.lock index 0eaec55..4ee05a5 100644 Binary files a/.gradle/8.4/executionHistory/executionHistory.lock and b/.gradle/8.4/executionHistory/executionHistory.lock differ diff --git a/.gradle/8.4/fileHashes/fileHashes.bin b/.gradle/8.4/fileHashes/fileHashes.bin index 39af837..1a7eced 100644 Binary files a/.gradle/8.4/fileHashes/fileHashes.bin and b/.gradle/8.4/fileHashes/fileHashes.bin differ diff --git a/.gradle/8.4/fileHashes/fileHashes.lock b/.gradle/8.4/fileHashes/fileHashes.lock index 52e689f..1e7b37e 100644 Binary files a/.gradle/8.4/fileHashes/fileHashes.lock and b/.gradle/8.4/fileHashes/fileHashes.lock differ diff --git a/.gradle/8.4/fileHashes/resourceHashesCache.bin b/.gradle/8.4/fileHashes/resourceHashesCache.bin index 1cbd72b..63e361e 100644 Binary files a/.gradle/8.4/fileHashes/resourceHashesCache.bin and b/.gradle/8.4/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 808e491..fa2d9ae 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/file-system.probe b/.gradle/file-system.probe index 392ab9f..90e2708 100644 Binary files a/.gradle/file-system.probe and b/.gradle/file-system.probe differ diff --git a/.idea/misc.xml b/.idea/misc.xml index 3e79c5f..2dfaca9 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/build/classes/java/main/de/dhbw/parprog/CalcTask.class b/build/classes/java/main/de/dhbw/parprog/CalcTask.class deleted file mode 100644 index b5c615b..0000000 Binary files a/build/classes/java/main/de/dhbw/parprog/CalcTask.class and /dev/null differ diff --git a/build/classes/java/main/de/dhbw/parprog/JavaPipe.class b/build/classes/java/main/de/dhbw/parprog/JavaPipe.class deleted file mode 100644 index de643a9..0000000 Binary files a/build/classes/java/main/de/dhbw/parprog/JavaPipe.class and /dev/null differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/JavaPipe.class.uniqueId0 b/build/tmp/compileJava/compileTransaction/stash-dir/JavaPipe.class.uniqueId0 deleted file mode 100644 index 01e044a..0000000 Binary files a/build/tmp/compileJava/compileTransaction/stash-dir/JavaPipe.class.uniqueId0 and /dev/null differ diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index 2f6d71c..e49d4b1 100644 Binary files a/build/tmp/compileJava/previous-compilation-data.bin and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 5cc946f..3bd8313 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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(); } } diff --git a/src/main/java/de/dhbw/parprog/JavaPipe.java b/src/main/java/de/dhbw/parprog/JavaPipe.java index 5bc1578..80a7014 100644 --- a/src/main/java/de/dhbw/parprog/JavaPipe.java +++ b/src/main/java/de/dhbw/parprog/JavaPipe.java @@ -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(); + */ } } diff --git a/src/main/java/smartBank/Account.java b/src/main/java/smartBank/Account.java new file mode 100644 index 0000000..e5a0f39 --- /dev/null +++ b/src/main/java/smartBank/Account.java @@ -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; + } + } +} diff --git a/src/main/java/smartBank/Bank.java b/src/main/java/smartBank/Bank.java new file mode 100644 index 0000000..c8182b8 --- /dev/null +++ b/src/main/java/smartBank/Bank.java @@ -0,0 +1,82 @@ +package smartBank; + +import java.util.Vector; + + +public class Bank { + static long AccountsInUse = 0; + public Vector allAccounts = new Vector(); + + /** + * 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; + } +} diff --git a/src/main/java/smartBank/IllegalAccountStateException.java b/src/main/java/smartBank/IllegalAccountStateException.java new file mode 100644 index 0000000..e472b45 --- /dev/null +++ b/src/main/java/smartBank/IllegalAccountStateException.java @@ -0,0 +1,5 @@ +package smartBank; + + +public class IllegalAccountStateException extends RuntimeException { +} diff --git a/src/main/java/smartBank/TransferSpam.java b/src/main/java/smartBank/TransferSpam.java new file mode 100644 index 0000000..971cfeb --- /dev/null +++ b/src/main/java/smartBank/TransferSpam.java @@ -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(); + } +}