To implement the desired functionality, you can follow the steps below:
- Create a class named Component with the desired attributes. The class should have setter and getter methods for each attribute.
public class Component {
private int componentID;
private String componentName;
// other attributes
// constructor
public Component(int componentID, String componentName) {
this.componentID = componentID;
this.componentName = componentName;
// initialize other attributes
}
// getter and setter methods for each attribute
// ...
}
- In the main class, create an array of Component objects to store the components.
public class Main {
public static void main(String[] args) {
Component[] components = new Component[20];
// initialize the component array with component objects
// ...
}
}
- Implement a method to enter component information from the user (Menu item 1).
private static void enterComponentInformation(Component[] components) {
// Prompt the user to enter the component ID and name
Scanner scanner = new Scanner(System.in);
System.out.print("Enter component ID: ");
int componentID = scanner.nextInt();
System.out.print("Enter component name: ");
String componentName = scanner.nextLine();
// Find an empty slot in the components array
int index = -1;
for (int i = 0; i < components.length; i++) {
if (components[i] == null) {
index = i;
break;
}
}
// Create a new Component object and assign it to the empty slot
components[index] = new Component(componentID, componentName);
}
- Implement a method to print component information for a selected component (Menu item 2).
private static void printComponentInformation(Component[] components) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter component ID: ");
int componentID = scanner.nextInt();
// Find the component with the given ID
Component selectedComponent = null;
for (Component component : components) {
if (component != null && component.getComponentID() == componentID) {
selectedComponent = component;
break;
}
}
if (selectedComponent != null) {
System.out.println("Component ID: " + selectedComponent.getComponentID());
System.out.println("Component Name: " + selectedComponent.getComponentName());
// print other attributes
} else {
System.out.println("Component not found.");
}
}
- Add a loop to the main method to display a menu and handle user input.
public class Main {
public static void main(String[] args) {
Component[] components = new Component[20];
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (choice != 3) {
System.out.println("Menu:");
System.out.println("1. Enter Component Information");
System.out.println("2. Print Component Information");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
enterComponentInformation(components);
break;
case 2:
printComponentInformation(components);
break;
case 3:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
This implementation should provide the desired functionality of creating and managing components through a menu-driven program.