Hi, i have a program that has a Database class that is used to create an ArrayList that stores patients. I have a Patient class which defines what a Patient is and i have a server class which creates a simple server and allows a user to connect to using telnet or similar program. patients can be added to the database but when i add the second one, it adds it ok to the next position in the array but it also overwrites the previous stored ones to make identical. I know i need to create a new object for the patient but i have tried to do this already both by adding it as a field and declaring it in the begining and by removing that declaration and declaring it in the relevant if statement and niether seem to fix the problem. how much of my code would i need to post? all the relevent pieces or all my code... this is for an assignment and i read on another post when i was looking for a solution similar before posting and read that turnitin scans the site so i wouldnt want to flag my work as somebody elses cos they find it posted on a forum :(
I think this is the relevent code if it isnt enough i will upload it all
import java.net.*; import java.util.StringTokenizer; import java.io.*; public class Server { public static void main(String args[]) { new SimpleServer(6001) ; } } // class Server1 class SimpleServer { // ArrayList<Patient> patients = new ArrayList<Patient>(); Database db; public ServerSocket sock; public Socket conn; public BufferedReader in; public BufferedWriter out; public String str; SimpleServer(int port) { super(); try { in = new BufferedReader(new InputStreamReader(conn.getInputStream())); } catch (Exception e) { System.out.println("Error : " + e); System.out.println("Can't get input stream for socket"); System.exit(1); } try { out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); } catch (Exception e) { System.out.println("Error : " + e); System.out.println("Can't get output stream for socket"); System.exit(1); } try { do { try { // wait for client to send data str = in.readLine(); if (str.endsWith("ADD")){ Patient pat = new Patient(); StringTokenizer st = new StringTokenizer(str, ":"); pat.setID(st.nextToken()); pat.setfName(st.nextToken()); pat.setlName(st.nextToken()); pat.setCondition(st.nextToken()); pat.setHealth(st.nextToken()); System.out.print(Patient.getID()); db.patients.add(pat); System.out.println("Output: " + db.patients.get(0)); out.write("#~ Patient Information. "); out.write("#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); out.write("#~ Patient ID: " + Patient.getID() +" "); out.write("#~ Patient Name: " + Patient.getfName() + Patient.getlName() + " "); out.write("#~ Patient Condition: " + Patient.getCondition() +" "); out.write("#~ Patient Health: " + Patient.getHealth() + " "); out.write("#~ "); out.write("#~ Patient has been added to system! "); out.write("#~ "); out.write("#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); out.write("##########################################################\n\nInput :>"); System.out.println(db.patients.size()); } out.newLine(); out.flush(); } catch(IOException e) { System.out.println("Error : " + e); System.exit(1); }catch(NullPointerException e){ System.out.println("Terminal has tried to exit without typing EXIT" + e); } System.out.println("Client sent string " + str.trim()); } while ( !str.startsWith("exit") ); System.out.println("Closing server now"); System.exit(0); } // SimpleServer constructor
This is my database class :-
import java.util.ArrayList; import java.io.Serializable; public class Database implements Serializable{ /** * A class to define the database, used to enable records to stored * as Patient Objects . * @author u0669469 */ private static final long serialVersionUID = 8397775991766188997L; //List of Patients. ArrayList<Patient> patients = new ArrayList<Patient>(); /** * Add the Patient to the array list. * @param newPatient */ public void addPatient(Patient newPatient) { patients.add(newPatient); } public Patient retrievePatient(String name) { System.out.println("Patient " + patients); return null; } public void removePatient(int remove) { patients.remove(remove); } public void removeList() { patients.clear(); } public int numberOfPatients(int patientNumber) { return patients.size(); } }
And this is my Patient Class:-
import java.io.Serializable; public class Patient implements Serializable { /** * A class to define what a patient is, used to enable records to be kept to a consistant * format. * @author u0669469 */ private static final long serialVersionUID = 8949306406483963579L; public static String patientID; public static String fName; public static String lName; public static String condition; public static String health; public Patient() { } public static String getCondition() { return condition; } public void setCondition(String condition) { Patient.condition = condition; } public static String getHealth() { return health; } public void setHealth(String health) { Patient.health = health; } public static String getID() { return patientID; } public void setID(String iD) { patientID = iD; } public static String getfName() { return fName; } public void setfName(String fName) { Patient.fName = fName; } public static String getlName() { return lName; } public void setlName(String lName) { Patient.lName = lName; } }
I have tried creating a new Patient in many places and non of them seem to work. I would appreciate any thoughts on what the problem might be? am i not referencing a new Patient object properly or is my arrayList not set up properly?
My code compiles ok and there are no errors present when i try to compile so im guessing its something obvious that im forgetting?