Assignment of a college student (2)-first reply

Source: Internet
Author: User

Code Description: because the main you provide only has two places // expand here .............
I had to write a stupid one. I only added code in these two places, but I used Java. io. * And java. util. * Because you have already imported data in main, it should not be considered a violation .... The other two classes are not in // expand because you need to customize the class

Import java. Io .*;
Import java. util .*;

/**
* Exercise 29
*
*/
Public class main {

Public static void main (string ARGs []) throws ioexception {

// Read input file and build data structure in Model

Filereader reader = new filereader ("E: // input.txt ");
Bufferedreader breader = new bufferedreader (Reader );

String line = breader. Readline ();

While (line! = NULL) {// check for EOF
Try {
Stringtokenizer parsestring = new stringtokenizer (line );

String delimiters = "(),/T/N ";

String connectKeyword = parseString. nextToken (delimiters );
String nodeName = parseString. nextToken (delimiters );
String netName = parseString. nextToken (delimiters );

Node. getInstance (). addNodes (nodeName, netName );
Network. getInstance (). addNetworks (netName, nodeName );
// Expand here to build data structure .............
}

Catch (NoSuchElementException e ){
System. out. println ("Invalid input line:" + line
+ ". Skipping ....");
}

Line = bReader. readLine ();
}

// Allow user to query data structure
Options ();
}

/**
* Prompt users with options to either search for node, or network, or
* Quit
*/
Public static void options (){

Export keyboard = new partition (System. in );
System. out. println ("Which search do you require? ");
System. out
. Println ("1. networks-list all networks connected to a participant node ");
System. out
. Println ("2. nodes-list all the nodes connected to a participant network ");
System. out. println ("3. quit .");
System. out. println ();

While (keyboard. hasNext ()){
String s = keyboard. next ();
If (s. equals ("1 ")){
System. out. println ("please type a network name, all name is :");
System. out. println (Network. getInstance (). getAllNames ());
System. out. println ();
While (keyboard. hasNext ()){
String name = keyboard. next ();
If (Network. getInstance (). getNetworkByName (name) = null ){
System. out
. Println ("please type a network name, all name is :");
System. out. println (Network. getInstance (). getAllNames ());
System. out. println ();
} Else {
System. out. println (Network. getInstance ()
. GetNetworkByName (name ));
System. out. println ("Which search do you require? ");
System. out
. Println ("1. networks-list all networks connected to a participant node ");
System. out
. Println ("2. nodes-list all the nodes connected to a participant network ");
System. out. println ("3. quit .");
System. out. println ();
Break;
}
}
} Else if (s. equals ("2 ")){
System. Out. println ("please type A node name, all name is :");
System. Out. println (node. getinstance (). getallnames ());
While (keyboard. hasnext ()){
String name = keyboard. Next ();
If (node. getinstance (). getnodebyname (name) = NULL ){
System. Out
. Println ("please type A network name, all name is :");
System. Out. println (node. getinstance (). getallnames ());
System. Out. println ();
} Else {
System. Out. println (node. getinstance (). getnodebyname (
Name ));
System. Out. println ("which search do you require? ");
System. Out
. Println ("1. Networks-list all networks connected to a participant node ");
System. Out
. Println ("2. nodes-list all the nodes connected to a participant network ");
System. out. println ("3. quit .");
System. out. println ();
Break;
}
}
} Else if (s. equals ("3 ")){
System. out. println ("quit ");
System. out. println ();
System. exit (0 );
} Else {
System. out. println ("only 1, 2, 3 wocould be right ");
System. out. println ("Which search do you require? ");
System. out
. Println ("1. networks-list all networks connected to a participant node ");
System. out
. Println ("2. nodes-list all the nodes connected to a participant network ");
System. out. println ("3. quit .");
System. out. println ();
}

}
// Expand here .............

} // End options

Static class node {
Private hashtable <string, string> nodes = new hashtable <string, string> ();

Private Static node instance;

Private node (){
}

Public static node getinstance (){
If (instance = NULL ){
Instance = new node ();
}
Return instance;
}

Public String getnodebyname (string name ){
Return nodes. Get (name );
}

Public String getallnames (){
String names = "";
Try {
Enumeration <string> keys = nodes. Keys ();
While (Keys. hasmoreelements ()){
String key = keys. nextelement ();
Names + = Key + ",";
}
} Catch (exception e ){
E. printStackTrace ();
}
Return names. equals ("")? Names: names. substring (0,
Names. length ()-1 );
}

Public void addNodes (String nodeName, String netName ){
If (nodes. containsKey (nodeName )){
Nodes. put (nodeName, nodes. get (nodeName) + "," + netName );
} Else {
Nodes. put (nodeName, netName );
}

}

}

Static class Network {
Private Hashtable <String, String> networks = new Hashtable <String, String> ();

Private static Network instance;

Private Network (){
}

Public static Network getInstance (){
If (instance = null ){
Instance = new Network ();

}
Return instance;
}

Public String getnetworkbyname (string name ){
Return networks. Get (name );
}

Public String getallnames (){

String names = "";
Try {
Enumeration <string> keys = networks. Keys ();
While (Keys. hasmoreelements ()){

String key = keys. nextelement ();
Names + = Key + ",";
}
} Catch (exception e ){
E. printstacktrace ();
}
Return names. Equals ("")? Names: names. substring (0,
Names. Length ()-1 );
}

Public void addnetworks (string netname, string nodename ){
If (networks. containskey (netname )){
Networks. Put (netname, networks. Get (netname) + "," + nodename );
} Else {
Networks. put (netName, nodeName );
}

}
}
} // End class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.