After successful implementation of your last BestBike component program, new requirements came
up. Users not only wanted to access the parameters for each product component, but also other
data. They provided the following list of attributes:
Component:
Component ID
Name
Delivery Date
Stock Amount
Vendor
Comment
Write a new program BestBikeComponents.java (you can re-use parts of your last version) that has
the following menu.
Manage Components
1 = Enter Component Information
2 = Print Component Information
3 = Change Component
4 = End Program
The program should provide the following functionality:
• You can create up to 20 components (in an array!) that will have the attributes defined
above (use a class Component and its objects).
• When you run the program, first initialize the component array with component objects.
• The user can enter new data for one or several components (Menu item 1 = Enter
Component Information).
• You can print out the information of the selected component (Menu item 2 = Print
Component Information).
• You can choose another component with the Component ID and enter/change its data or
print it out. with iotools please and shortly
Assistant
User
without scanner please
Assistant
User
with IOTools please
Assistant
import java.util.Scanner;
class Component {
private int componentID;
private String name;
private String deliveryDate;
private int stockAmount;
private String vendor;
private String comment;
code
public Component(int componentID, String name, String deliveryDate, int stockAmount, String vendor, String comment) {
this.componentID = componentID;
this.name = name;
this.deliveryDate = deliveryDate;
this.stockAmount = stockAmount;
this.vendor = vendor;
this.comment = comment;
}
public int getComponentID() {
return componentID;
}
public void setComponentID(int componentID) {
this.componentID = componentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDeliveryDate() {
return deliveryDate;
}
public void setDeliveryDate(String deliveryDate) {
this.deliveryDate = deliveryDate;
}
public int getStockAmount() {
return stockAmount;
}
public void setStockAmount(int stockAmount) {
this.stockAmount = stockAmount;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
public class BestBikeComponents {
private static Component[] components = new Component[20];
private static Scanner scanner = new Scanner(System.in);
code
public static void main(String[] args) {
initializeComponents();
int choice;
do {
displayMenu();
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
enterComponentInformation();
break;
case 2:
printComponentInformation();
break;
case 3:
changeComponent();
break;
case 4:
System.out.println("Program ended.");
break;
default:
System.out.println("Invalid choice. Please enter a valid menu option.");
break;
}
} while (choice != 4);
}
private static void initializeComponents() {
components[0] = new Component(1, "Handlebars", "2022-05-10", 25, "Vendor 1", "Good quality");
components[1] = new Component(2, "Brakes", "2022-05-12", 15, "Vendor 2", "Affordable price");
components[2] = new Component(3, "Gears", "2022-05-14", 20, "Vendor 3", "Durable");
// Initialize other components
}
private static void displayMenu() {
System.out.println("================");
System.out.println("Manage Components");
System.out.println("================");
System.out.println("1 = Enter Component Information");
System.out.println("2 = Print Component Information");
System.out.println("3 = Change Component");
System.out.println("4 = End Program");
System.out.print("Enter your choice: ");
}
private static void enterComponentInformation() {
System.out.print("Enter Component ID: ");
int componentID = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter Component Name: ");
String name = scanner.nextLine();
System.out.print("Enter Delivery Date: ");
String deliveryDate = scanner.nextLine();
System.out.print("Enter Stock Amount: ");
int stockAmount = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter Vendor: ");
String vendor = scanner.nextLine();
System.out.print("Enter Comment: ");
String comment = scanner.nextLine();
Component component = new Component(componentID, name, deliveryDate, stockAmount, vendor, comment);
for (int i = 0; i < components.length; i++) {
if (components[i] == null) {
components[i] = component;
System.out.println("Component information entered successfully.");
return;
}
}
System.out.println("Cannot enter more than 20 components.");
}
private static void printComponentInformation() {
System.out.print("Enter Component ID: ");
int componentID = scanner.nextInt();
scanner.nextLine(); // Consume newline character
for (Component component : components) {
if (component != null && component.getComponentID() == componentID) {
System.out.println("Component ID: " + component.getComponentID());
System.out.println("Name: " + component.getName());
System.out.println("Delivery Date: " + component.getDeliveryDate());
System.out.println("Stock Amount: " + component.getStockAmount());
System.out.println("Vendor: " + component.getVendor());
System.out.println("Comment: " + component.getComment());
return;
}
}
System.out.println("Component with ID " + componentID + " not found.");
}
private static void changeComponent() {
System.out.print("Enter Component ID: ");
int componentID = scanner.nextInt();
scanner.nextLine(); // Consume newline character
for (int i = 0; i < components.length; i++) {
if (components[i] != null && components[i].getComponentID() == componentID) {
System.out.print("Enter new Component Name: ");
String name = scanner.nextLine();
components[i].setName(name);
System.out.print("Enter new Delivery Date: ");
String deliveryDate = scanner.nextLine();
components[i].setDeliveryDate(deliveryDate);
System.out.print("Enter new Stock Amount: ");
int stockAmount = scanner.nextInt();
scanner.nextLine(); // Consume newline character
components[i].setStockAmount(stockAmount);
System.out.print("Enter new Vendor: ");
String vendor = scanner.nextLine();
components[i].setVendor(vendor);
System.out.print("Enter new Comment: ");
String comment = scanner.nextLine();
components[i].setComment(comment);
System.out.println("Component information updated successfully.");
return;
}
}
System.out.println("Component with ID " + componentID + " not found.");
}