Christian Baltzer
5 years ago
5 changed files with 182 additions and 0 deletions
-
5pom.xml
-
15src/main/java/ugsbo/com/buchhaltung/Auswertung.java
-
67src/main/java/ugsbo/com/buchhaltung/Block.java
-
32src/main/java/ugsbo/com/buchhaltung/Blockchain.java
-
63src/test/java/com/ugsbo/Buchhaltung/Test_Blockchain.java
@ -0,0 +1,15 @@ |
|||
package ugsbo.com.buchhaltung; |
|||
|
|||
public class Auswertung { |
|||
|
|||
public static void main(String[] args) { |
|||
// TODO Auto-generated method stub |
|||
Blockchain Work = new Blockchain(); |
|||
Work.add(50); |
|||
Work.add(-20); |
|||
|
|||
System.out.println(Work.kontostand()); |
|||
System.out.print(Work.toString()); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,67 @@ |
|||
package ugsbo.com.buchhaltung; |
|||
|
|||
import java.security.MessageDigest; |
|||
|
|||
public class Block { |
|||
|
|||
|
|||
int data; |
|||
int kontostand; |
|||
Block vorher; |
|||
|
|||
String ownHash; |
|||
String previousHash; |
|||
|
|||
|
|||
//erste Block |
|||
public Block(int data) { |
|||
this.data = data; |
|||
ownHash = createNewHash(Integer.toString(data)); |
|||
|
|||
kontostand = this.data; |
|||
|
|||
this.vorher = null; |
|||
this.previousHash = null; |
|||
} |
|||
|
|||
//Alle anderen Blöcke |
|||
public Block(int data, Block vorher, String previousHash, int konto) { |
|||
this.data = data; |
|||
ownHash = createNewHash(Integer.toString(data)+previousHash); |
|||
|
|||
kontostand = konto + this.data; |
|||
|
|||
this.vorher = vorher; |
|||
this.previousHash = previousHash; |
|||
} |
|||
|
|||
private String createNewHash(String input) { |
|||
try { |
|||
MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
|||
//Applies sha256 to our input, |
|||
byte[] hash = digest.digest(input.getBytes("UTF-8")); |
|||
StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal |
|||
for (int i = 0; i < hash.length; i++) { |
|||
String hex = Integer.toHexString(0xff & hash[i]); |
|||
if(hex.length() == 1) hexString.append('0'); |
|||
hexString.append(hex); |
|||
} |
|||
return hexString.toString(); |
|||
} |
|||
catch(Exception e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
} |
|||
|
|||
public int getKontostand() { |
|||
return kontostand; |
|||
} |
|||
|
|||
public Block getVorher() { |
|||
return vorher; |
|||
} |
|||
|
|||
public String getHash() { |
|||
return ownHash; |
|||
} |
|||
} |
@ -0,0 +1,32 @@ |
|||
package ugsbo.com.buchhaltung; |
|||
|
|||
import com.google.gson.*; |
|||
|
|||
public class Blockchain { |
|||
|
|||
Block Workingobjekt; |
|||
|
|||
|
|||
public Blockchain() { |
|||
Workingobjekt = new Block(0); |
|||
} |
|||
|
|||
|
|||
public void add(int eingabe) { |
|||
Block newWorkingobjekt = new Block(eingabe, Workingobjekt, Workingobjekt.getHash(), Workingobjekt.getKontostand()); |
|||
|
|||
Workingobjekt = newWorkingobjekt; |
|||
} |
|||
|
|||
|
|||
public int kontostand() { |
|||
return Workingobjekt.getKontostand(); |
|||
} |
|||
|
|||
public String toString() { |
|||
String JSON = new GsonBuilder().setPrettyPrinting().create().toJson(Workingobjekt); |
|||
return JSON; |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,63 @@ |
|||
package com.ugsbo.Buchhaltung; |
|||
|
|||
import static org.junit.Assert.*; |
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
import ugsbo.com.buchhaltung.Blockchain; |
|||
|
|||
public class Test_Blockchain { |
|||
|
|||
public Blockchain Workingobjekt; |
|||
|
|||
@Before |
|||
public void setUp() throws Exception { |
|||
Workingobjekt = new Blockchain(); |
|||
} |
|||
|
|||
@Test |
|||
public void hinzufügen() { |
|||
int eingabe = 500; |
|||
int ergebnis; |
|||
|
|||
Workingobjekt.add(eingabe); |
|||
ergebnis = Workingobjekt.kontostand(); |
|||
|
|||
assertEquals("eingabe und Ergebnis sind gleich", eingabe, ergebnis); |
|||
} |
|||
|
|||
@Test |
|||
public void hinzufügenNegativ() { |
|||
int eingabe = -500; |
|||
int ergebnis; |
|||
|
|||
Workingobjekt.add(eingabe); |
|||
ergebnis = Workingobjekt.kontostand(); |
|||
|
|||
assertEquals("eingabe und Ergebnis sind gleich", eingabe, ergebnis); |
|||
} |
|||
|
|||
@Test |
|||
public void hinzufügenIstNull() { |
|||
int eingabe = 0; |
|||
int ergebnis; |
|||
|
|||
Workingobjekt.add(eingabe); |
|||
ergebnis = Workingobjekt.kontostand(); |
|||
|
|||
assertEquals("eingabe und Ergebnis sind gleich", eingabe, ergebnis); |
|||
} |
|||
|
|||
@Test |
|||
public void hinzufügenMehrAlsEinmal() { |
|||
int eingabe = 100; |
|||
int erwartet = 300; |
|||
int ergebnis; |
|||
|
|||
Workingobjekt.add(eingabe); |
|||
Workingobjekt.add(eingabe); |
|||
Workingobjekt.add(eingabe); |
|||
ergebnis = Workingobjekt.kontostand(); |
|||
|
|||
assertEquals("eingabe und Ergebnis sind gleich", erwartet, ergebnis); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue