Tuesday, May 27, 2014

Unit Testing Visual Studio.

I create a unit test to test the CalcualteGPA method in the GPACalculator. First right click on the solution and add a new test project.

Right click on the new Test Project. Add a reference to the project you want to test

The Solution should look like this

Add code to test the method

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GradePointCalculator;
using System.Collections.Generic;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        GPACalculator gp = new GPACalculator();
     
        [TestMethod]
        public void TestMethod1()
        {
            //addGrades
            CreateGradeList();
            //call the method
            double gpa = gp.GetGpa();
            //assert the value you expect, and
            //the actual value. (rounded because hard to match all the decimals)
            Assert.AreEqual(3.5, Math.Round(gpa, 2));
            
        }

        private void CreateGradeList()
        {
            //add a couple of grades
            Grade g1 = new Grade();
            g1.ClassName = "ITC 110";
            g1.Credits = 2;
            g1.GradePoint = 1;

            gp.AddGrade(g1);

            Grade g2 = new Grade();
            g2.ClassName = "ITC 220";
            g2.Credits = 5;
            g2.GradePoint = 4;
            gp.AddGrade(g2);

            
        }
    }
}

Go to the Test menu in Visual Studio and choosr tun all tests. A test explorer will show up. Here is the result for a passed test

I changed the value so that the result will fail. Here is a failed result

Thursday, May 22, 2014

Park and Rec Sample Sequence Diagram

Here is a slightly revised version of the diagram we did in class

The alternate login class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// Summary description for Login
/// 
public class Login
{
    private string userName;
    private string password;
 public Login(string user, string pass)
 {
        userName = user;
        password = pass;
 }

    public int ValidateLogin()
    {
        int pKey = 0;
        AutomartEntities ae = new AutomartEntities();

        var loginData = from p in ae.RegisteredCustomers
                        where p.Email.Equals(userName)
                        select new
                        {
                            p.CustomerPassCode,
                            p.CustomerHashedPassword,
                            p.PersonKey
                        };

        int passcode=0;
        byte[] hashed=null;
        int personKey = 0;

     
     
            foreach (var ld in loginData)
            {
                passcode =(int) ld.CustomerPassCode;
                hashed = (byte[])ld.CustomerHashedPassword;
                personKey = (int)ld.PersonKey;
            }

            PasswordHash ph = new PasswordHash();
            if (passcode != 0) 
            {
                byte[] generatedPassword = ph.HashIt(password, passcode.ToString());

                if (hashed != null)
                {
                     if (generatedPassword.SequenceEqual(hashed))
                    {
                        pKey = personKey;
                    }//end if
                }//end first inner if
           }//end outer if
      

        

        return pKey;
    }
}

Sunday, May 18, 2014

Domain Diagram for Parks and Rec

Last week I posted a partial diagram for the Parks and Rec scenario. Here is a more complete one, though there are still some minor changes and additions that can be made.


Some Notes

Still missing from the diagram is a class to manage the check ins and check outs of equipment.

I made Person abstract. That means that you can never instantiate (use) Person directly. You can only use its children. Person has two children, Employee and client that inherit from it.

Client, Inventory and Park all implement the Interface I_Manage which gives them Add, Edit, and Remove methods. Implementing an interface is similar to Inheritance. You not only get the methods in the interface, you must implement them.

There are two Composition relations. Remember Composition represents a whole/part relationship where if the whole is destroyed the parts are also destroyed. Item is a part of Inventory. If the inventory is removed we will say all the items go with it. Fine is a part of Client. If the client is removed the fines go with him or her.

We have one Aggregate relationship. Employees are a part of a Park. But if the Park is removed, we will not remove all employees.

There is an Association relationship between Park and Inventory. Park calls a method in Inventory to get the Items for its park.

In Park I noted two constructors because they are not the default constructors. Also note that most of Park's methods are private, meaning they can only be called from within the class itself.


"Is a" versus "has a" Relationships

One way to look at relationships between classes is to determine if a relationship is an "is a" of a "has a" type of relationship. For instance Client is a type of person, Employee is a type of person also. Inventory has a collection of Item. Park has a collection of Employees. Client has a collection of fines.

Most relationships can be resolved into one of these two relations.

Tuesday, May 13, 2014

Interfaces and class relations

Here is our partial class diagram

Here is the code

IFRed the interface


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacesExample
{
    public interface IFred
    {
         void Add(object o);
        void Edit(object o);
        void Remove(object o);

         List<object> GetList();
    }
}


