Java Please Write the class files and a sample Driver class to test the code for
ID: 3866343 • Letter: J
Question
Java Please
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. The toString( ) of the Client class must concatenate the values of each Pet in the array: name and type.
Write an equals( ) method for the client based on the client id.
Write a program that reads in data from a file to create and array of five Clients. The driver will print each client’s information (along with the pet name and type)
I have provided a data file for testing located on the Canvas site.
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
Then, in the driver, present a menu to the user with the following repeating options:
Search for a client by ID number, if ID is found,
a) print the client name and outstanding balance b) print the list of the client’s pets, name, type and date of last visit c) change the client’s outstanding balance d) quit
nb: You do not have to change the underlying file, but it would be a good thing to try that.
___________
clientdata.txt:
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-2012,04-25-2012
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-2014
Pete,boa,8,NA,3-15-2014
Explanation / Answer
import java.io.*;
import java.util.*;
class Pet {
private String name;
private String type;
private int weight;
private String last_rabbies_shot;
private String last_visit;
public Pet(String nm, String typ, int wt, String rab, String lvisit){
name = nm;
type = typ;
weight = wt;
last_rabbies_shot = rab;
last_visit = lvisit;
}
public void getData(){
System.out.println(name + " " + type + " " + last_visit);
}
}
class Client {
private String lastname;
private String firstname;
private String address;
private String ID;
private int num_visits;
private double balance;
private int num_pets;
Pet[] pets;
public Client(String lname, String fname, String addr, String id, int nvisit, double bal, int npets){
lastname = lname;
firstname = fname;
address = addr;
ID = id;
num_visits = nvisit;
balance = bal;
num_pets = npets;
pets = new Pet[npets];
}
public void addPets(Pet[] p){
for (int i = 0; i<num_pets; i++)
pets[i] = p[i];
}
public String getName(){
return lastname + " " + firstname;
}
public String getId(){
return ID;
}
public double getBalance(){
return balance;
}
public void setBalance(double bal){
balance = bal;
}
public void printPets(){
for (int i = 0; i<num_pets; i++){
pets[i].getData();
}
}
}
public class DemoClient{
public static void main(String args[]){
ArrayList<Client> list = new ArrayList<Client>();
try {
File file = new File("clientdata.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null;
int count = 0;
char choice;
String id;
int found;
double bal;
Client client;
Scanner sc=new Scanner(System.in);
//line = br.readLine();
while( (line = br.readLine())!= null ){
String [] tokens = line.split(",");
//System.out.println(Integer.parseInt(tokens[7]));
client = new Client(tokens[0],tokens[1], tokens[2], tokens[3],Integer.parseInt(tokens[4]),Double.parseDouble(tokens[5]),Integer.parseInt(tokens[6]));
Pet[] p = new Pet[Integer.parseInt(tokens[6])];
for (int i = 0; i< Integer.parseInt(tokens[6]); i++){
line = br.readLine();
String [] tokens1 = line.split(",");
p[i] = new Pet(tokens1[0], tokens1[1], Integer.parseInt(tokens1[2]), tokens1[3], tokens1[4]);
}
client.addPets(p);
list.add(client);
}
br.close();
do {
System.out.println("a)print the client name and outstanding balance");
System.out.println("b)print the list of client's pets name, type and date of last visit");
System.out.println("c)change the clients's outstanding balance");
System.out.println("d)quit");
System.out.println("Enter your choice:");
choice = sc.next().charAt(0);
switch (choice) {
case 'a':
System.out.println("Enter ID:");
id = sc.next();
found = 0;
for (int i = 0; i<list.size(); i++){
if (list.get(i).getId().equals(id)){
found = 1;
System.out.println(list.get(i).getName() + " " + list.get(i).getBalance());
}
}
if (found == 0)
System.out.println("Not found");
break;
case 'b':
System.out.println("Enter ID:");
id = sc.next();
found = 0;
for (int i = 0; i<list.size(); i++){
if (list.get(i).getId().equals(id)){
found = 1;
list.get(i).printPets();
}
}
if (found == 0)
System.out.println("Not found");
break;
case 'c':
System.out.println("Enter ID:");
id = sc.next();
System.out.println("Enter outstanding balance:");
bal = sc.nextDouble();
found = 0;
for (int i = 0; i<list.size(); i++){
if (list.get(i).getId().equals(id)){
found = 1;
list.get(i).setBalance(bal);
}
}
if (found == 0)
System.out.println("Not found");
break;
}
} while (choice != 'd');
} catch (Exception e){
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.