Take your Array of InventoryItems from assignment 11 and add a ItemID as an Integer and change the test data in the program to have them. (This would be like an ID in a database and would be a number like 452 or 535. It wouldn’t be anything about the data in that object. It gives us another integer value to work with.) Make sure the test data doesn’t have the 5 InventoryItem ItemIDs in order yet at the start.
Write a linear search to find an InventoryItem by Description and tell whether that InventoryItem is in the set or not.
Sort the Array by using a Bubble Sort, Select Sort, or another sort you know of (and your instructor may have shown the Insert Sort in lecture) to sort the array by ItemID. Then loop through the array printing the values of each InventoryItem to show they are now in order.
Then ask the user for a ItemID and write a Binary Search to tell whether that InventoryItem is in the set or not.
public class Main {
public static void main(String[] args) {
final ArrayList<InventoryItem> items = new ArrayList<InventoryItem>
}
public InventoryItem readItem() {
// TODO: implement
return new InventoryItem();
}
}
class InventoryItem {
private int itemID;
private String decsription;
private double price;
public InventoryItem(int ID) {
this.itemID = ID;
this.description = "";
this.price = 0;
}
public InventoryItem(int ID, String desc, double price) {
this.itemID = ID;
this.description = desc;
this.price = price;
}
public int getItemID() { return itemID; }
public String getDescription() { return description; }
public double getPrice() { return price; }
public void setItemID(int ID) { this.itemID = ID; }
public void setDescription(String desc) { this.description = desc; }
public void setPrice(double price) { this.price = price; }
}
Comments
Leave a comment