[Solved] Storing data in proper OOP way [closed]


You should create a model class for your records:

Model Class

class Record
{
   private String item;
   private String category;
   private int quantity; 
   private String timestamp

   // constructor
   public Record (String item, String category, int quantity, String timestamp)
   {
       this.item = item;
       // assign rest of members...
   }

   public String getCategory ()
   {
       return category;
   }

   // rest of getters and setters for all members...
}

Next, have a parser class that reads the txt file and creates a List of Record objects. Your parser could return such a list:

List<Record> records = new ArrayList<Records>();

Searching

When you want to search your data just iterate through the list and gather the matches. For example, if you want to find all items in the “Food” category, you could do something like this:

String searchCategory = "food";
List<Record> matches = new ArrayList<Records>();

for (Record r : records)
{
    String currentCategory = r.getCategory();

    // this will do case-insensitive matches
    if (currentCategory.equalsIgnoreCase(searchCategory))
    {
        matches.add(r);
    }
}

solved Storing data in proper OOP way [closed]