Goal: This is a comprehensive project to leverage some major topics we have lear
ID: 3912489 • Letter: G
Question
Goal: This is a comprehensive project to leverage some major topics we have learned in this class. It is designed to help you understand OO design, Java collections, properties files, internationalization, and data persistence using JDBC in a fun setting.
Description: Frequently seen in the technology industry, your colleague John Smith just left your company for a better opportunity. All of John's work is in a Zip file attached with this assignment(files' code below). You are asked to finish his project, "MatchMaker" and make a few improvements on it. MatchMaker is a stand-alone command-line based Java application with a console dialog menu. It uses a simple tab-delimited text file to save member profiles. This application allows its users to register, login, view member profiles, and find the best match based on a proprietary algorithm. Here is what you need to do:
(1) Complete the functionality of "Find the best match" that John has left.
Hints: The following hints may help you with this assignment.
Item #1: Review List collection we learned in Unit 07.
Get java files here:
http://www.mediafire.com/folder/phw5zp7l0huuj/ProjectJavaFiles
Explanation / Answer
I will just add the code of the updated java file MatchMaker.java and the AppConstants.java file which i have created for importing the text file i will also be uploading the sample text file for better understanding.
MatchMaker.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class MatchMaker {
private ArrayList<Member> members;
private Member currentMember;
private Scanner keyboard = new Scanner(System.in);
public MatchMaker() {
members = new ArrayList<Member>();
currentMember = null;
}
public void start() {
loadData();
homeMenuHandler();
}
private void logout() {
saveData();
MenuHelper.printExitScreen();
}
private void loadData() {
BufferedReader inputStream = null;
try {
inputStream =
new BufferedReader(new FileReader(AppConstants.DATA_FILE));
String line = inputStream.readLine( );
while (line != null) {
String[] data = line.split(" ");
Member m = new Member();
m.setUserId(data[0]);
m.setPassword(data[1]);
m.setEmail(data[2]);
m.setAge(Integer.parseInt(data[3]));
m.setGender(data[4]);
m.setReading(Integer.parseInt(data[5]));
m.setEating(Integer.parseInt(data[6]));
m.setSporting(Integer.parseInt(data[7]));
m.setTraveling(Integer.parseInt(data[8]));
m.setMusic(Integer.parseInt(data[9]));
members.add(m);
line = inputStream.readLine( );
}
inputStream.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + AppConstants.DATA_FILE);
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO Exception");
e.printStackTrace();
} finally {
//Close the input and output stream.
//Otherwise the next time this program may not run properly.
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void saveData() {
if (members != null && members.size() != 0) {
PrintWriter outputStream = null;
try {
outputStream =
new PrintWriter(new FileOutputStream(AppConstants.DATA_FILE));
for (int i = 0; i < members.size(); i++) {
Member m = members.get(i);
outputStream.println(
m.getUserId() + " " +
m.getPassword() + " " +
m.getEmail() + " " +
m.getAge() + " " +
m.getGender() + " " +
m.getReading() + " " +
m.getEating() + " " +
m.getSporting() + " " +
m.getTraveling() + " " +
m.getMusic());
}
outputStream.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + AppConstants.DATA_FILE);
e.printStackTrace();
} finally {
//Close the input and output stream.
//Otherwise the next time this program may not run properly.
if (outputStream != null) {
outputStream.close();
}
}
}
}
private void login() {
MenuHelper.printLoginMenu();
System.out.println("Enter username: ");
String username = keyboard.nextLine();
System.out.println("Enter password: ");
String password = keyboard.nextLine();
for (int i = 0; i < members.size(); i++) {
Member m = members.get(i);
if (m.getUserId().equals(username)
&& m.getPassword().equals(password)) {
currentMember = m;
break;
}
}
if (currentMember == null) {
System.out.println("Invalid username/password!");
login();
} else {
mainMenuHandler();
}
}
private void homeMenuHandler() {
MenuHelper.printHomeMenu();
System.out.println("Please select a choice: ");
String choice = keyboard.nextLine();
char c = choice.charAt(0); //only take the first character
switch (c) {
case '1': login();
break;
case '2': register();
break;
case '0': logout();
break;
default: System.out.println("Invalid choice!");
homeMenuHandler();
}
}
private void mainMenuHandler() {
MenuHelper.printMainMenu();
System.out.println("Please select a choice: ");
String choice = keyboard.nextLine();
char c = choice.charAt(0); //only take the first character
switch (c) {
case '1': updateProfile();
break;
case '2': viewMembers();
break;
case '3': viewBestMatch();
break;
case '0': logout();
break;
default: System.out.println("Invalid choice!");
mainMenuHandler();
}
}
private void register() {
System.out.println("Please fill the follwoing info: ");
System.out.println("--------------------------------");
System.out.println("User ID: (max 10 characters) ");
String userId = keyboard.nextLine();
if (userExists(userId)) {
System.out.println("User ID already exists. Please choose another one.");
register();
} else {
System.out.println("Password: (max 10 characters) ");
String password = keyboard.nextLine();
System.out.println("Email address: ");
String email = keyboard.nextLine();
System.out.println("Age: ");
String ageStr = keyboard.nextLine();
int age = Integer.parseInt(ageStr);
System.out.println("Gender: ('F' or 'M') ");
String gender = keyboard.nextLine();
System.out.println();
System.out.println("Tell us about your personality.");
System.out.println("Between 0 (least) to 10 (most), give a score to the following: ");
System.out.println("-------------------------------------------------------------");
System.out.println("Love to read: ");
int read = Integer.parseInt(keyboard.nextLine());
System.out.println("Love to eat: ");
int eat = Integer.parseInt(keyboard.nextLine());
System.out.println("Love to do sport: ");
int sport = Integer.parseInt(keyboard.nextLine());
System.out.println("Love to travel: ");
int travel = Integer.parseInt(keyboard.nextLine());
System.out.println("Love music: ");
int music = Integer.parseInt(keyboard.nextLine());
Member m = new Member();
m.setUserId(userId);
m.setPassword(password);
m.setAge(age);
m.setEmail(email);
m.setGender(gender);
m.setEating(eat);
m.setReading(read);
m.setSporting(sport);
m.setTraveling(travel);
m.setMusic(music);
members.add(m);
System.out.println("Congratulations! You are a member of the Match Maker community.");
login();
}
}
private void updateProfile() {
System.out.println("Password: (max 8 characters) ");
String password = keyboard.nextLine();
System.out.println("Email address: ");
String email = keyboard.nextLine();
System.out.println("Age: ");
String ageStr = keyboard.nextLine();
int age = Integer.parseInt(ageStr);
System.out.println("Gender: ('F' or 'M') ");
String gender = keyboard.nextLine();
System.out.println();
System.out.println("Tell us about your personality.");
System.out.println("Between 0 (least) to 10 (most), give a score to the following: ");
System.out.println("-------------------------------------------------------------");
System.out.println("Love to read: ");
int read = Integer.parseInt(keyboard.nextLine());
System.out.println("Love to eat: ");
int eat = Integer.parseInt(keyboard.nextLine());
System.out.println("Love to do sport: ");
int sport = Integer.parseInt(keyboard.nextLine());
System.out.println("Love to travel: ");
int travel = Integer.parseInt(keyboard.nextLine());
System.out.println("Love music: ");
int music = Integer.parseInt(keyboard.nextLine());
currentMember.setPassword(password);
currentMember.setAge(age);
currentMember.setEmail(email);
currentMember.setGender(gender);
currentMember.setEating(eat);
currentMember.setReading(read);
currentMember.setSporting(sport);
currentMember.setTraveling(travel);
currentMember.setMusic(music);
System.out.println("Thank you! Your profile has been updated.");
mainMenuHandler();
}
private void viewMembers() {
System.out.println("**************************************** Member Profiles ******************************************");
System.out.println();
System.out.println("Username Email Age Gender Reading Eating Sporting Traveling Music");
System.out.println("=============== ======================================== === ====== ======= ====== ======== ========= =====");
for (int i = 0; i < members.size(); i++) {
Member m = members.get(i);
System.out.println(Utils.rpad(m.getUserId(), ' ', 15) + " " +
Utils.rpad(m.getEmail(), ' ', 40) + " " +
Utils.rpad(Integer.toString(m.getAge()), ' ', 3) + " " +
Utils.rpad(m.getGender(), ' ', 6) + " " +
Utils.rpad(Integer.toString(m.getReading()), ' ', 7) + " " +
Utils.rpad(Integer.toString(m.getEating()), ' ', 6) + " " +
Utils.rpad(Integer.toString(m.getSporting()), ' ', 8) + " " +
Utils.rpad(Integer.toString(m.getTraveling()), ' ', 9) + " " +
Utils.rpad(Integer.toString(m.getMusic()), ' ', 5));
}
mainMenuHandler();
}
private void viewBestMatch() {
Member m = getBestMatch();
//System.out.println("*****Best match profile displays here******");
if (m != null) {
System.out.println("Congratulations! Below is your best match profile:");
//TODO: Please insert your code here.
System.out.println(Utils.rpad(m.getUserId(), ' ', 15) + " " +
Utils.rpad(m.getEmail(), ' ', 40) + " " +
Utils.rpad(Integer.toString(m.getAge()), ' ', 3) + " " +
Utils.rpad(m.getGender(), ' ', 6) + " " +
Utils.rpad(Integer.toString(m.getReading()), ' ', 7) + " " +
Utils.rpad(Integer.toString(m.getEating()), ' ', 6) + " " +
Utils.rpad(Integer.toString(m.getSporting()), ' ', 8) + " " +
Utils.rpad(Integer.toString(m.getTraveling()), ' ', 9) + " " +
Utils.rpad(Integer.toString(m.getMusic()), ' ', 5));
} else {
System.out.println("Sorry no match for you at this moment!");
}
mainMenuHandler();
}
//This method will return a Member object or NULL value. NULL means no match.
private Member getBestMatch() {
Member bestMatch = null;
int j = 0;
double bestMatchScore = 999999; // The smaller the better. Negative means no match. 999999 is an unrealistically large number
for (int i = 0; i < members.size(); i++) {
//TODO: Please insert your code here.
double score=currentMember.getMatchScore(members.get(i));
if(score<0) {
continue;
}
if(score<bestMatchScore) {
bestMatchScore=score;
j=i;
}
}
bestMatch = members.get(j);
return bestMatch;
}
private boolean userExists(String userId) {
boolean answer = false;
for (int i = 0; i < members.size(); i++) {
Member m = members.get(i);
if (m.getUserId().equals(userId)) {
answer = true;
break;
}
}
return answer;
}
}
AppConstants.java
public class AppConstants {
public static final String DATA_FILE = "C:\Users\sharm\Desktop\programs\Java\practice\src\matchmaker\AppConstants";
//here it contains the file path with double backslash.
}
sample text file
a b c 1 F 2 3 4 5 6
ank ank ank 2 M 10 9 8 7 6
swati swati swati 2 F 9 8 7 6 5
rubel rubel rubel 3 F 7 4 7 6 5
Please let me know if you need any other help regarding this quesion, according to my understanding you just need to make changes in MatchMaker.java which i have shown and add a new file AppConstants.java like i have shown and add the path in that file and make a text file and add the entries in that, rest other files have no change, let me know if you have any doubts in that :)
Thanks and Regards
Ank
updates :
for second method to include the data file you can do these changes in your AppConstants.java file
import java.net.URL;
public class AppConstants {
static URL path = AppConstants.class.getResource("data.txt");
static String file = path.getFile();
public static final String DATA_FILE = file;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.