Question
Java: Please help me modify the following code according to the instructions. Thank you. Instructions: Account class Superclass Instance variables clearPassword String Must be at
Java: Please help me modify the following code according to the instructions. Thank you.
Instructions:
- Account class
- Superclass
- Instance variables
- clearPassword
- String
- Must be at least 8 characters long
- encryptedPassword : String
- key
- int
- Must be between 1 and 10(inclusive)
- accountId - A unique integer that identifies each account
- nextIDNum a static int that starts at 1000 and is used to generate the accountID
- no other instance variables needed.
- clearPassword
- Default constructor set all instance variables to a default value.
- Parameterized constructor
- Takes in clearPassword, key.
- Calls encrypt method to create encryptedPassword
- setClearPassWord(String newPassword)
- Takes in a new clear text password.
- Calls encrypt method as the encrypted password would need to change.
- no encryptedPassword mutator method
- setKey(int newKey)
- Takes in a new key
- calls encrypt method as the encrypted password would need to change.
- accountId mutator - uses the static variable nextIDNum to retrieve the next available userID number
- encrypt method
- Uses the instance variables clearPassword and key to encrypt the password.
- Stores the encrypted password in the encryptedPassword instance variable
- toString - returns a nicely formatted String representing the instance variables of the Account class
NOTE: Your Account class should ensure the clearPassword is valid as described in the requirements. If not write an error message to the standard output and continue. Set the clearPassword and encyrptedPassword to the empty String.
Code:
public class Account {
private String clearPassword; private String encryptedPassword; private String key; private int accountId; private static int nextIDNum = 1000; /** * Constructor that initializes the variables. */ public Account() { clearPassword = ""; encryptedPassword = ""; key = ""; accountId = 0; }
/** * Constructor that sets the variables equal to the input information. It calls on the passwordCheck * method to make sure the password meets the requirements. It calls the encrypt method to encrypt * the password. It calls the addAccountId method to give the Account and ID number. */ public Account(String newClearPassword, String newKey){ clearPassword = newClearPassword; key = newKey; passwordCheck(); if(passwordCheck() == false) { clearPassword = ""; encryptedPassword = ""; }
addAccountId(); encrypt(); } /** * Method to set the password. It calls on the passwordCheck method to make sure the password * meets the requirements. It also calls the encrypt method to encrypt the password. */ public void setClearPassword(String newClearPassword) { clearPassword = newClearPassword; passwordCheck(); if(passwordCheck() == false) { clearPassword = ""; encryptedPassword = ""; } encrypt(); }
/** * Retrieves the clear password */ public String getClearPassword(){ return clearPassword; } /** * Retrieves the encrypted password */ public String getEncryptedPassword() { return encryptedPassword; } /** * Retrieves the password ke */ public String getKey(){ return key; }
/** * Sets the key to the input key. Calls the encrypt method to use key for encryption. */ public void setKey(String newKey) { key = newKey; encrypt(); }
/** * Returns the account ID */ public int getAccountId() { return accountId; }
/** * Method to make sure the password meets the requirements of one special character, one number and 8 characters * long or more. */ private boolean passwordCheck() { int asciiValueClearPswd[] = new int [100]; int asciiValueClearPswdTwo[] = new int [100]; for(int i = 0; i < clearPassword.length(); i++) { asciiValueClearPswd[i] = clearPassword.charAt(i); for(int j = 0; j < clearPassword.length(); j++){ asciiValueClearPswdTwo[j] = clearPassword.charAt(j); if((asciiValueClearPswd[i] >= 35 && asciiValueClearPswd[i] <= 38) && (asciiValueClearPswdTwo[j] >= 48 && asciiValueClearPswdTwo[j] <= 57) && (clearPassword.length() > 7)){ return true; } }
} return false; }
/** * The method that encrypts the clear password. Uses Vigenere Cipher for Ascii characters */ private void encrypt() {
int j = 0; int encryptNum[] = new int[100]; int asciiValueKey[] = new int[100]; int asciiValueClearPswd[] = new int [100]; byte encryptByte [] = new byte [clearPassword.length()]; for(int i = 0; i < clearPassword.length(); i++) { asciiValueKey[i] = key.charAt(j); asciiValueClearPswd[i] = clearPassword.charAt(i); encryptNum[i] = asciiValueKey[i] - 33; encryptNum[i] = (encryptNum[i] + asciiValueClearPswd[i]) - 90; encryptByte[i] = (byte) encryptNum[i];
if(encryptNum[i] < 33) { encryptNum[i] = (encryptNum[i] - 32) + 122; encryptByte[i] = (byte) encryptNum[i]; } if(key.length() - j == 1) { j = 0; } else { j++; } encryptedPassword = new String(encryptByte); } }
/** * Adds an account ID number to the Account. */ public int addAccountId() { accountId = nextIDNum; nextIDNum++; return accountId; }
/** * Returns the information for the account. */ public String toString(){ String info; info = String.format("%-5d %s %s %s ", getAccountId(), getEncryptedPassword(), getClearPassword(), getKey()); return info; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started