Download presentation
Presentation is loading. Please wait.
Published byEthelbert Flynn Modified over 9 years ago
1
F27SA1 Software Development 1 10. Case study: Interactive Contacts System Greg Michaelson
2
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
3
Interface command line system repeatedly prompts for command: + == up - == down a == add d == delete f == find q == quit add/find then prompt for further information
4
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
5
Use contacts.txt: Alice alice@wonderland.co.uk Biggles biggles@rfc.uk Calculus c.calculus@marlinspike.be Dorothy dorothy@oz.org Edward e.scissorhands@burton.film Felix felix@cat.net Groke groke@moominvalley.fi
6
Use $ java TestContacts contacts.txt Commands: +, -, a(dd), d(elete), f(ind), q(uit) Alice alice@wonderland.co.uk next> + Biggles biggles@rfc.uk next> f Tintin Bad command: f Tintin Biggles biggles@rfc.uk next> f Name: Tintin can't find: Tintin Biggles biggles@rfc.uk next> d Calculus c.calculus@marlinspike.be next> a Name: Tintin Email: tt@marlinspike.be Calculus c.calculus@marlinspike.be next> - Alice alice@wonderland.co.uk next> q $
7
Objects: Contact fields – String name – String email methods – void showContact() display name & email – void writeContact(PrintWriter) write name & email to PrintWriter
8
Program 1 import java.util.*; import java.io.*; class Contact { String name; String email; Contact(String name,String email) { this.name = name; this.email = email; }
9
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...? }
10
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
11
Objects: Contacts methods – void doUp() move current up – void doDown() move current down – void doDelete() delete current – void doFind() prompt for & find by name
12
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
13
Program 3 class Contacts { Contact [] contacts; int contactNo; int current; File f; Contacts(int MAX) { contactNo = 0; contacts = new Contact[MAX]; }
14
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(); }
15
String comparison can only use comparison operators on primitive values int compareTo(String) lexical order string 1.compareTo( string 2 ) -1 if string 1 < string 2 0 if string 1 == string 2 1 if string 1 > string 2
16
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++; }
17
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)); }
18
Program 7 void doUp() { if(current<contactNo-1) current++; } void doDown() { if(current>0) current--; }
19
Delete to delete current – move subsequent Contact s back down array: contacts[current] contacts[current+1] contacts[current+1] contacts[current+2]... contacts[contactNo-2] contacts[contactNo-1] decrement contactNo (& current )
20
Program 8 void doDelete() { for(int i=current;i<contactNo-1;i++) contacts[i] = contacts[i+1]; contactNo--; if(current==contactNo) current--; }
21
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); }
22
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
23
Program 10 class 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)");
24
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
25
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); }
26
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
27
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
28
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
29
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
30
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
31
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
32
The Beginning...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.