Skip to content

Instantly share code, notes, and snippets.

@BenJamminnn
Created September 21, 2013 06:28
Show Gist options
  • Save BenJamminnn/6647774 to your computer and use it in GitHub Desktop.
Save BenJamminnn/6647774 to your computer and use it in GitHub Desktop.
/*
* File: FacePamphlet.java
* -----------------------
* When it is finished, this program will implement a basic social network
* management system.
*/
import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.*;
import javax.swing.text.html.HTMLDocument.Iterator;
public class FacePamphletMain extends Program
implements FacePamphletConstants {
//i vars
public HashMap<String, FacePamphletProfile> profileEntries = new HashMap<String , FacePamphletProfile>();
private GImage pic = null;
public FacePamphletProfile profile;
private JTextField nameField = new JTextField("Enter a name here");
public JTextField statusField = new JTextField();
public JTextField picField = new JTextField();
private JTextField friendField = new JTextField();
private FacePamphletCanvas canvas;
public void init() {
initButtons();
canvas = new FacePamphletCanvas();
add(canvas);
}
public void initButtons() {
add(new JLabel("Name") , NORTH);
add(nameField , NORTH);
add(new JButton("Add") , NORTH);
add(new JButton("Delete") , NORTH);
add(new JButton("Lookup") , NORTH);
add(statusField , WEST);
add(new JButton("Change Status") , WEST);
add(picField , WEST);
add(new JButton("Change picture") , WEST);
add(friendField , WEST);
add(new JButton("Add Friend") , WEST);
addActionListeners();
}
/**
* This class is responsible for detecting when the buttons are
* clicked or interactors are used, so you will have to add code
* to respond to these actions.
*/
public void actionPerformed(ActionEvent e) {
//image = this.profile.getImage();
String fieldStringName = nameField.getText();
String fieldStringStatus = statusField.getText();
String fieldStringPic = picField.getText();
String fieldStringFriend = friendField.getText();
String message = "";
if(e.getActionCommand().equals("Add")) {
if(!profileEntries.containsKey(fieldStringName)) {
ArrayList<String> friendsListMain = new ArrayList<String>();
profile = new FacePamphletProfile(fieldStringName , fieldStringStatus , pic , friendsListMain);
message = ("Add: new profile: " + profile.toString(fieldStringName));
profileEntries.put(fieldStringName , profile); //add this FacePamphlet profile entry to the list of profiles available.
canvas.displayProfile(profile);
canvas.showMessage(message);
} else {
println("Add: profile for " + fieldStringName + " already exists: " + profile.toString(profile.getName()));
}
}
else if(e.getActionCommand().equals("Delete")) {
if(containsProfile(fieldStringName , profileEntries)) {
message = ("Profile of " + fieldStringName + " deleted");
deleteOldFriends(profileEntries.get(fieldStringName)); //remove this friend from others' friends list
profileEntries.remove(fieldStringName); //removes the entry from the hashmap.
canvas.showMessage(message); //need to cut off all friend ties
} else {
println("profile with name " + fieldStringName + " does not exist");
}
}
else if(e.getActionCommand().equals("Lookup")) {
if(profileEntries.containsKey(fieldStringName)) {
message = ("Look up: " + profileEntries.get(fieldStringName).toString(fieldStringName));
profile = profileEntries.get(fieldStringName);
canvas.displayProfile(profileEntries.get(fieldStringName));
canvas.showMessage(message);
} else {
message = ("Lookup: profile with name " + fieldStringName + " does not exist");
canvas.showMessage(message);
}
}
else if(e.getActionCommand().equals("Change Status")) { //changing the status
if(profile != null) {
profile.status = fieldStringStatus;
message = (profile.getName() + " changed status to: " + fieldStringStatus);
} else {
message = ("select a profile to change the status");
}
canvas.displayProfile(profile);
canvas.showMessage(message);
}
else if(e.getActionCommand().equals("Change picture")) { //changing picture
if(containsProfile(profile.getName(), profileEntries)) {
try {
GImage image = new GImage(fieldStringPic);
if(image != null) {
profile = profileEntries.get(fieldStringName);
profile.setImage(image);
message = ("image successfully added!");
canvas.displayProfile(profile);
canvas.showMessage(message);
}
} catch (ErrorException ex) {
println("that is not a valid image name");
}
} else {
message =("please select a profile");
canvas.showMessage(message);
}
}
else if(e.getActionCommand().equals("Add Friend")) {
if(profileEntries.containsKey(fieldStringFriend)) {
if(!alreadyFriends(profileEntries.get(fieldStringFriend) , profile)) { //check if they are already friends
profileEntries.get(fieldStringFriend).friendsList.add(profile.getName()); //add each other as friends, each name is added
profile.friendsList.add(fieldStringFriend); // to the others' arraylist<String>.
message = (fieldStringFriend + " added as a friend");
canvas.displayProfile(profileEntries.get(fieldStringName));
canvas.showMessage(message);
}
} else {
message = ("not a valid friend name or you are already friends");
canvas.showMessage(message);
}
}
}
private boolean containsProfile(String name, HashMap<String, FacePamphletProfile> entries) {
FacePamphletProfile profile = entries.get(name);
if(name.length() > 0 && profile != null) {
return true;
} else {
return false;
}
}
private boolean alreadyFriends(FacePamphletProfile profileOne , FacePamphletProfile profileTwo) {
if(profileOne.friendsList.contains(profileTwo.getName())) {
return true;
}
return false;
}
private void deleteOldFriends(FacePamphletProfile pro) {
String name = pro.getName();
for (Map.Entry<String, FacePamphletProfile> entry : profileEntries.entrySet()) {
FacePamphletProfile value = entry.getValue();
value.friendsList.remove(name);
}
}
}
/*
* File: FacePamphletCanvas.java
* -----------------------------
* This class represents the canvas on which the profiles in the social
* network are displayed. NOTE: This class does NOT need to update the
* display when the window is resized.
*/
import acm.graphics.*;
import java.awt.*;
import java.util.*;
public class FacePamphletCanvas extends GCanvas
implements FacePamphletConstants {
/**
* This method displays a message string near the bottom of the
* canvas.
*/
public void showMessage(String msg) {
GLabel message = new GLabel(msg , getWidth() /2 - 70 , 500 - BOTTOM_MESSAGE_MARGIN);
message.setFont(MESSAGE_FONT);
add(message);
}
/**
* This method displays the given profile on the canvas. The
* canvas is first cleared of all existing items (including
* messages displayed near the bottom of the screen) and then the
* given profile is displayed. The profile display includes the
* name of the user from the profile, the corresponding image
* (or an indication that an image does not exist), the status of
* the user, and a list of the user's friends in the social network.
*/
public void displayProfile(FacePamphletProfile profile) {
removeAll();
addNameHeader(profile); //add the name to this display
addImage(profile);
addStatus(profile);
displayFriends(profile);
}
private void displayFriends(FacePamphletProfile pro) {
GLabel friendsLabel = new GLabel("Friends" , getWidth()/2 , TOP_MARGIN + IMAGE_MARGIN); //Display friends
friendsLabel.setFont(PROFILE_FRIEND_LABEL_FONT);
add(friendsLabel);
GLabel friend;
for(int i = 0; i < pro.friendsList.size(); i++) {
friend = new GLabel(pro.friendsList.get(i) , getWidth()/2, TOP_MARGIN + IMAGE_MARGIN + (i * 20 + 20));
friend.setFont(PROFILE_FRIEND_FONT);
add(friend);
}
}
private void addImageRect() {
GRect rect = new GRect(IMAGE_WIDTH , IMAGE_HEIGHT);
add(rect , LEFT_MARGIN , TOP_MARGIN + IMAGE_MARGIN);
GLabel noImage = new GLabel("No Image" , LEFT_MARGIN + 40, TOP_MARGIN + IMAGE_MARGIN + 100 );
noImage.setFont(PROFILE_IMAGE_FONT);
add(noImage);
}
private void addStatus(FacePamphletProfile pro) {
String status = pro.getStatus();
String noStatus = "No Current Status";
GLabel statusLabel = new GLabel(status);
statusLabel.setFont(PROFILE_STATUS_FONT);
if(status.length() > 0) {
add(statusLabel, LEFT_MARGIN , STATUS_MARGIN + IMAGE_HEIGHT + IMAGE_MARGIN + 50);
} else {
GLabel noStatusLabel = new GLabel(noStatus, LEFT_MARGIN , STATUS_MARGIN + IMAGE_HEIGHT + IMAGE_MARGIN + 50);
noStatusLabel.setFont(PROFILE_STATUS_FONT);
add(noStatusLabel);
}
}
private void addImage(FacePamphletProfile pro) {
GImage image = pro.getImage(); //decide on image and set image accordingly.
if(image == null) {
addImageRect();
} else {
image.setSize(IMAGE_WIDTH, IMAGE_HEIGHT);
add(image , LEFT_MARGIN , TOP_MARGIN + IMAGE_MARGIN);
}
}
private void addNameHeader(FacePamphletProfile pro) {
String proName = pro.getName();
GLabel name = new GLabel(proName , LEFT_MARGIN , TOP_MARGIN);
name.setFont(PROFILE_NAME_FONT);
name.setColor(Color.BLUE);
add(name);
}
}
/*
* File: FacePamphletConstants.java
* --------------------------------
* This file declares several constants that are shared by the
* different modules in the FacePamphlet application. Any class
* that implements this interface can use these constants.
*/
public interface FacePamphletConstants {
/** The width of the application window */
public static final int APPLICATION_WIDTH = 800;
/** The height of the application window */
public static final int APPLICATION_HEIGHT = 500;
/** Number of characters for each of the text input fields */
public static final int TEXT_FIELD_SIZE = 15;
/** Text to be used to create an "empty" label to put space
* between interactors on EAST border of application. Note this
* label is not actually the empty string, but rather a single space */
public static final String EMPTY_LABEL_TEXT = " ";
/** Name of font used to display the application message at the
* bottom of the display canvas */
public static final String MESSAGE_FONT = "Dialog-18";
/** Name of font used to display the name in a user's profile */
public static final String PROFILE_NAME_FONT = "Dialog-24";
/** Name of font used to display the text "No Image" in user
* profiles that do not contain an actual image */
public static final String PROFILE_IMAGE_FONT = "Dialog-24";
/** Name of font used to display the status in a user's profile */
public static final String PROFILE_STATUS_FONT = "Dialog-16-bold";
/** Name of font used to display the label "Friends" above the
* user's list of friends in a profile */
public static final String PROFILE_FRIEND_LABEL_FONT = "Dialog-16-bold";
/** Name of font used to display the names from the user's list
* of friends in a profile */
public static final String PROFILE_FRIEND_FONT = "Dialog-16";
/** The width (in pixels) that profile images should be displayed */
public static final double IMAGE_WIDTH = 200;
/** The height (in pixels) that profile images should be displayed */
public static final double IMAGE_HEIGHT = 200;
/** The number of pixels in the vertical margin between the bottom
* of the canvas display area and the baseline for the message
* text that appears near the bottom of the display */
public static final double BOTTOM_MESSAGE_MARGIN = 20;
/** The number of pixels in the hortizontal margin between the
* left side of the canvas display area and the Name, Image, and
* Status components that are display in the profile */
public static final double LEFT_MARGIN = 20;
/** The number of pixels in the vertical margin between the top
* of the canvas display area and the top (NOT the baseline) of
* the Name component that is displayed in the profile */
public static final double TOP_MARGIN = 20;
/** The number of pixels in the vertical margin between the
* baseline of the Name component and the top of the Image
* displayed in the profile */
public static final double IMAGE_MARGIN = 20;
/** The number of vertical pixels in the vertical margin between
* the bottom of the Image and the top of the Status component
* in the profile */
public static final double STATUS_MARGIN = 20;
}
/*
* File: FacePamphletProfile.java
* ------------------------------
* This class keeps track of all the information for one profile
* in the FacePamphlet social network. Each profile contains a
* name, an image (which may not always be set), a status (what
* the person is currently doing, which may not always be set),
* and a list of friends.
*/
import acm.graphics.*;
import java.awt.Font;
import java.util.*;
public class FacePamphletProfile extends GCanvas implements FacePamphletConstants {
//i vars
public FacePamphletMain main;
public String name;
public String status = "";
public GImage profileImage;
public ArrayList<String> friendsList = new ArrayList<String>(); //Collection of names of friends
public FacePamphletProfile(String name , String status , GImage profileImage , ArrayList<String> friendsList) {
this.name = name;
this.status = status;
this.profileImage = profileImage;
this.friendsList = friendsList;
}
/** This method returns the name associated with the profile. */
public String getName() {
return name;
}
public GImage getImage() {
return profileImage;
}
/** This method sets the image associated with the profile. */
public void setImage(GImage image) {
profileImage = image;
}
// This method returns the status associated with the profile.
public String getStatus() {
// You fill this in. Currently always returns the empty string.
return status;
}
public String ArrayListToString(ArrayList<String> list) {
String listString = "";
if(list.size() == 0) {
return listString;
}
for(int i = 0; i < list.size(); i++) {
if(i == list.size() -1) {
listString += list.get(i);
} else {
listString += list.get(i) + ", ";
}
}
return listString;
}
/**
* In a profile with name "Alice" whose status is
* "coding" and who has friends Don, Chelsea, and Bob, this method
* would return the string: "Alice (coding): Don, Chelsea, Bob"
*/
public String toString(String name) {
String profileString = name + "(" + getStatus() + ") : " + ArrayListToString(friendsList);
return profileString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment