Untitled

 avatar
unknown
plain_text
2 years ago
3.5 kB
5
Indexable
/**
 * PatientRecord.java
 * This class contains a Patient records information
 * @author 
 *
 */
public class PatientRecord {

	//private instance variables
	private int patientID;		//field that represents patient ID
	private int month;			//field that represents month (1-12)
	private int day;			//field that represents day (1-31)
	private int year;			//field that represents year (greater than 1900)
	private String complaint;	//field that represents complaint of the patient
	private String treatment;	//field that represents treatment to take
	
	//Constructor that accepts 6 arguments
	//Throws a BadVisitDateException when passing an invalid month, day or year value
	public PatientRecord(int patientID, int month, int day, int year, String complaint, String treatment) throws BadVisitDateException {
		//assign patientID value to the instance variable patientID
		this.patientID = patientID;
		
		//check if month is valid
		if(month < 1 || month > 12) {	//month is invalid, throw BadVisitDateException with corresponding message
			throw new BadVisitDateException("Month not in range 1-12");
		} else {	//month is valid
			//assign month value to the instance variable month
			this.month = month;
		}
		
		//check if day is valid
		if(day < 1 || day > 31) {		//day is invalid, throw BadVisitDateException with corresponding message
			throw new BadVisitDateException("Day not in range 1-31");
		} else {	//day is valid
			//assign day value to the instance variable day
			this.day = day;
		}
		
		//check if year is valid
		if(year <= 1900) {				//year is invalid, throw BadVisitDateException with corresponding message
			throw new BadVisitDateException("Year not greater than 1900");
		} else {	//year is valid
			//assign year value to the instance variable year
			this.year = year;
		}
		
		//assign complaint value to the instance variable complaint
		this.complaint = complaint;
		//assign treatment value to the instance variable treatment
		this.treatment = treatment;
	}
	
	//No logic implementation stated in the requirements, so to be safe,
	//I have used the functionality provided by Eclipse IDE
	@Override
	public int hashCode() {
		//Normally, for a hashcode, a primt number is used in the calculation to make
		//the hashcode more unique. In this case, it is 31
		final int prime = 31;
		//Results starts with value 1
		int result = 1;
		//add the complaint value. If complaint is null, add 0, otherwise, add the hashcode of complaint (String class)
		result = prime * result + ((complaint == null) ? 0 : complaint.hashCode());
		//add the day value. 
		result = prime * result + day;
		//add the month value. 
		result = prime * result + month;
		//add the patientID value. 
		result = prime * result + patientID;
		//add the treatment value. If treatment is null, add 0, otherwise, add the hashcode of treatment (String class)
		result = prime * result + ((treatment == null) ? 0 : treatment.hashCode());
		//add the year value. 
		result = prime * result + year;
		
		//return the calculated hashcode
		return result;
	}

	//Creates a formatted String of the PatientRecord information
	public String toString(){
		//Concatenate all the patient record information based on the sample output provided
		return "Patient:" + patientID + " [" + month + "/" + day + "/"+ year + "] Complaint: " + complaint + " Prescribed: " + treatment; 
	}
	
}
Editor is loading...