I have two class Task
and Award
which extends Task
. I create this class objects according JSON data. At the moment I have one method that read data passed data and create Task
. If I need Award
I use method which convert Task
to Award
. I would like to know if I am following best practice when I want have one method to read data and create object from this data.
I know that convertTaskToAward
could be moved to class Award
.
I see three possible path here:
Create another method
public Award createAward(JsonData json)
and use it to create Award instead of converting Task to Award.Replace
public Task createTask(JsonData json)
withpublic Award createAward(JsonData json)
and then downcastAward
toTask
if needed.Leave as it is right now but move
public Award convertTaskToAward(Task taskForConvert)
to classAward
.
class Task
public class Task {
protected int id;
protected String description;
private Type type;
private String difficulty;
protected int repeatCount;
private int points;
protected int tempPoints;
private int money;
private int tempMoney;
protected int time;
protected int tempTime;
private ArrayList<String> accessWords;
private ArrayList<Integer> lettersCount;
protected TaskCallback delegate;
protected boolean isDone;
private BaseGameActivity activity;
private boolean obstacleRemoved;
protected boolean powerupUsed;
private int award;
private SharedPreferences pref;
protected String startTime;
private Date endTimeData;
protected Date startTimeData;
private PauseableTimerHandler timer;
private boolean isBlock;
private boolean isCurrent;
public Task(Type type, int id, String difficulty, int repeatCount, int points, int money, int time, ArrayList<String> accessWords, String description, int award, String startTime, boolean isDone, ArrayList<Integer> lettersCount, boolean isCurrent, BaseGameActivity activity, SharedPreferences pref, TaskCallback delegate) {
this.type = type;
this.id = id;
this.difficulty = difficulty;
this.repeatCount = repeatCount;
this.points = points;
this.money = money;
this.time = time;
this.accessWords = accessWords;
this.startTime = startTime;
this.lettersCount = lettersCount;
this.isDone = isDone;
this.pref = pref;
this.delegate = delegate;
this.description = description;
this.activity = activity;
tempPoints = points;
tempMoney = money;
tempTime = time;
this.award = award;
this.isCurrent = isCurrent;
}
public String getStartTime() {
return startTime;
}
public String getDifficulty() {
return difficulty;
}
public int getId() {
return id;
}
public int getAward() {
return award;
}
public int getTempPoints() {
return tempPoints;
}
public String getDescription() {
return description;
}
public boolean isDone() {
return isDone;
}
public Type getType() {
return type;
}
public int getRepeatCount() {
return repeatCount;
}
public int getPoints() {
return points;
}
public int getMoney() {
return money;
}
public int getTime() {
return time;
}
public ArrayList<String> getAccessWords(Boolean forSave) {
if (!forSave){
return accessWords;
} else {
StringBuilder stringAW = new StringBuilder();
for (String s:accessWords){
stringAW.append(s).append("|");
}
return new ArrayList<String>(Arrays.asList(stringAW.toString()));
}
}
public ArrayList<Integer> getLettersCount(boolean forSave) {
if (!forSave){
return lettersCount;
} else {
StringBuilder stringLetterCount = new StringBuilder();
for (Integer i:lettersCount){
stringLetterCount.append(i);
}
int tempInt = 0;
if (stringLetterCount.toString() != ""){
tempInt = Integer.parseInt(stringLetterCount.toString());
}
return new ArrayList<Integer>(Arrays.asList(tempInt));
}
}
public void reset() {
tempPoints = points;
tempMoney = money;
tempTime = time;
isDone = false;
isBlock = false;
}
public void decreasePoints(int pPoints) {
if (accessWords.contains("CONTINUOUS")) {
points -= pPoints;
if (points <= 0) {
decreaseRepeat();
}
} else if (difficulty.equals("SPECIAL")) {
tempPoints -= pPoints;
if (tempPoints <= 0 && type != Type.Mazes && !isBlock) {
decreaseRepeat();
isBlock = true;
}
} else if (type == Type.Mazes) {
tempPoints -= pPoints;
if (tempPoints > 0) {
decreaseRepeat();
isBlock = true;
}
} else {
tempPoints -= pPoints;
if (tempPoints <= 0) {
decreaseRepeat();
}
}
}
public void decreaseMoney(int pMoney) {
if (!accessWords.contains("SPEND")) {
tempMoney -= pMoney;
if (tempMoney <= 0) {
decreaseRepeat();
}
} else {
money -= pMoney;
if (money <= 0) {
decreaseRepeat();
}
}
}
public void mazeTaskDoneWith(int mistakes) {
if (mistakes <= points) {
decreaseRepeat();
}
}
public void decreaseRepeat() {
if (isDone) return;
if (type != Type.Mazes) {
repeatCount--;
if (repeatCount <= 0) {
delegate.onDone(this);
}
} else {
repeatCount--;
if ((repeatCount <= 0 && !accessWords.contains("SOLVE") || (repeatCount <= 0 && accessWords.contains("SOLVE")))) {
delegate.onDone(this);
}
}
}
public void decreaseeTime() {
if (accessWords.contains("CONTINUOUS")) {
time--;
if (time <= 0 && !isDone) {
decreaseRepeat();
}
} else {
tempTime--;
Log.i("Award id:"+id,"[Temp]Second pass. "+tempTime+" to go.");
if (tempTime <= 0 && !isDone) {
decreaseRepeat();
}
}
}
public Task clone() throws CloneNotSupportedException {
return new Task(this.type, id, difficulty, this.repeatCount, this.points, this.Money, this.time, this.accessWords, this.description, this.award, this.startTime, this.isDone, this.lettersCount, this.isCurrent, this.activity, this.pref, this.delegate);
}
public void done() {
if(!isDone) {
Log.i("Task", id + "| " + this.getClass() + " is done: " + description);
isDone = true;
}
}
public TaskCallback getDelegate() {
return delegate;
}
}
class Award
public class Award extends Task {
private Integer oneShot;
private String title;
private Integer tempRepeatCount;
private HashMap<String, Integer> wordsUsedInGame;
private int tempOneShot;
public Award(Type type, int id, int repeatCount, int points, int szyflings, int time, ArrayList<String> accessWords, String description, int award, String startTime, boolean isDone, ArrayList<Integer> lettersCount, BaseGameActivity activity, SharedPreferences pref, TaskCallback pDelegate) {
super(type, id, "", repeatCount, points, szyflings, time, accessWords, description, award, startTime, isDone, lettersCount, false, activity, pref, pDelegate);
tempRepeatCount = repeatCount;
if (id == 7) wordsUsedInGame = new HashMap<String, Integer>();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getOneTime() {
return oneShot;
}
public void setOneShot(Integer oneShot) {
this.oneShot = oneShot;
}
@Override
public void decreaseRepeat() {
if (getAccessWords(false).contains("LETTER_GAME") || getAccessWords(false).contains("MAZE_GAME")) {
tempRepeatCount--;
if (tempRepeatCount <= 0) {
isDone = true;
delegate.onDone(this);
}
} else {
repeatCount--;
if (repeatCount <= 0) {
isDone = true;
delegate.onDone(this);
}
}
}
@Override
public void reset() {
tempRepeatCount = repeatCount;
switch (id) {
case 7:
wordsUsedInGame.clear();
break;
case 36:
tempTime = time;
break;
default:
super.reset();
break;
}
}
public void snapDate() {
DateFormat dateFormat = new SimpleDateFormat("HH:mm dd/MM/yyyy");
startTimeData = new Date();
startTime = dateFormat.format(startTimeData);
}
public void checkDate() {
if (!startTime.equals("")) {
try {
DateFormat dateFormat = new SimpleDateFormat("HH:mm dd/MM/yyyy");
Date currentDate = new Date();
startTimeData = dateFormat.parse(startTime);
float timePassed = currentDate.getTime() - startTimeData.getTime();
timePassed = ((timePassed / 1000) / 3600);
Log.i("Award id:" + id, "timePassed");
if (timePassed <= 1) decreaseRepeat();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
@Override
public void decreasePoints(int pPoints) {
if (isDone) return;
switch (id) {
case 10:
tempPoints -= pPoints;
if (tempTime > 0 && tempPoints <= 0) {
decreaseRepeat();
}
break;
default:
super.decreasePoints(pPoints);
break;
}
}
@Override
public void decreaseeTime() {
if (isDone) return;
switch (id) {
case 10:
if (tempTime > 0) tempTime--;
break;
default:
super.decreaseeTime();
break;
}
}
}
Main class
class MainActivity {
public void loadTaskAndAward(){
Task task = createTask(jsonDataForTask);
Award award = convertTaskToAward(createTask(jsonDataForAward));
}
public Task createTask(JsonData json){
//..read description,time,money from json
return new Task(dscription,time,money);
}
public Award convertTaskToAward(Task taskForConvert){
Award awardForReturn = new Award(title,taskForConvert.getDescription(),taskForConvert.getTime(),taskForConvert.getMoney());
}
}