[英]JAVA: Object Array only prints last element
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. 它要求我们做各种事情,例如向数组添加新的SystemTask对象,删除任务以及列出/打印排序后的对象数组。
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. 我的WorkOrders()构造函数必须在数组中放置4个默认的SystemTask对象。
- listSystemTask() method lists all SystemTask objects in the array (in order) with a number in front. listSystemTask()方法(按顺序)列出数组中的所有SystemTask对象,并在前面加一个数字。
NOTE: Please refrain from telling me to use List or any advance java methods. 注意:请不要告诉我使用List或任何高级java方法。 My homework has limitations to it. 我的作业有其局限性。
SystemTask Class 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());
}
}
There is another UserInput Class that I made. 我做了另一个UserInput类。 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? 或者也许在其他地方?
The problem is with the SystemTask
class 问题出在SystemTask
类上
public class SystemTask {
private static String month, message;
private static int day, hour, minute;
You have declared all the member variables as static
. 您已将所有成员变量声明为static
。 There will be multiple instances of SystemTask
but they are all storing their values in the same static
fields. 将有多个SystemTask
实例,但它们都将其值存储在相同的static
字段中。 It's printing "the last SystemTask
" because it was the last one to be created, overwriting the values set by the previous ones. 它正在打印“最后一个SystemTask
”,因为它是要创建的最后一个,覆盖了先前设置的值。
Simply remove the static
declarations... 只需删除static
声明...
public class SystemTask {
private String month, message;
private int day, hour, minute;
You have also defined all the accessor methods as static
. 您还已将所有访问器方法定义为static
。 Drop the static
from these too, to allow them to access the object scope variables. 也从其中删除static
变量,以允许它们访问对象作用域变量。
public static String getMonth() {
return month;
}
Becomes... 成为...
public String getMonth() {
return month;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.