The following is code from a university practical I am doing. It reads in a txt file of twenty clients, whose information is stored in the txt file like this:
Sophia Candappa F 23 00011
As per my lecturer's instructions, I have stored this information in a class called Client (although I know an ArrayList would be better I can't use it).
The code below is a method that is used to compare all the clients to one another and determine if they are a match. They are a match if they are all of the following:
- Opposite sex
- Age within five years of one another
- They have three interests in common
The latter is determined by the string "00011" in the example above. If the clients share the number "1" at the same place in the string on three or more occasions, then the third condition is satisfied.
My code works perfectly and outputs the desired result. However, I want to ask two questions.
Is it as efficient as it could be (without ArrayLists)? I had considered separating out all the if/else statements into separate methods, but decided against it as I thought it wouldn't reduce any of the actual loops.
How can I change the output slightly. Currently, if a client is matched it displays "[Client Name] is compatible with" then it takes a new line and outputs all the clients who are matched. I would like to change it so that if the client has just one match, it says "Client Name is compatible with"..., but if the client has two or more clients it says "Client Name is compatible with the following [two/three/four] clients...
I have tried doing the latter, but I always mess up the formatting. Thanks in advance for any help that can be offered.
public static void matchClients(Client[] clientDetails)
{
boolean anyMatch;
int count;
for (int b = 0; b < numberOfClients; b++)
{
anyMatch = false;
count = 0;
for (int c = 0; c < numberOfClients; c++)
{
if (clientDetails[b].getClientGender()!=clientDetails[c].getClientGender())
{
if (Math.abs(clientDetails[b].getClientAge() - clientDetails[c].getClientAge()) <= 5)
{
int interests = 0;
String clientOneInterests = clientDetails[b].getClientInterests();
String clientTwoInterests = clientDetails[c].getClientInterests();
int interestNumber = 0;
while (interestNumber < clientOneInterests.length())
{
if ((clientOneInterests.charAt(interestNumber) == clientTwoInterests.charAt(interestNumber))
&& (clientOneInterests.charAt(interestNumber) == '1' ))
interests++;
interestNumber++;
}
if (interests >= 3)
{
anyMatch = true;
if (count == 0)
{
System.out.println(clientDetails[b].getClientName() + "is compatible with the following client(s)");
System.out.println("\t" + clientDetails[c].getClientName());
}
else
{
System.out.println("\t" + clientDetails[c].getClientName());
}
count++;
}
interests = 0;
}
}
}
if (anyMatch == false)
System.out.println(clientDetails[b].getClientName() + "is not compatible with any client.");
System.out.println("");
}
}