Task 1: CD UI and Database

Main

package nmmu.wrap301.Assignment8_DBMenu;

import nmmu.wrap301.menu.Builder;
import nmmu.wrap301.menu.Menu;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.*;
import java.util.*;
import java.sql.*;

public class Main {

    public static Connection con = null;
    public static Statement stmt = null;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        try {
            //connect to DB
            System.out.print("Enter username: ");
            String un = scan.next();
            System.out.print("Enter password: ");
            String pw = scan.next();
            connectToDB(un, pw);

            //build menu and execute
            Menu dbmenu = Builder.build();
            dbmenu.run();

            //disconnect from DB
            disconnectDB();


        } catch (Exception e) {
            System.out.println("Exception while building menu");
            e.printStackTrace();
        }
    }

    public static void connectToDB(String username, String password) {
        System.out.println("Establishing connection to database...");

        System.out.println("   Loading JDBC driver for MS SQL Server database...");
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (Exception e) {
            System.out.printf("   Unable to load JDBC driver... '%s'\n", e.getMessage());
            return;
        }

        System.out.println("   Use driver to connect to MS SQL Server (OPENBOX\\WRR)...");
        if (true) {
            try {
                System.out.println("   Locate database to open (using connection string)...");

                String connectionString = "jdbc:sqlserver://10.113.0.14\\WRR;databaseName=WRAP301Music";
                System.out.println("      Connection string = " + connectionString);

                // create connection to DB, ubcluding username & password
                // NEVER, EVER, include a username and password in your code!!!!
                con = DriverManager.getConnection(connectionString, username, password);

                // create statement object for manipulating DB
                stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                System.out.println("CONNECTED TO DB.");
            } catch (Exception e) {
                System.out.printf("   Unable to connect to DB... '%s'\n", e.getMessage());
            }
        }
    }
    public static void disconnectDB() {
        System.out.println("Disconnecting from database...");

        try {
            //Important to close connection (same as with files)
            con.close();
        } catch (Exception ex) {
            System.out.println("   Unable to disconnect from database");
        }
    }
}

Main

package nmmu.wrap301;

import nmmu.wrap301.menu.Builder;
import nmmu.wrap301.menu.Controller;
import nmmu.wrap301.menu.Menu;

public class Main {
    public static void main(String[] args) {
        // create the object that has the logic of how the application is supposed to work,
        // and the methods that are triggered by each menu choice
        Controller controller = new Controller();

        // construct the menu
        Menu menu = new Menu("Main Menu");
        menu.add("What is your name?", controller::whatIsYourNameAction);
        menu.add("Display message", controller::helloWorldAction);
        menu.add("Display 5 x table", controller::fiveTimesTableAction);

        // construct a sub-menu
        Menu subMenu = new Menu("SubMenu");
        subMenu.add("Is prime?", controller::isPrimeAction);
        subMenu.add("Is odd?", controller::isOddAction);

        // attach the sub-menu to the menu
        menu.add("Other functions", subMenu);

        // start the console menu-based app to run
        menu.run();

        //Execute, this time from XML document using builder method created
        try {
            Menu builtmenu = Builder.build();
            builtmenu.run();
            System.out.println("Build menu successfully implemented. Finished processing.");
        }
        catch(Exception E) {
            System.out.println("Exception caught.");
            E.printStackTrace();
        }

    }
}

Task 2: Testing

Tester Class

/**
 * Created by Laurence on 2017/04/24.
 */
import org.junit.*;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;

public class Testing {

    //Test analyser with 30 items & 20 personitems is sorted
    @Test
    public void testSortFull() throws Exception {
        Analyser tester = new Analyser();
        Random ran = new Random();
        for (int i = 0; i < 30; i++) {
            Float value = ran.nextFloat() * 1000.0f;
            Item newItem = new Item();
            newItem.setValue(value);
            tester.add(newItem);
        }
        for (int j = 0; j < 20; j++) {
            Float nextvalue = ran.nextFloat() * 1000.0f;
            String nextname = String.valueOf((ran.nextDouble() * 100000.0));
            PersonItem nextitem = new PersonItem();
            nextitem.setValue(nextvalue);
            nextitem.setName(nextname);
            tester.add(nextitem);
        }
        tester.sort();
        for (int k = 0; k < tester.size() - 1; k++) {
            Assert.assertTrue("Asserting sorted", tester.getValue(k) <= tester.getValue(k + 1));
        }
    }

