Instructions
Objective
Write a java assignment program to design and develop a system.
Requirements and Specifications
Source Code
INVENTORY
package com.example.inventory;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class Inventory {
/**
* Collection of all parts, registered in inventory.
*/
private final ObservableList allParts = FXCollections.observableArrayList();
/**
* Collection of all products, registered in inventory.
*/
private final ObservableList allProducts = FXCollections.observableArrayList();
/**
* Method for adding new part to inventory
* @param newPart new part to add to inventory.
*/
public void addPart(Part newPart) {
if(!allParts.contains(newPart)) {
allParts.add(newPart);
}
}
/**
* Method for adding new product to inventory
* @param newProduct new product to add to inventory.
*/
public void addProduct(Product newProduct) {
if(!allProducts.contains(newProduct)) {
allProducts.add(newProduct);
}
}
/**
* Method for extracting part from inventory by id.
* @param partId part id to extract from inventory with.
* @return extracted part, or null if part with such id is not found.
*/
public Part lookupPart(int partId) {
for(Part part : allParts) {
if(part.getId() == partId) {
return part;
}
}
return null;
}
/**
* Method for extracting product from inventory by id.
* @param productId product id to extract from inventory with.
* @return extracted product, or null if product with such id is not found.
*/
public Product lookupProduct(int productId) {
for(Product product : allProducts) {
if(product.getId() == productId) {
return product;
}
}
return null;
}
/**
* Method for all parts, whose names match given partial name.
* @param partName part name to look for in inventory.
* @return collection of parts, whose name matches given name.
*/
public ObservableList lookupPart(String partName) {
ObservableList result = FXCollections.observableArrayList();
for(Part part : allParts) {
if(part.getName().toLowerCase().contains(partName.toLowerCase())) {
result.add(part);
}
}
return result;
}
/**
* Method for all products, whose names match given partial name.
* @param productName product name to look for in inventory.
* @return collection of products, whose name matches given name.
*/
public ObservableList lookupProduct(String productName) {
ObservableList result = FXCollections.observableArrayList();
for(Product product : allProducts) {
if(product.getName().toLowerCase().contains(productName.toLowerCase())) {
result.add(product);
}
}
return result;
}
/**
* Method for updating a part in inventory at a given index.
* @param index to update part on.
* @param selectedPart new part instance.
*/
public void updatePart(int index, Part selectedPart) {
Part oldPart = allParts.get(index);
for(Product product : allProducts) {
if(product.getAllAssociatedParts().contains(oldPart)) {
product.deleteAssociatedPart(oldPart);
product.addAssociatedPart(selectedPart);
}
}
allParts.set(index, selectedPart);
}
/**
* Method for updating a product in inventory at a given index.
* @param index to update product on.
* @param newProduct new product instance.
*/
public void updateProduct(int index, Product newProduct) {
allProducts.set(index, newProduct);
}
/**
* Method for removing given part from the inventory.
*
* @param selectedPart which is going to be removed from inventory parts.
* @return true, iff selected part was successfully deleted.
*/
public boolean deletePart(Part selectedPart) {
if(allParts.contains(selectedPart)) {
for(Product product : allProducts) {
product.deleteAssociatedPart(selectedPart);
}
allParts.remove(selectedPart);
return true;
}
return false;
}
/**
* Method for removing given product from the inventory.
*
* @param selectedProduct which is going to be removed from inventory products.
* @return true, iff selected part was successfully deleted.
*/
public boolean deleteProduct(Product selectedProduct) {
if(allProducts.contains(selectedProduct)) {
allProducts.remove(selectedProduct);
return true;
}
return false;
}
/**
* Method for getting all parts registered in inventory.
*
* @return observable collection of all parts registered in inventory.
*/
public ObservableList getAllParts() {
return allParts;
}
/**
* Method for getting all products registered in inventory.
*
* @return observable collection of all products registered in inventory.
*/
public ObservableList getAllProducts() {
return allProducts;
}
}
MAIN CONTROLLER
package com.example.inventory;
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.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class MainController implements Initializable {
@FXML
private TextField searchPartsField;
@FXML
private TableView partsTable;
@FXML
private TableColumn partsID;
@FXML
private TableColumn partsName;
@FXML
private TableColumn partsLevel;
@FXML
private TableColumn partsCost;
@FXML
private TextField searchProductsField;
@FXML
private TableView productsTable;
@FXML
private TableColumn productName;
@FXML
private TableColumn productID;
@FXML
private TableColumn productLevel;
@FXML
private TableColumn productCost;
private final Inventory inventory = new Inventory();
private Part selectedPart = null;
private Product selectedProduct = null;
@Override
public void initialize(URL url, ResourceBundle rb) {
partsTable.getItems().setAll(inventory.getAllParts());
partsID.setCellValueFactory(new PropertyValueFactory<>("partsID"));
partsLevel.setCellValueFactory(new PropertyValueFactory<>("partsLevel"));
partsCost.setCellValueFactory(new PropertyValueFactory<>("partsCost"));
partsName.setCellValueFactory(new PropertyValueFactory<>("partsName"));
partsTable.setOnMousePressed(event -> selectedPart = partsTable.getSelectionModel().getSelectedItem());
productsTable.getItems().setAll(inventory.getAllProducts());
productID.setCellValueFactory(new PropertyValueFactory<>("productID"));
productLevel.setCellValueFactory(new PropertyValueFactory<>("productLevel"));
productCost.setCellValueFactory(new PropertyValueFactory<>("productCost"));
productName.setCellValueFactory(new PropertyValueFactory<>("productName"));
productsTable.setOnMousePressed(event -> selectedProduct = productsTable.getSelectionModel().getSelectedItem());
}
@FXML
void addPartAction(ActionEvent event) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("AddPartForm.fxml"));
Parent root = (Parent) loader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
loader.getController().setParentController(this);
AddPartController api = loader.getController();
api.setID(generatePartsID());
stage.show();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
// Method is called when "Modify" button on parts Table is pressed
@FXML
void partsModifyButtonAction(ActionEvent event) {
//
// // Checking is any parts on table is selected
// if (partSelected != null) {
// try {
// // Loading the modify part window
// FXMLLoader loader = new FXMLLoader(getClass().getResource("AddPartForm.fxml"));
// Parent root = (Parent) loader.load();
// Stage stage = new Stage();
// stage.setTitle("Modify Part Window");
//
// stage.setScene(new Scene(root));
// loader.getController()
// .setParentController(this);
//
// AddPartInterfaceController api = loader.getController();
// // Sending the selected part object to the modify part class for operation
// api.setData(partSelected);
// stage.show();
//
// } catch (IOException ex) {
// Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
// }
// }else{
// // When no part was selected show alert to avoid exception
// Alert alert = new Alert(AlertType.INFORMATION);
// alert.setTitle("Error!");
// alert.setHeaderText("Please, Select a part to modify");
// alert.setContentText(null);
//
// alert.showAndWait();
// }
}
// Method is called when "Delete" button on Parts table is pressed
@FXML
void deletePartsAction(ActionEvent event) {
//
// // Checking If the one part is selected and it is not associated with any products
// if (partSelected != null && partSelected.getAssociatedPartID() == -1) {
// Alert alert = new Alert(AlertType.CONFIRMATION);
// alert.setTitle("Causion!");
// alert.setHeaderText("Are you sure you want to delete Part ID : " + partSelected.getPartsID() + "?");
// alert.setContentText(null);
//
// Optional result = alert.showAndWait();
// if (result.get() == ButtonType.OK) {
// // Upon conformation remove the selected part from the table
// partsTable.getItems().remove(partSelected);
// parts.remove(partSelected);
//
// }
//
// }
//
// // When a part is selected but it is associated with a product
// else if (partSelected != null && partSelected.getAssociatedPartID() != -1) {
//
// Alert alert = new Alert(AlertType.CONFIRMATION);
// alert.setTitle("Error!");
// alert.setHeaderText("Selected part is associated with Product ID : " + partSelected.associatedPartID + "\nCan't delete the item directly, Please, Go to the modify part section.");
// alert.setContentText(null);
//
// Optional result = alert.showAndWait();
// if (result.get() == ButtonType.OK) {
// try {
// FXMLLoader loader = new FXMLLoader(getClass().getResource("AddProductInterface.fxml")); // Loading the modify product window for modification
// Parent root = (Parent) loader.load();
// Stage stage = new Stage();
// stage.setTitle("Update Product Window");
//
// stage.setScene(new Scene(root));
// loader.getController()
// .setParentController(this);
//
// // Searching for the product associated with this part
// for (Product p : products) {
// if (partSelected.associatedPartID == p.getProductID()) {
// productSelected = p;
// }
// }
//
// AddProductInterfaceController api = loader.getController();
// api.setData(productSelected);
//
// // Setting the products associated parts to the Products parts table view via existingProductParts arraylist
// for (Part p : parts) {
// if (productSelected.getProductID() == p.getAssociatedPartID()) {
// api.existingProductParts.add(p);
// }
// }
//
// api.partsTable.getItems().setAll(api.existingProductParts);
//
// stage.show();
//
// } catch (IOException ex) {
// Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
// }
// } else {
// // ... user chose CANCEL or closed the dialog
// }
// } else {
// Alert alert = new Alert(AlertType.WARNING);
// alert.setTitle("Error!");
// alert.setHeaderText("Please, Select at-least one part to perform delete operation!");
// alert.setContentText(null);
//
// alert.showAndWait();
// }
}
// Method is called when "Search" button on Parts table is pressed
@FXML
void partsSearchButtonAction(ActionEvent event) {
// FilteredList filteredData = new FilteredList<>(parts,p -> true);
//
// filteredData.setPredicate(Part -> {
// // If filter text is empty, display all persons.
// if (partsFilterString.getText() == null || partsFilterString.getText().isEmpty()) {
// return true;
// }
//
// // Compare first name and last name of every person
// // with filter text.
// String lowerCaseFilter = partsFilterString.getText().toLowerCase();
//
// if(Part.getPartsName().toLowerCase().contains(lowerCaseFilter)) {
// return true; // Filter matches first name.
// }
//
// return false; // Does not match.
// });
//
//
// // Wrap the FilteredList in a SortedList.
// sortedData = new SortedList<>(filteredData);
//
// // Bind the SortedList comparator to the TableView comparator.
// sortedData.comparatorProperty().bind(partsTable.comparatorProperty());
//
// // Add sorted (and filtered) data to the table.
// partsTable.setItems(sortedData);
//
}
// Method is called when "Add" button on Product table is pressed
@FXML
void productAddButtonAction(ActionEvent event){
//
// try {
// FXMLLoader loader1 = new FXMLLoader(getClass().getResource("AddProductInterface.fxml")); // Load the AddProductInterface
// Parent root1 = (Parent) loader1.load();
// Stage stage1 = new Stage();
// stage1.setTitle("Add Product Window");
// stage1.setScene(new Scene(root1));
// loader1.getController()
// .setParentController(this);
// AddProductInterfaceController api = loader1.getController();
// api.setData(generateProductsID());
// stage1.show();
//
// } catch (IOException ex) {
// Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
// }
//
}
// Method is called when "Modify" button on products table is pressed
@FXML
void productModifyButtonAction(ActionEvent event) {
//
// if (productSelected != null) {
// try {
// FXMLLoader loader = new FXMLLoader(getClass().getResource("AddProductInterface.fxml")); // Load the add product interface
// Parent root = (Parent) loader.load();
// Stage stage = new Stage();
// stage.setTitle("Update Product Window");
//
// stage.setScene(new Scene(root));
// loader.getController()
// .setParentController(this);
//
// AddProductInterfaceController api = loader.getController();
// api.setData(productSelected); // Set the selected products data to the add product interface to auto fill all the fields
//
// // Populate the products part list table arreylist with all associated parts
// for (Part p : parts) {
// if (productSelected.getProductID() == p.getAssociatedPartID()) {
// api.existingProductParts.add(p);
// }
// }
//
// // Show the tableView with all associated parts
// api.partsTable.getItems().setAll(api.existingProductParts);
//
// stage.show();
//
// } catch (IOException ex) {
// Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
// }
// }else {
// Alert alert = new Alert(AlertType.WARNING);
// alert.setTitle("Error!");
// alert.setHeaderText("Please, Select at-least one part to modify!");
// alert.setContentText(null);
//
// alert.showAndWait();
// }
// }
//
// // Generates unique id for each new item
// int generatePartsID(){
// int a = 1;
//
// for( Part o: parts){
// if(o.getPartsID() >= a){
// a = o.getPartsID() + 1;
// }
// }
//
// return a;
// }
//
// // Generates unique id for each new item
// int generateProductsID(){
// int a = 1;
//
// for( Product o: products){
// if(o.getProductID() >= a){
// a = o.getProductID() + 1;
// }
// }
//
// return a;
}
// Method is called when "Delete" button on products table is pressed
@FXML
void deleteProductSelected(ActionEvent event) {
//
// // If any product is selected
// if (productSelected != null) {
//
// Alert alert = new Alert(AlertType.CONFIRMATION);
// alert.setTitle("Causion!");
// alert.setHeaderText("Are you sure you want to delete Part ID : " + productSelected.getProductID() + "?");
// String warningText = "Deleting this product will also delete assosciated parts:";
//
// // Showing all the parts associated with the part deleting in the warning
// for( Part p: parts){
// if(p.getAssociatedPartID() == productSelected.getProductID()){
// warningText += "\nProduct ID : "+p.getPartsID();
// }
// }
// alert.setContentText(warningText);
//
// Optional result = alert.showAndWait();
// if (result.get() == ButtonType.OK) {
// // looping for each selected parts
// for (Iterator p1 = parts.iterator(); p1.hasNext();) {
// Part p = p1.next();
// if (p.getAssociatedPartID() == productSelected.getProductID()) {
//
// p1.remove();
// partsTable.getItems().remove(p);
//
// }
// }
//
// // delete the product selected from arraylist
// productTable.getItems().remove(productSelected);
// products.remove(productSelected);
//
// Alert alert1 = new Alert(AlertType.INFORMATION);
// alert1.setTitle("Success!");
// alert1.setHeaderText("Successfully deleted product with all associated parts!");
// alert1.setContentText(null);
//
// alert1.showAndWait();
// }
// }else {
// // Showing no product selected dialog box
// Alert alert = new Alert(AlertType.WARNING);
// alert.setTitle("Error!");
// alert.setHeaderText("Please, Select at-least one part to perform delete operation!");
// alert.setContentText(null);
//
// alert.showAndWait();
// }
//
}
// Method is called when "Search" button on products table is pressed
@FXML
void productSearchButtonAction(ActionEvent event) {
// FilteredList filteredData = new FilteredList<>(products,p -> true);
//
// filteredData.setPredicate(Product -> {
// // If filter text is empty, display all persons.
// if (productFilterString.getText() == null || productFilterString.getText().isEmpty()) {
// return true;
// }
//
// // Compare first name and last name of every person
// // with filter text.
// String lowerCaseFilter = productFilterString.getText().toLowerCase();
//
// if(Product.getProductName().toLowerCase().contains(lowerCaseFilter)) {
// return true; // Filter matches first name.
// }
//
// return false; // Does not match.
// });
//
//
// // Wrap the FilteredList in a SortedList.
// SortedList sortedData1 = new SortedList<>(filteredData);
//
// // Bind the SortedList comparator to the TableView comparator.
// sortedData1.comparatorProperty().bind(productTable.comparatorProperty());
//
// // Add sorted (and filtered) data to the table.
// productTable.setItems(sortedData1);
//
// }
//
// // initial parts list
// public ObservableList parts(){
//
// parts.add(new Part(1, "Part 1", 2, 71.78, 7, 3, "1", true, -1));
// parts.add(new Part(2, "Part 2", 3, 42.89, 10, 1, "Apple", false, 3));
// parts.add(new Part(3, "Part 3", 5, 63.87, 5, 1, "Samsung", false, 4));
// parts.add(new Part(4, "Part 4", 6, 54.78, 8, 2, "EA-Sports", false, 4));
// parts.add(new Part(5, "Part 5", 7, 78.89, 10, 1, "MainGear", false, 2));
// parts.add(new Part(6, "Part 6", 8, 73.87, 5, 1, "Intel", false, 1));
// parts.add(new Part(7, "Part 7", 5, 44.78, 8, 2, "AMD", false, 1));
//
// return parts;
//
}
// // initial products list
// public ObservableList products(){
//
// products.add(new Product(1, "Product 1", 2, 1.78, 12, 2));
// products.add(new Product(2, "Product 2", 3, 2.89, 12, 2));
// products.add(new Product(3, "Product 3", 1, 3.87, 12, 2));
// products.add(new Product(4, "Product 4", 4, 4.78, 12, 1));
//
//
// return products;
//
// }
//
// // method receives parameters from add part window
// public void addNewPart(int pID, String pName, int pLevel, double pCost, int pMax, int pMin, String pCompMac, Boolean inHouse, int asID){
// // adds new parts to arraylist
// parts.add(new Part( pID, pName, pLevel, pCost, pMax, pMin, pCompMac, inHouse, asID));
// partsTable.getItems().clear();
// partsTable.getItems().setAll(parts);
//
// }
//
// // method receives parameters from add products window
// public void addNewProduct(int pID, String pName, int pLevel, double pCost, int pMax, int pMin){
// // adds new products to arraylist
// products.add(new Product( pID, pName, pLevel, pCost, pMax, pMin));
// productTable.getItems().clear();
// productTable.getItems().setAll(products);
//
// }
//
// // Receives parameters from the modify item window
// void updatePart(int pID, String pName, int pLevel, double pCost, Part selectedPart, int pMax, int pMin, String pCompMach, Boolean inHouse, int asID) {
// // setting the modified values
// selectedPart.setPartsCost(pID);
// selectedPart.setPartsName(pName);
// selectedPart.setPartsLevel(pLevel);
// selectedPart.setPartsCost(pCost);
// selectedPart.setPartMax(pMax);
// selectedPart.setPartMin(pMin);
// selectedPart.setCompanyNameOrMachineID(pCompMach);
// selectedPart.setInHouse(inHouse);
//
// partsTable.getItems().clear();
// partsTable.getItems().setAll(parts);
//
// }
//
// // Receives parameters from the modify item window
// void updateProduct(int pID, String pName, int pLevel, double pCost, Product productSelected, int pMax, int pMin) {
// // setting the modified values
// productSelected.setProductID(pID);
// productSelected.setProductName(pName);
// productSelected.setProductCost(pCost);
// productSelected.setProductLevel(pLevel);
// productSelected.setProductMax(pMax);
// productSelected.setProductMin(pMin);
//
// productTable.getItems().clear();
// productTable.getItems().setAll(products);
// }
}
OUT SOURCED
package com.example.inventory;
public class Outsourced extends Part {
/**
* Company name for outsourced part.
*/
private String companyName;
/**
* All-args constructor for outsourced part instance.
* @param id part id to set.
* @param name part name to set.
* @param price part price to set.
* @param stock part stock number to set.
* @param min part min value to set.
* @param max part max value to set.
* @param companyName company name for outsourced part to set.
*/
public Outsourced(int id, String name, double price, int stock, int min, int max, String companyName) {
super(id, name, price, stock, min, max);
this.companyName = companyName;
}
/**
* Getter for companyName field.
* @return outsourced part company name.
*/
public String getCompanyName() {
return companyName;
}
/**
* Setter for companyName field.
* @param companyName value to set.
*/
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
PRODUCT
package com.example.inventory;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class Product {
/**
* Collection of parts, associated with this product.
*/
private ObservableList associatedParts;
/**
* Product id.
*/
private int id;
/**
* Product name.
*/
private String name;
/**
* Product price.
*/
private double price;
/**
* Number of products in stock.
*/
private int stock;
/**
* Minimum number of products which must be available at any time.
*/
private int min;
/**
* Maximum number of products which can be stored.
*/
private int max;
/**
* All-args constructor for product instance.
*
* @param id product id to set.
* @param name product name to set.
* @param price product price to set.
* @param stock product stock number to set.
* @param min product min value to set.
* @param max product max value to set.
*/
public Product(int id, String name, double price, int stock, int min, int max) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
this.min = min;
this.max = max;
this.associatedParts = FXCollections.observableArrayList();
}
/**
* Getter for id field.
*
* @return product id.
*/
public int getId() {
return id;
}
/**
* Setter for id field.
*
* @param id value to set.
*/
public void setId(int id) {
this.id = id;
}
/**
* Getter for name field.
*
* @return product name.
*/
public String getName() {
return name;
}
/**
* Setter for name field.
*
* @param name product to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* Getter for price field.
*
* @return product price.
*/
public double getPrice() {
return price;
}
/**
* Setter for price field.
*
* @param price value to set.
*/
public void setPrice(double price) {
this.price = price;
}
/**
* Getter for stock field.
*
* @return number of product in stock available.
*/
public int getStock() {
return stock;
}
/**
* Setter for stock field.
*
* @param stock value to set.
*/
public void setStock(int stock) {
this.stock = stock;
}
/**
* Getter for min field.
*
* @return minimum number of product, which must be available.
*/
public int getMin() {
return min;
}
/**
* Setter for min field.
*
* @param min value to set.
*/
public void setMin(int min) {
this.min = min;
}
/**
* Getter for max field.
*
* @return maximum number of product, which can be stored.
*/
public int getMax() {
return max;
}
/**
* Setter for max field.
*
* @param max value to set.
*/
public void setMax(int max) {
this.max = max;
}
/**
* Method for adding new part as an associated part for this product
*
* @param part new associated part to add.
*/
public void addAssociatedPart(Part part) {
if (!associatedParts.contains(part)) {
associatedParts.add(part);
}
}
/**
* Method for removing given associated part for this product.
*
* @param selectedAssociatedPart which is going to be removed from associated parts.
* @return true, iff selected part was successfully deleted.
*/
public boolean deleteAssociatedPart(Part selectedAssociatedPart) {
if (associatedParts.contains(selectedAssociatedPart)) {
associatedParts.remove(selectedAssociatedPart);
return true;
}
return false;
}
/**
* Method for getting all associated parts of given product.
*
* @return observable collection of product associated parts.
*/
public ObservableList getAllAssociatedParts() {
return associatedParts;
}
}
Similar Samples
Discover our diverse collection of programming homework samples at ProgrammingHomeworkHelp.com. Whether you're tackling Java, Python, Machine Learning, or Data Structures, our samples exemplify our expertise in delivering tailored solutions. Each example showcases our commitment to clarity and excellence, offering insights into how we can assist you effectively with your programming assignments. Explore our samples to see how we can elevate your academic performance.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java