In: Computer Science
1. The Item class includes two fields: a String for the name and a double for the price of an item that will be added to the inventory.
2. The Item class will require one constructor that takes a name and price to initialize the fields. Since the name and price will be provided through TextFields, the parameters can be two Strings (be sure to convert the price to a double before storing this into the field).
3. Write getters and setters for the name and price. Try to be consistent with the behaviors of the setters compared to the constructor.
4. Write an equals method that returns true if two Item objects have the same name.
5. Write a toString method that returns the following String (replacing and with the fields of the object): “The item has a price of $.” Make sure the price is formatted to always round to two decimal places.
6. Study the code provided in the Inventory class. The addButton will add an Item to the ArrayList if that Item is new. This means the name of the Item must be different than any name already in the ArrayList.
7. In the Inventory class, you must implement the lambda expression for the addButton. Start by declaring a boolean to keep track of whether or not an item being added to the inventory (the ArrayList of Items) is actually new. This boolean is initialized to true.
8. Construct a new Item using the text from nameTextField for the name and the text from priceTextField for the price. If the constructor does not already handle the conversion, make sure the text for the price is converted to a double.
9. Write a for-each loop to iterate over the items in the ArrayList. Compare each item already in the ArrayList to the new item. If the name of any item in the ArrayList is the same as the name of the new item, set the boolean to false.
10. After the for-each loop completes, if the item is new, add the item to the ArrayList and display the following in the outputLabel: “Successfully added new item. The item has a price of $.” Keep in mind that the second sentence is the result of calling the object’s toString method. If the item is not new, display the following in the outputLabel: “Failed to add existing item.”
package inventory; import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.layout.VBox; import javafx.scene.layout.HBox; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.geometry.Insets; import javafx.geometry.Pos; import java.util.ArrayList; public class Inventory extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { ArrayList itemsList = new ArrayList<>(); Label nameLabel = new Label("Name: "); TextField nameTextField = new TextField(); HBox nameHBox = new HBox(nameLabel, nameTextField); nameHBox.setAlignment(Pos.CENTER); nameHBox.setPadding(new Insets(10, 0, 0, 0)); Label priceLabel = new Label("Price: "); TextField priceTextField = new TextField(); HBox priceHBox = new HBox(priceLabel, priceTextField); priceHBox.setAlignment(Pos.CENTER); Button addButton = new Button("Add Item"); Label outputLabel = new Label(); addButton.setOnAction(event -> { // IMPLEMENT LAMBDA EXPRESSION FOR ADDBUTTON }); VBox root = new VBox(10, nameHBox, priceHBox, addButton, outputLabel); root.setAlignment(Pos.TOP_CENTER); Scene scene = new Scene(root, 500, 500); primaryStage.setTitle("Inventory"); primaryStage.setScene(scene); primaryStage.show(); } }
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Make sure you copy below two classes into separate files as mentioned. Do not copy everything to a single file.
//Inventory.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import java.util.ArrayList;
public class Inventory extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
ArrayList- itemsList = new ArrayList<>();
Label nameLabel = new Label("Name: ");
TextField nameTextField = new TextField();
HBox nameHBox = new HBox(nameLabel, nameTextField);
nameHBox.setAlignment(Pos.CENTER);
nameHBox.setPadding(new Insets(10, 0, 0, 0));
Label priceLabel = new Label("Price: ");
TextField priceTextField = new TextField();
HBox priceHBox = new HBox(priceLabel, priceTextField);
priceHBox.setAlignment(Pos.CENTER);
Button addButton = new Button("Add Item");
Label outputLabel = new Label();
addButton.setOnAction(event -> {
boolean newItem = true; //initially assuming the item is new
//creating an Item object using texts from name and price text fields
Item item = new Item(nameTextField.getText(), priceTextField.getText());
//looping through each item in itemsList
for (Item it : itemsList) {
//if current item equals new one, setting newItem to false and exiting loop
if (it.equals(item)) {
newItem = false;
break;
}
}
//if newItem is still true, adding to list and displaying success message on output label
//else displaying an error message on output label.
if(newItem){
itemsList.add(item);
outputLabel.setText("Successfully added new item. "+item);
}else{
outputLabel.setText("Failed to add existing item.");
}
});
VBox root = new VBox(10, nameHBox, priceHBox, addButton, outputLabel);
root.setAlignment(Pos.TOP_CENTER);
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("Inventory");
primaryStage.setScene(scene);
primaryStage.show();
}
}
//Item.java
public class Item {
//attributes
private String name;
private double price;
//constructor taking text values for name and price
public Item(String name, String priceTxt) {
this.name = name;
//parsing price from priceTxt
this.price = Double.parseDouble(priceTxt);
}
//getters and setters
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public boolean equals(Object obj) {
//returning true if obj is an item and it has the same name as this item
return (obj instanceof Item) && ((Item) obj).getName().equalsIgnoreCase(this.getName());
}
public String toString() {
//returns a string containing item name and price, formatted.
return String.format("The item %s has a price of $%.2f", name, price);
}
}
/*OUTPUT*/