    //Test anaylser with 1 item is sorted and returns equal min/max
    @Test
    public void testOne() throws Exception {
        Analyser single = new Analyser();
        Item test = new Item();
        test.setValue(14.22f);
        single.add(test);
        single.sort();
        Assert.assertEquals("Testing min == max", single.min(), single.max());
    }

    //Test empty analyser is sorted and returns equal min/max null
    @Test
    public void testEmpty() throws Exception {
        Analyser empty = new Analyser();
        empty.sort();
        Assert.assertEquals("Testing min == max", empty.min(), empty.max());
        Assert.assertEquals("Testing min == null", null, empty.min());
    }

    //Test average is accurate
    @Test
    public void testAverage() throws Exception {
        Analyser tester = new Analyser();
        Random ran = new Random();
        for (int i = 0; i < 30; i++) {
            Float value = ran.nextFloat() * 1000.0f;
            Item newItem = new Item();
            newItem.setValue(value);
            tester.add(newItem);
        }
        for (int j = 0; j < 20; j++) {
            Float nextvalue = ran.nextFloat() * 1000.0f;
            String nextname = String.valueOf((ran.nextDouble() * 100000.0));
            PersonItem nextitem = new PersonItem();
            nextitem.setValue(nextvalue);
            nextitem.setName(nextname);
            tester.add(nextitem);
        }
        tester.sort();
        float expectedAverage = 0;
        for (int a = 0; a < tester.size(); a++) {
            expectedAverage += tester.getValue(a);
        }
        expectedAverage /= tester.size();
        float calculatedAverage = tester.average();
        Assert.assertTrue("Asserting averages are correct", expectedAverage == calculatedAverage);
    }

    //Test percentile analyser's contents are contained in main analyser
    @Test
    public void testPercentileContents() throws Exception {
        Analyser tester = new Analyser();
        Random ran = new Random();
        for (int i = 0; i < 30; i++) {
            Float value = ran.nextFloat() * 1000.0f;
            Item newItem = new Item();
            newItem.setValue(value);
            tester.add(newItem);
        }
        for (int j = 0; j < 20; j++) {
            Float nextvalue = ran.nextFloat() * 1000.0f;
            String nextname = String.valueOf((ran.nextDouble() * 100000.0));
            PersonItem nextitem = new PersonItem();
            nextitem.setValue(nextvalue);
            nextitem.setName(nextname);
            tester.add(nextitem);
        }
        tester.sort();
        Analyser fiftyeighty = tester.percentile(50, 80);
        Analyser sixtyfour = tester.percentile(64,64);
        sixtyfour.sort();
        fiftyeighty.sort();

        for (int f = 0; f <= 100; f++) {
            Assert.assertTrue(tester.contains(fiftyeighty.percentile(f)));
        }
        assert sixtyfour.average() == tester.percentile(64).getValue();
    }


}

Main

import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number of basic items to create: ");
        int size = scan.nextInt();

        Analyser testanalyser = new Analyser();
        //populate analyser
        for (int i = 0; i < size; i++) {
            Random ran = new Random();
            Float value = ran.nextFloat() * 1000;
            Item newItem = new Item();
            newItem.setValue(value);
            testanalyser.add(newItem);
        }
        PersonItem newNamed1 = new PersonItem();
        PersonItem newNamed2 = new PersonItem();
        newNamed1.setValue(200.6f);
        newNamed1.setName("Low");
        newNamed2.setValue(850.5f);
        newNamed2.setName("High");
        testanalyser.add(newNamed1);
        testanalyser.add(newNamed2);
        testanalyser.displayAll();
        System.out.println("Sorting...");
        testanalyser.sort();
        testanalyser.displayAll();

    }
}