Browse Source

Numbers Testklasse

master
d4v1davidson 2 years ago
parent
commit
09e3f7bddd
6 changed files with 296 additions and 1 deletions
  1. 116
    0
      Credentials.java
  2. 46
    0
      CredentialsTest.java
  3. 55
    0
      Helpers.java
  4. 7
    1
      Numbers.java
  5. 56
    0
      NumbersTest.java
  6. 16
    0
      prog3.iml

+ 116
- 0
Credentials.java View File

import java.util.Scanner;

public class Credentials {
private String login;
private String password;
private static Scanner s = new Scanner(System.in);

/*public Credentials(String login, String password) {
this.login = login;
this.password = password;
}*/

public static void main(String[] args) {
if (login()) {
register();
}
boolean valid = false;
//Scanner s = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.print("Benutzername: ");
String login = s.next();
System.out.print("Passwort: ");
String pwd = s.next();
if (valid(login, pwd)) {
System.out.println("Hello World");
valid = true;
}
if(valid) {
i = 3;
} else {
System.out.println("Der Benutzername oder das Passwort ist falsch. Übrige Versuche: " + (2 - i));
}
}
s.close();
if (!valid) {
System.out.println("Sie sind gesperrt.");
}
}

public static boolean valid(String login, String password) {
String[] data = Helpers.readTextdatei("passwd");
for (int i = 0; i < data.length; i++) {
if (data[i].equals(login)) {
if (Helpers.getHash(password).equals(data[i + 1])) {
return true;
} else {
return false;
}
}
}
return false;
}

public static boolean login() {
//Scanner s2 = new Scanner(System.in);
System.out.print("Tippen Sie '0', um sich zu registrieren; Tippen Sie '1', um sich anzumelden: ");
String answer = s.next();
//s2.close();
if (answer.equals("0")) {
return true;
} else {
return false;
}
}

public static void register() {
//Scanner s3 = new Scanner(System.in);
System.out.print("Ihr Benutzername: ");
String user = s.next();
if (checkUser(user)) {
System.out.print("Ihr Passwort: ");
String pwd1 = s.next();
System.out.print("Passwort erneut eingeben: ");
String pwd2 = s.next();
if(pwd1.equals(pwd2)) {
if(checkPassword(pwd1)) {
String[] data = Helpers.readTextdatei("passwd");
pwd1 = Helpers.getHash(pwd1);
String[] lines = new String[data.length + 2];
for (int i = 0; i < data.length; i++) {
lines[i] = data[i];
}
lines[data.length] = user;
lines[data.length + 1] = pwd1;
Helpers.writeTextdatei("passwd", lines);
System.out.println("Der Benutzer wurde erfolgreich hinzugefügt. Sie können sich nun anmelden:");
} else {
System.out.println("Das Passwort entspricht nicht den Sicherheitsvorgaben.");
}
} else {
System.out.println("Die Passwörter stimmen nicht überein.");
}
} else {
System.out.println("Dieser Benutzername ist bereits vergeben.");
}
//s3.close();
}

public static boolean checkUser(String user) {
String[] data = Helpers.readTextdatei("passwd");
for (int i = 0; i < data.length; i++) {
if (user.equals(data[i])) {
return false;
}
i++;
}
return true;
}

public static boolean checkPassword(String pwd) {
if (pwd.length() < 8) {
return false;
}
return true;
}
}

+ 46
- 0
CredentialsTest.java View File

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CredentialsTest {

Credentials credentials = new Credentials();

@Test
void valid() {
//Credentials credentials = new Credentials();
assertTrue(credentials.valid("hofmannol", "GERONIMO"));
assertTrue(credentials.valid("wimmerma", "GEHEIM123"));
assertFalse(credentials.valid("meierandr", "GERONIMO"));
assertFalse(credentials.valid("wimmerma", "ÖFFENTLICH123"));
}

@Test
void login() {
//Credentials credentials = new Credentials();

}

@Test
void register() {
//Credentials credentials = new Credentials();

}

@Test
void checkUser() {
assertTrue(credentials.checkUser("heinz"));
assertTrue(credentials.checkUser("hoffmannol"));
assertFalse(credentials.checkUser("hofmannol"));

//Credentials credentials = new Credentials();
}

@Test
void checkPassword() {
//Credentials credentials = new Credentials();
assertTrue(credentials.checkPassword("12345678"));
assertTrue(credentials.checkPassword("GERONIMO"));
assertFalse(credentials.checkPassword("D1234"));
}
}

