Write Unit Test Methods for the code snippets below: package InventoryManagement; /** * AddCommand */ public class AddProductCommand extends Command { //TODO: add necessary fields to this class public AddProductCommand(ProductCatalog productCatalog, User loggedOnUser) { super(productCatalog, loggedOnUser); } @Override public void Execute() { // TODO Add the code that will execute this command } } -------------- package InventoryManagement; import java.lang.reflect.Constructor; /** * Command */ public abstract class Command { //TODO: add necessary fields to this class public Command(ProductCatalog productCatalog, User loggedOnUser) { } public static Command CreateCommandDynamically(ProductCatalog productCatalog, User user, String commandClassName) { Class concreteCommandClass = null; Command command = null; String packageName = "InventoryManagement"; try { concreteCommandClass = Class.forName(packageName + "." + commandClassName); Constructor con = concreteCommandClass.getConstructor(ProductCatalog.class, User.class); command = (Command)con.newInstance(productCatalog, user); } catch (final Exception e) { e.printStackTrace(); } return command; } //An abstract method that must be overriden in subclasses of class Command public abstract void Execute(); } -------------- package InventoryManagement; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * InventoryManagementSecurity */ public class InventoryManagementSecurity { public static User AuthenticateUser(String username, String password) { User authenticatedUser = null; if((username.compareToIgnoreCase("admin") == 0) && (GetPasswordHash(password).compareToIgnoreCase("58c536ed8facc2c2a293a18a48e3e120") == 0)) { authenticatedUser = new User(username, GetPasswordHash(password), true); } else { //TODO get the password hash using GetPasswordHash() method then try to find the user in Users.dat file //TODO set the authenticatedUser object if successfully authenticated } return authenticatedUser; //Change the return value based on authentication result } public static boolean AddNewUser(User newUser) { //TODO hash password and save username and hashed password to Users.dat return true; } public static boolean RemoveUser(String userName) { //TODO remove username from Users.dat return true; } public static boolean ChangePassword(String username, String currentPassword, String newPassword) { //TODO change the password only if current password match what is on records return true; } public static String GetPasswordHash(String password) { String generatedPassword = null; try { byte[] salt = new byte[] {12, -12, 65, 61, 2, -6, -90, 12, 4, -7, -87, 2, 34, -102, 3, 115}; // Create MessageDigest instance for MD5 MessageDigest md = MessageDigest.getInstance("MD5"); // Add password bytes to digest md.update(salt); // Get the hash's bytes byte[] bytes = md.digest(password.getBytes()); // This bytes[] has bytes in decimal format; // Convert it to hexadecimal format StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); } // Get complete hashed password in hex format generatedPassword = sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return generatedPassword; } } -------------- package InventoryManagement; /** * Hello world! * */ public class Main { public static void main( String[] args ) { //THIS IS JUST AN EXAMPLE ON HOW TO AUTHENTICATE AND CREATE A COMMAND DYNAMICALLY. //REMOVE CODE AND CHANGE AS NEEDED. //Authenticate the user and get a user object back User user = InventoryManagementSecurity.AuthenticateUser("admin", "admin"); ProductCatalog productCatalog = new ProductCatalog(); //TODO: You will have to read the information below from the MenuList.dat file instead of hardcoding it here. String commandClassName = "AddProductCommand"; int optionNumber = 1; String description = "Add Product"; Boolean isRestricted = true; Command dynamicCommand = Command.CreateCommandDynamically(productCatalog, user, commandClassName); System.out.println("The command concrete type is " + dynamicCommand.getClass().getSimpleName()); MenuItem menuItem = new MenuItem(dynamicCommand, optionNumber, description, isRestricted); //Add all created MenuItems to a MenuList. Array list usage is permitted. } } -------------- package InventoryManagement; /** * MenuItem */ public class MenuItem { public MenuItem(Command command, int optionNumber, String description, Boolean isRestricted) { //TODO Finish the implementation of this class System.out.println("Menu item created with command " + command.getClass().getSimpleName()); } } -------------- package InventoryManagement; /** * MenuList */ public class MenuList { public MenuList(String menuHeader) { } public boolean AddMenuItem(MenuItem menuItem) { //TODO Add menu item to the list. return true; } public void StartMenu(User user) { //TODO Display menu items based on user type, prompt user for command, execute selected command. } }
Write Unit Test Methods for the code snippets below:
package InventoryManagement;
/**
* AddCommand
*/
public class AddProductCommand extends Command
{
//TODO: add necessary fields to this class
public AddProductCommand(ProductCatalog productCatalog, User loggedOnUser)
{
super(productCatalog, loggedOnUser);
}
@Override
public void Execute() {
// TODO Add the code that will execute this command
}
}
--------------
package InventoryManagement;
import java.lang.reflect.Constructor;
/**
* Command
*/
public abstract class Command
{
//TODO: add necessary fields to this class
public Command(ProductCatalog productCatalog, User loggedOnUser)
{
}
public static Command CreateCommandDynamically(ProductCatalog productCatalog, User user, String commandClassName)
{
Class<?> concreteCommandClass = null;
Command command = null;
String packageName = "InventoryManagement";
try
{
concreteCommandClass = Class.forName(packageName + "." + commandClassName);
Constructor<?> con = concreteCommandClass.getConstructor(ProductCatalog.class, User.class);
command = (Command)con.newInstance(productCatalog, user);
}
catch (final Exception e)
{
e.printStackTrace();
}
return command;
}
//An abstract method that must be overriden in subclasses of class Command
public abstract void Execute();
}
--------------
package InventoryManagement;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* InventoryManagementSecurity
*/
public class InventoryManagementSecurity
{
public static User AuthenticateUser(String username, String password)
{
User authenticatedUser = null;
if((username.compareToIgnoreCase("admin") == 0) &&
(GetPasswordHash(password).compareToIgnoreCase("58c536ed8facc2c2a293a18a48e3e120") == 0))
{
authenticatedUser = new User(username, GetPasswordHash(password), true);
}
else
{
//TODO get the password hash using GetPasswordHash() method then try to find the user in Users.dat file
//TODO set the authenticatedUser object if successfully authenticated
}
return authenticatedUser; //Change the return value based on authentication result
}
public static boolean AddNewUser(User newUser)
{
//TODO hash password and save username and hashed password to Users.dat
return true;
}
public static boolean RemoveUser(String userName)
{
//TODO remove username from Users.dat
return true;
}
public static boolean ChangePassword(String username,
String currentPassword,
String newPassword)
{
//TODO change the password only if current password match what is on records
return true;
}
public static String GetPasswordHash(String password)
{
String generatedPassword = null;
try
{
byte[] salt = new byte[] {12, -12, 65, 61,
2, -6, -90, 12,
4, -7, -87, 2,
34, -102, 3, 115};
// Create MessageDigest instance for MD5
MessageDigest md = MessageDigest.getInstance("MD5");
// Add password bytes to digest
md.update(salt);
// Get the hash's bytes
byte[] bytes = md.digest(password.getBytes());
// This bytes[] has bytes in decimal format;
// Convert it to hexadecimal format
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
// Get complete hashed password in hex format
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
}
--------------
package InventoryManagement;
/**
* Hello world!
*
*/
public class Main
{
public static void main( String[] args )
{
//THIS IS JUST AN EXAMPLE ON HOW TO AUTHENTICATE AND CREATE A COMMAND DYNAMICALLY.
//REMOVE CODE AND CHANGE AS NEEDED.
//Authenticate the user and get a user object back
User user = InventoryManagementSecurity.AuthenticateUser("admin", "admin");
ProductCatalog productCatalog = new ProductCatalog();
//TODO: You will have to read the information below from the MenuList.dat file instead of hardcoding it here.
String commandClassName = "AddProductCommand";
int optionNumber = 1;
String description = "Add Product";
Boolean isRestricted = true;
Command dynamicCommand = Command.CreateCommandDynamically(productCatalog, user, commandClassName);
System.out.println("The command concrete type is " + dynamicCommand.getClass().getSimpleName());
MenuItem menuItem = new MenuItem(dynamicCommand, optionNumber, description, isRestricted);
//Add all created MenuItems to a MenuList. Array list usage is permitted.
}
}
--------------
package InventoryManagement;
/**
* MenuItem
*/
public class MenuItem
{
public MenuItem(Command command,
int optionNumber,
String description,
Boolean isRestricted)
{
//TODO Finish the implementation of this class
System.out.println("Menu item created with command " + command.getClass().getSimpleName());
}
}
--------------
package InventoryManagement;
/**
* MenuList
*/
public class MenuList {
public MenuList(String menuHeader)
{
}
public boolean AddMenuItem(MenuItem menuItem)
{
//TODO Add menu item to the list.
return true;
}
public void StartMenu(User user)
{
//TODO Display menu items based on user type, prompt user for command, execute selected command.
}
}
--------------
Trending now
This is a popular solution!
Step by step
Solved in 2 steps