The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Submit one Java file
Problem Formulation
Definition:Â Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic.
This time your task is to extend Assets application even further to implement the following two features:
User interface that would allow creation of various Asset objects (of classes defined in A8, i.e. Switch, Server, Router, etc) by means of using menu with predefined actions and user input;
Building up collection of Assets where all assets are linked with respect to their child-parent relationship.
The first feature assumes creation of a two-level menu, with the first level being responsible for collection manipulation actions, for example:
1. Add a new asset
2. Remove an existing asset
3. Display an existing asset
4. Quit
Note: Each menu item processed by a separate method of the Driver class. The second level would depend on which action has been selected in the first level menu. For example, adding of an asset might be associated with the following menu:
1. Specify type of asset to be entered
2. (Re-)Run type-specific asset addition process
3. Go to the previous menu
Notes: Specification of asset type has to be performed with user input validation, i.e. only supported asset types have to be allowed. Asset addition process assumes displaying of a set of asset type-specific prompts that would allow user to populate all required asset attributes. For example, consider the following interactive session (user input is emphasized with italic font shape) for server-type asset:
Enter ID of the asset (or "quit" to go to the previous menu):Â 105
Enter Name of the asset (or "quit" to go to the previous menu):Â ESXi-25
Enter Vendor of the asset (or "quit" to go to the previous menu):Â HP
Enter Model of the asset (or "quit" to go to the previous menu):Â Proliant ML350 G6
Enter S/N of the asset (or "quit" to go to the previous menu):Â USE3256V23
Enter ID of the parent asset or -1 if asset doesn't have a parent (or "quit" to go to the previous menu):Â -1
Enter the type operating system running on the server:Â Vmware ESXi v6.0 U2
Enter the number of installed NICs:Â 3
Is this server virtualized [yes/no] (or "quit" to go to the previous menu):Â no
The last 3 questions are highlighted in blue to emphasize that they are only asked for server-type assets. After answering all 9 questions, the second-level menu has to be redisplayed to allow user to make corrections (if needed) or jump to the upper level menu.
The second feature assumes storing all entered assets in a collection where child-parent relationship is used to construct a  two-level tree of all assets:
Assets listed on the parent level do not have parents but may have children
Assets listed on the child level are associated with the same parent.
Here is an example of such tree:
Asset{ID=101, type=SERVER, name='ESXi-25', vendor='HP', model='Proliant ML350 G6', sn='USE3256V23', PID=-1}
Asset{ID=501, type=PSU, name='Primary PSU of ESXi-25', vendor='HP', model='HSTNS-PL18', sn='1245002933', PID=101}
Asset{ID=502, type=PSU, name='Secondary PSU of ESXi-25', vendor='HP', model='HSTNS-PL18', sn='1245002940', PID=101}
Asset{ID=201, type=SWITCH, name='Main switch', vendor='Cisco', model='C2960S-48LPS-L', sn='FOC1512Z0KJ', PID=-1}
Attached you can find a skeleton program that generates such an exampled output based on some hardcoded data. The attached code neither introduces sub-classes derived from base Asset class nor it addresses the first feature of this assignment, i.e. creation of a user interface. You are welcome to use this skeleton as the basis for your own implementation or come up with something fresh.
In case when you would like to understand the attached code, you can start from reading through official Oracle documentation for ArrayList and HashMap classes that are good candidates to build custom collections.
/* Java Program that reads Asset details and stores in an array */
import java.util.*;
//Asset class definition
class Asset
{
  //Private Attributes
  private String type;
  private int id;
  private String name;
  private String serial;
  private int pid;
  //Default Constructor
  public Asset()
  {
      type = "";
      id = 0;
      name = "";
      serial = "";
      pid = -1;
  }
  //Setter method for attribute type
  public void setType(String type)
  {
      this.type = type;
  }
  //Getter method for attribute type
  public String getType()
  {
      return this.type;
  }
  //Setter method for attribute name
  public void setName(String name)
  {
      this.name = name;
  }
  //Getter method for attribute name
  public String getName()
  {
      return this.name;
  }
  //Setter method for attribute serial
  public void setSerial(String serial)
  {
      this.serial = serial;
  }
  //Getter method for attribute serial
  public String getSerial()
  {
      return this.serial;
  }
  //Setter method for attribute id
  public void setId(int id)
  {
      this.id = id;
  }
  //Getter method for attribute id
  public int getId()
  {
      return this.id;
  }
  //Setter method for attribute PID
  public void setPid(int pid)
  {
      this.pid = pid;
  }
  //Getter method for attribute PID
  public int getPid()
  {
      return this.pid;
  }
  //Method that returns complete Asset details for printing
  public String toString()
  {
      return "nn Type: " + getType() + " n ID: " + getId() + " n Name: " + getName() + " n Serial: " + getSerial() + " n PID: " + getPid() + " n";
  }
}
// i have declared only basic skeleton of derived classes.
// derived class Router
class Router extends Asset
{
  String application;
  int numOfNICS;
  String OS;
  boolean hasConsole;
}
// another derived class Switch
class Switch extends Asset
{
  int numOf_100M;
  int numOf_1G;
  int numOf_10G;
  int numOf_SFP;
  String OS;
  String level;
}
// derived class Server
class Server extends Asset
{
  String OS;
  int NumOfNICS;
  boolean isVirtualized;
}
// derived class PSU
class PSU extends Asset
{
  // it has its own variables and methods
}
//Driver class
class AssetDriver
{
  //Main method
  public static void main(String args[])
  {
      //Array list to hold asset data
      ArrayList<Asset> assets = new ArrayList<Asset>();
      //Server Asset
      Asset server = new Asset();
   Â
      //Setting Attribute values
      server.setType("Server");
      server.setId(123);
      server.setName("ABC 2142");
      server.setSerial("DFDSS78FSA58FSA8A");
   Â
      //Adding to array
      assets.add(server);
   Â
      //NIC Asset
      Asset NIC1 = new Asset();
   Â
      //Setting Attribute values
      NIC1.setType("NIC");
      NIC1.setId(897);
      NIC1.setName("DLink 458");
      NIC1.setSerial("D89AD5FS");
      NIC1.setPid(123);
   Â
      //Adding to array
      assets.add(NIC1);
   Â
      //NIC Asset
      Asset NIC2 = new Asset();
   Â
      //Setting Attribute values
      NIC2.setType("NIC");
      NIC2.setId(324);
      NIC2.setName("BELKIN 235");
      NIC2.setSerial("R5WC5WD");
   Â
      //Adding to array
      assets.add(NIC2);
   Â
      //Iterating over array list and printing each asset data
      System.out.println("nn Assets Data: n");
      for(int i=0; i<assets.size(); i++)
          System.out.println(assets.get(i));
   Â
      System.out.println("nn");
  }
}
Â