+ 55
- 0
Helpers.java View File

import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

public class Helpers {

// Lesen einer Textdatei in ein Stringarray
public static String[] readTextdatei(String name) {
String line;
ArrayList<String> result = new ArrayList<String>();
try {
FileReader fr = new FileReader(name);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null)
result.add(line);
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
return result.toArray(new String[]{});
}

// Schreiben eines Stringarray in eine Textdatei
public static void writeTextdatei(String name, String[] lines) {
try {
FileWriter fw = new FileWriter(name);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
for (int i = 0; i < lines.length; i++)
pw.println(lines[i]);
pw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}

// Berechnen des Hashwerts zu einem String und
// Rückgabe als Hex-Zeichenkette
public static String getHash(String w) {
try {
byte[] bytesOfMessage = w.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
return String.format("%032x", new BigInteger(1, thedigest));
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}

}

+ 7
- 1
Numbers.java View File

public class Numbers { public class Numbers {


private int number;

public static void main(String[] args) { public static void main(String[] args) {
for (int i = 1; i <= 200; i++) { for (int i = 1; i <= 200; i++) {
if (isDivisibleBy(5, i)) { if (isDivisibleBy(5, i)) {
} }
} }


public static int sumWithPredecessor(int number) {
return 2 * number -1;
}

public static boolean sumWithPredecessorIsDivisibleBy( int divisor, int number) { public static boolean sumWithPredecessorIsDivisibleBy( int divisor, int number) {
int s = 2 * number - 1;
int s = sumWithPredecessor(number);
if (isDivisibleBy(divisor, s)) { if (isDivisibleBy(divisor, s)) {
return true; return true;
} else { } else {

+ 56
- 0
NumbersTest.java View File

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class NumbersTest {

@org.junit.jupiter.api.Test
void isDivisibleBy() {
Numbers number = new Numbers();
assertTrue(number.isDivisibleBy(3 ,12));
assertTrue(number.isDivisibleBy(4,12));
assertTrue(number.isDivisibleBy(6,12));
assertTrue(number.isDivisibleBy(12, 12));
assertFalse(number.isDivisibleBy(5,12));

assertTrue(number.isDivisibleBy(3 ,9));
assertTrue(number.isDivisibleBy(9, 9));
assertFalse(number.isDivisibleBy(4,9));
assertFalse(number.isDivisibleBy(5,9));
assertFalse(number.isDivisibleBy(6,9));
}

@org.junit.jupiter.api.Test
void lastDigitIs() {
Numbers number = new Numbers();
assertTrue(number.lastDigitIs(0, 90));
assertTrue(number.lastDigitIs(1, 1));
assertTrue(number.lastDigitIs(1, 41));
assertTrue(number.lastDigitIs(2, 32));
assertTrue(number.lastDigitIs(7, 67));
assertTrue(number.lastDigitIs(9, 10399));

assertFalse(number.lastDigitIs(1, 12));
assertFalse(number.lastDigitIs(2, 23));
assertFalse(number.lastDigitIs(10, 110));
assertFalse(number.lastDigitIs(5, 551));
}

@org.junit.jupiter.api.Test
void sumWithPredecessor() {
Numbers number = new Numbers();
assertEquals(11, number.sumWithPredecessor(6));
assertEquals(0, number.sumWithPredecessor(1));
}

@org.junit.jupiter.api.Test
void sumWithPredecessorIsDivisibleBy() {
Numbers number = new Numbers();
assertTrue(number.sumWithPredecessorIsDivisibleBy(1,14));
assertTrue(number.sumWithPredecessorIsDivisibleBy(3,14));
assertFalse(number.sumWithPredecessorIsDivisibleBy(4,36));
assertTrue(number.sumWithPredecessorIsDivisibleBy(5,18));
assertFalse(number.sumWithPredecessorIsDivisibleBy(10,100));
assertFalse(number.sumWithPredecessorIsDivisibleBy(12,24));
assertTrue(number.sumWithPredecessorIsDivisibleBy(13,85));
}
}

+ 16
- 0
prog3.iml View File

</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component> </component>
</module> </module>

Loading…
Cancel
Save