Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.90 KB | None | 0 0
  1.  
  2. public class Node {
  3. private int where;
  4. private String key;
  5. public Node leftChild;
  6. public Node rightChild;
  7. public int getWhere;
  8.  
  9. public Node(String k, int w) {
  10. key = new String(k);
  11. where = w;
  12. }
  13.  
  14. public int compareTo(String k) {
  15. return key.compareTo(k);
  16. }
  17.  
  18. public String toString(){
  19. return key + where;
  20. }
  21.  
  22. public int getWhere(){
  23. return where;
  24. }
  25.  
  26. }
  27.  
  28.  
  29. import java.util.Stack;
  30.  
  31. public class Tree {
  32. private Node root;
  33.  
  34. public Tree() {
  35. root = null;
  36. }
  37.  
  38. public int find(String key) {
  39. Node current = root;
  40. while (current.compareTo(key) != 0) {
  41. if (current.compareTo(key) < 0)
  42. current = current.leftChild;
  43. else
  44. current = current.rightChild;
  45. if (current == null)
  46. return -1;
  47. }
  48. return current.getWhere;
  49. }
  50.  
  51. public void insert(String key, int where) {
  52. Node newNode = new Node(key,where);
  53. if (root == null)
  54. root = newNode;
  55. else {
  56. Node current = root;
  57. Node parent;
  58. while (true) {
  59. parent = current;
  60. if (current.compareTo(key) < 0) {
  61. current = current.leftChild;
  62. if (current == null) {
  63. parent.leftChild = newNode;
  64. return;
  65. }
  66. } else {
  67. current = current.rightChild;
  68. if (current == null) {
  69. parent.rightChild = newNode;
  70. return;
  71. }
  72. }
  73. }
  74. }
  75. }
  76.  
  77. public boolean delete(String key) {
  78. Node current = root;
  79. Node parent = root;
  80. boolean isLeftChild = true;
  81.  
  82. while (current.compareTo(key) != 0) {
  83. parent = current;
  84. if (current.compareTo(key) < 0) {
  85. isLeftChild = true;
  86. current = current.leftChild;
  87. } else {
  88. isLeftChild = false;
  89. current = current.rightChild;
  90. }
  91. if (current == null)
  92. return false;
  93. }
  94.  
  95. if (current.leftChild == null && current.rightChild == null) {
  96. if (current == root)
  97. root = null;
  98. else if (isLeftChild)
  99. parent.leftChild = null;
  100. else
  101. parent.rightChild = null;
  102. } else if (current.rightChild == null)
  103. if (current == root)
  104. root = current.leftChild;
  105. else if (isLeftChild)
  106. parent.leftChild = current.rightChild;
  107. else
  108. parent.rightChild = current.leftChild;
  109. else if (current.leftChild == null)
  110. if (current == root)
  111. root = current.rightChild;
  112. else if (isLeftChild)
  113. parent.rightChild = current.rightChild;
  114. else
  115. parent.rightChild = current.rightChild;
  116. else {
  117. Node successor = getSuccessor(current);
  118. if (current == root)
  119. root = successor;
  120. else if (isLeftChild)
  121. parent.leftChild = successor;
  122. else
  123. parent.rightChild = successor;
  124. successor.leftChild = current.leftChild;
  125. }
  126. return true;
  127. }
  128.  
  129. private Node getSuccessor(Node delNode) {
  130. Node successorParent = delNode;
  131. Node successor = delNode;
  132. Node current = delNode.rightChild;
  133. while (current != null) {
  134. successorParent = successor;
  135. successor = current;
  136. current = current.leftChild;
  137. }
  138. if(successor != delNode.rightChild){
  139. successorParent.leftChild = successor.rightChild;
  140. successor.rightChild = delNode.rightChild;
  141. }
  142. return successor;
  143. }
  144. public void traverse(int traverseType, DataBaseArray DBA){
  145. switch(traverseType){
  146. case 0: descendingOrder(root, DBA);break;
  147. case 1: ascendingOrder(root,DBA);break;
  148. }
  149. System.out.println();
  150. }
  151.  
  152. private void ascendingOrder(Node localRoot, DataBaseArray DBA){
  153. if(localRoot != null){
  154. ascendingOrder(localRoot.leftChild, DBA);
  155. System.out.println(DBA.toString(localRoot.getWhere()));
  156. ascendingOrder(localRoot.rightChild,DBA);
  157. }
  158. }
  159.  
  160. private void descendingOrder(Node localRoot, DataBaseArray DBA){
  161. if(localRoot != null){
  162. descendingOrder(localRoot.rightChild,DBA);
  163. System.out.println(DBA.toString(localRoot.getWhere()));
  164. descendingOrder(localRoot.leftChild,DBA);
  165. }
  166. }
  167. }
  168.  
  169.  
  170. /*Austin Thornton - E01401657
  171. * Programming Project Two
  172. * COSC 311 - Winter 2017
  173. * DUE:3/8/17
  174. */
  175.  
  176. import java.io.*;
  177. import java.util.Scanner;
  178.  
  179. public class DataBase {
  180. // Declaring necessary variables
  181. private final int numElements = 100;
  182. private DataBaseArray DBA;
  183. private Tree id, first, last;
  184. private IndexRecord tempStorage;
  185.  
  186. Scanner scan = new Scanner(System.in);
  187.  
  188. // Constructor to initialize the variables
  189. public DataBase() {
  190. DBA = new DataBaseArray(numElements);
  191. id = new Tree();
  192. first = new Tree();
  193. last = new Tree();
  194. }
  195.  
  196. // AddIt method to add new students to the database
  197. public void addIt() {
  198. // Declaring variables
  199. String studentID = null;
  200. String studentLast, studentFirst = null;
  201. boolean continueLoop = true;
  202. int idIndex = 0;
  203. // While loop to loop through and ask for the user to enter ID's
  204. while (continueLoop) {
  205. System.out.println("Enter a new ID: ");
  206. studentID = scan.next();
  207. idIndex = id.find(studentID);
  208. // If the index is >=0 that means the ID number is already in use,
  209. // and user is prompted to enter a different ID
  210. if (idIndex >= 0) {
  211. System.out.println("The ID is already in use, please try again");
  212. } else {
  213. continueLoop = false;
  214. }
  215. }
  216. // Prompting the user to enter the student's first and last name
  217. System.out.print("Enter a first name: ");
  218. studentFirst = scan.next();
  219. System.out.println("Enter a last Name: ");
  220. studentLast = scan.next();
  221. // Finally inserting the student into the database
  222. insertRecord(studentLast, studentFirst, studentID);
  223. }
  224.  
  225. public void findIt() {
  226. // Declaring variables
  227. String studentID = null;
  228. int where = 0;
  229. System.out.print("Please enter the ID you would like to find");
  230. studentID = scan.next();
  231. // Finding the student's ID
  232. where = id.find(studentID);
  233. if (where < 0) {
  234. System.out.println("ID not found");
  235. } else {
  236. DBA.printRecord(where);
  237. }
  238. }
  239.  
  240. // DeleteIt method to delete a student
  241. public void deleteIt() {
  242. // Declaring variables
  243. String studentID, studentLast, studentFirst = null;
  244. boolean delete, delete2, delete3 = true;
  245. int where = 0;
  246. // Print statement prompting user for the student's ID to be deleted
  247. System.out.println("Please enter the ID you would like to delete");
  248. studentID = scan.next();
  249. // storing the student and finding them
  250. where = id.find(studentID);
  251. if (where >= 0) {
  252. // Getting the location of the student's first and last name and
  253. // removing them
  254. studentFirst = DBA.getFirst(where);
  255. studentLast = DBA.getLast(where);
  256. delete = id.delete(studentID);
  257. delete2 = first.delete(studentFirst);
  258. delete3 = last.delete(studentLast);
  259. if (delete && delete2 && delete3 == true) {
  260. DBA.printRecord(where);
  261. } else {
  262. System.out.println("ID not found");
  263. }
  264. } else {
  265. System.out.println("ID not found");
  266. }
  267. }
  268.  
  269. // InsertRecord method to insert new students into the database
  270. public boolean insertRecord(String studentLast, String studentFirst, String studentID) {
  271.  
  272. int index = DBA.getRecCount();
  273. // Checking to see if the database is full
  274. if (index == numElements) {
  275. System.out.println("Database full");
  276. return false;
  277. }
  278. // Inserting data into the database
  279. DBA.insertRecord(studentLast, studentFirst, studentID);
  280. id.insert(studentID, index);
  281. first.insert(studentFirst, index);
  282. last.insert(studentLast, index);
  283. return true;
  284. }
  285.  
  286. // Find method to find students within the database
  287.  
  288. // OutputList method to iterate through the data and print in different
  289. // orders
  290. public void displayList(Tree tree, int x) {
  291. tree.traverse(x,DBA);
  292. }
  293.  
  294. // Methods to print in various orders
  295. public void ListByFirstAscending() {
  296. displayList(first, 0);
  297. }
  298.  
  299. public void ListByLastAscending() {
  300. displayList(last, 0);
  301. }
  302.  
  303. public void ListByIDAscending() {
  304. displayList(id, 0);
  305. }
  306.  
  307. public void ListByFirstDescending() {
  308. displayList(first, 1);
  309. }
  310.  
  311. public void ListByLastDescending() {
  312. displayList(last, 1);
  313. }
  314.  
  315. public void ListByIDDescending() {
  316. displayList(id, 1);
  317. }
  318.  
  319. }
  320.  
  321. /*Austin Thornton - E01401657
  322. * Programming Project Two
  323. * COSC 311 - Winter 2017
  324. * DUE:3/8/17
  325. */
  326.  
  327. public class DataBaseArray {
  328. // Declaring necessary variables
  329. private DataBaseRecord[] DBRA;
  330. private int numRecords = 0;
  331.  
  332. // Constructor to set records
  333. DataBaseArray(int s) {
  334. DBRA = new DataBaseRecord[s];
  335. }
  336.  
  337. // InsertRecord method to insert a record into the array
  338. public void insertRecord(String studentLast, String studentFirst, String studentID) {
  339. DBRA[numRecords] = new DataBaseRecord(studentLast, studentFirst, studentID);
  340. numRecords++;
  341. }
  342.  
  343. // UpdateRecord method to update the array with the new empty location from
  344. // the stack
  345. public void updateRecord(int emptyLocation, String studentLast, String studentFirst, String studentID) {
  346. DBRA[emptyLocation] = new DataBaseRecord(studentLast, studentFirst, studentID);
  347. return;
  348. }
  349.  
  350. // PrintRecord method to print a record
  351. public void printRecord(int x) {
  352. DBRA[x].printArray();
  353. }
  354.  
  355. // PrintArray method to print
  356. public void printArray() {
  357. for (int x = 0; x < numRecords + 1; x++) {
  358. DBRA[x].printArray();
  359. }
  360. }
  361.  
  362. // "Getters" to get the first/last name and ID
  363. public String getID(int x) {
  364. String temp;
  365. temp = DBRA[x].getID();
  366. return temp;
  367. }
  368.  
  369. public String getFirst(int x) {
  370. String temp;
  371. temp = DBRA[x].getFirst();
  372. return temp;
  373. }
  374.  
  375. public String getLast(int x) {
  376. String temp;
  377. temp = DBRA[x].getLast();
  378. return temp;
  379. }
  380.  
  381. // Getting the number of records
  382. public int getRecCount() {
  383. return numRecords;
  384. }
  385.  
  386. // ToString method to convert the array to a string
  387. public String toString(int x) {
  388. String temp;
  389. temp = DBRA[x].toString();
  390. return temp;
  391. }
  392.  
  393. // Length method to get the length of the array
  394. public int length() {
  395. return DBRA.length;
  396. }
  397.  
  398. }
  399.  
  400.  
  401. /*Austin Thornton - E01401657
  402. * Programming Project Two
  403. * COSC 311 - Winter 2017
  404. * DUE:3/8/17
  405. */
  406.  
  407. public class DataBaseRecord {
  408. // Declaring necessary variables
  409. private String ID;
  410. private String fName;
  411. private String lName;
  412.  
  413. // Constructor to set the variables
  414. public DataBaseRecord(String i, String f, String l) {
  415. ID = new String(i);
  416. fName = new String(f);
  417. lName = new String(l);
  418. }
  419.  
  420. // UpdateRecord method to set the student's first/last name and ID
  421. public void updateRecord(String i, String f, String l) {
  422. ID = new String(i);
  423. fName = new String(f);
  424. lName = new String(l);
  425. }
  426.  
  427. // PrintArray method to print the student's first/last name and ID
  428. public void printArray() {
  429. System.out.println(lName + " " + fName + " " + ID);
  430. }
  431.  
  432. // "Getters" to get the ID/first/last name
  433. public String getID() {
  434. return ID;
  435. }
  436.  
  437. public String getFirst() {
  438. return fName;
  439. }
  440.  
  441. public String getLast() {
  442. return lName;
  443. }
  444.  
  445. // ToString method to convert the data into a string
  446. public String toString() {
  447. return lName + " " + fName + " " + ID;
  448. }
  449.  
  450. // CompareTo method to compare 2 students
  451. public int compareTo(DataBaseRecord otherStudent) {
  452. return (lName.compareTo(otherStudent.lName));
  453. }
  454.  
  455. }
  456.  
  457.  
  458. /*Austin Thornton - E01401657
  459. * Programming Project Two
  460. * COSC 311 - Winter 2017
  461. * DUE:3/8/17
  462. */
  463.  
  464. import java.io.File;
  465. import java.io.FileInputStream;
  466. import java.io.FileNotFoundException;
  467.  
  468. import java.util.*;
  469.  
  470. public class Driver {
  471.  
  472. public static void main(String[] args) {
  473. // Declaring necessary variables
  474. DataBase d = new DataBase();
  475. int response;
  476. Scanner keyboard = new Scanner(System.in);
  477. // Try/catch to setup the file read and write into the database
  478. try {
  479. Scanner fileScanner = new Scanner(new FileInputStream(new File("data.txt")));
  480. while (fileScanner.hasNext()) {
  481. String last = fileScanner.next();
  482. String first = fileScanner.next();
  483. String id = fileScanner.next();
  484.  
  485. d.insertRecord(last, first, id);
  486. }
  487.  
  488. } catch (FileNotFoundException f) {
  489. System.out.println(f.getMessage() + "File not found");
  490. System.exit(0);
  491. }
  492.  
  493. // The menu for the user to select their action
  494. do {
  495. System.out.println(" 1 Add a new student");
  496. System.out.println(" 2 Delete a student");
  497. System.out.println(" 3 Find a student by ID");
  498. System.out.println(" 4 List students by ID increasing");
  499. System.out.println(" 5 List students by first name increasing");
  500. System.out.println(" 6 List students by last name increasing");
  501. System.out.println(" 7 List students by ID decreasing");
  502. System.out.println(" 8 List students by first name decreasing");
  503. System.out.println(" 9 List students by last name decreasing");
  504. System.out.println(" ");
  505. System.out.println(" 0 End");
  506.  
  507. response = keyboard.nextInt();
  508.  
  509. switch (response) {
  510. case 1:
  511. d.addIt(); // Note: if the user enters an ID already in use,
  512. // issue a warning and return to the menu
  513. break;
  514. case 2:
  515. d.deleteIt(); // Note: output either "Deleted" or "ID not Found"
  516. // and return to menu
  517. break;
  518. case 3:
  519. d.findIt(); // Note: output the entire record or the message
  520. // "ID not Found" and return to menu
  521. break;
  522. case 4:
  523. d.ListByIDAscending();
  524. break;
  525. case 5:
  526. d.ListByFirstAscending();
  527. break;
  528. case 6:
  529. d.ListByLastAscending();
  530. break;
  531. case 7:
  532. d.ListByIDDescending();
  533. break;
  534. case 8:
  535. d.ListByFirstDescending();
  536. break;
  537. case 9:
  538. d.ListByLastDescending();
  539. break;
  540. default:
  541. }
  542.  
  543. } while (response != 0);
  544. }
  545. }
  546.  
  547. /*Austin Thornton - E01401657
  548. * Programming Project Two
  549. * COSC 311 - Winter 2017
  550. * DUE:3/8/17
  551. */
  552.  
  553. public class IndexRecord {
  554. // Declaring necessary variables
  555. private String key;
  556. private int position;
  557. public IndexRecord next;
  558. public IndexRecord previous;
  559.  
  560. // Constructor to set the variables
  561. public IndexRecord(String k, int p) {
  562. key = k;
  563. position = p;
  564. }
  565.  
  566. // CompareTo method to compare records
  567. public int compareTo(String k) {
  568. return key.compareTo(k);
  569. }
  570.  
  571. // ToString method to print the key and position of a record
  572. public String toString() {
  573. return key + " " + position;
  574. }
  575.  
  576. // GetWhere method to get the position
  577. public int getWhere() {
  578. return position;
  579. }
  580.  
  581. // DisplayLink method to display a link of the linked list
  582. public void displayLink() {
  583. System.out.println(toString());
  584. }
  585.  
  586. }
  587.  
  588. Dunn Sean 31111
  589. Duong Geoffrey 29922
  590. Fazekas Nicholas 31100
  591. Prezioso Stefano 22223
  592. Puvvada Mohana 11224
  593. Ravikumar Rakhi 11226
  594. Salyers Matthew 11227
  595. Gillespie William 49587
  596. Hess Caleb 29282
  597. Armatis Jared 34512
  598. Beckman Allan 35176
  599. Wang Zhen 22113
  600. Wingett Jordan 1234
  601. Belt Keith 34987
  602. Bixler Tyler 22234
  603. Chambers Quentin 22567
  604. Chinni Adithya 28456
  605. Donheiser Michael 28456
  606. Kondrashov Mikhail 33331
  607. Kraus Laura 33332
  608. Krupp Phillip 49888
  609. Maass John 44112
  610. McCarty Amanda 44223
  611. Moldovan Gregory 44335
  612. Oshiyoye Adekunle 44556
  613. Pagalos Frank 33112
  614. Perski Zackery 33221
  615. Saunders Jordan 77556
  616. Simpson Ashlynne 77665
  617. Szalai Kyle 33112
  618. Witting Robert 21354
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement