From b2523a83850726e86489cc6d01d0810ce1a5fd51 Mon Sep 17 00:00:00 2001 From: Joey Eamigh <55670930+JoeyEamigh@users.noreply.github.com> Date: Wed, 29 Nov 2023 20:06:47 -0500 Subject: [PATCH] assn07 --- src/assn07/Account.java | 50 +++++++++ src/assn07/Main.java | 82 +++++++++++++++ src/assn07/Map.java | 66 ++++++++++++ src/assn07/PasswordManager.java | 178 ++++++++++++++++++++++++++++++++ 4 files changed, 376 insertions(+) create mode 100644 src/assn07/Account.java create mode 100644 src/assn07/Main.java create mode 100644 src/assn07/Map.java create mode 100644 src/assn07/PasswordManager.java diff --git a/src/assn07/Account.java b/src/assn07/Account.java new file mode 100644 index 0000000..2fbcee9 --- /dev/null +++ b/src/assn07/Account.java @@ -0,0 +1,50 @@ +package assn07; + +public class Account { + private K _website; + private V _password; + private Account _next; + + public Account(K website, V password) { + _website = website; + _password = password; + _next = null; + } + + public K getWebsite() { + return _website; + } + + public K getKey() { + return getWebsite(); + } + + public void setWebsite(K website) { + this._website = website; + } + + public V getPassword() { + return _password; + } + + public V getValue() { + return getPassword(); + } + + public void setPassword(V password) { + this._password = password; + } + + public void setValue(V value) { + setPassword(value); + } + + public Account getNext() { + return _next; + } + + public void setNext(Account next) { + _next = next; + } + +} diff --git a/src/assn07/Main.java b/src/assn07/Main.java new file mode 100644 index 0000000..5d9d433 --- /dev/null +++ b/src/assn07/Main.java @@ -0,0 +1,82 @@ +package assn07; + +import java.util.List; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Map passwordManager = new PasswordManager<>(); + + System.out.println("Enter Master Password"); + while (!passwordManager.checkMasterPassword(scanner.nextLine())) + System.out.println("Enter Master Password"); + + String command = scanner.nextLine(); + + while (!command.equals("Exit")) { + switch (command) { + case "New password": + String website = scanner.nextLine(); + String password = scanner.nextLine(); + + passwordManager.put(website, password); + System.out.println("New password added"); + + break; + case "Get password": + website = scanner.nextLine(); + String result = passwordManager.get(website); + + if (result == null) + System.out.println("Account does not exist"); + else + System.out.println(result); + + break; + case "Delete account": + website = scanner.nextLine(); + result = passwordManager.remove(website); + + if (result == null) + System.out.println("Account does not exist"); + else + System.out.println("Account deleted"); + + break; + case "Check duplicate password": + password = scanner.nextLine(); + List list = passwordManager.checkDuplicate(password); + + if (list.isEmpty()) + System.out.println("No account uses that password"); + else + System.out.println("Websites using that password:"); + for (String key : list) + System.out.println(key); + + break; + + case "Get accounts": + System.out.println("Your accounts:"); + for (String key : passwordManager.keySet()) + System.out.println(key); + + break; + case "Generate random password": + int length = scanner.nextInt(); + scanner.nextLine(); + System.out.println(passwordManager.generateRandomPassword(length)); + + break; + default: + System.out.println("Command not found"); + break; + } + + command = scanner.nextLine(); + } + + scanner.close(); + } +} diff --git a/src/assn07/Map.java b/src/assn07/Map.java new file mode 100644 index 0000000..b9d2687 --- /dev/null +++ b/src/assn07/Map.java @@ -0,0 +1,66 @@ +package assn07; + +import java.util.List; +import java.util.Set; + +public interface Map { + + /** + * Creates an Account object using the key value pair, + * and inserts the object at the appropriate index + * based on the hash of they key. If the key already + * exists in map, update its value. + * @param key: the website name + * @param value: the password + */ + void put(K key, V value); + + /** + * Returns the value associated with the given key. + * This operation should have O(1) runtime. + * If the key is not in the array, return null. + * @param key + * @return the value (password) associated with that key + */ + V get(K key); + + /** + * Returns the number of key-value mappings in the map. + * @return the number of accounts in the map. + */ + int size(); + + /** + * Returns a Set of all the keys (websites) contained in this map. + * @return A set of the keys contained in the map + */ + Set keySet(); + + /** + * Removes the Key and value pair from the map + * and returns the removed value. + * If the key is not in the array, return null. + * @param key to be removed + * @return the value (password) that was removed + */ + V remove(K key); + + /** + * Returns an Arraylist of the website names + * that have a password matching the given value + * @return A List containing the keys of accounts whose password + * match the given value + */ + List checkDuplicate(V value); + + /** + * Checks to see if the entered Master Password matches + * the password stored in MASTER_PASSWORD + * @param enteredPassword - the password the user typed in + * @return true if passwords match, false otherwise + */ + boolean checkMasterPassword(String enteredPassword); + + String generateRandomPassword(int length); + +} diff --git a/src/assn07/PasswordManager.java b/src/assn07/PasswordManager.java new file mode 100644 index 0000000..4dfbf5c --- /dev/null +++ b/src/assn07/PasswordManager.java @@ -0,0 +1,178 @@ +package assn07; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; + +public class PasswordManager implements Map { + private static final String MASTER_PASSWORD = "password321"; + private Account[] _passwords; + + @SuppressWarnings("unchecked") + public PasswordManager() { + _passwords = new Account[50]; + } + + @Override + public void put(K key, V value) { + int hash = Math.abs(key.hashCode()); + int index = hash % _passwords.length; + + if (_passwords[index] == null) + _passwords[index] = new Account(key, value); + else { + Account current = _passwords[index]; + + while (true) { + if (current.getKey().equals(key)) { + current.setValue(value); + return; + } + + if (current.getNext() == null) { + current.setNext(new Account(key, value)); + return; + } + + current = current.getNext(); + } + + } + } + + @Override + public V get(K key) { + int hash = Math.abs(key.hashCode()); + int index = hash % _passwords.length; + + if (_passwords[index] == null) + return null; + + Account current = _passwords[index]; + while (current != null) { + if (current.getKey().equals(key)) + return current.getValue(); + + current = current.getNext(); + } + + return null; + } + + @Override + public int size() { + int size = 0; + + for (int i = 0; i < _passwords.length; i++) + if (_passwords[i] != null) { + Account current = _passwords[i]; + while (current != null) { + size++; + current = current.getNext(); + } + } + + return size; + } + + @Override + public Set keySet() { + Set keys = new HashSet(); + + for (int i = 0; i < _passwords.length; i++) { + if (_passwords[i] == null) + continue; + + Account current = _passwords[i]; + while (current != null) { + keys.add(current.getKey()); + current = current.getNext(); + } + } + + return keys; + } + + @Override + public V remove(K key) { + int hash = Math.abs(key.hashCode()); + int index = hash % _passwords.length; + + if (_passwords[index] == null) + return null; + + Account current = _passwords[index]; + Account previous = null; + while (current != null) { + if (current.getKey().equals(key)) { + if (previous == null) + _passwords[index] = current.getNext(); + else + previous.setNext(current.getNext()); + + return current.getValue(); + } + + previous = current; + current = current.getNext(); + } + + return null; + } + + @Override + public List checkDuplicate(V value) { + List keys = new ArrayList(); + + for (int i = 0; i < _passwords.length; i++) { + if (_passwords[i] == null) + continue; + + Account current = _passwords[i]; + while (current != null) { + if (current.getValue().equals(value)) + keys.add(current.getKey()); + + current = current.getNext(); + } + } + + return keys; + } + + @Override + public boolean checkMasterPassword(String enteredPassword) { + if (enteredPassword.equals(MASTER_PASSWORD)) + return true; + + return false; + } + + /* + * Generates random password of input length + */ + @Override + public String generateRandomPassword(int length) { + int leftLimit = 48; // numeral '0' + int rightLimit = 122; // letter 'z' + int targetStringLength = length; + Random random = new Random(); + + String generatedString = random.ints(leftLimit, rightLimit + 1) + .filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97)) + .limit(targetStringLength) + .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) + .toString(); + + return generatedString; + } + + /* + * Used for testing, do not change + */ + public Account[] getPasswords() { + return _passwords; + } +}