Professional Documents
Culture Documents
Message 2
Message 2
Message 2
*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/*
* Complete the function below.
*/
/*
Browser needs TCPIP and HTML
TCPIP needs NETCARD
HTML needs nothing
public Component() {}
/*
We use a Hash Set to retrieve components and check for their
installation with O(1) speed
We use a list for InstalledOrder because a hidden testcase is to return
the correct order
We use a Hash Table to keep track of existing dependency components
(nodes) and attribute them to their dependors or dependencies
*/
public DependencyManager() {
this.installedComponents = new HashSet<>();
this.installedOrder = new ArrayList<>();
this.components = new HashMap<>();
}
if (parentItem.getDependencies().contains(child)) {
System.out.println(parentItem.getName() + " depends on " +
itemName + ", ignoring command");
continue;
}
child.getDependencies().add(parentItem);
parentItem.getDependers().add(child); //Doubly linked mapping
}
components.put(itemName, child);
}
public void install (String itemName, boolean directlyInstalled) {
if (itemName == null || itemName.length() == 0) return; // Improvement:
Throw exception
if (installedComponents.contains(itemName)) {
System.out.println(itemName + " is already installed");
return; // Improvement: Throw exception
}
switch(cmd){
case("DEPEND"):
mainItem = split[1]; // Component to run commands on
StringBuilder echo = new StringBuilder();
echo.append(cmd + " " + mainItem);
List<String> dependencies = new ArrayList<>();
for (int i = 2; i < split.length; i++) {
dependencies.add(split[i]);
echo.append(" " + split[i]);
}
System.out.println(echo.toString());
depManager.depend(mainItem, dependencies);
break;
case("INSTALL"):
mainItem = split[1]; // Component to run commands on
System.out.println(String.format("%s %s", cmd, mainItem));
depManager.install(mainItem, true);
break;
case("REMOVE"):
mainItem = split[1]; // Component to run commands on
System.out.println(String.format("%s %s", cmd, mainItem));
depManager.remove(mainItem);
break;
case("LIST"):
System.out.println(cmd);
depManager.list();
break;
case("END"):
System.out.println(cmd);
break;
default:
System.out.println("Invalid command. Skipping"); //
Improvement: Throw Exception
break;
}
}
}
int _input_size = 0;
_input_size = Integer.parseInt(in.nextLine().trim());
String[] _input = new String[_input_size];
String _input_item;
for(int _input_i = 0; _input_i < _input_size; _input_i++) {
try {
_input_item = in.nextLine();
} catch (Exception e) {
_input_item = null;
}
_input[_input_i] = _input_item;
}
doIt(_input);
}
}