My homework for a data & algorithms class tells us to create a System Task Workorder Program. It's asking us to do various things like add a new SystemTask object to an array, delete a task and list/print the sorted object array.
I've done most of the coding already, but I'm stuck in printing out my objects array. It seems that whatever the last data I add into the object array, it becomes the data for all the elements in my array.
Some instructions:
- My WorkOrders() constructor must place the 4 default SystemTask objects in the array.
- listSystemTask() method lists all SystemTask objects in the array (in order) with a number in front.
NOTE: Please refrain from telling me to use List or any advance java methods. My homework has limitations to it.
SystemTask Class
public class SystemTask{
private static String month, message;
private static int day, hour, minute;
//constructor methods
public SystemTask(){
month = "Jan";
day = 1;
hour = 00;
minute = 00;
message = "Task Here";
}
public SystemTask(String mon, int d, int h, int mnt, String msg){
month = mon;
day = d;
hour = h;
minute = mnt;
message = msg;
/*setMonth(mon);
setDay(d);
setHour(h);
setMinute(mnt);
setMessage(msg);*/
}
//get and set methods
public static String getMonth(){
return month;
}
public static int getDay(){
return day;
}
public static int getHour(){
return hour;
}
public static int getMinute(){
return minute;
}
public static String getMessage(){
return message;
}
public static void setMonth (String mon){
if(mon.length()!= 3)
System.out.println("Invalid 3 Letter Code. Try Again.");
else
month = mon;
}
public static void setDay(int d){
if(d<1 || d>31)
System.out.println("Invalid Day. Try Again.");
else
day = d;
}
public static void setHour(int h){
if(h<0 || h>23)
System.out.println("Invalid Hour. Try Again.");
else
hour = h;
}
public static void setMinute(int mnt){
if(mnt<0 || mnt>59)
System.out.println("Invalid Minute. Try Again.");
else
minute = mnt;
}
public static void setMessage(String msg){
if(msg.length()<1 || msg.length()>40)
System.out.println("Invalid Message. Try Again.");
else
message = msg;
}
@Override
public String toString(){
return getMonth()+" "+getDay()+", "+String.format("%02d",getHour())
+":"+String.format("%02d",getMinute())+" "+getMessage();
}
}
WorkOrder Class
public class WorkOrders {
private final SystemTask[] ary = new SystemTask[20]; //declare private 20 objects array
//public static int index, empty;
public static void main(String args[]){
WorkOrders object = new WorkOrders(); //create a WorkOrders object
object.run(); //call a run method
}
public WorkOrders(){
// for(int i = 0;i<ary.length;i++){
// ary[i] = new SystemTask();
// }
/*SystemTask t1 = new SystemTask("Mar", 4, 21, 30, "Backup Users");
ary[0] = t1;
SystemTask t2 = new SystemTask("Apr", 1, 17, 0, "Upgrade Hard Drives");
ary[1] = t2;
SystemTask t3 = new SystemTask("May", 6, 10, 45, "Virus Scan");
ary[2] = t3;
SystemTask t4 = new SystemTask("Jun", 3, 9, 15, "Database Backup");
ary[3] = t4;*/
ary[0] = new SystemTask("Mar", 4, 21, 30, "Backup Users");
ary[1] = new SystemTask("Apr", 1, 17, 0, "Upgrade Hard Drives");
ary[2] = new SystemTask("May", 6, 10, 45, "Virus Scan");
ary[3] = new SystemTask("Jun", 3, 9, 15, "Database Backup");
}
public void run(){
char choice;
do{
System.out.println("SYSTEM WORKORDER PROGRAM:");
System.out.println("A)dd SystemTask");
System.out.println("D)elete SystemTask");
System.out.println("L)ist SystemTask");
System.out.println("E)xit");
System.out.print("Select an option: ");
choice = UserInput.getChar();
switch (choice) {
case 'a':
case 'A':
//addSystemTask();
break;
case 'd':
case 'D':
//deleteSystemTask();
break;
case 'l':
case 'L':
listSystemTask();
break;
case 'e':
case 'E':
System.out.println("You quit the program.");
System.out.println("\nThanks for using System Workorder!");
System.exit(0);
break;
default:
System.out.println("\'" + choice + "\' does not exist. Try again.");
break;
}
System.out.println();
}while(choice !='E'|| choice !='e');
}
public void listSystemTask(){
System.out.println("\nALL LISTED TASKS");
for(int c=0;c<ary.length;c++){
if(ary[c]!=null)
System.out.println(c+1 + ": " + ary[c].toString());
}
}
There is another UserInput Class that I made. I didn't include it because it merely for my keyboard input. I have omitted some of the code/methods that has nothing to do with my question. I'm only concerned with the printing of the objects in my array.
The problem here is this is what I get for output when I print the list:
SYSTEM WORKORDER PROGRAM:
A)dd SystemTask
D)elete SystemTask
L)ist SystemTask
E)xit
Select an option: L
ALL LISTED TASKS
1: Jun 3, 09:15 Database Backup
2: Jun 3, 09:15 Database Backup
3: Jun 3, 09:15 Database Backup
4: Jun 3, 09:15 Database Backup
I don't know where the problem is coming from. I'm suspecting it's in the initializing of the arrays in the constructor but I can't seem to fix it. Or maybe it's in other place?