I need help on the following program: Your assignment is to write a program for
ID: 3684566 • Letter: I
Question
I need help on the following program:
Your assignment is to write a program for a computer dating service. Each client gives you his or her name, phone number, and a list of interests. It is your job to maintain lists of men and women using the service and to match up the compatible couples. You will find more details about the assignment, including the inputs, outputs, and deliverables in the course environment.
Input
Data about current clients should be on file "Clients.mf". For each current client, the file contains the following information:
Sex 1 character, 'M' or 'F'
Name up to 20 characters, followed by comma
Phone Number 8 characters
Number of Interests an integer
List of Interests 10 characters each separated by commas with a period after the final interest.
Match up to 20 characters, followed by end-of-line.
From the keyboard: The user may enter on of the following commands.
Command Processing
NEWCLIENT <sex> <name> <phone number><number of interests> <interests> Add the client to the appropriate list by storing the appropriate information. Attempt to match this new client with a member of the opposite sex. A match occurs when the clients have three or more of the same interests. (You only have to find the first match for each new client.) Make sure you then designate both persons as matched, as described in the section on data structures above. Print the name of the new client, the name of his or her match, and both phone numbers. If no match is found, print an appropriate message.
UNMATCH <name> Unmatch this name with his or her current match by removing <name> from the matched person.
PRINTMATCH Print a list of all matched pairs.
PRINTFREE Print the names and phone numbers of clients who are not currently matched.
QUIT Stop processing
Output
Echo print all screen input and output on file "Dates.out."
Data Structures
The problem requires you to maintain two lists of clients, one for men and one for women. When a new client is added to the list, he or she is added to the end of the appropriate list.
Each list element must include all the information for one client: name, phone number, number of interests (maximum number is 10), interests, and the name of this client's current match (empty string if not matched). You must use one of the list classes developed in Chapter 3.
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class DatingService
{
Scanner scanner = new Scanner(new InputStreamReader(System.in));
HashMap map = new HashMap();
void newClient()
{
System.out.println(" Added a new client. ");
}
void unmatchMenu()
{
System.out.println("Unmatch Client Name: ");
String name = scanner.nextLine();
try {
//Whatever the file path is.
File statText = new File("C:/Test/Clients.mf");
if (!statText.exists())
{
System.out.println("No entries to unmatch.");
System.exit(0);
}
else
{
try (BufferedReader br = new BufferedReader(new FileReader(statText))) {
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(" ");
ArrayList data = new ArrayList();
data.add(tokens[1]);
data.add(tokens[2]);
data.add(tokens[3]);
data.add(tokens[4]);
map.put(tokens[0], data);
System.out.println(map.toString());
}
findMatchedName(map, name);
} catch (IOException e) {
e.printStackTrace();
}
statText.delete();
}
FileOutputStream is = new FileOutputStream(statText, true);
OutputStreamWriter osw = new OutputStreamWriter(is);
BufferedWriter w = new BufferedWriter(osw);
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
String key = (String)pair.getKey();
ArrayList<String> userList = (ArrayList)pair.getValue();
w.write(key + " " + userList.get(0) + " " + userList.get(1) + " " + userList.get(2) + " " + userList.get(3));
w.newLine();
}
w.close();
} catch (IOException e) {
System.err.println("Problem writing to the file Clients.mf");
}
System.out.println(" Unmatched the client. ");
}
void findMatchedName(HashMap map, String name)
{
String otherUser = "";
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if (pair.getKey().equals(name))
{
ArrayList<String> userList = (ArrayList)pair.getValue();
String currMatch = userList.get(3);
if (!currMatch.isEmpty())
{
otherUser = currMatch;
userList.set(3, "");
}
}
}
it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if (pair.getKey().equals(otherUser))
{
ArrayList<String> userList = (ArrayList)pair.getValue();
String currMatch = userList.get(3);
if (!currMatch.isEmpty())
{
userList.set(3, "");
}
}
}
}
void printMatchMenu()
{
//Whatever the file path is.
File statText = new File("C:/Test/Clients.mf");
if (!statText.exists())
{
System.out.println("No entries to Print Matches.");
System.exit(0);
}
else
{
try (BufferedReader br = new BufferedReader(new FileReader(statText)))
{
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(" ");
ArrayList data = new ArrayList();
data.add(tokens[1]);
data.add(tokens[2]);
data.add(tokens[3]);
data.add(tokens[4]);
map.put(tokens[0], data);
System.out.println(map.toString());
}
Iterator it = map.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pair = (Map.Entry)it.next();
ArrayList<String> userList = (ArrayList)pair.getValue();
System.out.println(pair.getKey() + "--" + userList.get(3));
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(" List of all matched clients. ");
}
void printFreeMenu()
{
//Whatever the file path is.
File statText = new File("C:/Test/Clients.mf");
if (!statText.exists())
{
System.out.println("No entries to Print Matches.");
System.exit(0);
}
else
{
try (BufferedReader br = new BufferedReader(new FileReader(statText)))
{
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(" ");
ArrayList data = new ArrayList();
data.add(tokens[1]);
data.add(tokens[2]);
data.add(tokens[3]);
data.add(tokens[4]);
map.put(tokens[0], data);
System.out.println(map.toString());
}
Iterator it = map.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pair = (Map.Entry)it.next();
ArrayList<String> userList = (ArrayList)pair.getValue();
if (userList.get(3).equals(""))
{
System.out.println(pair.getKey() + "--" + userList.get(3));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(" List of all unmatched clients. ");
}
void quitMenu()
{
System.out.println(" You selected to Quit. Exiting the program.");
System.exit(0);
}
void newClientMenu()
{
System.out.println("New Client Menu");
System.out.println("--------- ");
System.out.println("Enter Name: ");
String name = scanner.nextLine();
System.out.println("Enter Sex (F/M): ");
String gender = scanner.nextLine();
System.out.println("Enter Number of Interests: ");
String numOfInterests = scanner.nextLine();
System.out.println("Enter List of Interests (comma seprated list): ");
String listofInterests = scanner.nextLine();
try {
//Whatever the file path is.
File statText = new File("C:/Test/Clients.mf");
if (!statText.exists())
{
statText.createNewFile();
}
else
{
try (BufferedReader br = new BufferedReader(new FileReader(statText))) {
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(" ");
ArrayList data = new ArrayList();
data.add(tokens[1]);
data.add(tokens[2]);
data.add(tokens[3]);
data.add(tokens[4]);
map.put(tokens[0], data);
System.out.println(map.toString());
}
String match = findMatch(map, listofInterests);
ArrayList val = (ArrayList) map.get(match);
map.remove(match);
map.put(match, val);
ArrayList newUser = new ArrayList();
newUser.add(gender);
newUser.add(numOfInterests);
newUser.add(listofInterests);
newUser.add(match);
map.put(name, newUser);
} catch (IOException e) {
e.printStackTrace();
}
statText.delete();
}
FileOutputStream is = new FileOutputStream(statText, true);
OutputStreamWriter osw = new OutputStreamWriter(is);
BufferedWriter w = new BufferedWriter(osw);
w.write(" " + name + " " + gender + " " + numOfInterests + " " + listofInterests);
w.newLine();
w.close();
} catch (IOException e) {
System.err.println("Problem writing to the file Clients.mf");
}
System.out.println(" Added a new client. ");
}
String findMatch(HashMap map, String listofInterests)
{
boolean founMatch = false;
String returnMatch = "";
String[] tokens = listofInterests.split(",");
ArrayList<String> newIntList = (ArrayList)Arrays.asList(tokens);
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
ArrayList<String> currentUserList = (ArrayList)pair.getValue();
String currMatch = currentUserList.get(3);
if (currMatch.isEmpty())
{
String currInterests = currentUserList.get(2);
String[] tokens2 = currInterests.split(",");
ArrayList<String> currIntList = (ArrayList)Arrays.asList(tokens2);
int counter = 0;
for (String newUserInt : newIntList)
{
if (currIntList.contains(newUserInt))
{
counter++;
}
if (counter >= 3)
{
returnMatch = (String)pair.getKey();
}
}
}
}
return returnMatch;
}
void mainMenu()
{
System.out.println("Main Menu");
System.out.println("--------- ");
System.out.println("1. NEW CLIENT");
System.out.println("2. UNMATCH");
System.out.println("3. PRINT MATCH");
System.out.println("4. PRINT FREE");
System.out.println("5. QUIT");
System.out.println(" Please select an option from the above list (enter number 1,2,3,4,5): ");
String selection = scanner.nextLine();
map.clear();
if (isNumeric(selection))
{
int option = Integer.parseInt(selection);
if (option < 1 || option > 5)
{
System.out.println(" Invalid entry. Exiting the program.");
System.exit(0);
}
switch (option)
{
case 1:
newClientMenu();
break;
case 2:
unmatchMenu();
break;
case 3:
printMatchMenu();
break;
case 4:
printFreeMenu();
break;
case 5:
quitMenu();
break;
default:
break;
}
}
else
{
System.out.println(" Invalid entry. Exiting the program.");
System.exit(0);
}
}
boolean isNumeric(String str)
{
return str.matches("-?\d+(.\d+)?");
}
@SuppressWarnings("resource")
public static void main(String args[])
{
DatingService obj = new DatingService();
while (true)
{
obj.mainMenu();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.