Skip to content

Commit 9d39680

Browse files
committed
AddPart scene generates unique ID (next in sequential order)
1 parent 425851c commit 9d39680

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src/main/java/sealey/javafxinventorysystem/AddPart.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ public class AddPart implements Initializable {
6464
@FXML
6565
private Button saveButton;
6666

67+
// Helper Functions
68+
@FXML
69+
boolean search(int id){
70+
for(Part p : Inventory.getAllParts())
71+
{
72+
if(p.getId() == id){
73+
return true;
74+
}
75+
}
76+
return false;
77+
}
78+
int generateID() {
79+
int id = 1;
80+
for(Part a : Inventory.getAllParts()) {
81+
if(search(id)){
82+
id++;
83+
} else {
84+
return id;
85+
}
86+
}
87+
return id;
88+
}
89+
6790
@FXML
6891
void onActionCancel(ActionEvent event) throws IOException {
6992
stage = (Stage)((Button)event.getSource()).getScene().getWindow();
@@ -73,7 +96,7 @@ void onActionCancel(ActionEvent event) throws IOException {
7396
}
7497
@FXML
7598
void onActionSave(ActionEvent event) throws IOException {
76-
int id = Integer.parseInt(partIDText.getText());
99+
int id = Integer.parseInt(partIDText.getPromptText());
77100
String name = partNameText.getText();
78101
int inv = Integer.parseInt(inventoryText.getText());
79102
double price = Double.parseDouble(priceText.getText());
@@ -108,6 +131,6 @@ public void onActionMachineLabel(ActionEvent actionEvent) {
108131

109132
@Override
110133
public void initialize(URL url, ResourceBundle resourceBundle) {
111-
System.out.println("initialized");
134+
partIDText.setPromptText(String.valueOf(generateID()));
112135
}
113136
}

0 commit comments

Comments
 (0)