Assignment: A large veterinarian services many pets and their owners. As new pet
ID: 3685480 • Letter: A
Question
Assignment: A large veterinarian services many pets and their owners. As new pets are added to the population of pets being serviced, their information is entered into a flat text file. Each month, the vet requests a listing of all pets sorted by their outstanding bill balance. You have to write a program to produce a report of animals and their owners sorted by their outstanding balances from the data in the flat text file, as follows.
THE TEXT FILE IS CALLED: program5.txt
A class named VetShop with the following details
– clients: an array of Animal objects
– A simple main method that calls the following input, sort, and report methods.
– An input method to fill the clients array with Animal objects created with the information in the input text file attached. The first line has the number of clients, followed by the data of one animal in each line.
– A method to sort the array of Animals by bill balance.
– A method to produce an output report with one Animal per line.
Explanation / Answer
program5.txt
3
xxx dog abc 1000
yyy dog xyz 500
zzz pig qqq 2000
VetShop.java
import java.io.*;
import java.util.*;
class Animal
{
String name, type, owner;
int balance;
Animal(String name, String type, String owner, int balance)
{
this.name = name;
this.type = type;
this.owner = owner;
this.balance = balance;
}
}
public class VetShop
{
Animal[] client;
public void input() throws Exception
{
FileReader fr = new FileReader("program5.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
int client_count = Integer.parseInt(br.readLine());
client = new Animal[client_count];
int i=0;
while ((line = br.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens())
{
//System.out.print("***" + st.nextToken());
String name = st.nextToken();
String type = st.nextToken();
String owner = st.nextToken();
int balance = Integer.parseInt(st.nextToken());
client[i] = new Animal(name, type, owner, balance);
}
i++;
}
br.close();
}
public void sort()
{
Arrays.sort(client, new Comparator<Animal>()
{
public int compare(Animal obj1, Animal obj2)
{
return obj1.balance - obj2.balance;
}
});
}
public void output()
{
for(int i=0;i<client.length;i++)
{
System.out.println("NAME: " + client[i].name + " TYPE: " + client[i].type + " OWNER : " + client[i].owner + " BALANCE: " + client[i].balance);
}
}
public static void main(String args[]) throws Exception
{
VetShop v = new VetShop();
v.input();
v.sort();
v.output();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.