Write the class files and a sample Driver class to test the code for the followi
ID: 3708735 • Letter: W
Question
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 has: A last name A first name An address A client ID number of visits outstanding Balance an array of Pets (aggregation) Each Pet has: A name An animal type A weight A date of last rabies shot (String) A date of last visit (String) Write at least one constructor methods and the toString ( ) method for each class.
The toString( ) method for the Client class must concatenate on the the information for each Pet the client owns. (name, type and date of last visit) Write an equals( ) method for the client based on the client id. Write a compareTo( ) method based the client’s last and first name. Write a driver class that reads in data from a file to create an array of five Clients with all the fields, including the array of Pets. The driver will print each client’s information (along with each the pet name and type and date of last visit) . This list of Clients must be sorted by name. (Use the selection sort we did in class for the Student class). I have provided a data file for testing located on the Canvas site.
File structure: First line: number of clients. Last name, first name, address, ID, number of visits, balance, number of pets Pet name, animal type, weight, rabies date, last visit date The fields in the file are comma delimited.
Allow the user to search for the array of clients based on Client ID. You must use the Client equals( ) method for this. Once the client is found print that Client’s information. If the client is not found, print a message stating so. Continue allowing the user to search using a loop.
Allow the user to add more pets for a client. Since these are high-quality veterinarians, we will not allow deletions of pets. Then save the entire modified client list to a file. To give you lots of practice working with arrays DO NOT use the ArrayList class. Use the same file as the input file for the output. This also requires a menu to print, search and add pets once a client is located. The menu should be in a loop.
String buffer can be used.
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.