The only way to achive to use the arrays is to expand the array dynamically as the new 'add' command is used.
Thus, you need this method to update and expand your array;
public static User[] add(User[] userArray, User user) {
//null array check
if( userArray == null ) {
userArray = new User[1];
userArray[0] = user;
return userArray;
}
User[] newArray = new User[userArray.length+1];
int i = 0;
for( ; i < userArray.length; i++ )
newArray[i] = userArray[i];
newArray[i] = user; //already incremented with post-increment operator
return newArray;
}
And the whole test code is as follows;
import java.util.*;
// ArrayList to store Users.
public class TestAppointment {
public static void main(String[] args) {
String command = "";
String date = "";
String appointment = "";
String time = "";
int id = 0;
User[] data = null;
Scanner input = new Scanner(System.in);
while (!command.equalsIgnoreCase("Exit")) {
System.out.println("Please Enter a Command or Type Exit: ");
command = input.nextLine();
if (command.equalsIgnoreCase("Add")) {
System.out.print("Data: ");
date = input.nextLine();
System.out.print("Time: ");
time = input.nextLine();
// System.out.println("Day of appointment:");
// date = input.nextLine();
System.out.print("Appointment: ");
appointment = input.nextLine();
User validUser = new User(date, time, appointment);
//data.add(validUser); // we dont need this anymore
data = add(data, validUser);
System.out.println("Successful!");
System.out.println(""); // Adds an appointment to database if
// it's valid.
}
}
System.out.println(data.length);
for(int i = 0; i < data.length; i++) {
System.out.printf("%d: %s:%s:%s\n",i,data[i].getDate(),data[i].getTime(),data[i].getAppointment());
}
}
public static User[] add(User[] userArray, User user) {
//null array check
if( userArray == null ) {
userArray = new User[1];
userArray[0] = user;
return userArray;
}
User[] newArray = new User[userArray.length+1];
int i = 0;
for( ; i < userArray.length; i++ )
newArray[i] = userArray[i];
newArray[i] = user; //already incremented with post-increment operator
return newArray;
}
private static class User {
private String date;
private String time;
private String appointment;
public User(String date, String time, String appointment) {
this.date = date;
this.time = time;
this.appointment = appointment;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getAppointment() {
return appointment;
}
public void setAppointment(String appointment) {
this.appointment = appointment;
}
}
}
The output of this test code is as below;
Please Enter a Command or Type Exit:
add
Data: testData1
Time: testTime1
Appointment: testApp1
Successful!
Please Enter a Command or Type Exit:
add
Data: testData2
Time: testTime2
Appointment: testApp2
Successful!
Please Enter a Command or Type Exit:
exit
2
0: testData1:testTime1:testApp1
1: testData2:testTime2:testApp2
Hope that this helps you.