f27sa1 software development 1 10. case study: interactive contacts system greg michaelson

32
F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Upload: ethelbert-flynn

Post on 02-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

F27SA1 Software Development 110. Case study: Interactive Contacts

SystemGreg Michaelson

Page 2: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Introduction• interactive contacts system holds:– name– email address

• users can:– browse up/down through contacts– add/delete contacts– search contacts by name

• system:– loads contacts from file at start– saves contacts to file at end

Page 3: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Interface

• command line• system repeatedly prompts for command:

+ == up- == downa == addd == deletef == findq == quit

• add/find then prompt for further information

Page 4: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Organisation

• contacts held in ascending alphabetic name order• current contact– displayed after each command– commands may change current

• up current = next, if not already last• down current = previous, if not already first• delete current removed

current = next (or last)• find current = required if found

Page 6: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Use$ java TestContacts contacts.txt

Commands: +, -, a(dd), d(elete), f(ind), q(uit)

Alice

[email protected]

next> +

Biggles

[email protected]

next> f Tintin

Bad command: f Tintin

Biggles

[email protected]

next> f

Name: Tintin

can't find: [email protected]> [email protected]> aName: TintinEmail: [email protected]@marlinspike.benext> [email protected]> q$

Page 7: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Objects: Contact

• fields– String name– String email

• methods– void showContact()• display name & email

– void writeContact(PrintWriter)• write name & email to PrintWriter

Page 8: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 1import java.util.*;import java.io.*;

class Contact{ String name; String email;

Contact(String name,String email) { this.name = name; this.email = email; }

Page 9: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 2

void showContact() { System.out.println(name); System.out.println(email); }

void writeContact(PrintWriter p) { p.println(name); p.println(email); }

• use a single method...?}

Page 10: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Objects: Contacts

• fields– Contact [] contacts– int contactNo

• number of contacts– int current

• methods– void getContacts(Scanner)

• read contacts from Scanner– void doAdd()

• prompt for and add contact

Page 11: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Objects: Contacts

• methods– void doUp()• move current up

– void doDown()• move current down

– void doDelete()• delete current

– void doFind()• prompt for & find by name

Page 12: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Objects: Contacts

• methods– void doQuit(PrintWriter)• write contacts to PrintWriter & exit

– showCurrent()• display current name & email

– String getString(Scanner,String)• prompt for input from Scanner• used by doAdd & doFind

– void insert(Contact)• used by getContacts & doAdd

Page 13: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 3

class Contacts

{ Contact [] contacts;

int contactNo;

int current;

File f;

Contacts(int MAX)

{ contactNo = 0;

contacts = new Contact[MAX];

}

Page 14: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 4 void showCurrent() { if(contactNo>0) contacts[current].showContact(); else System.out.println("No contacts."); }

String getString(Scanner s,String p) { System.out.print(p+": "); return s.nextLine(); }

Page 15: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

String comparison

• can only use comparison operators on primitive values

int compareTo(String) • lexical orderstring1.compareTo(string2) • -1 if string1 < string2

• 0 if string1 == string2

• 1 if string1 > string2

Page 16: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 5 void insert(Contact c) { if(contactNo==contacts.length) { System.out.println("Too many contacts."); return; } int i; for(i=0;i<contactNo;i++) if(c.name.compareTo(contacts[i].name)<0) break; for(int j=contactNo;j>i;j--) contacts[j] = contacts[j-1]; contacts[i] = c; contactNo++; }

Page 17: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 6 void getContacts(Scanner s) { while(s.hasNext()) insert( new Contact(s.nextLine(),s.nextLine())); s.close(); current = 0; }

void doAdd(Scanner s) { String n = getString(s,"Name"); String e = getString(s,"Email"); insert(new Contact(n,e)); }

Page 18: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 7 void doUp() { if(current<contactNo-1) current++; }

void doDown() { if(current>0) current--; }

Page 19: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Delete

• to delete current– move subsequent Contacts back down array:

contacts[current] contacts[current+1]contacts[current+1] contacts[current+2]...contacts[contactNo-2] contacts[contactNo-1]

• decrement contactNo (& current)

Page 20: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 8

void doDelete()

{ for(int i=current;i<contactNo-1;i++)

contacts[i] = contacts[i+1];

contactNo--;

if(current==contactNo)

current--;

}

Page 21: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 9 void doFind(Scanner s) { String n = getString(s,"Name"); int i; for(i=0;i<contactNo;i++) if(contacts[i].name.equals(n)) { current = i; break; } if(i==contactNo) System.out.println("can't find: "+n); }

void doQuit(PrintWriter p) { for(int i=0;i<contactNo;i++) contacts[i].writeContact(p); p.close(); System.exit(1); }}

Page 22: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

main

• create File for argv[0]• create Scanner for File• create Contacts object• getContacts from Scanner• display prompt• repeatedly– show current– get command– check command & carry out action

Page 23: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 10class TestContacts

{ public static void main(String [] argv)

throws FileNotFoundException

{ File f = new File(argv[0]);

Contacts c = new Contacts(100);

c.getContacts(new Scanner(f));

Scanner input = new Scanner(System.in);

System.out.println("Commands: +, -, a(dd), d(elete),

f(ind), q(uit)");

Page 24: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 11 while(true)

{ c.showCurrent();

System.out.print("next> ");

String a = input.nextLine();

if(a.equals("+"))

c.doUp();

else

if(a.equals("-"))

c.doDown();

else

if(a.equals("a"))

c.doAdd(input);

else

Page 25: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Program 12 if(a.equals("d"))

c.doDelete();

else

if(a.equals("f"))

c.doFind(input);

else

if(a.equals("q"))

c.doQuit(

new PrintWriter(new FileOutputStream(f)));

else

System.out.println("Bad command: "+a);

}

}

}

Page 26: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Limitations

• after add, current doesn’t change to new • doesn’t order on email as well as name• can’t:– display all– change contact– search on email– find all with same name/email– load from new file– save to different file

Page 27: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Course reflection

• focused on:– developing Java implementation– from very tightly constrained problem– with well defined solution structure

• good approach for developing well specified software components

Page 28: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Course reflection

• poor for large scale software development• going straight from head to implementation

ends up with poorly structured program– hard to understand– hard to maintain– hard to extend

Page 29: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Course reflection

• multiple stages to systematic software development

• specification– what solution must achieve

• requirements– what features solution must include

• design– how solution will provide requirements features

to satisfy specification

Page 30: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Course reflection

• implementation– how design is realised as program

• testing/evaluation– ensures implementation meets specification &

satisfies requirements

• documentation– describes how program works/how to install

program/how to configure program

Page 31: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

Course reflection• software development is not linear from stage to

stage• lots of methodologies• much realistic development is iterative– any stage may require changes to previous stages– e.g. implementation may show design faults

• much development is based on prototyping– build initial system that does not have all features– use prototype to explore requirements & design– iteratively refine prototype to full program

Page 32: F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson

The Beginning...