COMP1406/1006 - Design and Implementation of Computer Applications |
Winter 2006
|
1 User Interfaces |
Here are the individual topics found in this set of notes (click on one to go there):
1.1 User Interface Terminology |
a Bank has BankTellers as well as BankMachines,
calculators have physical components: ![]() ![]() ![]() |
a software application has buttons/text fields/menus: ![]() |
an On-line Shopping Cart has webpages containing
forms, text fields, lists, buttons:
|
In this course, we will consider software-based user interfaces (such as the software calculator shown above).
Some programs/applications do not really have any interactive user interface. For example, our Team/League example from COMP1405/1005 had a main method as a "pretend" user interface. We simply ran the code and it spewed output onto the console. We did not get to interact much at all with it, except to say "Go!" by running it.
What about simple text-based interfaces ? Recall the style of the basic text-based user interfaces as used on that we have seen in COMP1405/1005:
public class AverageTest {Here we received input from the keyboard, did a calculation, and then printed out the result. Later we wrote code that did not require any user input from the keyboard, but instead used simple test methods to simulate some "fixed scenario":
public static void main(String args[]) {
java.util.Scanner keyboard = new java.util.Scanner(System.in);
int sum = 0.0;
for (int i=0; i<5; i++) {
System.out.println("Enter the number " + i + ":");
sum += keyboard.nextInt();
}
System.out.print("The average is " + sum / 5.0);
}
}
public static void main(String args[]) {Nevertheless, all of our programs so far have been Java Applications which:
League aLeague = new League("NHL");aLeague.addTeam(new Team("Ottawa Senators"));
aLeague.addTeam(new Team("Montreal Canadians"));
aLeague.addTeam(new Team("Toronto Maple Leafs"));
aLeague.addTeam(new Team("Vancouver Cannucks"));aLeague.recordWinAndLoss("Ottawa Senators", "Toronto Maple Leafs");
aLeague.recordWinAndLoss("Montreal Canadians", "Toronto Maple Leafs");
aLeague.recordWinAndLoss("Ottawa Senators", "Vancouver Cannucks");
aLeague.recordTie("Vancouver Cannucks", "Toronto Maple Leafs");
aLeague.recordWinAndLoss("Toronto Maple Leafs", "Montreal Canadians");System.out.println("\nHere are the teams:");
aLeague.showTeams();
}
A Model:
A View is:
1.2 A Simple Text-Based User Interface |
public class DVD {There is not much to this class. Now what about the DVD collection itself ? It should probably look something like this in its most basic form:
private String title;
private int year;
private int duration;public DVD () { this("", 0, 0); }
public DVD (String newTitle, int y, int minutes) {
title = newTitle;
year = y;
duration = minutes;
}public String getTitle() { return title; }
public int getDuration() { return duration; }
public int getYear() { return year; }
public void setTitle(String t) { title = t; }
public void setDuration(int d) { duration = d; }
public void setYear(int y) { year = y; }public String toString() {
return ("DVD (" + year + "): " + title + " with length: " + duration);
}
}
import java.util.*;Notice when adding a DVD to the collection, we simply add it to the ArrayList. When removing, it is more convenient to have a method that removes according to title. The remove() method therefore takes a string and searched for the DVD with that title.
public class DVDCollection {
private ArrayList<DVD> dvds;public DVDCollection() { dvds = new ArrayList<DVD>(); }
public ArrayList<DVD> getDvds() { return dvds; }
public String toString() { return ("DVD Collection of size " + dvds.size()); }public void add(DVD aDvd) { dvds.add(aDvd); }
public boolean remove(String title) {
for (DVD aDVD: dvds) {
if (aDVD.getTitle().equals(title)) {
dvds.remove(aDVD);
return true;
}
}
return false;
}
}
OK. So now we can test the adding/removing and listing with a main() method as follows:
public static void main (String[] args) {Let us now make a user interface for this model. We should make a new class for this. We will make a simple text-based interface. It should perhaps bring up a menu and repeatedly prompt for a user choice. Here is what we will make appear on the screen:
DVDCollection c = new DVDCollection();
c.add(new DVD("Star Wars", 1978, 124));
c.add(new DVD("Java is cool", 2002, 93));
c.add(new DVD("Mary Poppins", 1968, 126));
c.add(new DVD("The Green Mile", 1999, 148));
c.remove("Mary Poppins");
// List the DVDs
for (DVD aDVD: c.getDvds())
System.out.println(aDVD);
}
Welcome to the Dvd
Collection User Interface
--------------------------------------------
1. Add DVD
2. Delete DVD
3. List DVDs
4. Exit
Please make a selection:
If the user makes an invalid selection, we print an error message out. This is considered to be our main menu. Once we make a selection and the action has been performed, this menu will be displayed again. Here is the user interface process:
public class DVDUI {Notice that the instance variable is named model which holds onto the DVDCollection which the interface is attached to. We could have picked any name for this variable, but we chose model here to help you understand that the rest of the code represents the user interface.
private DVDCollection model;public DVDUI() { model = new DVDCollection(); }
public DVDCollection getModel() { return model; }
public void showMainMenu() {
System.out.println("1. Add DVD");
System.out.println("2. Delete DVD");
System.out.println("3. List DVDs");
System.out.println("4. Exit");
System.out.println("\nPlease make a selection");
}
public static void main (String[] args) {
System.out.println("Welcome to the Dvd Collection User Interface");
System.out.println("--------------------------------------------");new DVDUI().showMainMenu();
}
}
The model is now "plugged-into" the user interface through this instance variable. However, we will want to make use of the model's methods. Let us now handle the keyboard input and make the main menu repeat until 4 is entered:
public
void showMainMenu() {
while(true)
{
System.out.println("1. Add DVD");
System.out.println("2. Delete DVD");
System.out.println("3. List DVDs");
System.out.println("4. Exit");
System.out.println("\nPlease make a selection");
int selection = new
java.util.Scanner(System.in).nextInt();
switch(selection) {
case 1: /* Handle the adding
of
DVDs */ break;
case 2: /* Handle the
removing
of DVDs */ break;
case 3: /* Handle the listing
of DVDs */ break;
case 4: System.exit(0);
default: System.out.println("Invalid
Selection");
}
}
}
If we run our code now, we will notice that it repeatedly brings up the main menu, waits for a response from the user and then displays the menu again. This repeats until the user selects option 4 to quit the program.
So we now have:
switch(selection) {
case 1: addDVD();
break;
case 2: deleteDVD(); break;
case 3: listDVDs(); break;
case 4: System.exit(0);
default: System.out.println("Invalid
Selection");
}
We must now decide what needs to be done in each of the helper methods:
private void addDVD() {
DVD aDVD = new DVD();
System.out.println("Enter DVD Title: ");
aDVD.setTitle(new
java.util.Scanner(System.in).nextLine());
System.out.println("Enter DVD Year (e.g., 2001):");
aDVD.setYear(new
java.util.Scanner(System.in).nextInt());
System.out.println("Enter DVD Duration
(minutes):");
aDVD.setDuration(new
java.util.Scanner(System.in).nextInt());
model.add(aDVD);
}
private void deleteDVD() {
System.out.println("Enter DVD Title: ");
model.remove(new
java.util.Scanner(System.in).nextLine());
}
private void listDVDs() {
for
(DVD
aDVD: model.getDvds())
System.out.println(aDVD);
}
private void deleteDVD() {
System.out.println("Enter DVD Title: ");
String title = new
java.util.Scanner(System.in).nextLine();
boolean success = model.remove(title);
if (success)
System.out.println("\nDVD: " + title + " was deleted
successfully \n");
else
System.out.println("\n*** Error: Could not
find DVD: " + title + "\n");
}
I am sure you can think of many ways to improve this code. Here are the important things to remember from this code: