I have items in a list which can have a severity level ( those levels are: info,warning,error,delay,…)
I want to program this in a OOP way so that I can add more severity levels easily if needed.
It’s been a while since I have used Java so I’m not sure how to do it the right way.
What I have is:
Public class ListItem
{
int sev;
String name;
public ListItem(String name, int s)
{
this.name = name; this.sev = s;
}
...
}
A severity should have an integer (like ‘1’) and just a name (like ‘information’)
I’d like to ‘predefine’ severities like:
public static final int WARNING = 1;
public static final int ERROR = 2;
But then, how can I display the name ‘WARNING’ when ‘1’ is given as severity to list item?
Am I having the best approach?
Thanks,
You should use an enum instead of constants:
You can print the
LevelusingtoString()prints
Changing to
ListItem(String name, Level level)has the advantage of preventing you from creating aListItemwith anintvalue that isn’t defined as a valid level.