Reputation: 29
Ok so basically i have to make an appointment record of doctors and patients. I have already made records of doctors and patients but now i have to make an appointment record in which i have to assign one patient to a doctor based on the patient's illness and the doctor's specialization. I am new to java so i am not so good at it so if you can help me out, i will highly appreciate that :-) i will show u the coding i did so far. It contains some extra stuff in it as well but i have to add it in it for my assignment. And yea, the names i gave to some variables and objects are sort of weird but that is cos java wont let me give them a proper and appropriate name.. Anyways here is my coding :-
package samplee.java;
import java.util.*;
import java.util.ArrayList;
import java.util.Scanner;
public class patient$ {
public static void main(String[] args) {
ArrayList<patientss> patient1= new ArrayList<patientss>();
ArrayList<doctorss> doctor1= new ArrayList<doctorss>();
Scanner src= new Scanner(System.in);
Scanner sc= new Scanner(System.in);
Scanner stc= new Scanner(System.in);
Scanner sdc= new Scanner(System.in);
Scanner update= new Scanner(System.in);
Scanner ill=new Scanner(System.in);
int id,it,num,i,docid,docit,docnum,docid1;
String name,docname,docspecial,illness;
int ages,docage;
int id1;
Boolean leave = false;
while(!leave){
patientss xx= new patientss();
System.out.println("Enter the patient's ID ");
System.out.println("-1 to finish entering");
id=src.nextInt();
if(id != -1){
xx.setId(id);
}
System.out.println("Enter the patient's name ");
System.out.println("-1 to finish entering");
name=stc.next();
if(id != -1){
xx.setName(name);
patient1.add(xx);
}
System.out.println("Enter the patient's phone number");
System.out.println("-1 to finish entering");
num=sdc.nextInt();
if(id != -1){
xx.setNum(num);
patient1.add(xx);
}
System.out.println("Enter the type of illness the patient has");
System.out.println("-1 to finish entering");
illness=ill.next();
if(id!= -1){
xx.setIllness(illness);
patient1.add(xx);
}
else { leave = true; }//Escape the while loop.
}
System.out.println("Enter the id of a patient");
it=sc.nextInt();
patientss tt= new patientss();
for(i=0;i<patient1.size();i++)
{
tt=patient1.get(i);
if(it==tt.getId())
System.out.println(tt.toString());
break;
}
System.out.println("Enter the id of the patient you want to update");
Scanner id11= new Scanner(System.in);
id1=id11.nextInt();
int up,id2;
String namess;
int phones;
patientss dd= new patientss();
for(i=0;i<patient1.size();i++){
dd=patient1.get(i);
if(id1==dd.getId()){
System.out.println("If you want to change the name, type 2");
System.out.println("If you want to change the number, type 3");
System.out.println("If you want to change the id, type 1");
up=update.nextInt();
if(up==1){
System.out.println("Enter a new ID ");
Scanner idd= new Scanner(System.in);
id2=idd.nextInt();
dd.setId(id2);
patient1.add(dd);
}
else if(up==2){
System.out.println("Enter a new name");
Scanner namme=new Scanner(System.in);
namess=namme.next();
dd.setName(namess);
patient1.add(dd);
}
else if(up==3){
System.out.println("Enter a new phone");
Scanner newphone= new Scanner(System.in);
phones=newphone.nextInt();
dd.setNum(phones);
patient1.add(dd);
}
}
System.out.println(dd);
break;
}
boolean over = false;
while(!over){
doctorss xt= new doctorss();
System.out.println("Enter the doctor's ID ");
System.out.println("-1 to finish entering");
docid=src.nextInt();
if(docid != -1){
xt.setDocid(docid);
}
System.out.println("Enter the doctor's name ");
System.out.println("-1 to finish entering");
docname=stc.next();
if(docid != -1){
xt.setDocname(docname);
doctor1.add(xt);
}
System.out.println("Enter the doctor's phone number");
System.out.println("-1 to finish entering");
docnum=sdc.nextInt();
if(docid != -1){
xt.setDocnum(docnum);
doctor1.add(xt);
}
System.out.println("What does the doctor specialize in?");
String specialize;
Scanner special= new Scanner(System.in);
specialize=special.next();
if(docid != -1){
xt.setSpecialize(specialize);
doctor1.add(xt);
}
else { over = true; }//Escape the while loop.
}
System.out.println("Enter the id of a doctor");
docit=sc.nextInt();
doctorss tti= new doctorss();
for(i=0;i<doctor1.size();i++){
tti=doctor1.get(i);
if(docit==tti.getDocid())
System.out.println(tti.toString());
}
}
}
and these r my classes..
package samplee.java;
public class patientss {
int id;
String name;
int num;
String illness;
public String getIllness() {
return illness;
}
public void setIllness(String illness) {
this.illness = illness;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString(){
return name+" "+id+" "+num+ " "+illness;
}
}
and here is my doctor's class:-
package samplee.java;
public class doctorss {
int docid;
String specialize;
public String getSpecialize() {
return specialize;
}
public void setSpecialize(String specialize) {
this.specialize = specialize;
}
public int getDocid() {
return docid;
}
public void setDocid(int docid) {
this.docid = docid;
}
public String getDocname() {
return docname;
}
public void setDocname(String docname) {
this.docname = docname;
}
public int getDocnum() {
return docnum;
}
public void setDocnum(int docnum) {
this.docnum = docnum;
}
String docname;
int docnum;
public String toString(){
return docname+" "+docid+" and "+docnum+" he specializes in"+specialize;
}
}
Upvotes: 0
Views: 8393
Reputation: 1
I'm not a bro but just a simple note, you don't have to create more objects of Scanner one is enough. I usually go with Scanner input = new Scanner (System.in); then just use it whenever you need, like: int x = input.nextInt(); String s = input.next(); etc.
Upvotes: 0
Reputation: 3043
First of all I want to make some comments regarding your code:
Doctors
but Doctor
. In your case it describes single entity, not a group of doctors or something.docid
, id
is enough as it's defined in Doctor
class. I would propose you the following classes(the details like getters-setters are omitted, but I hope you will get the idea):
class Doctor {
private int id;
private String name;
}
class Patient {
private int id;
private String name;
}
class Illness {
private int id;
private String name;
}
class Appointment {
private Doctor doctor;
private Patient patient;
private Illness illness;
private Date date;
public Appointment(
Doctor doctor, Patient patient, Illness illness, Date date
) {
this.doctor = doctor;
this.patient = patient;
this.illness = illness;
this.date = date;
}
}
Edit:
When user inputs doctors and patients you have to "remember" them somehow. Use, for example, array or ArrayList
for that:
ArrayList<Patient> patients = new ArrayList<Patient>();
// read the patient here
patients.add(patient)
So, in the step where you need to make an appointment you will have to ask user to input: doctor id, patient id, illness id and desired date. Then you have to find doctor, patient and illness in "remembered" lists:
Patient patient
for (Patient item : patients) {
if (item.getId() == idFromUser) {
patient = item
}
}
if (patient == null) {
System.out.println("Patient not found. Choose another ID.");
// here you have to force user to re-input the value.
}
Then just create new appointment:
new Appointment(doctor, patient, illness, date);
Upvotes: 2