I need a java oriented program that is for a pet store. The program must have a
ID: 3811826 • Letter: I
Question
I need a java oriented program that is for a pet store. The program must have a pet store class and a driver class where the main method is located and tests the program. The program must read data from a text file. The code must be able to read:
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)
Also, 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)
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.
Explanation / Answer
Pet.java
package petstore;
public class Pet {
String petName;
String petType;
double weight;
String lastRabiesShot;
String lastVisit;
/**
*
* @param name
* @param type
* @param weight
* @param lastRabiesShot
* @param lastVisit
*/
public Pet(String name, String type, double weight, String lastRabiesShot, String lastVisit) {
this.petName = name;
this.petType = type;
this.weight = weight;
this.lastRabiesShot = lastRabiesShot;
this.lastVisit = lastVisit;
}
public String getName() {
return petName;
}
public String getType() {
return petType;
}
public double getWeight() {
return weight;
}
public String getLastRabiesShot() {
return lastRabiesShot;
}
public String getLastVisit() {
return lastVisit;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pet pet = (Pet) o;
if (Double.compare(pet.getWeight(), getWeight()) != 0) return false;
if (!getName().equals(pet.getName())) return false;
if (!getType().equals(pet.getType())) return false;
if (!getLastRabiesShot().equals(pet.getLastRabiesShot())) return false;
return getLastVisit().equals(pet.getLastVisit());
}
@Override
public int hashCode() {
int result;
long temp;
result = getName().hashCode();
result = 31 * result + getType().hashCode();
temp = Double.doubleToLongBits(getWeight());
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + getLastRabiesShot().hashCode();
result = 31 * result + getLastVisit().hashCode();
return result;
}
@Override
public String toString() {
return "Pet [petName=" + petName + ", petType=" + petType + ", weight=" + weight + ", lastRabiesShot="
+ lastRabiesShot + ", lastVisit=" + lastVisit + "]";
}
}
Client.java
package petstore;
import java.util.Arrays;/**
*
*/
/**
*
* @author naresh
*
*/
public class Client {
String lastName;
String firstName;
String address;
int clientId;
int numberOfVisit;
double outstandingBalance;
Pet[] pests;
/**
*
* @param lastName
* @param firstName
* @param address
* @param clientId
* @param numberOfVisit
* @param outstandingBalance
* @param pests
*/
public Client(String lastName, String firstName, String address, int clientId, int numberOfVisit, double outstandingBalance, Pet[] pests) {
this.lastName = lastName;
this.firstName = firstName;
this.address = address;
this.clientId = clientId;
this.numberOfVisit = numberOfVisit;
this.outstandingBalance = outstandingBalance;
this.pests = pests;
}
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public String getAddress() {
return address;
}
public int getClientId() {
return clientId;
}
public int getNumberOfVisit() {
return numberOfVisit;
}
public double getOutstandingBalance() {
return outstandingBalance;
}
public Pet[] getPests() {
return pests;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Client client = (Client) o;
if (getClientId() != client.getClientId()) return false;
if (getNumberOfVisit() != client.getNumberOfVisit()) return false;
if (Double.compare(client.getOutstandingBalance(), getOutstandingBalance()) != 0) return false;
if (!getLastName().equals(client.getLastName())) return false;
if (!getFirstName().equals(client.getFirstName())) return false;
if (!getAddress().equals(client.getAddress())) return false;
// Probably incorrect - comparing Object[] arrays with Arrays.equals
return Arrays.equals(getPests(), client.getPests());
}
@Override
public int hashCode() {
int result;
long temp;
result = getLastName().hashCode();
result = 31 * result + getFirstName().hashCode();
result = 31 * result + getAddress().hashCode();
result = 31 * result + getClientId();
result = 31 * result + getNumberOfVisit();
temp = Double.doubleToLongBits(getOutstandingBalance());
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + Arrays.hashCode(getPests());
return result;
}
@Override
public String toString() {
return "Client [lastName=" + lastName + ", firstName=" + firstName + ", address=" + address + ", clientId="
+ clientId + ", numberOfVisit=" + numberOfVisit + ", outstandingBalance=" + outstandingBalance
+ ", pests=" + Arrays.toString(pests) + "]";
}
}
TestClass.java
package petstore;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class TestClass {
static ArrayList<Client> clients = new ArrayList<>();
/**
*
* @param fileName
*/
public static void readFile(String fileName) {
try (Scanner sc = new Scanner(new FileReader(fileName))) {
while (sc.hasNext()) {
String[] arr = sc.nextLine().split(",");
Pet []pet = {new Pet(arr[7],arr[8],Double.parseDouble(arr[9]),arr[10],arr[11])};
Client client = new Client(arr[0],arr[1],arr[2],Integer.parseInt(arr[3]),Integer.parseInt(arr[4]),Double.parseDouble(arr[5]),pet);
clients.add(client);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* This Method will Print the Clients List
*/
public static void printClients(){
for(Client client:clients){
System.out.println(client);
}
}
public static void main(String[] args) {
readFile("pet_data.txt");
printClients();
}
}
pet.txt
naresh,kumar,#405 abc,456,10,200,2,Canis,dog,10,"2011-02-12","2011-02-12"
Michael,Clark,#305 abc,456,10,200,2,Canis,dog,10,"2011-02-12","2011-02-12"
Output
Client [lastName=naresh, firstName=kumar, address=#203 abc, clientId=456, numberOfVisit=10, outstandingBalance=200.0, pests=[Pet [petName=Canis, petType=dog, weight=10.0, lastRabiesShot="2011-02-12", lastVisit="2011-02-12"]]]
Client [lastName=Michael, firstName=Clark, address=#204 abc, clientId=456, numberOfVisit=10, outstandingBalance=200.0, pests=[Pet [petName=Canis, petType=dog, weight=10.0, lastRabiesShot="2011-02-12", lastVisit="2011-02-12"]]]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.