Re: How to call a class from another class
On 2007-08-18, rossum <rossum48@coldmail.com> wrote:
You should also write a small test program to test your Client class:
put data into the class and retrieve data from it. This will also be
useful later.
When you have written and tested your Client class, then write your
Attorney class in much the same way, with its own data, getters and
setters. Again write a small test program to add and retrieve
Attorney data.
Once you have the two classes set up and tested then you can start on
the last part, creating the LawFirm application to populate your data
classes and display the data nicely. You can use your two small test
programs here as the basis of the final program as they will cover
many of the same things.
Again, if you have any problems then post what you have written here
and we can help you.
rossum
Greetings,
Being somewhat new to Java, I decided to give this assignment a try. I
was surprised at how easy it was even for me. I would like some
pointers on how my solution could be improved.
As you can see, I created two arrays, one of clients and one of lawyers.
I thought that iterating through an array would be easier should there
ever be more than 5 lawyers and 2 clients.
I also added a method to my client class called "displayAttorney".
Unfortunantly, in order for it to work, I have to pass the entire
attorney array into it so that it can search for the attorney who's
number matches the client's primary attorney ID number. Such a
technique may not be necessary, how ever I do not know of any with my
limited knowledge. Again, suggestions for improving are welcome.
I did this to comply with the requirment that the values be displayed in "...an
attractive format". Of course a database would be ideal for this task,
but I think my solution is quite good. Any suggestions on how to
improve it would be welcome.
First, my client class
I should apologize in advance as I am not sure how to configure Eclipse
to prevent the word wrapping you see here.
package howardFineAndHoward;
public class Client {
private int clientNumber;
private String lastName;
private String firstName;
private int primaryAttorneyIDNumber;
private float balanceOwing;
public Client(int clientNumber, String lastName, String
firstName,
int primaryAttorneyIDNumber, float balanceOwing)
{
this.clientNumber = clientNumber;
this.lastName = lastName;
this.firstName = firstName;
this.primaryAttorneyIDNumber = primaryAttorneyIDNumber;
this.balanceOwing = balanceOwing;
}
public int getClientNumber() {
return clientNumber;
}
public void setClientNumber(int clientNumber) {
this.clientNumber = clientNumber;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;a
}
public int getPrimaryAttorneyIDNumber() {
return primaryAttorneyIDNumber;
}
public void setPrimaryAttorneyIDNumber(int
primaryAttorneyIDNumber) {
this.primaryAttorneyIDNumber = primaryAttorneyIDNumber;
}
public float getBalanceOwing() {a
return balanceOwing;
}
public void setBalanceOwing(float balanceOwing) {
this.balanceOwing = balanceOwing;
}
public void displayClient()
{
System.out.println("Client Number: " +
this.getClientNumber());
System.out.println("Client Name: " +
this.getFirstName() + " " + this.getLastName());
System.out.println("Primary Lawyer Id: " +
this.getPrimaryAttorneyIDNumber());
System.out.println("Balance owing: " +
this.getBalanceOwing());
}
public void displayAttorney(Attorney [] attorneyList)
{
int attorneyIndex =0;
boolean found = false;
while (!found) {
if ( attorneyList[attorneyIndex].getIdNumber() ==
this.getPrimaryAttorneyIDNumber())
{
found = true;
System.out.println("Primary Attorney " +
attorneyList[attorneyIndex].getFirstName() + " " +
attorneyList[attorneyIndex].getLastName());
}
attorneyIndex++;
}
}
}
Now for my Attorney class
package howardFineAndHoward;
public class Attorney {
private int idNumber;
private String lastName;
private String firstName;
private float annualSalary;
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public float getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(float annualSalary) {
this.annualSalary = annualSalary;
}
public Attorney(int idNumber, String lastName, String firstName,
float annualSalary) {
this.idNumber = idNumber;
this.lastName = lastName;
this.firstName = firstName;
this.annualSalary = annualSalary;
}
public void displayAttorney()
{
System.out.println("ID Number: " + this.getIdNumber());
System.out.println("Attorney Name: " +
this.getFirstName() + " " + this.getLastName());
System.out.println("Annual Salary: " +
this.getAnnualSalary());
}
}
And finally, the LawFirm class
package howardFineAndHoward;
public class LawFirm {
public static void main(String [] args)
{
Client [] clientList = new Client[2];
Attorney [] attorneyList = new Attorney[5];
clientList[0] = new Client(1, "Smith", "John", 5,
10221.11f);
clientList[1] = new Client(2, "Ford", "Harrison", 2,
4932.33f);
attorneyList[0] = new Attorney(1,"Johnson","Mike",
140000.00f);
attorneyList[1] = new Attorney(2,"Marshall","Larry",
150000.00f);
attorneyList[2] = new Attorney(3,"Howard","Curly",
160000.00f);
attorneyList[3] = new Attorney(4,"Fine","Joe", 170000.00f);
attorneyList[4] = new Attorney(5,"Howard","Sally",
180000.00f);
System.out.println("Printing Client List");
for (int i=0;i<2;i++) {
clientList[i].displayClient();
clientList[i].displayAttorney(attorneyList);
}
System.out.println("Printing Attorney List");
for (int i=0;i<5;i++) {
attorneyList[i].displayAttorney();
}
}
}
Here is the output in case you are interested
Printing Client List
Client Number: 1
Client Name: John Smith
Primary Lawyer Id: 5
Balance owing: 10221.11
Primary Attorney Sally Howard
Client Number: 2
Client Name: Harrison Ford
Primary Lawyer Id: 2
Balance owing: 4932.33
Primary Attorney Larry Marshall
Printing Attorney List
ID Number: 1
Attorney Name: Mike Johnson
Annual Salary: 140000.0
ID Number: 2
Attorney Name: Larry Marshall
Annual Salary: 150000.0
ID Number: 3
Attorney Name: Curly Howard
Annual Salary: 160000.0
ID Number: 4
Attorney Name: Joe Fine
Annual Salary: 170000.0
ID Number: 5
Attorney Name: Sally Howard
Annual Salary: 180000.0
Obviously the Annual Salary could be displayed in a better format and I
know that it's possible by using formatted output, but I have never used
it before so I will attempt this on my next iteration. What I would
really like to be able to do is have the Attorney List available without
having to pass it in. This would save time, considering there could be
hundreds of lawayers in a large firm.
--
PSK - GG = bad slrn = good
<>
http://www.the-company.com/journal.htm
Huddled in the safety of a Pseudo Silk Kimono