Barney class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacesExample
{
    class Barney:IFred
    {
        List<object> itemList;

        public Barney()
        {
            itemList = new List<object>();
        }


        public void Add(object o)
        {
            //Item i = (Item)o;
            itemList.Add(o);
        }

        public void Edit(object o)
        {
            throw new NotImplementedException();
        }

        public void Remove(object o)
        {
            throw new NotImplementedException();
        }

        public List<object> GetList()
        {
            return itemList;
        }
    }
}


The Programclass


the Item class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacesExample
{
    class Item
    {
        public string Name { get; set; }
        public double Price { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacesExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Barney b = new Barney();
            Console.WriteLine("how many items?");
            int number = int.Parse(Console.ReadLine());
            for (int i = 0; i < number; i++)
            {
                Item item = new Item();
                Console.WriteLine("Enter item name");
                item.Name = Console.ReadLine();
                Console.WriteLine("Enter Price");
                item.Price = double.Parse(Console.ReadLine());
                b.Add(item);
            }

            Console.Clear();
            List<object> items = b.GetList();
            foreach(object o in items)
            {
                Item i = (Item)o;
       
                Console.WriteLine(i.Name + "  " + i.Price);
            }

            Console.ReadKey();
        }
    }
}

Thursday, May 8, 2014

Aggregation

Aggregation is a whole part relationship in which the parts can survive if the whole is destroyed. Here is a simple inventory example

Here is the code

Item


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aggregation
{
    class Item
    {
        public int ItemNumber { get; set; }
        public string ItemName { get; set; }
        public string ItemDescription { get; set; }
        public string Catagory { get; set; }
        public double PurchasePrice { get; set; }
        public string Condition { get; set; }
    }
}

Inventory


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aggregation
{
    class Inventory
    {
        private List<Item> itemList;

        public Inventory()
        {
            itemList = new List<Item>();
        }

        public void AddItem(Item i)
        {
            itemList.Add(i);
        }

        public void EditItem(Item i)
        {
            foreach(Item j in itemList)
            {
                int counter = 0;
                if(j.ItemNumber==i.ItemNumber)
                {
                    itemList[counter] = i;
                    counter++;
                }
            }
        }

        public List GetItems()
        {
            return itemList;
        }
    }
}


Here is the Program class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aggregation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many items do you want to enter");
            int number = int.Parse(Console.ReadLine());
            Inventory inv = new Inventory();
            for(int i=0;i<number;i++)
            {
                Item item = new Item();
                Console.WriteLine("Enter item number");
                item.ItemNumber = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Item name");
                item.ItemName = Console.ReadLine();
                Console.WriteLine("enter purchase price");
                item.PurchasePrice = double.Parse(Console.ReadLine());

                inv.AddItem(item);
            }

            Console.Clear();
            List<Item>items = inv.GetItems();
            foreach(Item i in items)
            {
                Console.WriteLine("{0}, {1}, {2}", i.ItemNumber, i.ItemName, i.PurchasePrice);
            }

            Console.ReadKey();
        }
    }
}

Tuesday, May 6, 2014

First Take at Inheritance

Here is the diagram

Here is the code for the Person class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inheritance
{
    class Person
    {
        string name;
        int age;
        string email;
        string gender;
        string address;

        public string Address
        {
            get { return address; }
            set { address = value; }
        }

        public string Gender
        {
            get { return gender; }
            set { gender = value; }
        }

        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public override string ToString()
        {
            return Name + " " + Email;
        }
        
            
        
    }
}


Here is the Employee class that inherits from Person


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inheritance
{
    class Employee:Person
    {
        int employeeNumber;

        public int EmployeeNumber
        {
            get { return employeeNumber; }
            set { employeeNumber = value; }
        }
        DateTime hireDate;

        public DateTime HireDate
        {
            get { return hireDate; }
            set { hireDate = value; }
        }
        string department;

        public string Department
        {
            get { return department; }
            set { department = value; }
        }

        public override string ToString()
        {
            return base.ToString() + " " + Department + " " + hireDate.ToString(); ;
        }
    }
}


here is the Program file with the main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime d = new DateTime(1956,5,5);
            Employee emp = new Employee();
            emp.Department = "IT";
            emp.Email = "emps@sccd.edu";
            emp.Name = "Employee1";
            emp.HireDate = d;

            Console.WriteLine(emp.ToString());
            Console.ReadKey();

        }
    }
}