Skip to content

Instantly share code, notes, and snippets.

@Kijimu7
Last active June 28, 2020 06:16
Show Gist options
  • Save Kijimu7/7abc302dae2bf285fb120fe7f146643b to your computer and use it in GitHub Desktop.
Save Kijimu7/7abc302dae2bf285fb120fe7f146643b to your computer and use it in GitHub Desktop.
Inventoryprojecttest
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="500.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ViewController.AddpartController">
<children>
<Label layoutX="35.0" layoutY="43.0" text="Add Part" />
<RadioButton fx:id="addPartInhouseRBtn" layoutX="138.0" layoutY="43.0" mnemonicParsing="false" onAction="#inhouseRadioButtonSelected" text="In-House">
<toggleGroup>
<ToggleGroup fx:id="toggleGroup" />
</toggleGroup></RadioButton>
<RadioButton fx:id="addPartOutsourcedRBtn" layoutX="291.0" layoutY="43.0" mnemonicParsing="false" onAction="#outsourcedRadioButtonSelected" selected="true" text="Outsourcesd" toggleGroup="$toggleGroup" />
<Label fx:id="addPartCnameLbl" layoutX="26.0" layoutY="376.0" prefHeight="17.0" prefWidth="120.0" text="Company Name" />
<Label layoutX="300.0" layoutY="330.0" text="Min" />
<Label layoutX="54.0" layoutY="119.0" text="ID" />
<Label layoutX="54.0" layoutY="175.0" text="Name" />
<Label layoutX="54.0" layoutY="330.0" text="Max" />
<Label layoutX="54.0" layoutY="279.0" text="Price/Cost" />
<Label layoutX="54.0" layoutY="225.0" prefHeight="17.0" prefWidth="32.0" text="Inv" />
<TextField fx:id="addPartMinTxt" layoutX="341.0" layoutY="326.0" prefHeight="25.0" prefWidth="86.0" />
<TextField fx:id="addPartPriceTxt" layoutX="164.0" layoutY="275.0" />
<TextField fx:id="addPartDynamicTxt" layoutX="164.0" layoutY="372.0" />
<TextField fx:id="addPartMaxTxt" layoutX="164.0" layoutY="326.0" prefHeight="25.0" prefWidth="88.0" />
<TextField fx:id="addPartInvTxt" layoutX="164.0" layoutY="221.0" />
<TextField fx:id="addPartNameTxt" layoutX="164.0" layoutY="171.0" />
<TextField fx:id="addPartIdTxt" disable="true" editable="false" layoutX="164.0" layoutY="115.0" />
<Button fx:id="addPartsaveBtn" layoutX="313.0" layoutY="419.0" mnemonicParsing="false" onAction="#onActionSavePart" text="Save" />
<Button fx:id="addPartcancelBtn" layoutX="399.0" layoutY="419.0" mnemonicParsing="false" onAction="#onActionCancel" text="Cancel" />
</children>
</AnchorPane>
package ViewController;
import javafx.event.ActionEvent;
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 java.io.IOException;
import java.net.URL;
import java.util.Random;
public class AddpartController implements Initializable {
Inventory inventory;
private boolean isOutsourced = true;
Stage stage;
Parent scene;
private Object ResourceBundle;
@FXML
private Object ActionEvent;
@Override
public void initialize(URL location, java.util.ResourceBundle resources) {
}
public AddpartController() {
}
public AddpartController(Inventory inventory) {
this.inventory = inventory;
}
@FXML
public Label addPartCnameLbl;
@FXML
private RadioButton addPartInhouseRBtn;
@FXML
private ToggleGroup toggleGroup;
@FXML
private RadioButton addPartOutsourcedRBtn;
@FXML
private TextField addPartMinTxt;
@FXML
private TextField addPartPriceTxt;
@FXML
private TextField addPartCnameTxt;
@FXML
private TextField addPartMachineTxt;
@FXML
private TextField addPartMaxTxt;
@FXML
private TextField addPartInvTxt;
@FXML
private TextField addPartNameTxt;
@FXML
private TextField addPartIdTxt;
@FXML
private TextField addPartDynamicTxt;
@FXML
private Button addPartsaveBtn;
@FXML
private Button addPartcancelBtn;
@FXML
void inhouseRadioButtonSelected(javafx.event.ActionEvent event) {
isOutsourced = false;
addPartCnameLbl.setText("Machine ID");
}
@FXML
void outsourcedRadioButtonSelected(javafx.event.ActionEvent event) {
isOutsourced = true;
addPartCnameLbl.setText("Company Name");
}
@FXML
void onActionSavePart(ActionEvent event) throws IOException {
//Create auto Ids
Random random = new Random();
int id = random.nextInt(50);
if (isOutsourced == false) {
InHouse newInHouse = new InHouse(0, "", 0, 0, 0, 0, 0);
if (isValid(addPartNameTxt.getText(), addPartPriceTxt.getText(), addPartInvTxt.getText(), addPartMinTxt.getText(),
addPartMaxTxt.getText(), addPartDynamicTxt.getText())) {
newInHouse.setId(id);
if (!addPartNameTxt.getText().isEmpty()) {
newInHouse.setName(addPartNameTxt.getText());
}
if (!addPartPriceTxt.getText().isEmpty()) {
newInHouse.setName(addPartPriceTxt.getText());
}
if (!addPartInvTxt.getText().isEmpty()) {
newInHouse.setName(addPartInvTxt.getText());
}
if (!addPartMinTxt.getText().isEmpty()) {
newInHouse.setName(addPartMinTxt.getText());
}
if (!addPartMaxTxt.getText().isEmpty()) {
newInHouse.setName(addPartMaxTxt.getText());
}
if (!addPartDynamicTxt.getText().isEmpty()) {
newInHouse.setName(addPartDynamicTxt.getText());
}
inventory.addPart(newInHouse);
System.out.println("Part Added");
Stage stage;
Parent root;
stage = (Stage) addPartsaveBtn.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(addPartNameTxt.getText(), addPartPriceTxt.getText(), addPartInvTxt.getText(), addPartMinTxt.getText(), addPartMaxTxt.getText(), addPartDynamicTxt.getText())) {
newOutsourced.setId(id);
if (!addPartNameTxt.getText().isEmpty()) {
newOutsourced.setName(addPartNameTxt.getText());
}
if (!addPartPriceTxt.getText().isEmpty()) {
newOutsourced.setName(addPartPriceTxt.getText());
}
if (!addPartInvTxt.getText().isEmpty()) {
newOutsourced.setName(addPartInvTxt.getText());
}
if (!addPartMinTxt.getText().isEmpty()) {
newOutsourced.setName(addPartMinTxt.getText());
}
if (!addPartMaxTxt.getText().isEmpty()) {
newOutsourced.setName(addPartMaxTxt.getText());
}
if (!addPartDynamicTxt.getText().isEmpty()) {
newOutsourced.setName(addPartDynamicTxt.getText());
}
inventory.addPart(newOutsourced);
System.out.println("Part Added");
Stage stage;
Parent root;
stage = (Stage) addPartsaveBtn.getScene().getWindow();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/ViewController/MainScreen.fxml"));
root = loader.load();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
}
@FXML
public void onActionCancel(javafx.event.ActionEvent event) throws IOException {
stage = (Stage) ((Button) event.getSource()).getScene().getWindow();
scene = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
stage.setScene(new Scene((Parent) scene));
stage.show();
}
private boolean isValid(String partName, String partInv, String partPrice, 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;
}
}
// // int id = Integer.parseInt(addPartIdTxt.getText());
// String name = addPartNameTxt.getText();
// int inv = Integer.parseInt(addPartInvTxt.getText());
// int max = Integer.parseInt(addPartMaxTxt.getText());
// int min = Integer.parseInt(addPartMinTxt.getText());
// double price = Double.parseDouble(addPartPriceTxt.getText());
//
//
// if(addPartInhouseRBtn.isSelected()){
// int machineId = Integer.parseInt(addPartMachineTxt.getText());
// InHouse newInhousePart = new InHouse(id, name, price, inv, min, max, machineId);
// Inventory.addPart(newInhousePart);
// }
// if(addPartOutsourcedRBtn.isSelected())
//
// {
// String Cname = addPartCnameTxt.getText();
// Outsourced newOutsourcedPart = new Outsourced(id, name, price, inv, min, max, Cname);
// Inventory.addPart(newOutsourcedPart);
//
// }
package model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class Inventory {
private static ObservableList<Part> allParts = FXCollections.observableArrayList();
private static ObservableList<Product> allProducts = FXCollections.observableArrayList();
//add
public static void addPart(Part newPart)
{
allParts.add(newPart);
}
public static void addProduct(Product newProduct)
{
allProducts.add(newProduct);
}
//lookup
public static Part lookupPart(int partId) {
for (Part part : allParts) {
if (part.getId() == partId) {
return part;
}
}
return null;
}
public static Product lookupProduct(int productId)
{
for(Product product : allProducts){
if(product.getId() == productId){
return product;
}
}
return null;
}
public static ObservableList<Part> lookupPart(String partName)
{
ObservableList<Part> foundParts = FXCollections.observableArrayList();
for(int i = 0; i < allParts.size(); i++){
if(allParts.get(i).getName().toLowerCase().contains(partName.toLowerCase())){
foundParts.add(allParts.get(i));
}
}
return foundParts;
}
public static ObservableList<Product> lookupProduct(String productName) {
ObservableList<Product> foundProducts = FXCollections.observableArrayList();
for (int i = 0; i < allProducts.size(); i++) {
if (allProducts.get(i).getName().toLowerCase().contains(productName.toLowerCase())) {
foundProducts.add(allProducts.get(i));
}
}
return foundProducts;
}
public int getPartIndex(Part part){
return allParts.indexOf(part);
}
//update
public static void updatePart(int index, Part selectedPart){
allParts.set(index, selectedPart);
}
public static void updateProduct(int index, Product selectedProduct){
allProducts.set(index, selectedProduct);
}
//delete
public static boolean deletePart(Part selectedPart){
allParts.remove(selectedPart);
return true;
}
public static boolean deleteProduct(Product selectedProduct){
allProducts.remove(selectedProduct);
return true;
}
//get
public static ObservableList<Part> getAllParts()
{
return allParts;
}
public static ObservableList<Product> getAllProducts()
{
return allProducts;
}
}
/*
* 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.Stage;
import model.InHouse;
import model.Inventory;
import model.Outsourced;
import model.Product;
/**
*
* @author pinko
*/
public class
InventoryProgram extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Inventory inventory = new Inventory();
addTestData(inventory);
Parent root = FXMLLoader.load(getClass().getResource("../ViewController/MainScreen.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
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);
InHouse part3 = new InHouse(3, "part3", 7.00, 30, 1, 100, 103);
Inventory.addPart(part1);
Inventory.addPart(part2);
Inventory.addPart(part3);
//Outsourced parts
Outsourced part6 = new Outsourced(6, "part6", 10.00, 60, 1, 100, "company1");
Outsourced part7 = new Outsourced(7, "part7", 11.00, 70, 1, 100, "company2");
Outsourced part8 = new Outsourced(8, "part8", 12.00, 80, 1, 100, "company3");
Inventory.addPart(part6);
Inventory.addPart(part7);
Inventory.addPart(part8);
//Products
Product product1 = new Product(1, "product1", 6.00, 20, 1, 100);
Product product2 = new Product(2, "product2", 5.00, 10, 1, 100);
Product product3 = new Product(3, "product3", 7.00, 30, 1, 100);
Product product4 = new Product(4, "product4", 8.00, 40, 1, 100);
Product product5 = new Product(5, "product5", 9.00, 50, 1, 100);
Inventory.addProduct(product1);
Inventory.addProduct(product2);
Inventory.addProduct(product3);
Inventory.addProduct(product4);
Inventory.addProduct(product5);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Main.MainScreenController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>
/*
* 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 ViewController;
import javafx.event.ActionEvent;
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.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import model.Inventory;
import model.Part;
import model.Product;
import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
/**
* FXML Controller class
*
* @author pinko
*/
public class MainScreenController implements Initializable {
Stage stage;
Parent scene;
@FXML
private Button imsPartSearchBtn;
@FXML
private TextField imsPartSearchTxt;
@FXML
private TableView<Part> partTableView;
@FXML
private TableColumn<Part, Integer> partIdCol;
@FXML
private TableColumn<Part, String> partNameCol;
@FXML
private TableColumn<Part, String> partInvCol;
@FXML
private TableColumn<Part, Double> partPriceCol;
@FXML
private Button imsPartAddBtn;
@FXML
private AnchorPane addPartButton;
@FXML
private Button imsSearchBtn;
@FXML
private TextField imsProductTxt;
@FXML
private TableView<Product> table1;
@FXML
private TableColumn<?, ?> productIdCol;
@FXML
private TableColumn<?, ?> productNameCol;
@FXML
private TableColumn<?, ?> productInvCol;
@FXML
private TableColumn<?, ?> productPriceCol;
@FXML
private Button imsAddBtn;
@FXML
private Button imsModifyBtn;
@FXML
private Button imsDeleteBtn;
@FXML
private Button imsExitBtn;
@FXML
void addHandler(MouseEvent event) {
}
@FXML
void addHandler1(MouseEvent event) {
}
@FXML
void deleteHandler(MouseEvent event) {
}
@FXML
void deleteHandler1(MouseEvent event) {
}
@FXML
void exitHandler(ActionEvent event) {
System.exit(0);
}
@FXML
void modifyHandler(MouseEvent event) {
}
@FXML
void modifyHandler1(MouseEvent event) {
}
@FXML
void partOnActionAdd(ActionEvent event) throws IOException {
stage = (Stage)(( Button)event.getSource()).getScene().getWindow();
scene = FXMLLoader.load(getClass().getResource("/ViewController/Addpart.fxml"));
stage.setScene(new Scene(scene));
stage.show();
}
//Part table delete button
@FXML
void partOnActionDelete(ActionEvent event) {
// Delete Part Items
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Delete Part");
alert.setContentText("Are you sure you want to delete this part?");
Optional<ButtonType>result = alert.showAndWait();
if(result.get() == ButtonType.OK){
Part partToDelete= partTableView.getSelectionModel().getSelectedItem();
Inventory.deletePart(partToDelete);
}
}
@FXML
void partOnActionModify(ActionEvent event) throws IOException{
stage = (Stage)(( Button)event.getSource()).getScene().getWindow();
scene = FXMLLoader.load(getClass().getResource("/ViewController/Modifypart.fxml"));
stage.setScene(new Scene(scene));
stage.show();
}
//Part table search button
@FXML
void partOnActionSearch(ActionEvent event) {
String searchedPart = imsPartSearchTxt.getText();
for(Part part : Inventory.getAllParts()){
if(part.getName().equals(searchedPart)||Integer.toString(part.getId()).equals(searchedPart)){
partTableView.getSelectionModel().select(part);
}
}
}
@FXML
void productOnActionAdd(ActionEvent event) throws IOException {
stage = (Stage)(( Button)event.getSource()).getScene().getWindow();
scene = FXMLLoader.load(getClass().getResource("/ViewController/Addproduct.fxml"));
stage.setScene(new Scene(scene));
stage.show();
}
//Product table delete
@FXML
void productOnActionDelete(ActionEvent event) {
// Delete Part Items
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Delete Product");
alert.setContentText("Are you sure you want to delete this product?");
Optional<ButtonType>result = alert.showAndWait();
if(result.get() == ButtonType.OK){
Product productToDelete= table1.getSelectionModel().getSelectedItem();
Inventory.deleteProduct(productToDelete);
}
}
@FXML
void productOnActionModify(ActionEvent event) throws IOException {
stage = (Stage)(( Button)event.getSource()).getScene().getWindow();
scene = FXMLLoader.load(getClass().getResource("/ViewController/Modifyproduct.fxml"));
stage.setScene(new Scene(scene));
stage.show();
}
//Product table search
@FXML
void productOnActionSearch(ActionEvent event) {
String searchedProduct = imsProductTxt.getText();
for(Product product : Inventory.getAllProducts()){
if(product.getName().equals(searchedProduct)||Integer.toString(product.getId()).equals(searchedProduct)){
table1.getSelectionModel().select(product);
}
}
}
@FXML
void searchHandler(MouseEvent event) {
}
//Initializes the controller class
@Override
public void initialize(URL location, ResourceBundle resources) {
//set the part table data
partTableView.setItems(Inventory.getAllParts());
partIdCol.setCellValueFactory(new PropertyValueFactory<>("id"));
partNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
partInvCol.setCellValueFactory(new PropertyValueFactory<>("inv"));
partPriceCol.setCellValueFactory(new PropertyValueFactory<>("price"));
//set the product table data
table1.setItems(Inventory.getAllProducts());
productIdCol.setCellValueFactory(new PropertyValueFactory<>("id"));
productNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
productInvCol.setCellValueFactory(new PropertyValueFactory<>("inv"));
productPriceCol.setCellValueFactory(new PropertyValueFactory<>("price"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment