Software Development 3 – MP’s management System

Below is full code for my SD3 assignment – or you can download it from here – zipped project or here – .jar

Raport

“You should write a report about your software (max 2 sides A4) in the report you should discuss the strengths and weaknesses of the Strategy pattern and discuss how you would have implemented your code not using the strategy pattern.”

How is it working?

Current solution as a first thing checks files for serialized data and if there are any it adds MP to arrayLists  – so in a sec it will allow us to operate on this objects.

After adding MP’s to lists my program creates new instance of Program Class which is basically engine that interacts with user (displays communicates, gather inputs), process events, and deal with MP objects.

Program Class is divided to flexible layers which are

  • Display Communicates – Methods that are called only to display information
  • Logic  Processors – Methods that manage whole input/output processed – heart of the program
  • Object Factories – Methods that are creating objects or  changing  attributes of already existing ones


Program Class at very first place displays us main menu that has 4 options

  1. Add an MP
  2. Print the List of MP’s
  3. Change Behaviour
  4. Exit

Add an MP option allows us to create MP of a type that we will use – on of

System.out.println(“1. Conservative”);

System.out.println(“2. Liberal”);

System.out.println(“3. Labour”);

System.out.println(“4. Green Party”);

After selecting MP type user is asked for putting name and surname of MP.

MP is created and added to correct group – ArrayList

Print the List of MP’s option displays us list of groups and back option, when we will specifies a group that we want to see it displays all of MP for this group, if there is at least one. It allows us as well at this point select mp to display full information about. After that user is set back to main menu.

Change Behaviour option displays us a list of groups and then if user select one of them list of MP in it – we are able to select one of them and list of available options appear:

do you want to change its behavour?

1. Change Lives In

2. Change Believies In

3. No, it is fine.

When we select one of it a list of all options for the exact one will be displayed: for instance if we select “Change Lives In” a option list of:

0. big detached houses

1. cloud cuckoo land

2. modest houses

3. trees,

Will be displayed. After selection mp will be updated and main menu will show up. Method as well is user proof – id doesn’t allow to editing empty list of MP’s.

Exit ends a main loop and sends us buck to Main function where:

At very end all lists are sent to save methods that will serialize them and save to the file as compressed/serialized data.

Overview

Pattern used in this assignment has pros and cons as everything – there is not the best solution for every situation.  However my feeling about this assignment tells me it could be solved in easier way without creation separate classes for every type of MP, and what is more important without creating separate classes for every “Believe In” and “Live In”.

Yes using this solution we are able in easy way to change values of attributes – without searching the code for it, and we can expand it in easy way by new attributes or classes which may be different for different MP types, the solution tree is clear and easy to navigate and accessible.

My solution presents understanding of Interfaces and abstract classes, smart usage of collections, understanding principals of serialization and effective code optimization as well as object modeling.

However I can see better way of designing this program:

First of all instead of keeping different types MP’s in different ArrayList of a MP’s type I would create Some congaing object let’s call it parliament – and I would use only one type of MP that can keep object or attribute of enum type for “Believe In” and “Live In” in this case changes this two attributes would be so much easier to do. Serialization in this case would be applied only for a parliament Object and it could be stored in one file.

In case of using a parliament container we could create multi instances of it to manage more than one parliament. That is possible feature and flexibility.

In current solution we can access only MP’s from exact group and we can change theirs “believes in” and “Live In” – solution has an error preventer and it doesn’t allow us to change details of MP if there is no MP inside of group, however we are not able to “move” MP from one group to another, what could be is easy to implement in this solution – in my solution it would be as well kept inside MP object as enum type attribute or as custom class object.

Main Class

package coursework.main;

import coursework.main.Program;

public class Main implements Lists {

static private Serialisation serialisation = new Serialisation();
static private Program program;

public static void main(String[] args){

//load serialisation from files
//for files used to store every type of MP's in different file

listConservative.addAll(serialisation.conservative());
listLiberal.addAll(serialisation.democracy());
listLabour.addAll(serialisation.labour());
listGreenParty.addAll(serialisation.green());
program = new Program();

program.Start();

//save serialisation into a files

serialisation.saveMPConservative(listConservative);
serialisation.saveMPDemocracy(listLiberal);
serialisation.saveMPLabour(listLabour);
serialisation.saveMPGreen(listGreenParty);
}
}

