f27sa1 software development 1 10. case study: interactive contacts system greg michaelson
TRANSCRIPT
F27SA1 Software Development 110. Case study: Interactive Contacts
SystemGreg 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
Interface
• command line• system repeatedly prompts for command:
+ == up- == downa == addd == deletef == findq == quit
• add/find then prompt for further information
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
Usecontacts.txt:[email protected]@[email protected]@[email protected]@[email protected]
Use$ java TestContacts contacts.txt
Commands: +, -, a(dd), d(elete), f(ind), q(uit)
Alice
next> +
Biggles
next> f Tintin
Bad command: f Tintin
Biggles
next> f
Name: Tintin
can't find: [email protected]> [email protected]> aName: TintinEmail: [email protected]@marlinspike.benext> [email protected]> q$
Objects: Contact
• fields– String name– String email
• methods– void showContact()• display name & email
– void writeContact(PrintWriter)• write name & email to PrintWriter
Program 1import java.util.*;import java.io.*;
class Contact{ String name; String email;
Contact(String name,String email) { this.name = name; this.email = email; }
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...?}
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
Objects: Contacts
• methods– void doUp()• move current up
– void doDown()• move current down
– void doDelete()• delete current
– void doFind()• prompt for & find by name
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
Program 3
class Contacts
{ Contact [] contacts;
int contactNo;
int current;
File f;
Contacts(int MAX)
{ contactNo = 0;
contacts = new Contact[MAX];
}
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(); }
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
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++; }
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)); }
Program 7 void doUp() { if(current<contactNo-1) current++; }
void doDown() { if(current>0) current--; }
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)
Program 8
void doDelete()
{ for(int i=current;i<contactNo-1;i++)
contacts[i] = contacts[i+1];
contactNo--;
if(current==contactNo)
current--;
}
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); }}
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
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)");
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
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);
}
}
}
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
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
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
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
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
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
The Beginning...