Hii, friends...today I am going to show you another important and interesting Java programming interview question with answers.

java_interview_questions_Chaitu_Informative_Blogs

If you have not seen my previous Java interview questions then I have given the links below for you to refer them once.

Also, Read

The questions given below are all the questions the company asked me during the interview process.

Check the questions:

Dear Candidates,
Please read this file completely before doing the task.

TASK: Write a Java console program to create a simple Hospital Management System with the given
specs and guidelines.

Specs:

1. Provide options to add / update / remove doctors' and patient's details.
2. Store doctors' and patients' details in a file.
3. A Doctor should have the following properties.
  • Name
  • Phone Number
  • specialization
  • availability
4. A patient should have the following properties.
  • Name
  • Age
  • phone number
  • gender
  • medical condition
  • prescriptions
5. A patient can consult a doctor and get prescriptions if any.
6. A patient can consult a doctor only if the doctor is available. create and
Throw an exception if a doctor is not available.
7. Provide an option to display doctor's and patient's details.
8. check for duplicate details when adding/updating doctor and patient details.

Guidelines:

Use Inheritance and other OOP concepts wherever possible.
Use collections to import/export details from files.

Check the below code:

FileName: Doctor.java

package chaitu.info.blogs;
import java.io.Serializable;

public class Doctor implements Serializable {
String name;
String phnumber;
String specialization;
String availability;
public Doctor() {

}

@Override
public String toString() {
return name+"\t"+phnumber+"\t"+specialization+"\t"+availability;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhnumber() {
return phnumber;
}
public void setPhnumber(String phnumber) {
this.phnumber = phnumber;
}
public String getSpecialization() {
return specialization;
}
public void setSpecialization(String specialization) {
this.specialization = specialization;
}
public String getAvailability() {
return availability;
}
public void setAvailability(String availability) {
this.availability = availability;
}
}

FileName: Hospital.java

package org.jsp.app;

public interface Hospital {
void addDoctors(Doctor doctor);
void updateDoctors(String name,Doctor doctor);
void removeDoctors(String name, String Specilization);
void addPatients(Patient patient);
void updatePatients(Patient patient);
void removePatients(Patient patient);
}

FileName: HospitalManagement.java

package org.jsp.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class HospitalManagement implements Hospital {
private final String filepath="D:\\hospital.ser";
private File file = new File(filepath);
private FileOutputStream output;
private FileInputStream input = null;;
private ObjectInputStream iStream = null;;
private ObjectOutputStream oStream;
static Scanner scan = new Scanner(System.in);
public  HospitalManagement() {
try {
output = new FileOutputStream(file);
oStream = new ObjectOutputStream(output);
        ArrayList<Doctor> doctors=new ArrayList<Doctor>();
ArrayList<Patient> patients=new ArrayList<Patient>();
ArrayList<ArrayList> hospital=new ArrayList<ArrayList>();
hospital.add(doctors);
hospital.add(patients);
oStream.writeObject(hospital);
oStream.flush();
}
catch(Exception e) {
System.out.println("Exception occured");
}
}
@Override
public void addDoctors(Doctor doctor) {
try {
if(input==null && iStream==null) {
input = new FileInputStream(file);
iStream=new ObjectInputStream(input);
}
ArrayList<ArrayList> tHospital= (ArrayList<ArrayList>) iStream.readObject();
ArrayList<Doctor> aDoctor = tHospital.get(0);
if(aDoctor.size()>0) {
for(int i=0;i<aDoctor.size();i++) {
if(doctor.getName().equalsIgnoreCase(aDoctor.get(i).getName()) && doctor.getPhnumber().equalsIgnoreCase(aDoctor.get(i).getPhnumber()) &&
doctor.getSpecialization().equalsIgnoreCase(aDoctor.get(i).getAvailability())) {
System.out.println("Doctor details are duplicate...");
askUser();
return;
}
  }
    }
aDoctor.add(doctor);
tHospital.remove(0);
tHospital.add(0,aDoctor);
oStream.writeObject(tHospital);
oStream.flush();
System.out.println("Successfully doctor details are added.");
askUser();
return;
} catch(Exception e) {
System.err.println("unable t perform add operation...");
MainClass.start();
}
}

private void askUser() {
System.out.println("do you want to perform other operations...[Y|N]?");
String ch = scan.next();
if(ch.equalsIgnoreCase("y")) {
MainClass.start();
}

}
public void viewDoctors() {
try {
if(input==null && iStream==null) {
input = new FileInputStream(file);
iStream=new ObjectInputStream(input);
}
ArrayList<ArrayList> tHospital= (ArrayList<ArrayList>) iStream.readObject();
ArrayList<Doctor> viewDoctor = tHospital.get(0);
if(viewDoctor.size()==0) {
System.err.println("No doctors details are present.");
askUser();
return;
}
System.out.println("Name\t\tPhoneNumber\t\tSpecialization");
for(int i=0;i<viewDoctor.size();i++) {
System.out.println(viewDoctor.get(i));
}
askUser();
return;
}
                catch(Exception e) {
System.err.println("unable to perform view operation.");
}
}
@Override
public void updateDoctors(String name, Doctor doctor) {
int index = -1;
try {
if(input==null && iStream==null) {
input = new FileInputStream(file);
iStream=new ObjectInputStream(input);
}
ArrayList<ArrayList> tHospital= (ArrayList<ArrayList>) iStream.readObject();
ArrayList<Doctor> aDoctor = tHospital.get(0);
if(aDoctor.size()>0) {
for(int i=0;i<aDoctor.size();i++) {
if(aDoctor.get(i).getName().equalsIgnoreCase(name)) {
index = i;
if(doctor.getName().equalsIgnoreCase(aDoctor.get(i).getName()) &&
doctor.getPhnumber().equalsIgnoreCase(aDoctor.get(i).getPhnumber()) &&
doctor.getSpecialization().equalsIgnoreCase(aDoctor.get(i).getAvailability())) {
System.out.println("Doctor details are duplicate...");
askUser();
return;
}

break;
}
}
aDoctor.remove(index);
aDoctor.add(index,doctor);
tHospital.remove(0);
tHospital.add(aDoctor);
oStream.writeObject(tHospital);
oStream.flush();
System.out.println("Doctor details are successfully updated...");
askUser();
return;
}
                else {
System.err.println("no doctor details are there to update...");
askUser();
return;
}
}
               catch(Exception e) {
System.err.println("unable to perform update...");
MainClass.start();
}

}
@Override
public void removeDoctors(String name, String specilization) {
int index = -1;
try {
if(input==null && iStream==null) {
input = new FileInputStream(file);
iStream=new ObjectInputStream(input);
}
ArrayList<ArrayList> tHospital= (ArrayList<ArrayList>) iStream.readObject();
ArrayList<Doctor> aDoctor = tHospital.get(0);
if(aDoctor.size()>0) {
for(int i=0;i<aDoctor.size();i++) {
if(aDoctor.get(i).getName().equalsIgnoreCase(name)) {
index = i;
break;
}
 }
aDoctor.remove(index);
tHospital.remove(0);
tHospital.add(aDoctor);
oStream.writeObject(tHospital);
oStream.flush();
System.out.println("Doctor details are removed successfully...");
askUser();
return;
}
                else {
System.err.println("no doctor details are present to remove...");
askUser();
return;
}
}
               catch(Exception e) {
       System.out.println("unable to perform remove operation...");
       MainClass.start();
       }
}
@Override
public void addPatients(Patient patient) {
try {
//input = new FileInputStream(file);
//iStream=new ObjectInputStream(input);
ArrayList<ArrayList> tHospital= (ArrayList<ArrayList>) iStream.readObject();
//System.out.println(tHospital);
ArrayList<Patient> aPatient = tHospital.get(1);
aPatient.add(patient);
//oStream = new ObjectOutputStream(output);
oStream.writeObject(tHospital);
oStream.flush();
}
                catch(Exception e) {
System.out.println("Exception in add doctor");
}
}

public void viewPatients() {
try {
if(input==null && iStream==null) {
input = new FileInputStream(file);
iStream=new ObjectInputStream(input);
}
ArrayList<ArrayList> tHospital= (ArrayList<ArrayList>) iStream.readObject();
ArrayList<Patient> viewPatient = tHospital.get(1);
System.out.println(viewPatient);
//iStream.close();
}
                catch(Exception e) {
System.out.println("Exception in view patients");
}
}

@Override
public void updatePatients(Patient patient) {

}
@Override
public void removePatients(Patient patient) {


}
}

FileName: MainClass.java

package chaitu.info.blogs;

import java.util.Scanner;

public class MainClass {
static Scanner scan = new Scanner(System.in);
static HospitalManagement hospital = new HospitalManagement();
public static void main(String[] args) {
start();
}

public static void start() {
    System.out.println();
    System.out.println("PLease select the given option to proceed...");
    System.out.println("1.Add Doctor\t2.View Doctor\n3.Update Doctor\t4.Delete Doctor");
    int mainOption = scan.nextInt();
    switch(mainOption) {
case 1:
Doctor doctor = new Doctor();
System.out.println("enter the name...");
doctor.setName(scan.next());
System.out.println("Enter the phone number...");
doctor.setPhnumber(scan.next());
System.out.println("Enter the specialization...");
doctor.setSpecialization(scan.next());
System.out.println("Enter the availability...");
doctor.setAvailability(scan.next());
hospital.addDoctors(doctor);
break;
case 2:
hospital.viewDoctors();
break;
case 3:
System.out.println("enter the doctor name to update...");
String name = scan.next();
Doctor doctor1 = new Doctor();
System.out.println("enter the name...");
doctor1.setName(scan.next());
System.out.println("Enter the phone number...");
doctor1.setPhnumber(scan.next());
System.out.println("Enter the specialization...");
doctor1.setSpecialization(scan.next());
System.out.println("Enter the availability...");
doctor1.setAvailability(scan.next());
hospital.updateDoctors(name, doctor1);
break;
case 4:
System.out.println("enter the doctor name to update...");
String rname = scan.next();
System.out.println("enter the doctor name to update...");
String rSpec = scan.next();
hospital.removeDoctors(rname,rSpec);
break;
default:
System.out.println("Please choose correct optio to proceed...");
start();
}

}
}

FileName: Patient.java

package org.jsp.app;

import java.io.Serializable;

public class Patient implements Serializable {
String name;
int age;
int phnumber;
String gender;
String medicalcondition;
String prescriptions;
public Patient(String name, int age, int phnumber, String gender, String medicalcondition, String prescriptions) {
super();
this.name = name;
this.age = age;
this.phnumber = phnumber;
this.gender = gender;
this.medicalcondition = medicalcondition;
this.prescriptions = prescriptions;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPhnumber() {
return phnumber;
}
public void setPhnumber(int phnumber) {
this.phnumber = phnumber;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMedicalcondition() {
return medicalcondition;
}
public void setMedicalcondition(String medicalcondition) {
this.medicalcondition = medicalcondition;
}
public String getPrescriptions() {
return prescriptions;
}
public void setPrescriptions(String prescriptions) {
this.prescriptions = prescriptions;
}
@Override
public String toString() {
return "Patient [name=" + name + ", age=" + age + ", phnumber=" + phnumber + ", gender=" + gender + ", medicalcondition=" + medicalcondition + ", prescriptions=" + prescriptions + "]";
}
}

Result:

Java_output_Chaitu_Informative_Blogs


I think this post will definitely help you crack the interviews. so please share with your friends and family who are seriously preparing for interviews because it may help them.