Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Code for COMP 102 Assignment 8
- * Name:
- * Usercode:
- * ID:
- */
- import comp102.*;
- import java.util.*;
- import java.io.*;
- /** Reads a genealogy database from a file, and allows the user
- to display information about people in the database.
- Each line of the file (except the first) contains information
- about one person:
- - an ID number (an integer from 0 to 1 less than the number of persons)
- - their name
- - the year of their birth
- - the ID of their mother (or -1 if the mother is unknown)
- - the ID of their father (or -1 if the father is unknown)
- The first line in the file contains the number of Persons in the file
- Note, the entries are not necessarily in ID order.
- The program will read the data into an array of Person objects.
- Note, since the file specifies the number of people, and the
- program does not add or delete people, we do not need a count
- field - we can construct an array of the right size.
- The program then allows the user to print out
- - the names of all the people in the database
- (note, names are just the first name - no spaces)
- - date of birth of a given person
- - parents of a person (if known) and their dates of birth
- - the number of (known) children of a person and all their names
- and dates of birth.
- */
- public class Genealogy implements UIButtonListener, UITextFieldListener{
- // Fields
- private Person[] db; // array of Person objects recording the parents of a person
- private int currentID = -1; // default is no selected person.
- /** Construct a new Genealogy object
- * and set up the GUI
- */
- public Genealogy(){
- UI.initialise();
- UI.addButton("Reload DB", this);
- UI.addButton("All Names", this);
- UI.addTextField("Name", this);
- UI.addButton("Birth", this);
- UI.addButton("Parents", this);
- UI.addButton("Children", this);
- UI.addButton("GrandChildren", this);
- UI.addButton("Clear", this);
- this.loadDatabase("big-database.txt");
- }
- // GUI Methods
- /** Respond to button presses */
- public void buttonPerformed(String action){
- if (action.equals("Reload DB") ){
- this.loadDatabase(UIFileChooser.open("Choose database file"));
- }
- else if (action.equals("All Names") ) { this.printAllNames(); }
- else if (action.equals("Birth") ) { this.printPerson(); }
- else if (action.equals("Parents") ) { this.printParents(); }
- else if (action.equals("Children") ) { this.printChildren(); }
- else if (action.equals("GrandChildren") ) { this.grandChildren(); }
- else if (action.equals("Clear") ) { UI.clearText(); }
- }
- public void textFieldPerformed(String field, String value){
- if (field.equals("Name")){
- this.setCurrentID(value);
- UI.println("----------------------");
- }
- }
- /** Reads the data from the database file into an array.
- First reads the maximum ID from the file then
- creates an array of the right size.
- Then reads the data on each line,
- constructs a Person object, and
- puts the Person object into the correct cell of the array
- (specified by the person's ID number).
- The method may assume that the database is correctly formatted,
- and does not need to do any checking of the input.
- */
- public void loadDatabase(String filename){
- UI.printf("Reading Database from %s ....\n", filename);
- try{
- Scanner scan = new Scanner(new File(filename));
- int size = scan.nextInt();
- db = new Person[size];
- for(int i = 0;i<db.length;i++){
- Person[] temp = new Person[size];
- temp[i]= new Person(scan);
- int tempID = temp[i].getID();
- db[tempID] = temp[i];
- }
- scan.close();
- UI.println("Database loaded");
- }catch (Exception e){UI.printf("file failure. You chose an invalid image type %s\n",e);}
- }
- /** Print out names of all the people in the database */
- public void printAllNames(){
- UI.println();
- UI.println("All names:");
- UI.println("-------------------");
- for(int i=0; i<db.length; i++){
- String name = db[i].getName();
- UI.println(name);
- }
- UI.println("-------------------");
- }
- /** Looks for the ID of a person with the given name in the database.
- sets currentID to the index,
- if not found, prints message and sets currentID to -1.*/
- public void setCurrentID(String name){
- for(int i=0; i<db.length; i++){
- if(name.equalsIgnoreCase(db[i].getName())){
- this.currentID = db[i].getID();
- return;
- }
- }
- UI.println("That person does not exist in this database.");
- this.currentID = -1;
- }
- /** Prints the name and year of birth of the currently selected person.
- [Note, the toString() method of the Person class returns a string
- containing the name and year of birth of the person.]
- If it doesn't find the name, prints a message.
- */
- public void printPerson(){
- for(int i=0; i<db.length; i++){
- if(currentID==(db[i].getID())){
- UI.println(db[i].toString());
- }
- }
- }
- /** Prints the names of the mother and the father if they are known
- (or appropriate messages if they are unknown).
- */
- public void printParents(){
- Person current = db[currentID];
- UI.println("Parents of " + current.getName());
- int mID = db[currentID].getMotherID();
- if(mID!=-1){
- UI.println(" Mother: " + db[mID].getName());
- }else{UI.println(" Mother Unknown");}
- int fID = db[currentID].getFatherID();
- if(fID!=-1){
- UI.println(" Father: " +db[fID].getName());
- }else{UI.println(" Father Unknown");}
- }
- /** Prints the number of children of the given person,
- followed by the names and years of birth all the children.
- Searches the array for Persons who have the currently specified person as one of their parents.
- Any such person is added to an array.
- It then prints out the information from the array of children.
- [You may assume that no person has more than 20 children.]
- */
- public void printChildren(){
- Person current = db[currentID];
- Person [] children = new Person[20];
- int childrenCount = 0;
- for(int i=0; i<db.length; i++){
- if(db[i].getMotherID()==currentID){
- children[childrenCount] = db[i];
- childrenCount++;
- }
- if(db[i].getFatherID()==currentID){
- children[childrenCount] = db[i];
- childrenCount++;
- }
- }
- UI.println(current.getName() + " has " + childrenCount + " children");
- if(childrenCount>0){
- for(int i =0; i<=childrenCount; i++){
- if(children[i]!=null){
- UI.println(" " + children[i].toString());
- }
- }
- }
- }
- /** Completion: Prints (to textArea) names of all grandchildren (if any)
- of the currently specified person */
- public void grandChildren(){
- Person current = db[currentID];
- int mID = -1;
- int fID = -1;
- UI.println("Grandchildren of " + current.getName() + ":");
- for(int i=0; i<db.length; i++){
- if(db[i].getMotherID()==currentID){
- mID = (db[i].getID());
- for(int j=0; j<db.length; j++){
- if(mID!=-1 && db[j].getMotherID()==mID){
- UI.println(" " + db[j].getName());
- }
- if(db[j].getFatherID()==db[i].getID()){
- UI.println(" " + db[j].getName());
- }
- }
- }
- if(db[i].getFatherID()==currentID){
- fID = (db[i].getID());
- for(int j=0; j<db.length; j++){
- if(fID!=-1 && db[j].getFatherID()==fID){
- UI.println(" " + db[j].getName());
- }
- if(db[j].getMotherID()==db[i].getID()){
- UI.println(" " + db[j].getName());
- }
- }
- }
- }
- }
- // Main
- public static void main(String[] arguments){
- Genealogy g = new Genealogy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment