Final Project Birdwatching - Frederic Desautels

 
  1 package Brids;
  2 import java.io.File;
  3 import java.io.FileNotFoundException;
  4 import java.util.Scanner;
  5 import javafx.application.Application;
  6 import static javafx.application.Application.launch;
  7 import javafx.event.ActionEvent;
  8 import javafx.event.EventHandler;
  9 import javafx.geometry.Pos;
 10 import javafx.scene.Scene;
 11 import javafx.scene.control.Button;
 12 import javafx.scene.control.Label;
 13 import javafx.scene.layout.HBox;
 14 import javafx.scene.layout.VBox;
 15 import javafx.stage.FileChooser;
 16 import javafx.stage.Stage;
 17 
 18 public class Brids extends Application {
 19     private BirdNode head;//no Brids constructor because the head is null by default
 20     private class BirdNode {
 21         private String name;
 22         private int frequency = 1;
 23         private BirdNode next;
 24         BirdNode(String n)//constructor for a node
 25             {name = n;}//next is already null, frequency is always 1 to start
 26     }
 27 
 28     public void insertEnd(String n) {
 29         BirdNode newNode = new BirdNode(n);//create a new node
 30         if (head == null) {//if the list is empty
 31         newNode.next = head;//make the new node point to the head 
 32         head = newNode;//make the new node the head
 33         return;
 34         }
 35         BirdNode ref = head;
 36         while (ref.next != null)
 37             ref = ref.next;//loop until the end of the linked list is reached
 38         ref.next = newNode;//add the node at the end if the linked list is not empty
 39     }
 40 
 41     public BirdNode isFound(String n) {
 42         BirdNode found = head;//begin at the first node
 43         for (BirdNode ref = head; ref.next != null; ref = ref.next)
 44             if (n.equals(ref.next.name))
 45                 found = ref;//the node before the one that is being looked for (because the removeNode function needs this)
 46         return found;//return the found node, or the head if it wasn't found
 47     }
 48 
 49     public String print() {
 50         String output = "";
 51         for (BirdNode ref = head.next.next; ref != null; ref = ref.next)
 52             output += ref.name + " " + ref.frequency + "\n";//add the name of the bird and its frequency to a string until the last node is reached
 53         return output;
 54     }
 55     
 56     public Brids sortList() {//selection sort in ascending order of BirdNode frequencies
 57         for (BirdNode i = head; i != null; i = i.next) {
 58             BirdNode low = i;
 59             for (BirdNode j = i.next; j != null; j = j.next)
 60                 if (j.frequency < low.frequency)
 61                     low = j;
 62             int temp = i.frequency;
 63             i.frequency = low.frequency;
 64             low.frequency = temp;
 65             String tempS = i.name;
 66             i.name = low.name;
 67             low.name = tempS;
 68         }
 69         return this;//return the sorted linked list
 70     }
 71     
 72     
 73     public static Brids fileReading(File f, Brids list) {
 74         boolean test = list.head == null;
 75         if (test) {//if the first button was pressed
 76             list.insertEnd("N/A");
 77             list.insertEnd("N/A");
 78         }//create 2 dummy birds, because I need to use .next in removeNode, and I need A first Bird for isFound
 79         try {
 80             Scanner reader = new Scanner(f); //create a scanner to read through the file
 81             while (reader.hasNext()) {
 82                 String currentBird = reader.nextLine(); //read the bird from the file
 83                 if (test) {
 84                     if (list.isFound(currentBird) != list.head)
 85                         list.isFound(currentBird).next.frequency++; //Increment the frequency of the bird if it is already in the list
 86                     else
 87                         list.insertEnd(currentBird); //add the bird to the list if it is not in the list already
 88                 } else if (list.isFound(currentBird) != list.head)//if the 2nd button was pressed and the bird is in the list
 89                     list.isFound(currentBird).next = list.isFound(currentBird).next.next;//remove the node
 90             }
 91             reader.close();//close the file
 92         } catch (FileNotFoundException ex)
 93             {System.out.println("Error, file not found!");}//catch the exception if the file is not found
 94         return list;
 95     }
 96 
 97     @Override public void start(final Stage stage) {
 98         final FileChooser fileChooser = new FileChooser();//create the file chooser
 99         final Button openButton1 = new Button("Select the Birdwatching List"), openButton2 = new Button("Select the list of non-indigenous Birds");//create 2 buttons
100         final Label path1 = new Label(), path2 = new Label();//create 2 empty labels
101         openButton1.setOnAction(
102                 new EventHandler<ActionEvent>() {
103                     @Override public void handle(final ActionEvent e) {
104                         File file1 = fileChooser.showOpenDialog(stage);
105                         if (file1 != null){//if a file was selected
106                             openButton1.setText(file1.getName());//set the text of the button to the name of the file
107                             path1.setText(file1.getPath());//set the label to the full path for use later on
108                         }
109                     }
110                 });
111         openButton2.setOnAction(
112                 new EventHandler<ActionEvent>() {
113                     @Override public void handle(final ActionEvent e) {
114                         File file2 = fileChooser.showOpenDialog(stage);
115                         if (file2 != null){//if a file was selected
116                             openButton2.setText(file2.getName());//set the text of the button to the name of the file
117                             path2.setText(file2.getPath());//set the label to the full path for use later on
118                         }
119                     }
120                 });
121         Button original = new Button("Display Original List"), removing = new Button("Remove non-indigenous Birds"), sorting = new Button("Sort By Frequency");
122         final Label label = new Label("No birds yet!");//creating the 3 buttons and an empty label
123         original.setOnAction(new EventHandler<ActionEvent>() {
124             @Override public void handle(ActionEvent event)
125                 {label.setText(Brids.fileReading(new File(path1.getText()), new Brids()).print());}//set the label to the original list when the original button is pressed
126         });
127         removing.setOnAction(new EventHandler<ActionEvent>() {
128             @Override public void handle(ActionEvent event)
129                 {label.setText(Brids.fileReading(new File(path2.getText()), Brids.fileReading(new File(path1.getText()), new Brids())).print());}//set the label to the shortened list when the removing button is pressed
130         });
131         sorting.setOnAction(new EventHandler<ActionEvent>() {
132             @Override public void handle(ActionEvent event)
133                 {label.setText(Brids.fileReading(new File(path2.getText()), Brids.fileReading(new File(path1.getText()), new Brids())).sortList().print());}//set the label to the sorted list when the sorting button is pressed
134         });
135         HBox hb = new HBox(50), hb2 = new HBox(10), hb3 = new HBox(10);
136         hb.getChildren().addAll(original, removing, sorting);//put the buttons in the HBOX
137         hb.setAlignment(Pos.CENTER);//Center the buttons
138         hb2.getChildren().addAll(openButton1, path1);//place the 1st button and label in the 1st hbox
139         hb3.getChildren().addAll(openButton2, path2);//place the 2nd button and label in the 2nd hbox
140         VBox vb = new VBox(25);
141         vb.getChildren().addAll(hb2, hb3, hb, label);//put the hboxes and the label in a vbox
142         stage.setTitle("Bird Spotting");
143         stage.setScene(new Scene(vb, 700, 520));
144         stage.show();//display the scene
145     }
146 
147     public static void main(String[] args) {
148         launch(args);
149     }
150 }
UML Diagram
 
Download