Christian Baltzer
5 years ago
4 changed files with 193 additions and 0 deletions
-
15src/main/java/ugsbo/com/notenSpeicher/Auswertung.java
-
77src/main/java/ugsbo/com/notenSpeicher/Noten.java
-
38src/main/java/ugsbo/com/notenSpeicher/NotenKette.java
-
63src/test/java/com/ugsbo/notenSpeicher/Test_NotenChain.java
@ -0,0 +1,15 @@ |
|||||
|
package ugsbo.com.notenSpeicher; |
||||
|
|
||||
|
public class Auswertung { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
NotenKette Work = new NotenKette("Prog 1", 1); |
||||
|
Work.add("Prog 2", 2); |
||||
|
Work.add("Wi", 3); |
||||
|
|
||||
|
System.out.println(Work.durchschnitt() + "\n"); |
||||
|
|
||||
|
System.out.print(Work.toString()); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,77 @@ |
|||||
|
package ugsbo.com.notenSpeicher; |
||||
|
|
||||
|
import java.security.MessageDigest; |
||||
|
|
||||
|
public class Noten { |
||||
|
|
||||
|
|
||||
|
String Fach; |
||||
|
int Note; |
||||
|
double durchschnitt; |
||||
|
Noten vorher; |
||||
|
|
||||
|
String ownHash; |
||||
|
String previousHash; |
||||
|
|
||||
|
public Noten(String eingabeFach, int eingabeNote) { |
||||
|
Fach = eingabeFach; |
||||
|
Note = eingabeNote; |
||||
|
durchschnitt = eingabeNote; |
||||
|
|
||||
|
vorher = null; |
||||
|
previousHash = null; |
||||
|
|
||||
|
ownHash = createNewHash(eingabeFach); |
||||
|
} |
||||
|
|
||||
|
public Noten(String eingabeFach, int eingabeNote, String previousHash, double durchschnitt, Noten vorher) { |
||||
|
Fach = eingabeFach; |
||||
|
Note = eingabeNote; |
||||
|
ownHash = createNewHash(eingabeFach+previousHash); |
||||
|
|
||||
|
this.durchschnitt = (durchschnitt+eingabeNote)/2; |
||||
|
|
||||
|
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 String getFach() { |
||||
|
return Fach; |
||||
|
} |
||||
|
|
||||
|
public int getNote() { |
||||
|
return Note; |
||||
|
} |
||||
|
|
||||
|
public double getDurchschnitt() { |
||||
|
return durchschnitt; |
||||
|
} |
||||
|
|
||||
|
public Noten getVorher() { |
||||
|
return vorher; |
||||
|
} |
||||
|
|
||||
|
public String getOwnHash() { |
||||
|
return ownHash; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,38 @@ |
|||||
|
package ugsbo.com.notenSpeicher; |
||||
|
|
||||
|
public class NotenKette { |
||||
|
|
||||
|
Noten Workingobjekt; |
||||
|
|
||||
|
public NotenKette(String eingabeFach, int eingabeNote) { |
||||
|
Workingobjekt = new Noten(eingabeFach, eingabeNote); |
||||
|
} |
||||
|
|
||||
|
public double durchschnitt() { |
||||
|
return Workingobjekt.getDurchschnitt(); |
||||
|
} |
||||
|
|
||||
|
public void add(String eingabeFach, int eingabeNote) { |
||||
|
if (eingabeNote == 0) return; |
||||
|
|
||||
|
Noten newWorkingObjekt = new Noten(eingabeFach,eingabeNote, Workingobjekt.previousHash, Workingobjekt.durchschnitt, Workingobjekt); |
||||
|
Workingobjekt = newWorkingObjekt; |
||||
|
} |
||||
|
|
||||
|
public String toString() { |
||||
|
String erg = ""; |
||||
|
Noten temp = Workingobjekt; |
||||
|
|
||||
|
while (true) { |
||||
|
erg += temp.getFach() + "\t"+ temp.getNote() +"\n"; |
||||
|
|
||||
|
temp = temp.getVorher(); |
||||
|
|
||||
|
if (temp == null) { |
||||
|
return erg; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,63 @@ |
|||||
|
package com.ugsbo.notenSpeicher; |
||||
|
|
||||
|
import static org.junit.Assert.*; |
||||
|
import org.junit.Before; |
||||
|
import org.junit.Test; |
||||
|
import ugsbo.com.notenSpeicher.NotenKette; |
||||
|
|
||||
|
public class Test_NotenChain { |
||||
|
|
||||
|
public NotenKette Workingobjekt; |
||||
|
|
||||
|
@Before |
||||
|
public void setUp() throws Exception { |
||||
|
Workingobjekt = new NotenKette("first", 2); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void hinzufügen() { |
||||
|
String eingabeFach = "Mathe"; |
||||
|
int eingabeNote = 2; |
||||
|
|
||||
|
double erwartet = 2; |
||||
|
double ergebnis; |
||||
|
|
||||
|
Workingobjekt.add(eingabeFach, eingabeNote); |
||||
|
ergebnis = Workingobjekt.durchschnitt(); |
||||
|
|
||||
|
assertEquals(erwartet, ergebnis, 0.1); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void hinzufügenMehrAlsEinmal() { |
||||
|
String eingabeFach = "Mathe"; |
||||
|
int eingabeNote = 2; |
||||
|
double ergebnis; |
||||
|
double erwartet = 2; |
||||
|
|
||||
|
Workingobjekt.add(eingabeFach, eingabeNote); |
||||
|
Workingobjekt.add(eingabeFach, eingabeNote); |
||||
|
Workingobjekt.add(eingabeFach, eingabeNote); |
||||
|
|
||||
|
ergebnis = Workingobjekt.durchschnitt(); |
||||
|
|
||||
|
assertEquals(erwartet, ergebnis, 0.1); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void hinzufügenistNull() { |
||||
|
String eingabeFach = "Mathe"; |
||||
|
int eingabeNote = 2; |
||||
|
int eingabeNotezwei = 0; |
||||
|
double ergebnis; |
||||
|
double erwartet = 2; |
||||
|
|
||||
|
Workingobjekt.add(eingabeFach, eingabeNote); |
||||
|
Workingobjekt.add(eingabeFach, eingabeNotezwei); |
||||
|
|
||||
|
ergebnis = Workingobjekt.durchschnitt(); |
||||
|
|
||||
|
assertEquals(erwartet, ergebnis, 0.1); |
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue