Skip to content

Instantly share code, notes, and snippets.

@umuturan
Created April 17, 2016 21:08
Show Gist options
  • Select an option

  • Save umuturan/6e80b2836f730edc97a9c880ab86b046 to your computer and use it in GitHub Desktop.

Select an option

Save umuturan/6e80b2836f730edc97a9c880ab86b046 to your computer and use it in GitHub Desktop.
package project01;
import java.util.ArrayList;
import java.util.Iterator;
public class Calendar {
// properties
private ArrayList<Task> tasks;
// constructor
public Calendar(ArrayList<Task> tasks) {
this.tasks = tasks;
}
// methods
public ArrayList<Task> getTasks() {
return tasks;
}
public ArrayList<Task> getActiveTasks() {
ArrayList<Task> actives;
Iterator it;
it = tasks.iterator();
actives = new ArrayList<Task>();
while (it.hasNext()) {
Task check = (Task) it.next();
if (check.isActive())
actives.add(check);
}
return actives;
}
public ArrayList<Task> getPassiveTasks() {
ArrayList<Task> passives;
Iterator it;
it = tasks.iterator();
passives = new ArrayList<Task>();
while (it.hasNext()) {
Task check = (Task) it.next();
if (!check.isActive())
passives.add(check);
}
return passives;
}
/************************************************************************
public ArrayList<Task> getTasksByInterval(String interval) {
return null; }
********************************************************************/
// it calculates the average progress of the tasks in the ArrayList
public int getProgress() {
int size;
int sum;
Iterator it;
size = tasks.size();
sum = 0;
it = tasks.iterator();
while (it.hasNext()) {
sum += ((Task) it.next()).getProgress();
}
return sum / size;
}
// it calculates only the average progress of the active tasks
public int getActiveProgress() {
int size;
int sum;
Iterator it;
ArrayList<Task> actives;
actives = getActiveTasks();
size = actives.size();
sum = 0;
it = actives.iterator();
while (it.hasNext()) {
sum += ((Task) it.next()).getProgress();
}
return sum / size;
}
public void addTask(Task task){
tasks.add(task);
}
public boolean removeTask(Task task){
return tasks.remove(task);
}
//it adds the parameter calendar's tasks to this.
public void merge(Calendar calendar){
Iterator it;
it = calendar.getTasks().iterator();
while(it.hasNext()){
this.addTask(((Task)it.next()));
}
}
//it prints the active , passive tasks separately along with the progresses
public String generateCalendarReport(){
String progress = "Progress is "+getProgress();
String activeProgress="Progress of active tasks is "+getActiveProgress();
String active ="";
String passive="";
Iterator actives;
Iterator passives;
actives = this.getActiveTasks().iterator();
passives = this.getPassiveTasks().iterator();
while(actives.hasNext())
active+=actives.next();
while(passives.hasNext())
passive+=passives.next();
return progress+"\n"+activeProgress+"\n"+active+passive;
}
}
package project01;
public class Task {
// properties
private String description;
private String date;
private int progress;
private boolean active;
// constructor
public Task(String description, String date, int progress, boolean active) {
this.description = description;
this.date = date;
this.progress = progress;
this.active = active;
}
// methods
public String getDesc() {
return description;
}
public int getProgress(){
return progress;
}
public String getDate() {
return date;
}
public void setDesc(String desc) {
desc = description;
}
public void setDate(String date) {
this.date = date;
}
public void setActive(boolean active) {
this.active = active;
}
public boolean isActive() {
return active;
}
/*
* only the description is checked since the date,active and progress may
* vary in different variations of the same task
*/
public boolean equals(Task other) {
if (getDesc().equals(other.getDesc()))
return true;
else
return false;
}
public String toString(){
String desc = "Description "+getDesc();
String date = "Date "+getDate();
String progress = "Progress "+getProgress();
String active;
if(isActive())
active = "Active Task";
else
active ="Passive Task";
return desc + "\n"+date+"\n"+progress+"\n"+active+"\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment