Java This assignment requires me to modify and add to the code of a previous ass
ID: 3779923 • Letter: J
Question
Java
This assignment requires me to modify and add to the code of a previous assignment
Note please just edit or modify the code I have given below. Please don't write your own program from scratch.
Context of previous assignment in parentheses below
(Companies with lots of employees need to manage user information and passwords. It would be helpful to have the facility to generate a userid and password when a new employee is entered)
In a prior assignment you created the User class
It should meet this criteria:
The class must store first name, last name, phone number, userid, and password.
The class must generate the id based on first initial, lastname.
The class must generate a random based 6 position password.
The private information should all be strings.
The class must allow for changes to these fields.
The class must allow for display of all the fields but password
The class must allow for display of the fields including password
The class must allow for the comparison of userid and password, returning true or false.
Mutator methods should include:
updateName, updatePhone, updatePassword, updateUserID.
All should accept data passed to the method.
Accessor methods must include:
displayInfo
which doesn’t display the password and displays a formatted line using System.out.print
returns void
displayInfoAll
which displays all info including the password on a formatted line using System.out.print
returns void
isPasswordEqual
which compares a password passed to the method with the one stored.
Returns true or false
isUseridEqual
which compares a userid passed to the method with the one stored.
Returns true or false
Helper (private) methods should be created to:
Generate the random password
Generate the userid.
(Completed all the requirements above. Generated a 6 position mixed upper/lower case alpha password with alternating consonants & vowels)
Code for previous assignment below
import java.util.Random;
public class User {
private String firstName;
private String lastName;
private String phoneNum;
private String userID;
private String password;
/**
* @param firstName
* @param lastName
* @param phoneNum
* @param userID
* @param password
*/
public User() {
this.firstName = "";
this.lastName = "";
this.phoneNum = "";
this.userID = "";
this.password = "";
}
/**
* @param firstName
* @param lastName
* @param phoneNum
* @param userID
* @param password
*/
public User(String firstName, String lastName, String phoneNum) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNum = phoneNum;
createUserID();
createPassword();
}
/**
* Generates a random password
*/
private void createPassword() {
String alphabet = new String(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
int n = alphabet.length();
String result = new String();
Random r = new Random();
for (int i = 0; i < 6; i++)
result = result + alphabet.charAt(r.nextInt(n)); // 13
this.password = result;
}
/**
* Generates an id based on first initial, last name
*/
private void createUserID() {
this.userID = firstName.substring(0,1) + lastName;
}
/**
* @param firstName
* @param lastName
* Allow updating of first and last names
*/
public void updateName(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
createUserID();
}
/**
* @param phoneNum
* Allow updating of phone number
*/
public void updatePhone(String phoneNum) {
this.phoneNum = phoneNum;
}
public void updatePassword() {
createPassword();
}
/**
* Allow updating of Userid Will call private function to generate Returns
* void
*/
public void updateUserID() {
createUserID();
}
/**
* Doesn’t display the password and displays a formatted line
*/
public void displayInfo() {
System.out.println("firstName=" + firstName + ", lastName=" + lastName
+ ", phoneNum=" + phoneNum + ", userID=" + userID);
}
/**
* Displays all info including the password on a formatted line
*/
public void displayInfoAll() {
System.out.println("firstName=" + firstName + ", lastName=" + lastName
+ ", phoneNum=" + phoneNum + ", userID=" + userID
+ ", password=" + password);
}
/**
* Compares a password passed to the method with the one stored.
*
* @param password
* @return
*/
public boolean isPasswordEqual(String password) {
if (this.password == password) {
return true;
} else
return false;
}
/**
* Compares a userid passed to the method with the one stored.
*
* @param userid
* @return
*/
public boolean isUseridEqual(String userid) {
if (this.userID == userid) {
return true;
} else
return false;
}
}
import java.util.Scanner;
public class Assign05 {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
User user1 = new User("Hermione", "Granger", "9887654557");
User user2 = new User();
System.out.println("user1 :");
user1.displayInfo();
System.out.println("user2 :");
user2.displayInfo();
System.out.println("user1 include password:");
user1.displayInfoAll();
System.out.println("user2 include password:");
user2.displayInfoAll();
System.out.println("password equality of user1 and user2:"
+ user1.isPasswordEqual("rediif"));
System.out.println("userid equality of user1 and user2:"
+ user1.isUseridEqual("DMalfoy"));
user2.updateName("Harry ", "Potter");
} catch (Exception e) {
// TODO: handle exception
}
} // end main
} //end class
New Assignment requirements below
New Accessor methods are needed for this assignment.
returnID
will return as a string the userid for a User.
returnFullName
will return as a string the first name & last name for a User with a space between.
(Hint: this means you’ll have to create the string to be passed back by the method)
Documentation is required in the program.
You will create an array of Users allowing for a maximum of 50 users. Fill in 10 users.
Modify the passwords on two of them so you know them. You will need to allow the user to enter Q or q to quit entering Users
Test your IsUserEqual and isPasswordEqual functions so that you have match and unmatched conditions tested.
Create a routine in the driver that loops through the array for each User compare against the Users from that point on to make sure the userid is unique. If it’s not update the userID to add a number to the UserID to force uniqueness. For the first occurrence put 1 at the end of the id, for the second, put 2 at the end.
You may wish to use the following code to help add to the string:
tempid = userid + (char)(uniqueNum + 48)
uniqueNum is an int. By adding 48 to it you can change the int (0 – 9 only) to a display.
Looping through the array, run the displayInfoAll functions so a list is displayed of the user information.
Compare each userid to the other userids to make sure it is unique.
A copy of the input file used for this display is available.
For testing purposes you may wish to have an input file that you can use to override keyboard input.
An example of how to run that is:
G:Java2552>java UserDriver
An sample of what is in the UserInput file:
Mary Smith 630-121-2345
John Doe 815-222-2222
Jane Doe 708-231-5555
Q
MSmith2
MyPass
KMartin
Q
MSmith2
SJones
What
Q
Note the Q is sentinel value for a number of prompts.
Example of a run:
G:Java2552>java UserDriver
Enter first, last, and phone, Enter Q to quit
Read in: Mary Smith 630-121-2345
count is: 0
Enter first, last, and phone, Enter Q to quit
Read in: John Doe 815-222-2222
count is: 1
Enter first, last, and phone, Enter Q to quit
Read in: Calvin Klien 630-333-4444
count is: 2
Enter first, last, and phone, Enter Q to quit
List of User Information
Mary Smith 630-121-2345 MSmith DuZite
John Doe 815-222-2222 JDoe zoGeRa
Calvin Klien 630-333-4444 CKlien bujobu
Martin Smith 733-222-5555 MSmith LobAfI
Mike Smith 847-221-1111 MSmith zOqiJO
Jane Doe 708-231-5555 JDoe bUPIfU
Sue Jones 775-555-1212 SJones puRIpe
Bill Brown 333-111-5554 BBrown LokUYi
John Brown 222-111-4444 JBrown kUMobI
Terry Green 312-121-3223 TGreen MARUMO
Uniqueness Check
Changing id for: Martin Smith to MSmith1
Changing id for: Mike Smith to MSmith2
Changing id for: Jane Doe to JDoe1
See if password changes needed
Enter userid for which password is to be changed. Enter Q to quit
Enter new password
Updated password for Mike Smith
Enter userid for which password is to be changed. Enter Q to quit
Enter new password
Updated password for John Brown
Enter userid for which password is to be changed. Enter Q to quit
ERROR: Userid: KMartin Not found ***************
Enter userid for which password is to be changed. Enter Q to quit
Verify if password checking accurate
Enter userid for which password is to be checked. Enter Q to quit
Enter password
Yes, that is correct
Enter userid for which password is to be changed. Enter Q to quit
ERROR: Userid: KMartin Not found ***************
Enter userid for which password is to be changed. Enter Q to quit
Enter password
******No, incorrect *****
Enter userid for which password is to be changed. Enter Q to quit
List of User Information
Mary Smith 630-121-2345 MSmith DuZite
John Doe 815-222-2222 JDoe zoGeRa
Calvin Klien 630-333-4444 CKlien bujobu
Martin Smith 733-222-5555 MSmith1 LobAfI
Mike Smith 847-221-1111 MSmith2 MyPass
Jane Doe 708-231-5555 JDoe1 bUPIfU
Sue Jones 775-555-1212 SJones puRIpe
Bill Brown 333-111-5554 BBrown LokUYi
John Brown 222-111-4444 JBrown NewOne
Terry Green 312-121-3223 TGreen MARUMO
(You can use note pad to create the input file)
Mary Smith 630-121-2345
John Doe 815-222-2222
Calvin Klien 630-333-4444
Martin Smith 733-222-5555
Mike Smith 847-221-1111
Jane Doe 708-231-5555
Sue Jones 775-555-1212
Bill Brown 333-111-5554
John Brown 222-111-4444
Terry Green 312-121-3223
Q
MSmith2
MyPass
JBrown
NewOne
KMartin
Q
MSmith2
MyPass
KMartin
SJones
What
Q
Explanation / Answer
SOURCE CODE:
User.java
import java.util.Random;
public class User {
private String firstName;
private String lastName;
private String phoneNum;
private String userID;
private String password;
/**
* @param firstName
* @param lastName
* @param phoneNum
* @param userID
* @param password
*/
public User() {
this.firstName = "";
this.lastName = "";
this.phoneNum = "";
this.userID = "";
this.password = "";
}
/**
* @param firstName
* @param lastName
* @param phoneNum
* @param userID
* @param password
*/
public User(String firstName, String lastName, String phoneNum) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNum = phoneNum;
createUserID();
createPassword();
}
/**
* Generates a random password
*/
private void createPassword() {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int n = alphabet.length();
String result = new String();
Random r = new Random();
for (int i = 0; i < 6; i++)
result = result + alphabet.charAt(r.nextInt(n)); // 13
this.password = result;
}
/**
* Accessor method for the userid
*/
public String returnUserID() {
return userID;
}
/**
* Generates an id based on first initial, last name
*/
private void createUserID() {
this.userID = firstName.substring(0,1) + lastName;
}
/**
* @param firstName
* @param lastName
* Allow updating of first and last names
*/
public void updateName(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
createUserID();
}
/**
* @param phoneNum
* Allow updating of phone number
*/
public void updatePhone(String phoneNum) {
this.phoneNum = phoneNum;
}
public String returnName()
{
return this.firstName +" "+this.lastName;
}
public void updatePassword() {
createPassword();
}
/**
* Allow updating of Userid Will call private function to generate Returns
* void
*/
public void updateUserID(int uniqueNum) {
this.userID = this.userID + (char)(uniqueNum + 48);
}
/**
* Doesn’t display the password and displays a formatted line
*/
public void displayInfo() {
System.out.println(firstName + " " + lastName + " " + phoneNum + " " + userID);
}
/**
* Displays all info including the password on a formatted line
*/
public void displayInfoAll() {
System.out.println(firstName + " " + lastName + " " + phoneNum + " " + userID + " " + password);
}
/**
* Compares a password passed to the method with the one stored.
*
* @param password
* @return
*/
public boolean isPasswordEqual(String password) {
if ( this.password.equals(password)) {
return true;
} else
return false;
}
/**
* Compares a userid passed to the method with the one stored.
*
* @param userid
* @return
*/
public boolean isUseridEqual(String userid) {
if ( this.userID.equals(userid)) {
return true;
} else
return false;
}
}
-----------------------------------------------------------------------------------------
Assign05.java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Assign05 {
public static int count = 0;
public static User u[] = new User[50];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the filename: ");
String filename = br.readLine();
FileInputStream fis = new FileInputStream(filename);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br1 = new BufferedReader(isr);
String feed = br1.readLine();
while (feed!=null && count<50) {
String t[]=feed.split(" ");
User temp = new User(t[0],t[1],t[1]);
u[count++]=temp;
feed = null;
feed = br1.readLine();
}
br1.close();
isr.close();
fis.close();
sort();
remove_duplicate();
display();
String inp="y";
while(true)
{
System.out.println("Enter userid for which password is to be changed. Enter Q to quit:");
inp=br.readLine();
if(inp.equals("Q"))
break;
for(int i=0;i<count;i++)
if(u[i].returnUserID().equals(inp))
{
u[i].updatePassword();
break;
}
System.out.println("Updated Password for "+inp);
display();
}
}
public static void sort()
{
for(int i=0;i<count-1;i++)
for(int j=0;j<count-i-1;i++)
if(u[j+1].returnUserID().compareTo(u[j].returnUserID())>0)
{
User temp = u[j];
u[j] = u[j+1];
u[j+1] = temp;
}
}
public static void remove_duplicate()
{
int k=1;
for(int i=0;i<count;i++)
{
k=1;
for(int j=0;j<count;j++)
if(i!=j)
{
if(u[i].returnUserID().equals(u[j].returnUserID()))
{
u[j].updateUserID(k++);
}
}
}
}
public static void display()
{
System.out.println("List of all information:");
System.out.println("----------------------------------------");
System.out.println("Name PhoneNo User ID Password");
System.out.println("----------------------------------------");
for(int i=0;i<count;i++)
u[i].displayInfoAll();
}
}
-------------------------------------------------------------------------------
SAMPLE OUTPUT:
output:
Enter the filename: D:.txt
List of all information:
----------------------------------------
Name PhoneNo User ID Password
----------------------------------------
Mary Smith Smith MSmith eDbCAd
John Doe Doe JDoe yPMFwl
Calvin Klien Klien CKlien cRFMhs
Martin Smith Smith MSmith1 ifIhIu
Mike Smith Smith MSmith2 fKEiWr
Jane Doe Doe JDoe1 pnRKcv
Sue Jones Jones SJones cKLgAv
Bill Brown Brown BBrown WjNhex
John Brown Brown JBrown hoYttv
Terry Green Green TGreen apubvo
Enter userid for which password is to be changed. Enter Q to quit:
TGreen
Updated Password for TGreen
List of all information:
----------------------------------------
Name PhoneNo User ID Password
----------------------------------------
Mary Smith Smith MSmith eDbCAd
John Doe Doe JDoe yPMFwl
Calvin Klien Klien CKlien cRFMhs
Martin Smith Smith MSmith1 ifIhIu
Mike Smith Smith MSmith2 fKEiWr
Jane Doe Doe JDoe1 pnRKcv
Sue Jones Jones SJones cKLgAv
Bill Brown Brown BBrown WjNhex
John Brown Brown JBrown hoYttv
Terry Green Green TGreen qWLCxV
Enter userid for which password is to be changed. Enter Q to quit:
Q
INPUT FILE:
a.txt
Mary Smith 630-121-2345
John Doe 815-222-2222
Calvin Klien 630-333-4444
Martin Smith 733-222-5555
Mike Smith 847-221-1111
Jane Doe 708-231-5555
Sue Jones 775-555-1212
Bill Brown 333-111-5554
John Brown 222-111-4444
Terry Green 312-121-3223
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.