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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

1
2
3
4
5
6
package coursework.behaviour.livein;
 
public interface LiveInBehaviour {
 
public String liveIn();
}

ModestHouses Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

1
2
3
4
5
6
7
package coursework.behaviour.believein;
 
public interface BelieveInBehaviour  {
 
public String believeIn();
 
}

EverybodyShouldHaveJob class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
}
}

Leave a Reply

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