This commit is contained in:
Joey Eamigh
2023-11-29 20:06:47 -05:00
parent f1793eafa8
commit b2523a8385
4 changed files with 376 additions and 0 deletions

50
src/assn07/Account.java Normal file
View File

@@ -0,0 +1,50 @@
package assn07;
public class Account<K, V> {
private K _website;
private V _password;
private Account<K, V> _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<K, V> getNext() {
return _next;
}
public void setNext(Account<K, V> next) {
_next = next;
}
}

82
src/assn07/Main.java Normal file
View File

@@ -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<String, String> 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<String> 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();
}
}

66
src/assn07/Map.java Normal file
View File

@@ -0,0 +1,66 @@
package assn07;
import java.util.List;
import java.util.Set;
public interface Map <K, V> {
/**
* 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<K> 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<K> 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);
}

View File

@@ -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<K, V> implements Map<K, V> {
private static final String MASTER_PASSWORD = "password321";
private Account<K, V>[] _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<K, V>(key, value);
else {
Account<K, V> current = _passwords[index];
while (true) {
if (current.getKey().equals(key)) {
current.setValue(value);
return;
}
if (current.getNext() == null) {
current.setNext(new Account<K, V>(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<K, V> 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<K, V> current = _passwords[i];
while (current != null) {
size++;
current = current.getNext();
}
}
return size;
}
@Override
public Set<K> keySet() {
Set<K> keys = new HashSet<K>();
for (int i = 0; i < _passwords.length; i++) {
if (_passwords[i] == null)
continue;
Account<K, V> 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<K, V> current = _passwords[index];
Account<K, V> 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<K> checkDuplicate(V value) {
List<K> keys = new ArrayList<K>();
for (int i = 0; i < _passwords.length; i++) {
if (_passwords[i] == null)
continue;
Account<K, V> 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<K, V>[] getPasswords() {
return _passwords;
}
}