我的数据和作业的功课; algorithms类告诉我们创建一个System Task Workorder Program。它要求我们做各种事情,例如向数组添加新的SystemTask对象,删除任务以及列出/打印已排序的对象数组。
我已经完成了大部分编码工作,但是我仍然无法打印出我的对象数组。似乎无论我添加到对象数组中的最后一个数据,它都成为我数组中所有元素的数据。
一些说明:
- 我的WorkOrders()构造函数必须将4个默认的SystemTask对象放在数组中。
- listSystemTask()方法列出数组中的所有SystemTask对象(按顺序),前面有一个数字。
注意:请不要告诉我使用List或任何高级java 方法。我的作业有局限性。
SystemTask类
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());
}
}
我制作了另一个UserInput类。我没有包含它,因为它仅仅用于我的键盘输入。我省略了一些与我的问题无关的代码/方法。我只关心打印数组中的对象。
这里的问题是我打印列表时输出的内容:
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
我不知道问题的来源。我怀疑它是在构造函数中初始化数组但我似乎无法修复它。或者它可能在其他地方?
答案 0 :(得分:3)
问题在于SystemTask
类
public class SystemTask {
private static String month, message;
private static int day, hour, minute;
您已将所有成员变量声明为static
。将有多个SystemTask
实例,但它们都将其值存储在相同的static
字段中。它是打印的最后一个SystemTask
&#34;因为它是最后一个被创建的,覆盖了之前设置的值。
只需删除static
声明...
public class SystemTask {
private String month, message;
private int day, hour, minute;
您还将所有访问者方法定义为static
。从这些中删除static
,以允许它们访问对象范围变量。
public static String getMonth() {
return month;
}
...变为
public String getMonth() {
return month;
}