Purpose: To practice writing a class that uses aggregation and that has an array
ID: 654976 • Letter: P
Question
Purpose: To practice writing a class that uses aggregation and that has an array as a field. To read data from a text file.
Write the class files and a sample Driver class to test the code for the following:
You are writing code for a veterinary office. Each human client will have:
A last name
A first name
An address
A client ID
number of visits
Outstanding Balance
an array of Pets (aggregation)
Each Pet will have:
A name
An animal type
A weight
A date of last rabies shot (String)
A date of last visit (String)
Write the constructor methods and toString ( ) method for each class. Write an equals( ) method for the client based on the client id
Write a program that reads in data from a file to create an array of five clients. The driver will print each client's information (along with the pet name and type) for extra credit print the data in ascending order by client name.
File Structure:
Last name, first name, address, ID, number of visits, balance, number of pets, pet name, animal type, weight, rabies date, last visit date.
Here is the text file
Morely,Robert,123 Anywhere Street,15396,4,234.56,2
Bubba,Bulldog,58,4-15-2010,6-14-2011
Lucy,Bulldog,49,4-15-2010,6-14-2011
Wilder,John,457 Somewhere Road,78214,3,124.53,1
Ralph,Cat,12,01-16-2011,04-21-2012
Miller,John,639 Green Glenn Drive,96258,5,0.00,3
Major,Lab,105,07-10-2012,06-13-2013
King,Collie,58,06-14-2012,10-05-2012
Pippy,cat,10,04-25-2015,04-25-2015
Jones,Sam,34 Franklin Apt B,47196,1,32.09,1
Gunther,Swiss Mountain Dog,125,10-10-2013,10-10-2013
Smith,Jack,935 Garrison Blvd,67125,4,364.00,4
Perry,Parrot,5,NA,3-13-2014
Jake,German Shepherd,86,11-14-2013,11-14-2013
Sweetie,tabby cat,15,12-15-2013,2-15-2015
Pete,boa,8,NA,3-15-2015
Explanation / Answer
public class Pet
{
String name, petType,rabiesShot,dateOfLastVisit;
double weight;
public Pet()
{
name = petType = rabiesShot=dateOfLastVisit = "unknown";
weight = 1;
}
public Pet(String inName, String inPetType,String inRabiesShot, String inDateOfLastVisit, double inWeight)
{
name = inName;
petType = inPetType;
rabiesShot = inRabiesShot;
dateOfLastVisit = inDateOfLastVisit;
weight = inWeight;
}
public String toString()
{
return (name + ", " + petType + ", " + rabiesShot + ", " + dateOfLastVisit + ", " + weight);
}
}
public class Human
{
private String lastName;
private String firstName;
private String address;
private String id;
private int visits;
private double balance;
private Pet [] petsArray;
public Human ()
{
lastName = firstName = address = id = "unknown";
visits = 0;
balance = 0;
petsArray = new Pet [1];
petsArray [0] = new Pet ();
}
public Human(String inLastName, String inFirstName, String inAddress,String inId, int inVisits, double inBalance, Pet [] inPets)
{
lastName = inLastName;
firstName = inFirstName;
address= inAddress;
id = inId;
visits = inVisits;
balance = inBalance;
petsArray = new Pet [inPets.length];
for (int i = 0; i < inPets.length; i ++)
{
petsArray[i] = inPets[i];
}
}
public String toString()
{
String text;
text = lastName + ", " + firstName + ", " + address + ", " + id + ", " + visits + ", " + balance;
for (int i = 0; i < petsArray.length; i ++)
{
text = text + petsArray [i];
}
return text;
//inside loop do text.append(petArray[i]
//return new String(text);
}
public boolean idEquals(Human other)
{
if(this.id == other.id)
{
return true;
}
else
return false;
}
driver :
import java.util.Scanner;
import java.io.*;
import java.util.StringTokenizer;
public class Driver
{
public static void main(String[] args) throws IOException
{
String lastName, firstName, address, id;
int visits;
double balance;
String petName, petType, rabiesShot, dateOfLastVisit;
double weight;
File inFile = new File("data.txt");
Scanner input = new Scanner(inFile);
while (input.hasNext())
{
String txt = input.nextLine();
StringTokenizer tokens = new StringTokenizer(txt,",");
while(tokens.hasMoreTokens())
{
System.out.println(tokens.nextToken());
}
}
}
}
private class AddButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tempNumbers1 == 0) {
tempNumbers1 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
} else {
tempNumbers2 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
}
function = 2;
}
}
private class SubtractButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tempNumbers1 == 0) {
tempNumbers1 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
} else {
tempNumbers2 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
}
function = 3;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.