Question
Hello I need help with this JAVA CODE. I am adding a previous code I did. I saw someone has posted the same question but
Hello I need help with this JAVA CODE. I am adding a previous code I did. I saw someone has posted the same question but you guys have not answered that person.
The LinkedIn Command Line Interface Class Package: edu.sinclair
1) Add two additional private member variables to the LinkedInUser class: // holds the last date/time that this user logged in java.util.Date lastLoginDate; // holds the number of times this user has logged in int loginCounter; 2) Add and implement the following public methods to the LinkedInUser class: /** * Increments the number of times this user has logged in and assigns today's * date to the last login date. */ public void incrementLoginCounter() { } // hint: to assign todays date to a Date type variable, simply create a new // instance of Date. Date today = new Date(); /** * Returns the number of times this user has logged in. * @return the login counter. */ public int getLoginCounter() { } /** * Returns the Date that this used last logged in. * @return the Date. */ public Date getLastLoginDate() { } LinkedInCLI Create a class called, LinkedInCLI which will serve as the command line interface between the user and your software. Define the following properties. /** * Holds the list of users in the system. */ private List users; /** * Holds the logged in user. */ private LinkedInUser loggedInUser; public LinkedInCLI() { users = new ArrayList<>(); } The LinkedInCLI class should have methods to list all the users (i.e. print out their usernames), sign up a new user, delete a user, print who the logged in user is, sign off, and quit. 1. Create a public static void main method which prints the following menu to the console and awaits user input. Upon entry the application should do the following: a. Load any previously saved data. See the Serializing and Deserializing section below for the details b. If there are no users in the system, do the following i. prompt the user to establish the root user and password. ii. Default the LinkedInUser type to P for premier and call the newly added incrementLoginCounter() method to establish that this user has logged in once. iii. Add the newly created user to the list of users defined within the constructor. iv. Assign the newly created user to loggedInUser property on the class to indicate that this new user is the logged in user. Display the Welcome menu c. If there is at least one user in the system, do the following i. Prompt the user to login by supplying a user name and password. ii. If the user name doesnt exist, display the message, There is no user with that user name and re-display the login prompt. iii. If the user does exist and the password is incorrect, display the message, Password mismatch and re-display the login prompt. iv. If both username and password are valid 1. Call the newly added incrementLoginCounter() to increment the number of times this user has logged in. 2. Assign the user to loggedInUser property on the class to indicate that this is the logged in user. 3. display the Welcome menu Display the Welcome menu. Welcome 1. List all Users, 2. Sign up a New User, 3. Delete a User, 4. Who am I 5. Sign Off 6. Quit (substitute the actual logged in user name for the text). Read the users choice and call the appropriate method on the LinkedInCLI object. This should continue until the user chooses to Quit. (See the below sections for the details on each menu choice) Serializing and De-serializing We want the data entered within our LinkedIn CLI to persist between program executions. To do this, serialize your list of LinkedInUser objects before the program terminates (see the Quit section below for the details on this) AND after each action that modifies the data. When the program starts up again, de-serialize the list of LinkedInUser objects rather than making the user always create new ones. Remember: to serialize and de-serialize the LinkedInUser class, it (along with its super class) must implement Serializable. Menu Options List all Users Loop through all the LinkedIn users and print their user name to the console. Sign up a New User Prompt the user for the user name to add. Check if the supplied user name already exists in the users list. If it does, display an error message to the console and re-display the menu. If the supplied user name is unique, do the following o prompt the user to enter a password and what type of user he/she is Valid types are S for Standard and P for Premier. If the user enters any other value, display the message, Invalid user type. Valid types are P or S and redisplay the menu. construct a LinkedInUser instance and add the instance to the users list on the LinkedInCLI class. o Serialize your list of LinkedInUser objects to save the newly added user. o Afterwards, re-display the menu. Delete a User Prompt for the user name to delete. Check if the supplied user name exists in the user list. If the user does NOT exist, display an error message to the console and re-display the menu. If the user does exist, prompt for the password of the user being deleted. If the password is not correct, display an error message to the console and re-display the menu. If the password is correct, remove the user from the users list, serialize your list of LinkedInUser objects to save the new list, and re-display the menu. If the deleted user was the logged in user, null out the loggedInUser property on the LinkedInCLI class and re-present the user interface. o Since the logged in user is now null, the application should act the same as the application did upon start up (either making the user establish a root user or login with an existing user). See step 1 b. Who am I Display the following logged in user information o The user name o The user type o The last logged in date/time. Use the java.text.SimpleDateFormat class to format the date/time to be printable. o The login count SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String lastLoggedInDate = formatter.format(loggedInUser.getLastLoginDate()); System.out.println("Last Logged In: " + lastLoggedInDate); Sign off Null out the loggedInUser property and re-present the user interface. Since the logged in user is now null, the application should act the same as the application does upon start up (either making the user establish a root user or login with an existing user). See step 1 b. Quit private static final String PATH = System.getProperty("user.home") + File.separator + "sinclair" + File.separator; private static final String FILE_NAME = "LinkedInUsers.dat"; Place the above static final fields in your LinkedInCLI class. Using the above static final fields, serialize the users list to the PATH + FILE_NAME location. o new FileOutputStream(PATH + FILE_NAME); o The PATH constant will point to your computers home directory. o The File.separator constant will contain the directory separator character for your operating system. / for linux or macs \ for windows If any of the folders in the PATH do not exist, create them. o new File(PATH).mkdirs(); If an existing LinkedInUsers.dat file already exists, delete it and recreate within this save process. Write Goodbye to the system.out console.
Previous Code for the Assignment. Below.
UserAccount
package edu.sinclair;
public abstract class UserAccount {
private String username;
private String password;
public UserAccount(String username, String password) {
this.username = username;
this.password = password;
}
public boolean isPasswordCorrect(String password) {
return this.password == null?
false : this .password.equals(password);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
System.out.println(this.getUsername());
return this.username;
}
@Override
public int hashCode() {
return this.username.hashCode();
}
@Override
public boolean equals(Object o) {
UserAccount obj = (UserAccount) (o);
if (this.username.equals(obj.getUsername())) {
return true;
} else {
return false;
}
}
public abstract void setType(String type);
}
LinkedInUser
package edu.sinclair;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class LinkedInUser extends UserAccount implements Comparable
private String type;
private List
public LinkedInUser(String username, String password) {
super(username, password);
// TODO Auto-generated constructor stub
}
public String type() {
return type;
}
@Override
public void setType(String type) {
this.type = type;
}
public void addConnection(LinkedInUser User) throws LinkedInException{
//linkedInUser = null;
for(LinkedInUser linkedInUser : this.connections) {
if(linkedInUser.equals(User)) {
}
}
this.connections.add(User);
}
public void removeConnection(LinkedInUser User)throws LinkedInException{
// Iterator to traverse the list
Iterator
while(connectionsIterator.hasNext()) {
LinkedInUser linkedInUser = connectionsIterator.next();
if(linkedInUser.equals(User))
{
this.connections.remove(User);
return;
}
}
}
LinkedInExceptions
public List
{
return new ArrayList
}
public int compareTo(LinkedInUser user)
{
return this.getUsername().compareToIgnoreCase(user.getUsername());
}
}
package edu.sinclair;
public class LinkedInException extends Exception
{
/**
*
*/
private static final long serialVersionUID = 1L;
public LinkedInException()
{
super();
}
public LinkedInException(String errorMessage)
{
super(errorMessage);
}
public LinkedInException(String message, Throwable cause)
{
super(message, cause);
}
public LinkedInException(Throwable cause)
{
super(cause);
}
public LinkedInException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)
{
super(message,cause,enableSuppression,writableStackTrace);
}
}
LinkedInUserTest
package edu.sinclair;
import org.junit.Assert.*;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
public class LinkedInUserTest{
@Test
public void addRetrieveAndDeleteConnection() throws LinkedInException {
LinkedInUser han = new LinkedInUser("han", "han1234");
LinkedInUser luke = new LinkedInUser("Luke","Luke1234");
LinkedInUser leia = new LinkedInUser("leila","leila1234");
han.addConnection(luke);
han.addConnection(leia);
assertEquals(han.getConnections().size(), 2);
assertTrue(han.getConnections().get(0).equals(luke));
assertTrue(han.getConnections().get(1).equals(leia));
han.removeConnection(leia);
assertEquals(han.getConnections().size(), 1);
assertTrue(han.getConnections().get(0).equals(luke));
}
@Test
public void linkedInUserType() {
LinkedInUser linkedInUser = new LinkedInUser("user", "password");
linkedInUser.setType("P");
assertTrue(linkedInUser.type().equals("P"));
}
@Test
public void errorCheckAddingAConnectionTest() throws LinkedInException {
LinkedInUser han = new LinkedInUser("han", "han1234");
LinkedInUser luke = new LinkedInUser("Luke","Luke1234");
han.addConnection(luke);
}
@Test
public void errorCheckRemovingAConnectionTest() throws LinkedInException {
LinkedInUser han = new LinkedInUser("han", "han1234");
LinkedInUser luke = new LinkedInUser("Luke","Luke1234");
LinkedInUser leia = new LinkedInUser("leila","leila1234");
han.addConnection(luke);
}
@Test
public void sortingTest() throws LinkedInException {
LinkedInUser han = new LinkedInUser("han", "han1234");
LinkedInUser luke = new LinkedInUser("Luke","Luke1234");
LinkedInUser leia = new LinkedInUser("leila","leila1234");
han.addConnection(luke);
han.addConnection(leia);
}
}
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