Guest User

Untitled

a guest
May 22nd, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. /* Code for COMP 102 Assignment 8
  2. * Name:
  3. * Usercode:
  4. * ID:
  5. */
  6.  
  7. import comp102.*;
  8. import java.util.*;
  9. import java.io.*;
  10.  
  11. /** Reads a genealogy database from a file, and allows the user
  12. to display information about people in the database.
  13. Each line of the file (except the first) contains information
  14. about one person:
  15. - an ID number (an integer from 0 to 1 less than the number of persons)
  16. - their name
  17. - the year of their birth
  18. - the ID of their mother (or -1 if the mother is unknown)
  19. - the ID of their father (or -1 if the father is unknown)
  20. The first line in the file contains the number of Persons in the file
  21. Note, the entries are not necessarily in ID order.
  22.  
  23. The program will read the data into an array of Person objects.
  24. Note, since the file specifies the number of people, and the
  25. program does not add or delete people, we do not need a count
  26. field - we can construct an array of the right size.
  27.  
  28. The program then allows the user to print out
  29. - the names of all the people in the database
  30. (note, names are just the first name - no spaces)
  31. - date of birth of a given person
  32. - parents of a person (if known) and their dates of birth
  33. - the number of (known) children of a person and all their names
  34. and dates of birth.
  35.  
  36. */
  37.  
  38. public class Genealogy implements UIButtonListener, UITextFieldListener{
  39. // Fields
  40. private Person[] db; // array of Person objects recording the parents of a person
  41. private int currentID = -1; // default is no selected person.
  42.  
  43. /** Construct a new Genealogy object
  44. * and set up the GUI
  45. */
  46. public Genealogy(){
  47. UI.initialise();
  48.  
  49. UI.addButton("Reload DB", this);
  50. UI.addButton("All Names", this);
  51. UI.addTextField("Name", this);
  52. UI.addButton("Birth", this);
  53. UI.addButton("Parents", this);
  54. UI.addButton("Children", this);
  55. UI.addButton("GrandChildren", this);
  56. UI.addButton("Clear", this);
  57. this.loadDatabase("big-database.txt");
  58. }
  59.  
  60. // GUI Methods
  61. /** Respond to button presses */
  62. public void buttonPerformed(String action){
  63. if (action.equals("Reload DB") ){
  64. this.loadDatabase(UIFileChooser.open("Choose database file"));
  65. }
  66. else if (action.equals("All Names") ) { this.printAllNames(); }
  67. else if (action.equals("Birth") ) { this.printPerson(); }
  68. else if (action.equals("Parents") ) { this.printParents(); }
  69. else if (action.equals("Children") ) { this.printChildren(); }
  70. else if (action.equals("GrandChildren") ) { this.grandChildren(); }
  71. else if (action.equals("Clear") ) { UI.clearText(); }
  72. }
  73.  
  74. public void textFieldPerformed(String field, String value){
  75. if (field.equals("Name")){
  76. this.setCurrentID(value);
  77. UI.println("----------------------");
  78. }
  79. }
  80.  
  81. /** Reads the data from the database file into an array.
  82. First reads the maximum ID from the file then
  83. creates an array of the right size.
  84. Then reads the data on each line,
  85. constructs a Person object, and
  86. puts the Person object into the correct cell of the array
  87. (specified by the person's ID number).
  88. The method may assume that the database is correctly formatted,
  89. and does not need to do any checking of the input.
  90. */
  91. public void loadDatabase(String filename){
  92. UI.printf("Reading Database from %s ....\n", filename);
  93. try{
  94. Scanner scan = new Scanner(new File(filename));
  95. int size = scan.nextInt();
  96. db = new Person[size];
  97. for(int i = 0;i<db.length;i++){
  98. Person[] temp = new Person[size];
  99. temp[i]= new Person(scan);
  100. int tempID = temp[i].getID();
  101.  
  102. db[tempID] = temp[i];
  103. }
  104.  
  105. scan.close();
  106. UI.println("Database loaded");
  107. }catch (Exception e){UI.printf("file failure. You chose an invalid image type %s\n",e);}
  108. }
  109.  
  110. /** Print out names of all the people in the database */
  111. public void printAllNames(){
  112. UI.println();
  113. UI.println("All names:");
  114. UI.println("-------------------");
  115. for(int i=0; i<db.length; i++){
  116. String name = db[i].getName();
  117. UI.println(name);
  118. }
  119. UI.println("-------------------");
  120. }
  121.  
  122. /** Looks for the ID of a person with the given name in the database.
  123. sets currentID to the index,
  124. if not found, prints message and sets currentID to -1.*/
  125. public void setCurrentID(String name){
  126. for(int i=0; i<db.length; i++){
  127. if(name.equalsIgnoreCase(db[i].getName())){
  128. this.currentID = db[i].getID();
  129. return;
  130. }
  131.  
  132. }
  133.  
  134. UI.println("That person does not exist in this database.");
  135. this.currentID = -1;
  136.  
  137. }
  138.  
  139. /** Prints the name and year of birth of the currently selected person.
  140. [Note, the toString() method of the Person class returns a string
  141. containing the name and year of birth of the person.]
  142. If it doesn't find the name, prints a message.
  143. */
  144. public void printPerson(){
  145.  
  146. for(int i=0; i<db.length; i++){
  147. if(currentID==(db[i].getID())){
  148. UI.println(db[i].toString());
  149.  
  150. }
  151. }
  152. }
  153.  
  154. /** Prints the names of the mother and the father if they are known
  155. (or appropriate messages if they are unknown).
  156. */
  157. public void printParents(){
  158. Person current = db[currentID];
  159.  
  160. UI.println("Parents of " + current.getName());
  161. int mID = db[currentID].getMotherID();
  162. if(mID!=-1){
  163. UI.println(" Mother: " + db[mID].getName());
  164. }else{UI.println(" Mother Unknown");}
  165.  
  166. int fID = db[currentID].getFatherID();
  167. if(fID!=-1){
  168. UI.println(" Father: " +db[fID].getName());
  169. }else{UI.println(" Father Unknown");}
  170.  
  171. }
  172.  
  173. /** Prints the number of children of the given person,
  174. followed by the names and years of birth all the children.
  175. Searches the array for Persons who have the currently specified person as one of their parents.
  176. Any such person is added to an array.
  177. It then prints out the information from the array of children.
  178. [You may assume that no person has more than 20 children.]
  179.  
  180. */
  181. public void printChildren(){
  182. Person current = db[currentID];
  183. Person [] children = new Person[20];
  184. int childrenCount = 0;
  185.  
  186. for(int i=0; i<db.length; i++){
  187. if(db[i].getMotherID()==currentID){
  188. children[childrenCount] = db[i];
  189. childrenCount++;
  190.  
  191. }
  192. if(db[i].getFatherID()==currentID){
  193. children[childrenCount] = db[i];
  194. childrenCount++;
  195. }
  196.  
  197. }
  198. UI.println(current.getName() + " has " + childrenCount + " children");
  199. if(childrenCount>0){
  200. for(int i =0; i<=childrenCount; i++){
  201. if(children[i]!=null){
  202. UI.println(" " + children[i].toString());
  203. }
  204.  
  205. }
  206. }
  207.  
  208. }
  209.  
  210. /** Completion: Prints (to textArea) names of all grandchildren (if any)
  211. of the currently specified person */
  212. public void grandChildren(){
  213. Person current = db[currentID];
  214.  
  215. int mID = -1;
  216. int fID = -1;
  217. UI.println("Grandchildren of " + current.getName() + ":");
  218. for(int i=0; i<db.length; i++){
  219. if(db[i].getMotherID()==currentID){
  220. mID = (db[i].getID());
  221. for(int j=0; j<db.length; j++){
  222. if(mID!=-1 && db[j].getMotherID()==mID){
  223. UI.println(" " + db[j].getName());
  224.  
  225. }
  226.  
  227. if(db[j].getFatherID()==db[i].getID()){
  228. UI.println(" " + db[j].getName());
  229.  
  230. }
  231.  
  232. }
  233. }
  234.  
  235. if(db[i].getFatherID()==currentID){
  236. fID = (db[i].getID());
  237. for(int j=0; j<db.length; j++){
  238. if(fID!=-1 && db[j].getFatherID()==fID){
  239. UI.println(" " + db[j].getName());
  240.  
  241. }
  242.  
  243. if(db[j].getMotherID()==db[i].getID()){
  244. UI.println(" " + db[j].getName());
  245.  
  246. }
  247.  
  248. }
  249.  
  250. }
  251.  
  252.  
  253.  
  254. }
  255. }
  256.  
  257. // Main
  258. public static void main(String[] arguments){
  259. Genealogy g = new Genealogy();
  260. }
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment