Assignment: A large veterinarian services many pets and their owners. As new pet
ID: 3687435 • 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.
***THIS IS WHAT I DID SO FAR, NOT SURE IF IT IS RIGHT BUT MUST FOLLOW THIS FORMAT ..
import java.io.*;
import java.util.Scanner;
public class VetShop
{
Animal[ ] client;
public static void main(String[ ] args) throws Exception {
java.io.File file = new java.io.File("program5.txt");
Scanner input = new Scanner(file);
while(input.hasNext()) {
String ownerName = input.next();
String species = input.next();
int birthYear = input.nextInt();
double balance = input.nextDouble();
System.out.println(
ownerName + " " + species + " " + birthYear + " " + balance);
}
input.close();
}
}
Explanation / Answer
public class Animal implements Comparable<Animal> {
String ownerName;
String species;
int birthYear;
double balance;
/**
* @param ownerName
* @param species
* @param birthYear
* @param balance
*/
public Animal(String ownerName, String species, int birthYear,
double balance) {
this.ownerName = ownerName;
this.species = species;
this.birthYear = birthYear;
this.balance = balance;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "ownerName=" + ownerName + ", species=" + species
+ ", birthYear=" + birthYear + ", balance=" + balance;
}
/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Animal o) {
// TODO Auto-generated method stub
if (this.balance > o.balance)
return 1;
else
return -1;
}
}
import java.util.Arrays;
import java.util.Scanner;
public class VetShop {
static Animal[] client;
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File("program5.txt");
Scanner input = new Scanner(file);
int n = input.nextInt();
client = new Animal[n];
int counter = 0;
while (input.hasNext()) {
String ownerName = input.next();
String species = input.next();
int birthYear = input.nextInt();
double balance = input.nextDouble();
client[counter] = new Animal(ownerName, species, birthYear, balance);
counter++;
}
System.out.println("Before Sorting Animals");
printAnimals();
sortAnimals();
System.out.println("After Sorting Animals by balance");
printAnimals();
input.close();
}
/**
*
*/
public static void sortAnimals() {
Arrays.sort(client);
}
/**
*
*/
public static void printAnimals() {
for (int i = 0; i < client.length; i++)
System.out.println(client[i]);
}
}
program5.txt
3
ownerName1 species1 1990 400
ownerName2 species2 1991 402
ownerName3 species3 1992 401
OUTPUT:
Before Sorting Animals
ownerName=ownerName1, species=species1, birthYear=1990, balance=400.0
ownerName=ownerName2, species=species2, birthYear=1991, balance=402.0
ownerName=ownerName3, species=species3, birthYear=1992, balance=401.0
After Sorting Animals by balance
ownerName=ownerName1, species=species1, birthYear=1990, balance=400.0
ownerName=ownerName3, species=species3, birthYear=1992, balance=401.0
ownerName=ownerName2, species=species2, birthYear=1991, balance=402.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.