Skip to content
Advertisement

where to store data read from file [closed]

I am making a semester project for my University OOP course. I am making a Restaurant Management System as my project.

I have an Order class, Main class, and my constants stored in a class called GlobalConstants.

I read in a post on this site that said constants should be in related classes. In GlobalConstants I have a method running in a static block that reads order data (order no., time, items etc.) from a file and stores it in an ArrayList (static and final) which is accessed by multiple methods in the Main class.

So, can anyone suggest a better way to do this that follows OOP practices?

Advertisement

Answer

First, don’t create any logic in a class, called GlobalConstants. This is the Separation of Concerns principle. This made clear, you could:

First: create a util class e.g. FileUtil where you will move your static method. It wont store the result in a field, but will return the result instead (or alternatively take the list as an argument and populate it). Storing (if needed) or whatever other work with the result is needed is the job of the class that is calling the method, not of the util.

Second: I suppose that you have to store the content of your file in memory in order to do something with it for your order. You must decide what is the scope of this data.

  • If these are some global settings to your applicaton comming from this file – you could easily create a Settings class that will be Singleton (find info and read about Singleton – this will be a good start to get known with Design Patterns).
  • If it is some session data – maybe the order itself is coming from a file – then store this data in your Order class – this is its purpose after all.
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement