My program read's a file with 5 parameters about a unit. I have established a unit class with these parameters however it was asked now to be able to read another file, this one had 6 parameters but it got me thinking if i could get a file with 10+ parameters and my unit class would not be ready to store all that data, so i was wondering if i could add more variables to a class in runtime.
Sample
Unit Class
public class Unit implements Serializable {
private String name;
private String unitId;
private byte year;
private String semester;
private String type;
private int credits;
public Unit(String name, String unitId, byte year, String semester, int credits) {
setName(name);
setUnitId(unitId);
setYear(year);
setSemester(semester);
setType(null);
setCredits(credits);
}
public Unit(String name, String unitId, byte year, String semester, String type, int credits) {
setName(name);
setUnitId(unitId);
setYear(year);
setSemester(semester);
setType(type);
setCredits(credits);
}
// Set's get's and all that stuff.
}
Sample code to read the files
Scanner input = new Scanner(f);
ArrayList<Unit> units = new ArrayList();
while (input.hasNext()) {
String str = input.nextLine();
if (ignoreFirstLine) {
ignoreFirstLine = false;
} else {
String[] ArrayStr = str.split(";");
if(ArrayStr.length == 5){
Unit unit = new Unit(ArrayStr[0], ArrayStr[1], Byte.parseByte(ArrayStr[2]), ArrayStr[3], Integer.parseInt(ArrayStr[4]));
units.add(unit);
} else if (ArrayStr.length == 6){
Unit unit = new Unit(ArrayStr[0], ArrayStr[1], Byte.parseByte(ArrayStr[2]), ArrayStr[3], ArrayStr[4], Integer.parseInt(ArrayStr[5]));
units.add(unit);
} else {
//Modify classes in Runtime?
}
edit: My english is amazing :D