From d06c407c17f49c318ac2f90a26f5ce4d5a55f1c6 Mon Sep 17 00:00:00 2001 From: fdai6352 Date: Fri, 18 Feb 2022 13:08:33 +0100 Subject: [PATCH] refactored if statements to switch cases --- src/main/java/Vault.java | 120 +++++++++++++++++++---------------- src/test/java/VaultTest.java | 5 +- 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/main/java/Vault.java b/src/main/java/Vault.java index 9b5b8a5..318c603 100644 --- a/src/main/java/Vault.java +++ b/src/main/java/Vault.java @@ -21,10 +21,10 @@ public class Vault implements VaultInterface { OutputStream outputS = System.out; private CredentialRepository credentialRepository = new CredentialRepository(); - public static void main (String args[]) { + /*public static void main (String args[]) { Vault vault = new Vault(); vault.credentialMenu(); - } + }*/ private void println(String output) { @@ -51,25 +51,36 @@ public class Vault implements VaultInterface { String input = scan.nextLine(); - if (input.equals("e")) { - config = false; - } else if (input.equals("l")) { - println("Set PW length:"); - String inputPw = scan.nextLine(); - setPWLength(inputPw); - } else if (input.equals("h")) { - println("Should your PW have capitals? Type in yes or no."); - String inputC = scan.nextLine(); - setCapital(inputC); - } else if (input.equals("s")){ - println("Should you PW have special characters? Type in yes or no."); - String inputS = scan.nextLine(); - setSpecialChar(inputS); - } else if (input.equals("n")) { - println("Should you PW have numbers? Type in yes or no."); - String inputN = scan.nextLine(); - setNumbers(inputN); + switch(input) { + case "e": + config = false; + break; + + case "l": + println("Set PW length:"); + String inputPw = scan.nextLine(); + setPWLength(inputPw); + break; + + case "h": + println("Should your PW have capitals? Type in yes or no."); + String inputC = scan.nextLine(); + setCapital(inputC); + break; + + case "s": + println("Should you PW have special characters? Type in yes or no."); + String inputS = scan.nextLine(); + setSpecialChar(inputS); + break; + + case "n": + println("Should you PW have numbers? Type in yes or no."); + String inputN = scan.nextLine(); + setNumbers(inputN); + break; } + } public void setPWLength(String pwLength) { @@ -157,40 +168,41 @@ public class Vault implements VaultInterface { String input = scan.nextLine(); - - if (input.equals("e")) { - credentialM = false; - } - - if (input.equals("a")) { - println("Type in username"); - userName = scan.nextLine(); - - println("Type in password"); - password = scan.nextLine(); - addCredential(userName, password); - } - - if (input.equals("c")) { - println("Type in ID or username"); - - String str = scan.nextLine(); - showCredential(str); - - } else if (input.equals("l")) { - println("Type ID to select credential:"); - newID = Integer.parseInt(scan.nextLine()); - println("-change username: u\n-change password: p"); - input = scan.nextLine(); - - if (input.equals("u")) { - decision = true; - }else{ - decision = false; - } - println("Type new:"); - newString = scan.nextLine(); - //editCredential(newID, newString, decision); + switch(input) { + case "e": + credentialM = false; + break; + + case "a": + println("Type in username"); + userName = scan.nextLine(); + + println("Type in password"); + password = scan.nextLine(); + addCredential(userName, password); + break; + + case "c": + println("Type in ID or username"); + + String str = scan.nextLine(); + showCredential(str); + break; + + case "l": + println("Type ID to select credential:"); + newID = Integer.parseInt(scan.nextLine()); + println("-change username: u\n-change password: p"); + + input = scan.nextLine(); + + if (!input.equals("u")) { + decision = false; + } + println("Type new:"); + newString = scan.nextLine(); + //editCredential(newID, newString, decision); + break; } } } diff --git a/src/test/java/VaultTest.java b/src/test/java/VaultTest.java index 9dbc3dd..30dbada 100644 --- a/src/test/java/VaultTest.java +++ b/src/test/java/VaultTest.java @@ -1,7 +1,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -12,8 +11,6 @@ import static org.junit.jupiter.api.Assertions.*; public class VaultTest { - //Vault vlt = new Vault(); - static Vault vlt; @BeforeAll @@ -74,7 +71,7 @@ public class VaultTest { vlt.credentialMenu(); assertTrue(outputStream.toString().contains("Type")); assertEquals(1,vlt.newID); - assertEquals(true,vlt.decision); + assertTrue(vlt.decision); assertEquals("peter",vlt.newString); }