Program Class

package coursework.main;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import coursework.members.*;
import coursework.behaviour.livein.*;
import coursework.behaviour.believein.*;

public class Program implements Lists{

//Get Input methods

public int getIntInput(){
return Integer.parseInt(getStringInput());
}

public String getNameInput()
{
System.out.println("-Enter name:");
return getStringInput();
}

public int getChoice(){
System.out.print("Chose: ");
return getIntInput();
}

public String getSurnameInput()
{
System.out.println("-Enter surname:");
return getStringInput();
}

public String getStringInput() {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try
{
return br.readLine();
}
catch (Exception e)
{
return "";
}
}

// Prints MP's
public void printConservative(){

System.out.println("Conservatist Party (" + listConservative.size() + "): ");

for(int i = 0; i < listConservative.size(); i++) 		{ 			System.out.println(i + " " + ((MPConservative)listConservative.get(i)).getDetails()); 		} 		if(listConservative.size()>0){
System.out.print("Chose one of the Conservatists to get full details: ");
detailsMPCons(getIntInput());
}

}

public void printLib(){

System.out.println("Liberal Party (" + listLiberal.size() + "):");

for(int i = 0; i < listLiberal.size(); i++) 		{ 			System.out.println(i + " " + ((MPLiberalDemocrat)listLiberal.get(i)).getDetails()); 		} 		if(listLiberal.size()>0){
System.out.print("Chose one of the Liberal-Democrat to get full details: ");
detailsMPLib(getIntInput());
}
}

public void printLab(){

System.out.println("Labour Party (" + listLabour.size() + "):");

for(int i = 0; i < listLabour.size(); i++) 		{ 			System.out.println(i + " " + ((MPLabour)listLabour.get(i)).getDetails()); 		} 		if(listLiberal.size()>0){
System.out.print("Chose one of the Labour to get full details: ");
detailsMPLab(getIntInput());
}
}

public void printGreen(){

System.out.println("Green Party (" + listGreenParty.size() + "):");

for(int i = 0; i < listGreenParty.size(); i++) 		{ 			System.out.println(i + " " +  ((MPGreenParty)listGreenParty.get(i)).getDetails()); 		} 		if(listGreenParty.size()>0){
System.out.print("Chose one of the Green to get full details: ");
detailsMPGreen(getIntInput());
}
}

public void detailsMPCons(int i){
System.out.println(i + " "+ ((MPConservative)listConservative.get(i)).getFullDetails());
}

public void detailsMPLab(int i){
System.out.println(i + " " + ((MPLabour)listLabour.get(i)).getFullDetails());
}

public void detailsMPGreen(int i){
System.out.println(i + " " +((MPGreenParty)listGreenParty.get(i)).getFullDetails());
}

public void detailsMPLib(int i){
System.out.println(i + " " + ((MPLiberalDemocrat)listLiberal.get(i)).getFullDetails());
}

public static void behaviourChangeDisplay() {
System.out.println("do you want to change its behavour?");
System.out.println("1. Change Lives In");
System.out.println("2. Change Believies In");
System.out.println("3. No, it is fine.");
}

// Menu Displays

public static void showMenu() {
//Display main menu

System.out.println(":Main Menu");
System.out.println("1. Add an MP");
System.out.println("2. Print the list of MPs");
System.out.println("3. Change Behaviours");
System.out.println("4. Exit");
}

public static void showMenuCreate() {
//Display MP creation menu
System.out.println(":MP Creation ");
showMPTypes();

}

public static void showMenuPrint() {

System.out.println(":MP Print ");
showMPTypes();

}

public static void showMPTypes(){
//Display options for MP's types

System.out.println("1. Conservative");
System.out.println("2. Liberal");
System.out.println("3. Labour");
System.out.println("4. Green Party");
System.out.println("5. back");
}

public static void livesInDisplay(){
System.out.println("0. " + BigDetachedHouses.getDesc());
System.out.println("1. "+ CloudCuckooLand.getDesc());
System.out.println("2. "+ ModestHouses.getDesc());
System.out.println("3. "+ Trees.getDesc());

}

public static void believeInDisplay(){
System.out.println("0. " + EverybodyShouldHaveJob.getDesc());
System.out.println("1. "+ PublicAreDaft.getDesc());
System.out.println("2. "+ RecyclingIsFun.getDesc());
System.out.println("3. "+ TheyKnowBest.getDesc());

}

public static void showThankYou(){
System.out.println("Thank You");
}
//Program Logic

private int 	mSelectedMP;
//keep tracking of selected MP - required for mp update
private int mGroupToChose;
//keep tracking what group has been used - required for mp update

//seters and getters

public void setSelectedMP(int mMpId)
{
this.mSelectedMP = mMpId;
}

public void setGroupId(int mGroupId)
{
this.mGroupToChose = mGroupId;
}

public int getSelectedMP()
{
return this.mSelectedMP;
}

public int getGroupId()
{
return this.mGroupToChose;
}

public void LogicCreateMP(){
//MP logic creation
showMenuCreate();
int option = getIntInput();
while (option != 5) {
switch (option) {
case 1:
createConservative();
break;
case 2:
createLiberalDemocrat();
break;
case 3:
createLabour();
break;
case 4:
createGreenParty();
break;
case 5:
showMenu();
break;
default:
errorHandler();
break;
}
System.out.println("Created");
showMenuCreate();
option = getIntInput();
}
}

public void LogicPrintMP(){
//MP logic print
showMenuPrint();
int option = getIntInput();
while (option != 5) {
switch (option){
case 1:
setGroupId(1);
printConservative();
break;
case 2:
setGroupId(2);
printLib();

break;
case 3:
setGroupId(3);
printLab();

break;
case 4:
setGroupId(4);
printGreen();

break;
case 5:
showMenu();
default:
errorHandler();

}
showMenuPrint();
option = getIntInput();
}
}

public void LogicDisplayMPOnce(){

showMenuPrint();
int option = getIntInput();

switch (option){
case 1:
setGroupId(1);
printConservative();
break;
case 2:
setGroupId(2);
printLib();

break;
case 3:
setGroupId(3);
printLab();

break;
case 4:
setGroupId(4);
printGreen();

break;
case 5:
Start();
default:
errorHandler();
}

}

public int LogicGroupCount(){
if(getGroupId() == 1)
{
return listConservative.size();
}else if(getGroupId() == 2){
return listLiberal.size();
}else if(getGroupId() == 3){
return listLabour.size();
}else if(getGroupId() == 4){
return listGreenParty.size();
}
return 0;
}

public int Start() {

System.out.println("==== MP Management System ====");
showMenu();

int input = getIntInput();

//main loop for the program

while(input != 5)
{
switch (input) {

case 1:
LogicCreateMP();
break;
case 2:
LogicPrintMP();
break;
case 3:
changeBehaviour();
break;
case 4:
showThankYou();
return 0;
default:
errorHandler();

}
showMenu();
input = getIntInput();
}

//return to our Main class and do serialisation save
return 0;
}

public void createConservative(){

MP conservative = new MPConservative(getNameInput(), getSurnameInput());
listConservative.add((MPConservative) conservative);

}

public void createLiberalDemocrat(){

MP lib = new MPLiberalDemocrat(getNameInput(), getSurnameInput());
listLiberal.add((MPLiberalDemocrat) lib);

}

public void createLabour(){

MP lab = new MPLabour(getNameInput(), getSurnameInput());
listLabour.add((MPLabour) lab);

}

public void createGreenParty(){

MP green = new MPGreenParty(getNameInput(), getSurnameInput());
listGreenParty.add((MPGreenParty) green);

}

public static void errorHandler(){
System.out.println("Options allowed 1 - 4.");
}

public void changeBehaviour(){

LogicDisplayMPOnce();
int choice = 3;
if(LogicGroupCount() <= 0){
System.out.println("Nothing to Display");
}else {
behaviourChangeDisplay();
choice = getIntInput();
if(getGroupId() == 1){

MPConservative temp = new MPConservative();
temp = listConservative.get(getSelectedMP());
if(choice == 1){
livesInDisplay();

switch(getChoice()){
case 0:
temp.setLiveInBehaviour(new BigDetachedHouses());
break;
case 1:
temp.setLiveInBehaviour(new CloudCuckooLand());
break;
case 2:
temp.setLiveInBehaviour(new ModestHouses());
break;
case 3:
temp.setLiveInBehaviour(new Trees());
break;
}

}else if(choice == 2){
believeInDisplay();

switch(getChoice()){
case 0:
temp.setBelieveInBehaviour(new EverybodyShouldHaveJob());
break;
case 1:
temp.setBelieveInBehaviour(new PublicAreDaft());
break;
case 2:
temp.setBelieveInBehaviour(new RecyclingIsFun());
break;
case 3:
temp.setBelieveInBehaviour(new TheyKnowBest());
break;
}
}
// update our MP in a list
listConservative.get(getSelectedMP()).setBelieveInBehaviour(temp.getBelieveInBehaviour());
listConservative.get(getSelectedMP()).setLiveInBehaviour(temp.getLiveInBehaviour());

}else if(getGroupId() == 2){
MPLiberalDemocrat temp = new MPLiberalDemocrat();
temp = listLiberal.get(getSelectedMP());
if(choice == 1){
livesInDisplay();

switch(getChoice()){
case 0:
temp.setLiveInBehaviour(new BigDetachedHouses());
break;
case 1:
temp.setLiveInBehaviour(new CloudCuckooLand());
break;
case 2:
temp.setLiveInBehaviour(new ModestHouses());
break;
case 3:
temp.setLiveInBehaviour(new Trees());
break;
}

}else if(choice == 2){
believeInDisplay();

switch(getChoice()){
case 0:
temp.setBelieveInBehaviour(new EverybodyShouldHaveJob());
break;
case 1:
temp.setBelieveInBehaviour(new PublicAreDaft());
break;
case 2:
temp.setBelieveInBehaviour(new RecyclingIsFun());
break;
case 3:
temp.setBelieveInBehaviour(new TheyKnowBest());
break;
}
}
listLiberal.get(getSelectedMP()).setBelieveInBehaviour(temp.getBelieveInBehaviour());
listLiberal.get(getSelectedMP()).setLiveInBehaviour(temp.getLiveInBehaviour());

}else if(getGroupId() == 3){
MPLabour temp = new MPLabour();
temp = listLabour.get(getSelectedMP());
if(choice == 1){
livesInDisplay();

switch(getChoice()){
case 0:
temp.setLiveInBehaviour(new BigDetachedHouses());
break;
case 1:
temp.setLiveInBehaviour(new CloudCuckooLand());
break;
case 2:
temp.setLiveInBehaviour(new ModestHouses());
break;
case 3:
temp.setLiveInBehaviour(new Trees());
break;
}

}else if(choice == 2){
believeInDisplay();

switch(getChoice()){
case 0:
temp.setBelieveInBehaviour(new EverybodyShouldHaveJob());
break;
case 1:
temp.setBelieveInBehaviour(new PublicAreDaft());
break;
case 2:
temp.setBelieveInBehaviour(new RecyclingIsFun());
break;
case 3:
temp.setBelieveInBehaviour(new TheyKnowBest());
break;
}
}
listLabour.get(getSelectedMP()).setBelieveInBehaviour(temp.getBelieveInBehaviour());
listLabour.get(getSelectedMP()).setLiveInBehaviour(temp.getLiveInBehaviour());

}else if(getGroupId() == 4){
MPGreenParty temp = new MPGreenParty();
temp = listGreenParty.get(getSelectedMP());
if(choice == 1){
livesInDisplay();

switch(getChoice()){
case 0:
temp.setLiveInBehaviour(new BigDetachedHouses());
break;
case 1:
temp.setLiveInBehaviour(new CloudCuckooLand());
break;
case 2:
temp.setLiveInBehaviour(new ModestHouses());
break;
case 3:
temp.setLiveInBehaviour(new Trees());
break;
}

}else if(choice == 2){
believeInDisplay();

switch(getChoice()){
case 0:
temp.setBelieveInBehaviour(new EverybodyShouldHaveJob());
break;
case 1:
temp.setBelieveInBehaviour(new PublicAreDaft());
break;
case 2:
temp.setBelieveInBehaviour(new RecyclingIsFun());
break;
case 3:
temp.setBelieveInBehaviour(new TheyKnowBest());
break;

}
}

listGreenParty.get(getSelectedMP()).setBelieveInBehaviour(temp.getBelieveInBehaviour());
listGreenParty.get(getSelectedMP()).setLiveInBehaviour(temp.getLiveInBehaviour());
}
}
}

}

