11package sealey .javafxinventorysystem ;
22
3+ import javafx .collections .FXCollections ;
4+ import javafx .collections .ObservableList ;
35import javafx .event .ActionEvent ;
6+ import javafx .event .EventHandler ;
47import javafx .fxml .FXML ;
58import javafx .fxml .FXMLLoader ;
69import javafx .fxml .Initializable ;
@@ -66,6 +69,79 @@ public class MainWindow implements Initializable {
6669 @ FXML
6770 private TextField searchProductText ;
6871
72+ boolean isInt (String str ) {
73+ try {
74+ Integer .valueOf (str );
75+ return true ;
76+ } catch (NumberFormatException e ) { return false ; }
77+ }
78+
79+ private Part selectPart (int id ) {
80+ for (Part p : Inventory .getAllParts ()){
81+ if (p .getId () == id ) {
82+ return p ;
83+ }
84+ }
85+ return null ;
86+ }
87+ private Product selectProduct (int id ){
88+ for (Product p : Inventory .getAllProducts ()){
89+ if (p .getId () == id ) {
90+ return p ;
91+ }
92+ }
93+ return null ;
94+ }
95+ private ObservableList <Part > partFilter (String search ){
96+
97+ ObservableList <Part > temp = FXCollections .observableArrayList ();
98+ if (search .isEmpty ()) return Inventory .getAllParts ();
99+
100+ for (Part p : Inventory .getAllParts ()) {
101+ if (p .getName ().toLowerCase ().contains (search .toLowerCase ()))
102+ {
103+ temp .add (p );
104+ }
105+ if (isInt (search ) && p .getId () == Integer .parseInt (search )) {
106+ temp .add (p );
107+ }
108+ }
109+ return temp ;
110+ }
111+ private ObservableList <Product > productFilter (String search ){
112+
113+ ObservableList <Product > temp = FXCollections .observableArrayList ();
114+ if (search .isEmpty ()) return Inventory .getAllProducts ();
115+
116+ for (Product p : Inventory .getAllProducts ()) {
117+ if (p .getName ().toLowerCase ().contains (search .toLowerCase ()))
118+ {
119+ temp .add (p );
120+ }
121+ if (isInt (search ) && p .getId () == Integer .parseInt (search )) {
122+ temp .add (p );
123+ }
124+ }
125+ return temp ;
126+ }
127+ boolean deletePart (int id ) {
128+ for (Part p : Inventory .getAllParts ()){
129+ if (p .getId () == id ) {
130+ return Inventory .getAllParts ().remove (p );
131+ }
132+ }
133+
134+ return false ;
135+ }
136+ boolean deleteProduct (int id ) {
137+ for (Product p : Inventory .getAllProducts ()){
138+ if (p .getId () == id ) {
139+ return Inventory .getAllProducts ().remove (p );
140+ }
141+ }
142+ return false ;
143+ }
144+
69145 @ FXML
70146 void onActionAddPart (ActionEvent event ) throws IOException {
71147 stage = (Stage )((Button )event .getSource ()).getScene ().getWindow ();
@@ -91,7 +167,7 @@ void onActionDeletePart(ActionEvent event) {
91167
92168 @ FXML
93169 void onActionDeleteProduct (ActionEvent event ) {
94- System .out .println ("Delete Part " );
170+ System .out .println ("Delete Product " );
95171 }
96172
97173 @ FXML
@@ -117,11 +193,9 @@ void onActionModifyProduct(ActionEvent event) throws IOException {
117193 stage .show ();
118194 }
119195
120- @ Override
121- public void initialize (URL url , ResourceBundle resourceBundle ) {
122-
123- partTable .setItems (Inventory .getAllParts ());
124- productTable .setItems (Inventory .getAllProducts ());
196+ void displayData (ObservableList <Part > parts , ObservableList <Product > products ) {
197+ partTable .setItems (parts );
198+ productTable .setItems (products );
125199
126200 partIDCol .setCellValueFactory (new PropertyValueFactory <>("id" ));
127201 partNameCol .setCellValueFactory (new PropertyValueFactory <>("name" ));
@@ -133,4 +207,22 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
133207 productInvCol .setCellValueFactory (new PropertyValueFactory <>("stock" ));
134208 productPriceCol .setCellValueFactory (new PropertyValueFactory <>("price" ));
135209 }
210+
211+ @ Override
212+ public void initialize (URL url , ResourceBundle resourceBundle ) {
213+ displayData (Inventory .getAllParts (),Inventory .getAllProducts ());
214+ searchPartText .setOnAction (new EventHandler <ActionEvent >() {
215+ @ Override
216+ public void handle (ActionEvent actionEvent ) {
217+ displayData (partFilter (searchPartText .getText ()),productFilter (searchProductText .getText ()));
218+ }
219+ });
220+
221+ searchProductText .setOnAction (new EventHandler <ActionEvent >() {
222+ @ Override
223+ public void handle (ActionEvent actionEvent ) {
224+ displayData (partFilter (searchPartText .getText ()),productFilter (searchProductText .getText ()));
225+ }
226+ });
227+ }
136228}
0 commit comments