3333* */
3434
3535public class MainWindow implements Initializable {
36+
3637 Stage stage ;
3738 Parent scene ;
3839
@@ -84,52 +85,63 @@ public class MainWindow implements Initializable {
8485 * @return boolean Returns true if string is also an integer, false if exception is caught
8586 */
8687 boolean isInt (String str ) {
88+
8789 try {
8890 Integer .valueOf (str );
8991 return true ;
9092 } catch (NumberFormatException e ) { return false ; }
9193 }
9294
9395 /*
94- * The partFilter() method checks a string input (the text retrieved from the searchPartText TextField)
95- * and returns a list of Parts whose name contains the string, and/or whose ID is equal to the string.
96- * Returns an ObservableList containing all parts if the TextField is empty. If the search term does not
97- * find any matches, error message is displayed in dialog box.
98- *
99- * @param search String retrieved from searchPartText
100- * @return ObservableList of Parts containing all parts whose name contains the search parameter
101- * and/or whose id is equal to the search parameter, or list of all Parts.
102- * */
103- private ObservableList <Part > partFilter (String search ){
96+ * The filterParts() method checks a string input (searchPartText TextField)
97+ * and returns a list of Parts whose name contains the string, and/or whose ID is equal to the string.
98+ * Returns an ObservableList containing all parts if the TextField is empty. If the search term does not
99+ * find any matches, error message is displayed in dialog box.
100+ *
101+ * @param search String retrieved from searchPartText
102+ * @return ObservableList of Parts containing all parts whose name contains the search parameter
103+ * and/or whose id is equal to the search parameter, or list of all Parts.
104+ * */
105+ ObservableList <Part > filterParts (){
106+
107+ String search = searchPartText .getText ();
108+ ObservableList <Part > temp = Inventory .lookupPart (search );
104109
105- ObservableList <Part > temp = FXCollections .observableArrayList ();
106- if (search .isEmpty ()) return Inventory .getAllParts ();
107- temp = Inventory .lookupPart (search );
108110 if (isInt (search )){
109111 temp .add (Inventory .lookupPart (Integer .parseInt (search )));
110112 }
111- return temp ;
113+
114+ if (temp .isEmpty ()){
115+ return Inventory .getAllParts ();
116+ } else {
117+ return temp ;
118+ }
112119 }
113120
114121 /*
115- * The productFilter () method checks a string input (the text retrieved from the searchProductText TextField)
122+ * The filterProducts () method checks a string input (searchProductText TextField)
116123 * and returns a list of Products whose name contains the string, and/or whose ID is equal to the string.
117124 * Returns an ObservableList containing all products if the TextField is empty. If the search term does not
118125 * find any matches, error message is displayed in dialog box.
119126 *
120127 * @param search String retrieved from searchProductText
121- * @return ObservableList of Products containing all products whose name contains the search parameter
128+ * @return temp ObservableList of Products containing all products whose name contains the search parameter
122129 * and/or whose id is equal to the search parameter, or list of all Products.
123130 * */
124- private ObservableList <Product > productFilter (String search ){
131+ ObservableList <Product > filterProducts (){
132+
133+ String search = searchProductText .getText ();
134+ ObservableList <Product > temp = Inventory .lookupProduct (search );
125135
126- ObservableList <Product > temp = FXCollections .observableArrayList ();
127- if (search .isEmpty ()) return Inventory .getAllProducts ();
128- temp = Inventory .lookupProduct (search );
129136 if (isInt (search )){
130137 temp .add (Inventory .lookupProduct (Integer .parseInt (search )));
131138 }
132- return temp ;
139+
140+ if (temp .isEmpty ()){
141+ return Inventory .getAllProducts ();
142+ } else {
143+ return temp ;
144+ }
133145 }
134146
135147 /*
@@ -140,6 +152,7 @@ private ObservableList<Product> productFilter(String search){
140152 * */
141153 @ FXML
142154 void onActionAddPart (ActionEvent event ) throws IOException {
155+
143156 stage = (Stage )((Button )event .getSource ()).getScene ().getWindow ();
144157 scene = FXMLLoader .load (Objects .requireNonNull (getClass ().getResource ("AddPart.fxml" )));
145158 stage .setScene (new Scene (scene ));
@@ -155,6 +168,7 @@ void onActionAddPart(ActionEvent event) throws IOException {
155168 * */
156169 @ FXML
157170 void onActionAddProduct (ActionEvent event ) throws IOException {
171+
158172 stage = (Stage )((Button )event .getSource ()).getScene ().getWindow ();
159173 scene = FXMLLoader .load (Objects .requireNonNull (getClass ().getResource ("AddProduct.fxml" )));
160174 stage .setScene (new Scene (scene ));
@@ -171,7 +185,8 @@ void onActionAddProduct(ActionEvent event) throws IOException {
171185 * */
172186 @ FXML
173187 void onActionDeletePart (ActionEvent event ) {
174- System .out .println ("Delete Part" );
188+
189+ Inventory .deletePart (partTable .getSelectionModel ().getSelectedItem ());
175190 }
176191
177192 /*
@@ -183,7 +198,8 @@ void onActionDeletePart(ActionEvent event) {
183198 * */
184199 @ FXML
185200 void onActionDeleteProduct (ActionEvent event ) {
186- System .out .println ("Delete Product" );
201+
202+ Inventory .deleteProduct (productTable .getSelectionModel ().getSelectedItem ());
187203 }
188204
189205 /*
@@ -193,6 +209,7 @@ void onActionDeleteProduct(ActionEvent event) {
193209 * */
194210 @ FXML
195211 void onActionExit (ActionEvent event ) {
212+
196213 System .exit (0 );
197214 }
198215
@@ -204,8 +221,16 @@ void onActionExit(ActionEvent event) {
204221 * */
205222 @ FXML
206223 void onActionModifyPart (ActionEvent event ) throws IOException {
224+
225+ FXMLLoader loader = new FXMLLoader ();
226+ loader .setLocation (Objects .requireNonNull (getClass ().getResource ("ModifyPart.fxml" )));
227+ loader .load ();
228+
229+ ModifyPart modPartController = loader .getController ();
230+ modPartController .sendPart (partTable .getSelectionModel ().getSelectedItem ());
231+
207232 stage = (Stage )((Button )event .getSource ()).getScene ().getWindow ();
208- scene = FXMLLoader . load ( Objects . requireNonNull ( getClass (). getResource ( "ModifyPart.fxml" )) );
233+ scene = loader . getRoot ( );
209234 stage .setScene (new Scene (scene ));
210235 stage .setTitle ("Modify Part" );
211236 stage .show ();
@@ -219,6 +244,7 @@ void onActionModifyPart(ActionEvent event) throws IOException {
219244 * */
220245 @ FXML
221246 void onActionModifyProduct (ActionEvent event ) throws IOException {
247+
222248 stage = (Stage )((Button )event .getSource ()).getScene ().getWindow ();
223249 scene = FXMLLoader .load (Objects .requireNonNull (getClass ().getResource ("ModifyProduct.fxml" )));
224250 stage .setScene (new Scene (scene ));
@@ -233,6 +259,7 @@ void onActionModifyProduct(ActionEvent event) throws IOException {
233259 * @param products ObservableList of products to be displayed in Products table
234260 * */
235261 void displayData (ObservableList <Part > parts , ObservableList <Product > products ) {
262+
236263 partTable .setItems (parts );
237264 productTable .setItems (products );
238265
@@ -257,18 +284,20 @@ void displayData(ObservableList<Part> parts, ObservableList<Product> products) {
257284 * */
258285 @ Override
259286 public void initialize (URL url , ResourceBundle resourceBundle ) {
287+
260288 displayData (Inventory .getAllParts (),Inventory .getAllProducts ());
261289 searchPartText .setOnAction (new EventHandler <ActionEvent >() {
262290 @ Override
263291 public void handle (ActionEvent actionEvent ) {
264- displayData (partFilter ( searchPartText . getText ()), productFilter ( searchProductText . getText () ));
292+ displayData (filterParts (), filterProducts ( ));
265293 }
266294 });
267295
268296 searchProductText .setOnAction (new EventHandler <ActionEvent >() {
297+
269298 @ Override
270299 public void handle (ActionEvent actionEvent ) {
271- displayData (partFilter ( searchPartText . getText ()), productFilter ( searchProductText . getText () ));
300+ displayData (filterParts (), filterProducts ( ));
272301 }
273302 });
274303 }
0 commit comments