Interface Lists

package coursework.main;

import java.util.ArrayList;

import coursework.members.MPConservative;
import coursework.members.MPGreenParty;
import coursework.members.MPLabour;
import coursework.members.MPLiberalDemocrat;

public interface Lists {

public ArrayList listConservative = new ArrayList();
public ArrayList listLiberal  = new ArrayList();
public ArrayList listLabour =  new ArrayList();
public ArrayList listGreenParty = new ArrayList() ;
}

Class Serialisation

package coursework.main;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import coursework.members.MPConservative;
import coursework.members.MPGreenParty;
import coursework.members.MPLabour;
import coursework.members.MPLiberalDemocrat;

public class Serialisation  {

ArrayList mMPConservative = new ArrayList();
ArrayList mMPGreen = new ArrayList();
ArrayList mMPLabour= new ArrayList();
ArrayList mMPDemocracy = new ArrayList();

/**
* @param fileName - The file name to load from the Asset directory
*/

@SuppressWarnings("unchecked")
public ArrayList conservative() {

try{
// Open the file that is the first
// command line parameter
FileInputStream  fstream = new FileInputStream("cons");
// Get the object of DataInputStream
ObjectInputStream in = new ObjectInputStream(fstream);
//Read File Line By Line

mMPConservative = (ArrayList)in.readObject();
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

return mMPConservative;
}

@SuppressWarnings("unchecked")
public ArrayList green() {
try{
// Open the file that is the first
// command line parameter
InputStream fstream = new FileInputStream("green");
// Get the object of DataInputStream
ObjectInputStream in = new ObjectInputStream(fstream);
//Read File Line By Line
mMPGreen = (ArrayList)in.readObject();
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

return mMPGreen;
}

@SuppressWarnings("unchecked")
public ArrayList labour() {
try{
// Open the file that is the first
// command line parameter
InputStream fstream = new FileInputStream("labour");
// Get the object of DataInputStream
ObjectInputStream in = new ObjectInputStream(fstream);
//Read File Line By Line
mMPLabour = (ArrayList)in.readObject();
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

return mMPLabour;
}

@SuppressWarnings("unchecked")
public ArrayList democracy() {
try{
// Open the file that is the first
// command line parameter
InputStream fstream = new FileInputStream("lib");
// Get the object of DataInputStream
ObjectInputStream in = new ObjectInputStream(fstream);
//Read File Line By Line
mMPDemocracy = (ArrayList)in.readObject();
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

return mMPDemocracy;
}

public void saveMPConservative(ArrayList mMps) {
try{
// Create file
FileOutputStream fstream = new FileOutputStream("cons");
ObjectOutputStream out = new ObjectOutputStream(fstream);
out.writeObject(mMps);

//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
e.printStackTrace();
}

}

public void saveMPGreen(ArrayList mMps) {
try{
// Create file
FileOutputStream fstream = new FileOutputStream("green");
ObjectOutputStream out = new ObjectOutputStream(fstream);
out.writeObject(mMps);
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

}

public void saveMPLabour(ArrayList mMps) {
try{
// Create file
FileOutputStream fstream = new FileOutputStream("labour");
ObjectOutputStream out = new ObjectOutputStream(fstream);
out.writeObject(mMps);
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

}

public void saveMPDemocracy(ArrayList mMps) {
try{
// Create file
FileOutputStream fstream = new FileOutputStream("lib");
ObjectOutputStream out = new ObjectOutputStream(fstream);
out.writeObject(mMps);

//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

}

}

MP Class

package coursework.members;

import java.io.Serializable;
import coursework.behaviour.believein.*;
import coursework.behaviour.livein.*;

public abstract class MP implements Serializable {

/**
*
*/
private static final long serialVersionUID = 8201597419869108367L;
/**
*
*/
private String name;
private String surname;
private LiveInBehaviour liveInBehaviour;
private BelieveInBehaviour believeInBehaviour;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setSurname(String surname) {
this.surname = surname;
}

public String getSurname() {
return surname;
}

public MP() {
this.setName("");
this.setSurname("");
}

public MP(String name, String surname) {
this.setName(name);
this.setSurname(surname);
}

public MP(String name, String surname, LiveInBehaviour liveIn, BelieveInBehaviour beliveIn) {
this.setName(name);
this.setSurname(surname);
this.setLiveInBehaviour(liveIn);
this.setBelieveInBehaviour(beliveIn);

}

public LiveInBehaviour getLiveInBehaviour() {
return this.liveInBehaviour;
}

public void setLiveInBehaviour(LiveInBehaviour liveInBehaviour) {
this.liveInBehaviour  = liveInBehaviour;
}

public BelieveInBehaviour getBelieveInBehaviour() {
return this.believeInBehaviour;
}

public void setBelieveInBehaviour(BelieveInBehaviour aviour) {
this.believeInBehaviour = aviour;
}

public String getBelieves() {
return this.believeInBehaviour.believeIn();
}

public String getWhereLive() {
return this.liveInBehaviour.liveIn();
}

public String getDetails() {
return this.getName()+ " "+ this.getSurname();
}

public String getFullDetails(){
return this.getName() + " " + this.getSurname() + " " + getBelieves() + " " + getWhereLive();

}
}

MPConservative Class

package coursework.members;

import java.io.Serializable;

import coursework.behaviour.believein.TheyKnowBest;
import coursework.behaviour.livein.BigDetachedHouses;

public class MPConservative extends MP implements Serializable{

private static final long serialVersionUID = 1L;
public MPConservative(){}

public MPConservative(String name, String surname){

super(name, surname);
this.setBelieveInBehaviour(new TheyKnowBest());
this.setLiveInBehaviour(new BigDetachedHouses());
}
}

MPGreenParty Class

package coursework.members;

import coursework.behaviour.believein.RecyclingIsFun;
import coursework.behaviour.livein.Trees;

public class MPGreenParty extends MP {

/**
*
*/
private static final long serialVersionUID = 1L;
public MPGreenParty(){}
public MPGreenParty(String name, String surname){
super(name, surname);
this.setBelieveInBehaviour(new RecyclingIsFun());
this.setLiveInBehaviour(new Trees());
}
}

MPLabour Class

package coursework.members;

import coursework.behaviour.believein.RecyclingIsFun;
import coursework.behaviour.livein.Trees;

public class MPGreenParty extends MP {

/**
*
*/
private static final long serialVersionUID = 1L;
public MPGreenParty(){}
public MPGreenParty(String name, String surname){
super(name, surname);
this.setBelieveInBehaviour(new RecyclingIsFun());
this.setLiveInBehaviour(new Trees());
}
}

MPLiberalDemocrat Class

package coursework.members;

import coursework.behaviour.believein.RecyclingIsFun;
import coursework.behaviour.livein.Trees;

public class MPGreenParty extends MP {

/**
*
*/
private static final long serialVersionUID = 1L;
public MPGreenParty(){}
public MPGreenParty(String name, String surname){
super(name, surname);
this.setBelieveInBehaviour(new RecyclingIsFun());
this.setLiveInBehaviour(new Trees());
}
}

Jtest Class

package coursework.members;

import junit.framework.TestCase;
import coursework.behaviour.livein.*;
import coursework.behaviour.believein.*;

public class MPTest extends TestCase {

MP mp1 = new MPConservative();

protected void setUp() throws Exception {
super.setUp();

mp1.setName("Someone");
mp1.setSurname("New");
mp1.setLiveInBehaviour(new Trees());
mp1.setBelieveInBehaviour(new RecyclingIsFun());

}

protected void tearDown() throws Exception {
super.tearDown();
}

public void testGetName() {
assertEquals("Someone", mp1.getName());
}

public void testGetSurname() {
assertEquals("New", mp1.getSurname());
}

public void testGetLiveInBehaviour() {
assertEquals("trees", mp1.getLiveInBehaviour().liveIn());
}

public void testGetBelieveInBehaviour() {
assertEquals("recycling is fun", mp1.getBelieveInBehaviour().believeIn());
}
}

BigDetachedHouses Class

package coursework.behaviour.livein;
import java.io.Serializable;
public class BigDetachedHouses implements LiveInBehaviour, Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private static String desc = "big detached houses";

@Override
public String liveIn() {
return desc;
}

public static String getDesc()
{
return desc;
}
}

CloudCuckooLand Class

package coursework.behaviour.livein;

import java.io.Serializable;

public class CloudCuckooLand implements LiveInBehaviour, Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private static String desc = "cloud cuckoo land";

@Override
public String liveIn() {
return desc;
}

public static String getDesc()
{
return desc;
}

}

LiveInBehaviour interface

package coursework.behaviour.livein;

public interface LiveInBehaviour {

public String liveIn();
}

ModestHouses Class

package coursework.behaviour.livein;

import java.io.Serializable;

public class ModestHouses implements LiveInBehaviour, Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private static String desc = "modest houses";

@Override
public String liveIn() {
return desc;
}

public static String getDesc()
{
return desc;
}
}

Trees Class

package coursework.behaviour.livein;

import java.io.Serializable;
public class Trees implements LiveInBehaviour, Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private static String desc = "trees";

@Override
public String liveIn() {
return desc;
}

public static String getDesc()
{
return desc;
}

}

BelieveInBehaviour interface

package coursework.behaviour.believein;

public interface BelieveInBehaviour  {

public String believeIn();

}

EverybodyShouldHaveJob class

package coursework.behaviour.believein;

import java.io.Serializable;

public class EverybodyShouldHaveJob implements BelieveInBehaviour, Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private static String desc = "everybody should have a job";

public static String getDesc()
{
return desc;
}

@Override
public String believeIn()
{
return desc;
}
}

PublicAreDaft Class

package coursework.behaviour.believein;

import java.io.Serializable;

public class PublicAreDaft implements BelieveInBehaviour, Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private static String desc = "the public are daft";

public static String getDesc()
{
return desc;
}

@Override
public String believeIn()
{
return desc;
}

}

RecyclingIsFun Class

package coursework.behaviour.believein;

import java.io.Serializable;

public class RecyclingIsFun implements BelieveInBehaviour, Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private static String desc = "recycling is fun";

public static String getDesc()
{
return desc;
}

@Override
public String believeIn()
{
return desc;
}

}

TheyKnowBest Class

package coursework.behaviour.believein;

import java.io.Serializable;

public class TheyKnowBest implements BelieveInBehaviour, Serializable  {

/**
*
*/
private static final long serialVersionUID = 1L;
private static String desc = "they know best";

public static String getDesc()
{
return desc;
}

@Override
public String believeIn()
{
return desc;
}
}

Published by

Luke Iwanski

Senior Graphics Programmer @ CD Projekt RED

Leave a Reply

Your email address will not be published. Required fields are marked *