Last active
July 7, 2020 06:08
-
-
Save Kijimu7/4444b1f202f29d80aa62354e14efec50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package Main; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Modality; | |
import javafx.stage.Stage; | |
import model.InHouse; | |
import model.Inventory; | |
import model.Outsourced; | |
import model.Product; | |
import sun.tools.jar.Main; | |
import java.io.IOException; | |
/** | |
* | |
* @author pinko | |
*/ | |
public class InventoryProgram extends Application implements InventoryProgram1 { | |
//Error Class 'InventoryProgram' must either be declared abstract or implement abstract method 'start(Stage)' in 'Application' | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
launch(args); | |
} | |
//Test prevent duplicate scene to create main scene | |
private static Scene scene; | |
private static Stage primaryStage; | |
private static Stage popUp; | |
//load a change the "main" view | |
public static void setFXML(String fxml, String title) throws IOException{ | |
scene.setRoot((Parent) loadFXML(fxml)); | |
primaryStage.setResizable(false); | |
primaryStage.centerOnScreen(); | |
primaryStage.setResizable(false); | |
primaryStage.setTitle(title); | |
} | |
private static Object loadFXML(String fxml)throws IOException{ | |
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/ViewController/MainScreen.fxml")); | |
return fxmlLoader.load(); | |
} | |
//load popUp windows | |
public static void popUp(String fxml, String title) { | |
try { | |
Parent node = (Parent) loadFXML(fxml); | |
popUp = new Stage(); | |
Scene scene = new Scene(node); | |
popUp.setScene(scene); | |
popUp.setTitle(title); | |
popUp.initOwner(primaryStage); | |
popUp.initModality(Modality.WINDOW_MODAL); | |
popUp.centerOnScreen(); | |
popUp.setResizable(false); | |
popUp.showAndWait(); | |
} catch (IOException e) { | |
//To Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static void setScene(Scene scene) { | |
InventoryProgram.scene = scene; | |
} | |
public static void setPrimaryStage(Stage primaryStage) { | |
InventoryProgram.primaryStage = primaryStage; | |
Inventory inventory = new Inventory(); | |
addTestData(inventory); | |
} | |
public static Stage getPopUp(){ | |
return popUp; | |
} | |
// @Override | |
// public void start(Stage primaryStage) throws Exception { | |
// | |
// Inventory inventory = new Inventory(); | |
// addTestData(inventory); | |
// | |
// Parent root = FXMLLoader.load(getClass().getResource("/ViewController/MainScreen.fxml")); | |
// Scene scene = new Scene(root); | |
// primaryStage.setScene(scene); | |
// primaryStage.show(); | |
// } | |
static void addTestData(Inventory inv) { | |
//Add InHouse Parts | |
InHouse part1 = new InHouse(1, "part1", 5.00, 10, 1, 100, 101); | |
InHouse part2 = new InHouse(2, "part2", 6.00, 20, 1, 100, 102); | |
Inventory.addPart(part1); | |
Inventory.addPart(part2); | |
//Outsourced parts | |
Outsourced part3 = new Outsourced(3, "part3", 10.00, 60, 1, 100, "company1"); | |
Inventory.addPart(part3); | |
//Products | |
Product product1 = new Product(10, "product1", 6.00, 20, 1, 100); | |
Product product2 = new Product(20, "product2", 5.00, 10, 1, 100); | |
Product product3 = new Product(30, "product3", 7.00, 30, 1, 100); | |
Inventory.addProduct(product1); | |
Inventory.addProduct(product2); | |
Inventory.addProduct(product3); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ViewController; | |
import javafx.fxml.FXML; | |
import javafx.fxml.FXMLLoader; | |
import javafx.fxml.Initializable; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.scene.control.*; | |
import javafx.stage.Stage; | |
import model.InHouse; | |
import model.Inventory; | |
import model.Outsourced; | |
import model.Part; | |
import java.awt.event.ActionEvent; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Optional; | |
import java.util.ResourceBundle; | |
public class ModifypartController implements Initializable { | |
Part part; | |
private boolean isOutsourced = true; | |
private int index = 0; | |
@Override | |
public void initialize(URL location, ResourceBundle resources) { | |
} | |
public ModifypartController(){ | |
} | |
Stage stage; | |
Parent scene; | |
@FXML | |
private RadioButton inHouseRBtn; | |
@FXML | |
private ToggleGroup toggleGroup; | |
@FXML | |
private RadioButton outsourcedRBtn; | |
@FXML | |
private TextField modifyPartMinTxt; | |
@FXML | |
private TextField modifyPartPriceTxt; | |
@FXML | |
private TextField modifyPartDynamicTxt; | |
@FXML | |
private TextField modifyPartMaxTxt; | |
@FXML | |
private TextField modifyPartInvTxt; | |
@FXML | |
private TextField modifyPartNameTxt; | |
@FXML | |
private TextField modifyPartIdTxt; | |
@FXML | |
private Button saveBtn; | |
@FXML | |
private Button cancelBtn; | |
@FXML | |
public Label modifyPartCnameLbl; | |
public void setPart(Part part){ | |
this.part = part; | |
modifyPartIdTxt.setText(Integer.toString(part.getId())); | |
modifyPartNameTxt.setText(part.getName()); | |
modifyPartInvTxt.setText(Integer.toString(part.getInv())); | |
modifyPartPriceTxt.setText(Double.toString(part.getPrice())); | |
modifyPartMaxTxt.setText(Integer.toString(part.getMax())); | |
modifyPartMinTxt.setText(Integer.toString(part.getMin())); | |
if(part instanceof InHouse){ | |
inHouseRBtn.fire(); | |
modifyPartDynamicTxt.setText(String.valueOf(((InHouse)part).getMachineId())); | |
}else{ | |
outsourcedRBtn.fire(); | |
modifyPartDynamicTxt.setText(String.valueOf(((Outsourced)part).getCompanyName())); | |
} | |
} | |
@FXML | |
void modifyPartSave(javafx.event.ActionEvent event) throws IOException{ | |
InHouse newInHouse = new InHouse(0, "", 0, 0, 0, 0, 0); | |
index = Inventory.getAllParts().indexOf(part); | |
if (!isOutsourced) { | |
if (isValid(modifyPartNameTxt.getText(), modifyPartPriceTxt.getText(), modifyPartInvTxt.getText(), modifyPartMinTxt.getText(), | |
modifyPartMaxTxt.getText(), modifyPartDynamicTxt.getText())) { | |
table(newInHouse, modifyPartNameTxt, modifyPartPriceTxt, modifyPartInvTxt, modifyPartMinTxt, modifyPartMaxTxt, modifyPartDynamicTxt); | |
Inventory.updatePart(index, newInHouse); | |
System.out.println("Part modified"); | |
Stage stage; | |
Parent root; | |
stage = (Stage) saveBtn.getScene().getWindow(); | |
FXMLLoader loader = new FXMLLoader(getClass().getResource("/ViewController/MainScreen.fxml")); | |
root = loader.load(); | |
Scene scene = new Scene(root); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
}else { | |
Outsourced newOutsourced = new Outsourced(0, "", 0, 0, 0, 0, ""); | |
if (isValid(modifyPartNameTxt.getText(), modifyPartPriceTxt.getText(), modifyPartInvTxt.getText(), modifyPartMinTxt.getText(), modifyPartMaxTxt.getText(), modifyPartDynamicTxt.getText())) { | |
table(newOutsourced, modifyPartNameTxt, modifyPartPriceTxt, modifyPartInvTxt, modifyPartMinTxt, modifyPartMaxTxt, modifyPartDynamicTxt); | |
Inventory.updatePart(index, newOutsourced); | |
System.out.println("Part modified"); | |
// Paret tableViewParet loader = new FXMLLoader(getClass().getResource("/ViewController/MainScreen.fxml")); | |
// Stage window = (Stage)((Node).eventgetSource()).getScene.getWindow(); | |
// | |
// window.setScene(tableViewScene); | |
// window.show(); | |
Stage stage; | |
Parent root; | |
stage = (Stage) saveBtn.getScene().getWindow(); | |
FXMLLoader loader = new FXMLLoader(getClass().getResource("/ViewController/MainScreen.fxml")); | |
root = loader.load(); | |
Scene scene = new Scene(root); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
} | |
} | |
static void table(InHouse newInHouse, TextField modifyPartNameTxt, TextField modifyPartPriceTxt, TextField modifyPartInvTxt, TextField modifyPartMinTxt, TextField modifyPartMaxTxt, TextField modifyPartDynamicTxt) { | |
if (!modifyPartNameTxt.getText().isEmpty()) { | |
newInHouse.setName(modifyPartNameTxt.getText()); | |
} | |
if (!modifyPartPriceTxt.getText().isEmpty()) { | |
newInHouse.setPrice(Double.parseDouble(modifyPartPriceTxt.getText())); | |
} | |
if (!modifyPartInvTxt.getText().isEmpty()) { | |
newInHouse.setInv(Integer.parseInt(modifyPartInvTxt.getText())); | |
} | |
if (!modifyPartMinTxt.getText().isEmpty()) { | |
newInHouse.setMin(Integer.parseInt(modifyPartMinTxt.getText())); | |
} | |
if (!modifyPartMaxTxt.getText().isEmpty()) { | |
newInHouse.setMax(Integer.parseInt(modifyPartMaxTxt.getText())); | |
} | |
if (!modifyPartDynamicTxt.getText().isEmpty()) { | |
newInHouse.setMachineId(Integer.parseInt(modifyPartDynamicTxt.getText())); | |
} | |
} | |
static void table(Outsourced newOutsourced, TextField modifyPartNameTxt, TextField modifyPartPriceTxt, TextField modifyPartInvTxt, TextField modifyPartMinTxt, TextField modifyPartMaxTxt, TextField modifyPartDynamicTxt) { | |
if (!modifyPartNameTxt.getText().isEmpty()) { | |
newOutsourced.setName(modifyPartNameTxt.getText()); | |
} | |
if (!modifyPartPriceTxt.getText().isEmpty()) { | |
newOutsourced.setPrice(Double.parseDouble(modifyPartPriceTxt.getText())); | |
} | |
if (!modifyPartInvTxt.getText().isEmpty()) { | |
newOutsourced.setInv(Integer.parseInt(modifyPartInvTxt.getText())); | |
} | |
if (!modifyPartMinTxt.getText().isEmpty()) { | |
newOutsourced.setMin(Integer.parseInt(modifyPartMinTxt.getText())); | |
} | |
if (!modifyPartMaxTxt.getText().isEmpty()) { | |
newOutsourced.setMax(Integer.parseInt(modifyPartMaxTxt.getText())); | |
} | |
if (!modifyPartDynamicTxt.getText().isEmpty()) { | |
newOutsourced.setCompanyName(modifyPartDynamicTxt.getText()); | |
} | |
} | |
private boolean isValid(String partName, String partPrice, String partInv, String partMin, String partMax, String toggleGroup ) { | |
String errorMessage = ""; | |
Integer intMin = null, intMax = null; | |
boolean isValid; | |
if (partName == null || partName.isEmpty()) { | |
errorMessage += ("Need to input part name\n"); | |
} | |
try { | |
intMin = Integer.parseInt(partMin); | |
} catch (NumberFormatException e) { | |
errorMessage += ("Min must a number\n"); | |
} | |
try { | |
intMax = Integer.parseInt(partMax); | |
} catch (NumberFormatException e) { | |
errorMessage += ("Maximum must be a number\n"); | |
} | |
try { | |
if (intMin > intMax) { | |
errorMessage += ("Minimum must be less than maximum \n"); | |
} | |
if (intMin < 0 || intMax < 0) { | |
errorMessage += ("Quantity cannot be less than zero\n"); | |
} | |
} catch (NullPointerException e) { | |
errorMessage += ("Min and Max cannot be less than zero \n"); | |
} | |
try { | |
int intInv = Integer.parseInt(partInv); | |
if (intMax != null && intMin != null) { | |
if (intInv < intMin || intInv > intMax) { | |
errorMessage += ("Inventory must be between minimum and maximum \n"); | |
} | |
} else { | |
errorMessage += ("Inventory cannot be blank \n"); | |
} | |
} catch (NumberFormatException e) { | |
errorMessage += ("Inventory cannot be blank and must be a number\n"); | |
} | |
try { | |
double price = Double.parseDouble(partPrice); | |
if (price < 0) { | |
errorMessage += ("Price cannot be less than zero\n"); | |
} | |
} catch (NumberFormatException e) { | |
errorMessage += ("Price cannot be blank and must be a number\n"); | |
} | |
if (!isOutsourced) { | |
if (!toggleGroup.isEmpty()) { | |
try { | |
Integer.parseInt(toggleGroup); | |
} catch (NumberFormatException e) { | |
errorMessage += ("MachineId must be a number"); | |
} | |
} else { | |
errorMessage += ("MachineId cannot be black\n"); | |
} | |
} else { | |
if (toggleGroup.isEmpty()) { | |
errorMessage += ("Company name cannot be blank \n"); | |
} | |
} | |
if (errorMessage.isEmpty() == true) { | |
isValid = true; | |
} else { | |
isValid = false; | |
Alert alert = new Alert(Alert.AlertType.ERROR); | |
alert.setTitle("Part Validation error"); | |
alert.setHeaderText("Error"); | |
alert.setContentText(errorMessage); | |
alert.showAndWait(); | |
} | |
return isValid; | |
} | |
@FXML | |
void inHouseRBtnS(javafx.event.ActionEvent event) { | |
isOutsourced = false; | |
modifyPartCnameLbl.setText("Machine ID"); | |
} | |
@FXML | |
void outsourcedRBtnS(javafx.event.ActionEvent event) { | |
isOutsourced = true; | |
modifyPartCnameLbl.setText("Company Name"); | |
} | |
@FXML | |
void modifyPartCompanyTxt(ActionEvent event) { | |
} | |
@FXML | |
void modifyPartCostTxt(ActionEvent event) { | |
} | |
@FXML | |
void modifyPartInvTxt(ActionEvent event) { | |
} | |
@FXML | |
void modifyPartMaxTxt(ActionEvent event) { | |
} | |
@FXML | |
void modifyPartMinTxt(ActionEvent event) { | |
} | |
@FXML | |
void modifyPartNameTxt(ActionEvent event) { | |
} | |
public void modifyPartMinTxt(javafx.event.ActionEvent actionEvent) { | |
} | |
public void modifyPartCostTxt(javafx.event.ActionEvent actionEvent) { | |
} | |
public void modifyPartCompanyTxt(javafx.event.ActionEvent actionEvent) { | |
} | |
public void modifyPartMaxTxt(javafx.event.ActionEvent actionEvent) { | |
} | |
private void ReturnToMainScreen(javafx.event.ActionEvent event) throws IOException { | |
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "This will clear all text field values, do you want to continue?"); | |
Optional<ButtonType> results = alert.showAndWait(); | |
if(results.isPresent() && results.get() == ButtonType.OK) { | |
stage = (Stage) ((Button) event.getSource()).getScene().getWindow(); | |
scene = FXMLLoader.load(getClass().getResource("MainScreen.fxml")); | |
stage.setScene(new Scene(scene)); | |
stage.show(); | |
} | |
} | |
public void modifyPartCancel(javafx.event.ActionEvent event) throws IOException { | |
ReturnToMainScreen(event); | |
} | |
public void modifyProductIdTxt(javafx.event.ActionEvent actionEvent) { | |
} | |
public void modifyProductSearch(javafx.event.ActionEvent actionEvent) { | |
} | |
public void modifyProductSave(javafx.event.ActionEvent actionEvent) { | |
System.out.println("test"); | |
} | |
public void modifyProductCancel(javafx.event.ActionEvent actionEvent) { | |
} | |
public void modifyProductDelete(javafx.event.ActionEvent actionEvent) { | |
} | |
public void modifyProductAdd(javafx.event.ActionEvent actionEvent) { | |
} | |
public void modifyPartInvOnAction(javafx.event.ActionEvent actionEvent) { | |
} | |
public void modifyPartNameOnAction(javafx.event.ActionEvent actionEvent) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment