Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a java question Please do part 5 To get things started, lets create our custom checked exception. LinkedInException Create a checked exception called,

This is a java question

Please do part 5

To get things started, lets create our custom checked exception. LinkedInException Create a checked exception called, LinkedInException, and make it extend java.lang.Exception. In order for an exception to behave like an exception, you must override all the inherited constructors from java.lang.Exception. There are five constructors that you will need to override. Refer to the Java API for the details of each constructor at ( https://docs.oracle.com/javase/8/docs/api/ ). This API documentation lists all of the classes along the left side of the page. Scroll down to the Exception class and click that class to view the API documentation. public class LinkedInException extends Exception // override all constructors in the Exception class After you have this exception created, then its time to move on to the LinkedIn user class.

1 - Change the UserAccount class to be an abstract class and define the below abstract method: /** * Sets the type of this user. * @param type the type. */ public abstract void setType(String type); Create a subclass of UserAccount called LinkedInUser. Define the following fields and methods on the LinkedInUser class: Fields private String type; private List connections = new ArrayList<>(); Methods /** * Returns the type of this LinkedIn user. * * @return the type. */ public String getType() /** * Adds the supplied connection to the list of connections for this user. * * @param user the connection to add. * @throws LinkedInException thrown if the supplied user is already * connected to this user. */ public void addConnection(LinkedInUser user) throws LinkedInException .

2 LinkedInUser This class will represent a user within your LinkedIn application. To keep things simple, our LinkedIn user will extend the UserAccount class that you created in the prior assignment and add an additional user type and a list of connections that this user has connected with in your LinkedIn application. Copy the UserAccount class from your prior assignment into this project and paste it into the edu.institution.asn2 package. You dont need to copy your prior unit tests into this project.

3 The addConnection method should add the supplied LinkedInUser argument to this users connections List. If the supplied connection already exists in this users connection List, then throw a new LinkedInException with the message, You are already connected with this user. /** * Removes the supplied user from the list of connections for this user. * @param user the user to remove. * @throws LinkedInException thrown if the supplied user is not connected to * this user. */ public void removeConnection(LinkedInUser user) throws LinkedInException The removeConnection method should remove the supplied user from this users connections List. If the supplied user is not connected to this user, then throw a new LinkedInException with the message, You are NOT connected with this user. /** * Returns a list of users that are connected to this user. * @return the list or empty list if this user has no connections. */ public List getConnections() The getConnections method should return a copy of this users connections create a new List with the same LinkedInUsers in it as this users connections. Do not return the connections List directly, since this violates the principle of encapsulation.

4 When we sort our LinkedIn users, we want the natural order of our LinkedIn users to be ordered alphabetically by their user name. To do that, make your LinkedInUser class implement the Comparable interface and write the code to ensure that the natural order for the LinkedInUser is by username, ignoring capitalization. Now lets move on to our unit tests. LinkedInUserTest Create a class called, LinkedInUserTest within the edu.institution package in the test folder. The LinkedInUserTest class contains the JUnit test methods (annotated with @Test). Create unit test methods to test the following: Add, retrieve, and delete connections o Create a unit test that creates three LinkedIn users. Assign the first username as han, the second username as luke, and the last username as leia o Make han connect to both luke and leia

5 o Assert that han has two connections and that they are luke and leia o Remove leia from hans connection list o Assert that han has one connection and that it is luke LinkedIn User Type o Create a unit test which creates a LinkedIn user. Call the setType method, passing P for premier. o Assert that the getType method returns the text, P Error check adding a connection o Create a unit test which creates two LinkedIn users. Assign the first username as han and the second username as luke o Connect han to luke o Connect han to luke again o Assert that the addConnection method threw a LinkedInException with the message, You are already connected with this user on the second connection attempt between han and luke. Error check removing a connection o Create a unit test which creates three LinkedIn users. Assign the first username as han, the second username as luke, and the last username as leia o Connect han with luke o Call the removeConnection method, attempting to remove the connection between han and leia. Assert that the method throws a LinkedInException with the message, You are NOT connected with this user Sorting o Create a unit test which creates three LinkedIn users. Assign the first username as Han, the second username as LUKE, and the last username as leia (make lukes user name uppercase and leias lowercase) o Connect han to both LUKE and leia. Be sure to connect LUKE first and leia second to test the sort functionality o Call the java.util.Collections.sort method, passing hans connections List to the sort method o Assert that the connections list is sorted alphabetically by user name First element should be leia and the second element should be LUKE.

Code I Have

package edu.institution.asn2;

public abstract class UserAccount { //Attribute private String username; private String password; //Constructor public UserAccount(String username, String password) { this.username = username; this.password = password; } //Getting return username public String getUsername () { return username; } //Checking for the correct password public boolean CorrectPassword(String password) { if (this.password.equals(password)) return true; else return false; } public void setType(String type) { // TODO Auto-generated method stub } //Returning the Username @Override public String toString() { return username; } //making sure hashcode is corresponding to the username @Override public int hashCode() { if (username == null) return 0; else return username.hashCode(); } //Checking for equality of two objects @Override public boolean equals(Object o) { if (o instanceof UserAccount) { UserAccount obj = (UserAccount) o; if (obj.getUsername() == this.getUsername()) return true; else return false; } else return false; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions