Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

---Member.java-----

public class Member { private String userId; private String password; private String email; private int age; private String gender; private int reading; private int eating; private int sporting; private int traveling; private int music; public Member(){ } public Member(String userId) { this.userId = userId; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getEating() { return eating; } public void setEating(int eating) { this.eating = eating; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getMusic() { return music; } public void setMusic(int music) { this.music = music; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getReading() { return reading; } public void setReading(int reading) { this.reading = reading; } public int getSporting() { return sporting; } public void setSporting(int sporting) { this.sporting = sporting; } public int getTraveling() { return traveling; } public void setTraveling(int traveling) { this.traveling = traveling; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } // This algorithm for this match score is totally experimental.  // The logic here is that if the two members are  // (1) of the opposite gender  // (2) close in age and  // (3) share similar personalities  // then they are a good match.  // The smaller the differences between the two, the better.  // Therefore, the smaller the match score, the better match they are.  // However a negative score means no match.  public double getMatchScore(Member other) { double score = -1.0; if (getGender().equalsIgnoreCase(other.getGender())) { score = -1.0; //no match for the same gender  } else if (getUserId().equalsIgnoreCase(other.getUserId())) { score = -1.0; //no match for oneself  } else { double ageDiff = Math.abs(this.getAge() - other.getAge()); double readingDiff = Math.abs(this.getReading() - other.getReading()); double eatingDiff = Math.abs(this.getEating() - other.getEating()); double sportingDiff = Math.abs(this.getSporting() - other.getSporting()); double travelingDiff = Math.abs(this.getTraveling() - other.getTraveling()); double musicDiff = Math.abs(this.getMusic() - other.getMusic()); double personalityDiff = ageDiff + readingDiff + eatingDiff + sportingDiff + travelingDiff + musicDiff; score = ageDiff * 0.4 + personalityDiff * 0.6; // personality has a bigger weight  } return score; } } -----------------------------------------------

---- MenuHelper.java-----

public class MenuHelper { public static void printHeading() { System.out.println(); System.out.println(); System.out.println(); System.out.println("*****************************"); System.out.println("** Welcome to MatchMaker **"); System.out.println("*****************************"); } public static void printExitScreen() { System.out.println("**************************************"); System.out.println("** Thank you for using MatchMaker **"); System.out.println("** Come back soon! **"); System.out.println("**************************************"); } public static void printLoginMenu() { printHeading(); System.out.println(); System.out.println(); System.out.println("Please log in with your username and password."); } public static void printMainMenu() { printHeading(); System.out.println(); System.out.println(); System.out.println("1. Update Profile"); System.out.println(); System.out.println("2. View All Members"); System.out.println(); System.out.println("3. View Best Match"); System.out.println(); System.out.println("0. Exit"); } public static void printHomeMenu() { printHeading(); System.out.println(); System.out.println(); System.out.println("1. Login"); System.out.println(); System.out.println("2. Join us"); System.out.println(); System.out.println("0. Exit"); } }

-----------------------------------------------------------

------Utils.java-------

 public class Utils { public static String rpad(String stringToPad, char aCharacter, int anInt) { String newString; if (stringToPad.length() >= anInt) return stringToPad; newString = stringToPad; while (newString.length() < anInt) { newString += aCharacter; } return newString; } }

-------------------------------------------------

----- AppConstants.java----

public class AppConstants { public static final String DATA_FILE = "data.txt"; }

---------------------------------------------------------

----------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 members; private Member currentMember; private Scanner keyboard = new Scanner(System.in); public MatchMaker() { members = new ArrayList(); 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("\t"); 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() + "\t" + m.getPassword() + "\t" + m.getEmail() + "\t" + m.getAge() + "\t" + m.getGender() + "\t" + m.getReading() + "\t" + m.getEating() + "\t" + m.getSporting() + "\t" + m.getTraveling() + "\t" + 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.  } 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; 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.  } 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; } }

------------------------------------------------

--------MatchMakerMain------------

public class MatchMakerMain { public static void main(String[] args) { MatchMaker mm = new MatchMaker(); mm.start(); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

5. Recognize your ability to repair and let go of painful conflict

Answered: 1 week ago