📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
查看: 3913| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

Stanford CS106A #7 Assignment7 讨论帖

全局:
时间:2012.8.22-2012.8.31

课程:
23. Searching and sorting
24. programming in the large data
25. additional topics
26. Stanford Java
27. life after CS106A
28. Review for final exam


作业:
Assignment 7 FacePamphlet.java
due: 2012.8.31

单选投票, 共有 13 人参与投票
您所在的用户组没有投票权限

评分

参与人数 1大米 +3 收起 理由
LLLLLin + 3 给你点个赞!

查看全部评分


上一篇:Stanford CS106A #6 Assignment6讨论帖
下一篇:求推荐关于data structure的公开课和教材
🔗
zhuolinliu8 2012-8-18 11:30:54 | 只看该作者
全局:
FacePamphlet.java
  1. /*
  2. * File: FacePamphlet.java
  3. * -----------------------
  4. * When it is finished, this program will implement a basic social network
  5. * management system.
  6. */

  7. import acm.program.*;
  8. import acm.graphics.*;
  9. import acm.util.*;
  10. import java.awt.event.*;
  11. import javax.swing.*;

  12. public class FacePamphlet extends Program
  13.                                         implements FacePamphletConstants {

  14.         /**
  15.          * This method has the responsibility for initializing the
  16.          * interactors in the application, and taking care of any other
  17.          * initialization that needs to be performed.
  18.          */
  19.         public void init() {
  20.                 /* Initialzing the interactors */
  21.                 add(new JLabel("Name"),NORTH);
  22.                 input_name.addActionListener(this);
  23.                 add(input_name,NORTH);
  24.                
  25.                 add(new JButton("Add"),NORTH);
  26.                 add(new JButton("Delete"),NORTH);
  27.                 add(new JButton("Lookup"),NORTH);
  28.                
  29.                 /* West */
  30.                 change_status.setActionCommand("Change Status");
  31.                 change_status.addActionListener(this);
  32.                 add(change_status,WEST);
  33.                 add(new JButton("Change Status"),WEST);
  34.                
  35.                 add(new JLabel(EMPTY_LABEL_TEXT),WEST);
  36.                 change_picture.setActionCommand("Change Picture");
  37.                 change_picture.addActionListener(this);
  38.                 add(change_picture,WEST);
  39.                 add(new JButton("Change Picture"),WEST);
  40.                
  41.                 add(new JLabel(EMPTY_LABEL_TEXT),WEST);
  42.                 add_friend.setActionCommand("Add Friend");
  43.                 add_friend.addActionListener(this);
  44.                 add(add_friend,WEST);
  45.                 add(new JButton("Add Friend"),WEST);
  46.                 addActionListeners();
  47.                
  48.                 /* Canvas and Database */
  49.                 fpdata = new FacePamphletDatabase();
  50.                 canvas = new FacePamphletCanvas();
  51.                 add(canvas);
  52.     }
  53.    
  54.   
  55.     /**
  56.      * This class is responsible for detecting when the buttons are
  57.      * clicked or interactors are used, so you will have to add code
  58.      * to respond to these actions.
  59.      */
  60.     public void actionPerformed(ActionEvent e) {
  61.                 String cmd = e.getActionCommand();
  62.                
  63.                 if(cmd.equals("Add")){
  64.                         String text = input_name.getText();
  65.                         if(text != null){
  66.                                 boolean f = true;
  67.                                 if(fpdata.containsProfile(text)) f = false;
  68.                                 current_profile = new FacePamphletProfile(text);
  69.                                 fpdata.addProfile(current_profile);
  70.                                 canvas.displayProfile(current_profile);
  71.                                 if(!f)
  72.                                         canvas.showMessage("A profile with the name "+text+" already exists");
  73.                                 else canvas.showMessage("New profile created");

  74.                         }
  75.                        
  76.                 }else if(cmd.equals("Delete")){
  77.                         String text = input_name.getText();
  78.                         if(text != null) {
  79.                                 canvas.removeAll();
  80.                                 if(fpdata.containsProfile(text)){
  81.                                         canvas.showMessage("Profile of "+ text+" deleted");
  82.                                         current_profile = null;
  83.                                 }
  84.                                 else canvas.showMessage("A profile with the name "+ text+" does not exist");
  85.                                 fpdata.deleteProfile(text);
  86.                         }
  87.                        
  88.                 }else if(cmd.equals("Lookup")){
  89.                        
  90.                         String text = input_name.getText();
  91.                         if(text != null){
  92.                                 if(fpdata.containsProfile(text))
  93.                                 {
  94.                                         current_profile = fpdata.getProfile(text);
  95.                                         canvas.displayProfile(current_profile);
  96.                                         canvas.showMessage("Displaying " + current_profile.getName());
  97.                                 }else {
  98.                                         canvas.removeAll();
  99.                                         current_profile = null;
  100.                                         canvas.showMessage(text +" does not exist");
  101.                                 }
  102.                         }
  103.                        
  104.                 }else if(cmd.equals("Change Status")){
  105.                        
  106.                         String text = change_status.getText();
  107.                         if(text != null)
  108.                                 if(current_profile != null) {
  109.                                         current_profile.setStatus(text);
  110.                                         canvas.displayProfile(current_profile);
  111.                                         canvas.showMessage("Status updated to " + text);
  112.                                 }else {
  113.                                         canvas.removeAll();
  114.                                         canvas.showMessage("Please select a profile to change status");
  115.                                 }
  116.                        
  117.                 }else if(cmd.equals("Change Picture")){
  118.                        
  119.                         String text = change_picture.getText();
  120.                         if(text != null)
  121.                                 if(current_profile != null) {
  122.                                         GImage image = null;
  123.                                         try{
  124.                                                 image = new GImage(text);
  125.                                                 current_profile.setImage(image);
  126.                                                 canvas.displayProfile(current_profile);
  127.                                                 canvas.showMessage("Picture updated");
  128.                                                
  129.                                         }catch(ErrorException ex){
  130.                                                 canvas.showMessage("Unable to open image file: "+ text);       
  131.                                         };

  132.                                 }else {
  133.                                         canvas.removeAll();
  134.                                         canvas.showMessage("Please select a profile to change picture");
  135.                                 }
  136.                        
  137.                 }else if(cmd.equals("Add Friend")){
  138.                        
  139.                         String text = add_friend.getText();
  140.                         int f = 0;
  141.                         if(text != null)
  142.                                 if(current_profile != null){
  143.                                         if(fpdata.containsProfile(text)){
  144.                                                 if(!current_profile.addFriend(text))
  145.                                                                 f = 1;
  146.                                                 else if(!fpdata.getProfile(text).addFriend(current_profile.getName()))
  147.                                                         f = 2;
  148.                                                
  149.                                                 canvas.displayProfile(current_profile);
  150.                                                
  151.                                                 if(f == 1)
  152.                                                         canvas.showMessage(current_profile.getName()+" already has " +
  153.                                                                         text + "as a friend");
  154.                                                 else if(f == 2)
  155.                                                         canvas.showMessage(text +" already has " +
  156.                                                                         current_profile.getName() + "as a friend");
  157.                                                 else
  158.                                                         canvas.showMessage(text +" added as a friend");
  159.                                         }else{
  160.                                                 canvas.showMessage(text + " does not exist");
  161.                                         }
  162.                                 }else {
  163.                                         canvas.removeAll();
  164.                                         canvas.showMessage("Please select a profile to add friend");
  165.                                 }
  166.                 }
  167.         }
  168.    
  169.     /** TextField initiate */
  170.         private JTextField input_name = new JTextField(TEXT_FIELD_SIZE);
  171.         private JTextField change_status = new JTextField(TEXT_FIELD_SIZE);
  172.         private JTextField change_picture = new JTextField(TEXT_FIELD_SIZE);
  173.         private JTextField add_friend = new JTextField(TEXT_FIELD_SIZE);
  174.        
  175.         private FacePamphletDatabase fpdata;
  176.         private FacePamphletProfile current_profile;
  177.         private FacePamphletCanvas canvas;

  178. }
复制代码
FacePamphletCanvas.java
  1. /*
  2. * File: FacePamphletCanvas.java
  3. * -----------------------------
  4. * This class represents the canvas on which the profiles in the social
  5. * network are displayed.  NOTE: This class does NOT need to update the
  6. * display when the window is resized.
  7. */


  8. import acm.graphics.*;
  9. import java.awt.*;
  10. import java.util.*;

  11. public class FacePamphletCanvas extends GCanvas
  12.                                         implements FacePamphletConstants {
  13.        
  14.         /**
  15.          * Constructor
  16.          * This method takes care of any initialization needed for
  17.          * the display
  18.          */
  19.         public FacePamphletCanvas() {
  20.         }

  21.        
  22.         /**
  23.          * This method displays a message string near the bottom of the
  24.          * canvas.  Every time this method is called, the previously
  25.          * displayed message (if any) is replaced by the new message text
  26.          * passed in.
  27.          */
  28.         public void showMessage(String msg) {
  29.                 GLabel ms= new GLabel(msg,getWidth()/2,getHeight()-BOTTOM_MESSAGE_MARGIN);
  30.                 ms.move(-ms.getWidth()/2, 0);
  31.                 ms.setFont(MESSAGE_FONT);
  32.                 if(getElementAt(getWidth()/2,getHeight()-BOTTOM_MESSAGE_MARGIN)!= null)
  33.                         remove(getElementAt(getWidth()/2,getHeight()-BOTTOM_MESSAGE_MARGIN));
  34.                 add(ms);
  35.         }
  36.        
  37.        
  38.         /**
  39.          * This method displays the given profile on the canvas.  The
  40.          * canvas is first cleared of all existing items (including
  41.          * messages displayed near the bottom of the screen) and then the
  42.          * given profile is displayed.  The profile display includes the
  43.          * name of the user from the profile, the corresponding image
  44.          * (or an indication that an image does not exist), the status of
  45.          * the user, and a list of the user's friends in the social network.
  46.          */
  47.         public void displayProfile(FacePamphletProfile profile) {
  48.                 removeAll();
  49.                
  50.                 /* Name */
  51.                 GLabel name = new GLabel(profile.getName(),LEFT_MARGIN,TOP_MARGIN);
  52.                 name.setFont(PROFILE_NAME_FONT);
  53.                 name.setColor(Color.BLUE);
  54.                 add(name);
  55.                
  56.                 /* Picture */
  57.                 add(new GRect(LEFT_MARGIN,TOP_MARGIN+IMAGE_MARGIN,IMAGE_WIDTH,IMAGE_HEIGHT));
  58.                 if(profile.getImage() == null){
  59.                         GLabel nopic = new GLabel("No Image", LEFT_MARGIN+IMAGE_WIDTH/2,
  60.                                         TOP_MARGIN+IMAGE_MARGIN+IMAGE_HEIGHT/2);
  61.                         nopic.setFont(PROFILE_IMAGE_FONT);
  62.                         nopic.move(-nopic.getWidth()/2, -nopic.getAscent()/2);
  63.                         add(nopic);
  64.                 }else {
  65.                           GImage pic = profile.getImage();
  66.                           pic.scale(IMAGE_WIDTH/pic.getWidth(),IMAGE_HEIGHT/pic.getHeight());
  67.                           add(pic,LEFT_MARGIN,TOP_MARGIN+IMAGE_MARGIN);
  68.                 }
  69.                
  70.                 /* Status */
  71.                 String status = profile.getName()+" is "+profile.getStatus();
  72.                 if( profile.getStatus() == "" ) status = "No current status";
  73.                 GLabel st = new GLabel(status,
  74.                                 LEFT_MARGIN,TOP_MARGIN+IMAGE_MARGIN+IMAGE_HEIGHT+STATUS_MARGIN);
  75.                 st.setFont(PROFILE_STATUS_FONT);
  76.                 add(st);
  77.                
  78.                 /* Friends */
  79.                 double x =getWidth()/2, y=TOP_MARGIN+IMAGE_MARGIN;
  80.                 GLabel friend_label = new GLabel("Friends:",x,y);
  81.                 friend_label.setFont(PROFILE_FRIEND_LABEL_FONT);
  82.                 add(friend_label);
  83.                
  84.                 Iterator<String> it = profile.getFriends();
  85.                 while(it.hasNext()){
  86.                         GLabel friend = new GLabel(it.next(),x,y);
  87.                         y += friend.getHeight();
  88.                         friend.move(0,friend.getHeight());
  89.                         friend.setFont(PROFILE_FRIEND_FONT);
  90.                         add(friend);
  91.                 }
  92.                
  93.         }
  94.        
  95. }
复制代码
File: FacePamphletConstants.java
  1. /*
  2. * File: FacePamphletConstants.java
  3. * --------------------------------
  4. * This file declares several constants that are shared by the
  5. * different modules in the FacePamphlet application.  Any class
  6. * that implements this interface can use these constants.
  7. */

  8. public interface FacePamphletConstants {

  9.         /** The width of the application window */
  10.         public static final int APPLICATION_WIDTH = 800;

  11.         /** The height of the application window */
  12.         public static final int APPLICATION_HEIGHT = 500;

  13.         /** Number of characters for each of the text input fields */
  14.         public static final int TEXT_FIELD_SIZE = 15;

  15.         /** Text to be used to create an "empty" label to put space
  16.          *  between interactors on EAST border of application.  Note this
  17.          *  label is not actually the empty string, but rather a single space */
  18.         public static final String EMPTY_LABEL_TEXT = " ";

  19.         /** Name of font used to display the application message at the
  20.          *  bottom of the display canvas */
  21.         public static final String MESSAGE_FONT = "Dialog-18";

  22.         /** Name of font used to display the name in a user's profile */
  23.         public static final String PROFILE_NAME_FONT = "Dialog-24";
  24.        
  25.         /** Name of font used to display the text "No Image" in user
  26.          *  profiles that do not contain an actual image */
  27.         public static final String PROFILE_IMAGE_FONT = "Dialog-24";
  28.        
  29.         /** Name of font used to display the status in a user's profile */
  30.         public static final String PROFILE_STATUS_FONT = "Dialog-16-bold";

  31.         /** Name of font used to display the label "Friends" above the
  32.          *  user's list of friends in a profile */
  33.         public static final String PROFILE_FRIEND_LABEL_FONT = "Dialog-16-bold";

  34.         /** Name of font used to display the names from the user's list
  35.          *  of friends in a profile */
  36.         public static final String PROFILE_FRIEND_FONT = "Dialog-16";

  37.         /** The width (in pixels) that profile images should be displayed */
  38.         public static final double IMAGE_WIDTH = 200;

  39.         /** The height (in pixels) that profile images should be displayed */
  40.         public static final double IMAGE_HEIGHT = 200;       

  41.         /** The number of pixels in the vertical margin between the bottom
  42.          *  of the canvas display area and the baseline for the message
  43.          *  text that appears near the bottom of the display */
  44.         public static final double BOTTOM_MESSAGE_MARGIN = 20;

  45.         /** The number of pixels in the hortizontal margin between the
  46.          *  left side of the canvas display area and the Name, Image, and
  47.          *  Status components that are display in the profile */       
  48.         public static final double LEFT_MARGIN = 20;       

  49.         /** The number of pixels in the vertical margin between the top
  50.          *  of the canvas display area and the top (NOT the baseline) of
  51.          *  the Name component that is displayed in the profile */       
  52.         public static final double TOP_MARGIN = 30;       
  53.        
  54.         /** The number of pixels in the vertical margin between the
  55.          *  baseline of the Name component and the top of the Image
  56.          *  displayed in the profile */       
  57.         public static final double IMAGE_MARGIN = 20;

  58.         /** The number of vertical pixels in the vertical margin between
  59.          *  the bottom of the Image and the top of the Status component
  60.          *  in the profile */               
  61.         public static final double STATUS_MARGIN = 20;

  62. }

复制代码
FacePamphletDatabase.java
  1. /*
  2. * File: FacePamphletDatabase.java
  3. * -------------------------------
  4. * This class keeps track of the profiles of all users in the
  5. * FacePamphlet application.  Note that profile names are case
  6. * sensitive, so that "ALICE" and "alice" are NOT the same name.
  7. */

  8. import java.util.*;

  9. public class FacePamphletDatabase implements FacePamphletConstants {

  10.         /**
  11.          * Constructor
  12.          * This method takes care of any initialization needed for
  13.          * the database.
  14.          */
  15.         public FacePamphletDatabase() {
  16.                  database = new HashMap<String,FacePamphletProfile>();
  17.         }
  18.        
  19.        
  20.         /**
  21.          * This method adds the given profile to the database.  If the
  22.          * name associated with the profile is the same as an existing
  23.          * name in the database, the existing profile is replaced by
  24.          * the new profile passed in.
  25.          */
  26.         public void addProfile(FacePamphletProfile profile) {
  27.                 if(database.containsKey(profile.getName()))
  28.                                 database.remove(profile.getName());
  29.                 database.put(profile.getName(), profile);                       
  30.         }

  31.        
  32.         /**
  33.          * This method returns the profile associated with the given name
  34.          * in the database.  If there is no profile in the database with
  35.          * the given name, the method returns null.
  36.          */
  37.         public FacePamphletProfile getProfile(String name) {
  38.                 if(database.containsKey(name))
  39.                         return database.get(name);
  40.                 return null;
  41.         }
  42.        
  43.        
  44.         /**
  45.          * This method removes the profile associated with the given name
  46.          * from the database.  It also updates the list of friends of all
  47.          * other profiles in the database to make sure that this name is
  48.          * removed from the list of friends of any other profile.
  49.          *
  50.          * If there is no profile in the database with the given name, then
  51.          * the database is unchanged after calling this method.
  52.          */
  53.         public void deleteProfile(String name) {
  54.                 if(database.containsKey(name)){
  55.                         /* Remove name from friend list */       
  56.                         Iterator<String> it = database.get(name).getFriends();
  57.                         while(it.hasNext()) database.get(it.next()).removeFriend(name);
  58.                         /* Remove profile */
  59.                         database.remove(name);
  60.                 }
  61.         }



  62.         /**
  63.          * This method returns true if there is a profile in the database
  64.          * that has the given name.  It returns false otherwise.
  65.          */
  66.         public boolean containsProfile(String name) {
  67.                 if(database.containsKey(name))
  68.                         return true;
  69.                 return false;
  70.         }
  71.        
  72.         private HashMap<String, FacePamphletProfile> database;
  73. }
复制代码
FacePamphletProfile.java
  1. /*
  2. * File: FacePamphletProfile.java
  3. * ------------------------------
  4. * This class keeps track of all the information for one profile
  5. * in the FacePamphlet social network.  Each profile contains a
  6. * name, an image (which may not always be set), a status (what
  7. * the person is currently doing, which may not always be set),
  8. * and a list of friends.
  9. */

  10. import acm.graphics.*;
  11. import java.util.*;

  12. public class FacePamphletProfile implements FacePamphletConstants {
  13.        
  14.         /**
  15.          * Constructor
  16.          * This method takes care of any initialization needed for
  17.          * the profile.
  18.          */
  19.         public FacePamphletProfile(String name) {
  20.                 this.name = name;
  21.                 this.status = "";
  22.                 this.friends = new ArrayList<String>();
  23.         }

  24.         /** This method returns the name associated with the profile. */
  25.         public String getName() {
  26.                 return this.name;
  27.         }

  28.         /**
  29.          * This method returns the image associated with the profile.  
  30.          * If there is no image associated with the profile, the method
  31.          * returns null. */
  32.         public GImage getImage() {
  33.                 return this.picture;
  34.         }

  35.         /** This method sets the image associated with the profile. */
  36.         public void setImage(GImage image) {
  37.                 this.picture = image;
  38.         }
  39.        
  40.         /**
  41.          * This method returns the status associated with the profile.
  42.          * If there is no status associated with the profile, the method
  43.          * returns the empty string ("").
  44.          */
  45.         public String getStatus() {
  46.                 return this.status;
  47.         }
  48.        
  49.         /** This method sets the status associated with the profile. */
  50.         public void setStatus(String status) {
  51.                 this.status = status;
  52.         }

  53.         /**
  54.          * This method adds the named friend to this profile's list of
  55.          * friends.  It returns true if the friend's name was not already
  56.          * in the list of friends for this profile (and the name is added
  57.          * to the list).  The method returns false if the given friend name
  58.          * was already in the list of friends for this profile (in which
  59.          * case, the given friend name is not added to the list of friends
  60.          * a second time.)
  61.          */
  62.         public boolean addFriend(String friend) {
  63.                 if(this.friends.contains(friend)) return false;
  64.                 this.friends.add(friend);
  65.                 return true;
  66.         }

  67.         /**
  68.          * This method removes the named friend from this profile's list
  69.          * of friends.  It returns true if the friend's name was in the
  70.          * list of friends for this profile (and the name was removed from
  71.          * the list).  The method returns false if the given friend name
  72.          * was not in the list of friends for this profile (in which case,
  73.          * the given friend name could not be removed.)
  74.          */
  75.         public boolean removeFriend(String friend) {
  76.                 if(this.friends.contains(friend)) return this.friends.remove(friend);
  77.                 return false;
  78.         }

  79.         /**
  80.          * This method returns an iterator over the list of friends
  81.          * associated with the profile.
  82.          */
  83.         public Iterator<String> getFriends() {
  84.                 return this.friends.iterator();
  85.         }
  86.        
  87.         /**
  88.          * This method returns a string representation of the profile.  
  89.          * This string is of the form: "name (status): list of friends",
  90.          * where name and status are set accordingly and the list of
  91.          * friends is a comma separated list of the names of all of the
  92.          * friends in this profile.
  93.          *
  94.          * For example, in a profile with name "Alice" whose status is
  95.          * "coding" and who has friends Don, Chelsea, and Bob, this method
  96.          * would return the string: "Alice (coding): Don, Chelsea, Bob"
  97.          */
  98.         public String toString() {
  99.                 String result = name + " (" + status + "): ";
  100.                 Iterator<String> it = this.getFriends();
  101.                 if(it.hasNext()) result +=it.next();
  102.                 while(it.hasNext()){result = result + ", " +it.next();}
  103.                 return result;
  104.         }
  105.        
  106.         /* Person's information */
  107.         private String name, status;
  108.         private ArrayList<String> friends;
  109.         private GImage picture = null;
  110.        
  111. }
复制代码
回复

使用道具 举报

🔗
wangtieguo 2012-8-18 12:01:41 | 只看该作者
全局:
好东西,值得学习~
回复

使用道具 举报

🔗
jasonilove 2012-8-18 13:52:05 | 只看该作者
全局:
FacePamphletProfile
  1. /*
  2. * File: FacePamphletProfile.java
  3. * ------------------------------
  4. * This class keeps track of all the information for one profile
  5. * in the FacePamphlet social network.  Each profile contains a
  6. * name, an image (which may not always be set), a status (what
  7. * the person is currently doing, which may not always be set),
  8. * and a list of friends.
  9. */

  10. import acm.graphics.*;
  11. import java.util.*;

  12. public class FacePamphletProfile implements FacePamphletConstants {
  13.        
  14.         /**
  15.          * Constructor
  16.          * This method takes care of any initialization needed for
  17.          * the profile.
  18.          */
  19.         public FacePamphletProfile(String name) {
  20.                 nameOfProfile=name;
  21.                 statusOfProfile=null;
  22.                 imageOfProfile=null;
  23.                 friendsOfProfile=new ArrayList<String>();
  24.                
  25.         }

  26.         /** This method returns the name associated with the profile. */
  27.         public String getName() {
  28.                 return nameOfProfile;
  29.         }

  30.         /**
  31.          * This method returns the image associated with the profile.  
  32.          * If there is no image associated with the profile, the method
  33.          * returns null. */
  34.         public GImage getImage() {
  35.                 return imageOfProfile;
  36.         }

  37.         /** This method sets the image associated with the profile. */
  38.         public void setImage(GImage image) {
  39.                 imageOfProfile=image;
  40.         }
  41.        
  42.         /**
  43.          * This method returns the status associated with the profile.
  44.          * If there is no status associated with the profile, the method
  45.          * returns the empty string ("").
  46.          */
  47.         public String getStatus() {
  48.                 return statusOfProfile;
  49.         }
  50.        
  51.         /** This method sets the status associated with the profile. */
  52.         public void setStatus(String status) {
  53.                 statusOfProfile=status;
  54.         }

  55.         /**
  56.          * This method adds the named friend to this profile's list of
  57.          * friends.  It returns true if the friend's name was not already
  58.          * in the list of friends for this profile (and the name is added
  59.          * to the list).  The method returns false if the given friend name
  60.          * was already in the list of friends for this profile (in which
  61.          * case, the given friend name is not added to the list of friends
  62.          * a second time.)
  63.          */
  64.         public boolean addFriend(String friend) {
  65.                 if(friendsOfProfile.contains(friend)){
  66.                         return false;
  67.                 }
  68.                 else{
  69.                         friendsOfProfile.add(friend);
  70.                         return true;
  71.                 }
  72.         }

  73.         /**
  74.          * This method removes the named friend from this profile's list
  75.          * of friends.  It returns true if the friend's name was in the
  76.          * list of friends for this profile (and the name was removed from
  77.          * the list).  The method returns false if the given friend name
  78.          * was not in the list of friends for this profile (in which case,
  79.          * the given friend name could not be removed.)
  80.          */
  81.         public boolean removeFriend(String friend) {
  82.                 if(friendsOfProfile.contains(friend)){
  83.                         friendsOfProfile.remove(friend);
  84.                         return true;
  85.                 }
  86.                 else{
  87.                         return false;
  88.                 }
  89.                
  90.         }

  91.         /**
  92.          * This method returns an iterator over the list of friends
  93.          * associated with the profile.
  94.          */
  95.         public Iterator<String> getFriends() {
  96.                 //if(friendsOfProfile.size()==0){
  97.                         //return null;
  98.                 //}
  99.                 return friendsOfProfile.iterator();
  100.         }
  101.        
  102.         /**
  103.          * This method returns a string representation of the profile.  
  104.          * This string is of the form: "name (status): list of friends",
  105.          * where name and status are set accordingly and the list of
  106.          * friends is a comma separated list of the names of all of the
  107.          * friends in this profile.
  108.          *
  109.          * For example, in a profile with name "Alice" whose status is
  110.          * "coding" and who has friends Don, Chelsea, and Bob, this method
  111.          * would return the string: "Alice (coding): Don, Chelsea, Bob"
  112.          */
  113.         public String toString() {
  114.                 String friends="";
  115.                 for(String name:friendsOfProfile){
  116.                         friends=friends+" "+name;
  117.                 }
  118.                 return nameOfProfile+" ("+statusOfProfile+") :"+friends;
  119.         }
  120.        
  121.         private String nameOfProfile;
  122.         private String statusOfProfile;
  123.         private GImage imageOfProfile;
  124.         private ArrayList<String> friendsOfProfile;
  125. }
复制代码
FacePamphletDatabase
  1. /*
  2. * File: FacePamphletDatabase.java
  3. * -------------------------------
  4. * This class keeps track of the profiles of all users in the
  5. * FacePamphlet application.  Note that profile names are case
  6. * sensitive, so that "ALICE" and "alice" are NOT the same name.
  7. */

  8. import java.util.*;

  9. public class FacePamphletDatabase implements FacePamphletConstants {

  10.         /**
  11.          * Constructor
  12.          * This method takes care of any initialization needed for
  13.          * the database.
  14.          */
  15.         public FacePamphletDatabase() {
  16.                
  17.         }
  18.        
  19.        
  20.         /**
  21.          * This method adds the given profile to the database.  If the
  22.          * name associated with the profile is the same as an existing
  23.          * name in the database, the existing profile is replaced by
  24.          * the new profile passed in.
  25.          */
  26.         public void addProfile(FacePamphletProfile profile) {
  27.                 if(dataBase.containsKey(profile.getName())){
  28.                         dataBase.remove(profile.getName());
  29.                 }
  30.                 dataBase.put(profile.getName(), profile);
  31.         }

  32.        
  33.         /**
  34.          * This method returns the profile associated with the given name
  35.          * in the database.  If there is no profile in the database with
  36.          * the given name, the method returns null.
  37.          */
  38.         public FacePamphletProfile getProfile(String name) {
  39.                 return dataBase.get(name);
  40.         }
  41.        
  42.        
  43.         /**
  44.          * This method removes the profile associated with the given name
  45.          * from the database.  It also updates the list of friends of all
  46.          * other profiles in the database to make sure that this name is
  47.          * removed from the list of friends of any other profile.
  48.          *
  49.          * If there is no profile in the database with the given name, then
  50.          * the database is unchanged after calling this method.
  51.          */
  52.         public void deleteProfile(String name) {
  53.                 dataBase.remove(name);
  54.         }

  55.        
  56.         /**
  57.          * This method returns true if there is a profile in the database
  58.          * that has the given name.  It returns false otherwise.
  59.          */
  60.         public boolean containsProfile(String name) {
  61.                 return dataBase.containsKey(name);
  62.         }
  63.        
  64.         private HashMap<String,FacePamphletProfile> dataBase=new HashMap<String,FacePamphletProfile>();

  65. }
复制代码
FacePamphletConstants
  1. /*
  2. * File: FacePamphletConstants.java
  3. * --------------------------------
  4. * This file declares several constants that are shared by the
  5. * different modules in the FacePamphlet application.  Any class
  6. * that implements this interface can use these constants.
  7. */

  8. public interface FacePamphletConstants {

  9.         /** The width of the application window */
  10.         public static final int APPLICATION_WIDTH = 800;

  11.         /** The height of the application window */
  12.         public static final int APPLICATION_HEIGHT = 500;

  13.         /** Number of characters for each of the text input fields */
  14.         public static final int TEXT_FIELD_SIZE = 15;

  15.         /** Text to be used to create an "empty" label to put space
  16.          *  between interactors on EAST border of application.  Note this
  17.          *  label is not actually the empty string, but rather a single space */
  18.         public static final String EMPTY_LABEL_TEXT = " ";

  19.         /** Name of font used to display the application message at the
  20.          *  bottom of the display canvas */
  21.         public static final String MESSAGE_FONT = "Dialog-18";

  22.         /** Name of font used to display the name in a user's profile */
  23.         public static final String PROFILE_NAME_FONT = "Dialog-24";
  24.        
  25.         /** Name of font used to display the text "No Image" in user
  26.          *  profiles that do not contain an actual image */
  27.         public static final String PROFILE_IMAGE_FONT = "Dialog-24";
  28.        
  29.         /** Name of font used to display the status in a user's profile */
  30.         public static final String PROFILE_STATUS_FONT = "Dialog-16-bold";

  31.         /** Name of font used to display the label "Friends" above the
  32.          *  user's list of friends in a profile */
  33.         public static final String PROFILE_FRIEND_LABEL_FONT = "Dialog-16-bold";

  34.         /** Name of font used to display the names from the user's list
  35.          *  of friends in a profile */
  36.         public static final String PROFILE_FRIEND_FONT = "Dialog-16";

  37.         /** The width (in pixels) that profile images should be displayed */
  38.         public static final double IMAGE_WIDTH = 200;

  39.         /** The height (in pixels) that profile images should be displayed */
  40.         public static final double IMAGE_HEIGHT = 200;       

  41.         /** The number of pixels in the vertical margin between the bottom
  42.          *  of the canvas display area and the baseline for the message
  43.          *  text that appears near the bottom of the display */
  44.         public static final double BOTTOM_MESSAGE_MARGIN = 20;

  45.         /** The number of pixels in the hortizontal margin between the
  46.          *  left side of the canvas display area and the Name, Image, and
  47.          *  Status components that are display in the profile */       
  48.         public static final double LEFT_MARGIN = 20;       

  49.         /** The number of pixels in the vertical margin between the top
  50.          *  of the canvas display area and the top (NOT the baseline) of
  51.          *  the Name component that is displayed in the profile */       
  52.         public static final double TOP_MARGIN = 20;       
  53.        
  54.         /** The number of pixels in the vertical margin between the
  55.          *  baseline of the Name component and the top of the Image
  56.          *  displayed in the profile */       
  57.         public static final double IMAGE_MARGIN = 20;

  58.         /** The number of vertical pixels in the vertical margin between
  59.          *  the bottom of the Image and the top of the Status component
  60.          *  in the profile */               
  61.         public static final double STATUS_MARGIN = 20;

  62. }
复制代码
FacePamphletCanvas
  1. /*
  2. * File: FacePamphletCanvas.java
  3. * -----------------------------
  4. * This class represents the canvas on which the profiles in the social
  5. * network are displayed.  NOTE: This class does NOT need to update the
  6. * display when the window is resized.
  7. */


  8. import acm.graphics.*;
  9. import java.awt.*;
  10. import java.util.*;

  11. public class FacePamphletCanvas extends GCanvas
  12.                                         implements FacePamphletConstants {
  13.        
  14.         /**
  15.          * Constructor
  16.          * This method takes care of any initialization needed for
  17.          * the display
  18.          */
  19.         public FacePamphletCanvas() {
  20.                 message=null;
  21.         }

  22.         public void removeProfile(){
  23.                 removeAll();
  24.                 message=null;
  25.         }
  26.        
  27.        
  28.         /**
  29.          * This method displays a message string near the bottom of the
  30.          * canvas.  Every time this method is called, the previously
  31.          * displayed message (if any) is replaced by the new message text
  32.          * passed in.
  33.          */
  34.         public void showMessage(String msg) {
  35.                 if(message!=null){
  36.                         remove(message);
  37.                 }
  38.                 message=new GLabel(msg);
  39.                 message.setFont(MESSAGE_FONT);
  40.                 message.setLocation((getWidth()-message.getWidth())/2,getHeight()-BOTTOM_MESSAGE_MARGIN);
  41.                 add(message);
  42.         }
  43.        
  44.        
  45.         /**
  46.          * This method displays the given profile on the canvas.  The
  47.          * canvas is first cleared of all existing items (including
  48.          * messages displayed near the bottom of the screen) and then the
  49.          * given profile is displayed.  The profile display includes the
  50.          * name of the user from the profile, the corresponding image
  51.          * (or an indication that an image does not exist), the status of
  52.          * the user, and a list of the user's friends in the social network.
  53.          */
  54.         public void displayProfile(FacePamphletProfile profile) {
  55.                 removeAll();
  56.                 displayNameOfProfile(profile.getName());
  57.                 if(profile.getImage()==null){
  58.                         displayNoImage();
  59.                 }
  60.                 else{
  61.                         displayImage(profile.getImage());
  62.                 }
  63.                 displayStatusOfProfile(profile);
  64.                 displayFriendsOfProfile(profile.getFriends());
  65.                 showMessage("Displaying "+profile.getName());
  66.         }
  67.        
  68.         private void displayFriendsOfProfile(Iterator<String> friends){
  69.                 GLabel friend=new GLabel("Friends:");
  70.                 friend.setFont(PROFILE_FRIEND_LABEL_FONT);
  71.                 add(friend,getWidth()/2,IMAGE_MARGIN+TOP_MARGIN+gname.getAscent());
  72.                 int i=0;
  73.                 while(friends.hasNext()){
  74.                         GLabel newFriend=new GLabel(friends.next());
  75.                         newFriend.setFont(PROFILE_FRIEND_FONT);
  76.                         newFriend.setLocation(getWidth()/2,IMAGE_MARGIN+TOP_MARGIN+gname.getAscent()+friend.getAscent()*2);
  77.                         newFriend.move(0,2*i*newFriend.getAscent());
  78.                         add(newFriend);
  79.                         i++;
  80.                 }
  81.         }
  82.        
  83.         private void displayStatusOfProfile(FacePamphletProfile profile){
  84.                 String status;
  85.                 if(profile.getStatus()==null){
  86.                         status="No current status";
  87.                 }
  88.                 else{
  89.                         status=profile.getName()+" is "+profile.getStatus();
  90.                 }
  91.                 GLabel gstatus=new GLabel(status);
  92.                 gstatus.setFont(PROFILE_STATUS_FONT);
  93.                 add(gstatus,LEFT_MARGIN,IMAGE_MARGIN+TOP_MARGIN+gname.getAscent()+IMAGE_HEIGHT+STATUS_MARGIN);
  94.         }
  95.        
  96.         private void displayNoImage(){
  97.                 add(new GRect(LEFT_MARGIN,IMAGE_MARGIN+TOP_MARGIN+gname.getAscent(),IMAGE_WIDTH,IMAGE_HEIGHT));
  98.                 GLabel noImage=new GLabel("No Image");
  99.                 noImage.setFont( PROFILE_IMAGE_FONT);
  100.                 add(noImage,LEFT_MARGIN+(IMAGE_WIDTH-noImage.getWidth())/2,IMAGE_MARGIN+TOP_MARGIN+gname.getAscent()+(IMAGE_HEIGHT+noImage.getAscent())/2);
  101.         }
  102.        
  103.         private void displayImage(GImage image){
  104.                 image.scale(IMAGE_WIDTH/image.getWidth(), IMAGE_HEIGHT/image.getHeight());
  105.                 add(image,LEFT_MARGIN,IMAGE_MARGIN+TOP_MARGIN+gname.getAscent());
  106.         }
  107.        
  108.         private void displayNameOfProfile(String name){
  109.                 gname=new GLabel(name);
  110.                 gname.setFont(PROFILE_NAME_FONT);
  111.                 gname.setLocation(LEFT_MARGIN,TOP_MARGIN+gname.getAscent());
  112.                 add(gname);
  113.         }
  114.        
  115.         private GLabel gname;
  116.         private GLabel message;

  117.        
  118. }
复制代码
FacePamphlet
  1. /*
  2. * File: FacePamphlet.java
  3. * -----------------------
  4. * When it is finished, this program will implement a basic social network
  5. * management system.
  6. */

  7. import acm.program.*;
  8. import acm.graphics.*;
  9. import acm.util.*;
  10. import java.awt.event.*;
  11. import java.util.ArrayList;
  12. import java.util.*;

  13. import javax.swing.*;

  14. public class FacePamphlet extends Program
  15.                                         implements FacePamphletConstants {

  16.         /**
  17.          * This method has the responsibility for initializing the
  18.          * interactors in the application, and taking care of any other
  19.          * initialization that needs to be performed.
  20.          */
  21.         public void init() {
  22.                 database=new FacePamphletDatabase();
  23.                 add(new JLabel("Name"),NORTH);
  24.                 name=new JTextField(TEXT_FIELD_SIZE);
  25.                 add(name,NORTH);
  26.                 add(new JButton("Add"),NORTH);
  27.                 add(new JButton("Delete"),NORTH);
  28.                 add(new JButton("Look up"),NORTH);
  29.                 changeStatus=new JTextField(TEXT_FIELD_SIZE);
  30.                 changeStatus.setActionCommand("Change Status");
  31.                 changePicture=new JTextField(TEXT_FIELD_SIZE);
  32.                 changePicture.setActionCommand("Change Picture");
  33.                 addFriend=new JTextField(TEXT_FIELD_SIZE);
  34.                 addFriend.setActionCommand("Add Friend");
  35.                 add(changeStatus,WEST);
  36.                 add(new JButton("Change Status"),WEST);
  37.                 add(new JLabel(EMPTY_LABEL_TEXT),WEST);
  38.                 add(changePicture,WEST);
  39.                 add(new JButton("Change Picture"),WEST);
  40.                 add(new JLabel(EMPTY_LABEL_TEXT),WEST);
  41.                 add(addFriend,WEST);
  42.                 add(new JButton("Add Friend"),WEST);
  43.                 addActionListeners();
  44.                 name.addActionListener(this);
  45.                 changeStatus.addActionListener(this);
  46.                 changePicture.addActionListener(this);
  47.                 addFriend.addActionListener(this);
  48.                 canvas=new FacePamphletCanvas();
  49.                 add(canvas);
  50.     }
  51.    
  52.   
  53.     /**
  54.      * This class is responsible for detecting when the buttons are
  55.      * clicked or interactors are used, so you will have to add code
  56.      * to respond to these actions.
  57.      */
  58.     public void actionPerformed(ActionEvent e) {
  59.                 if(e.getActionCommand().equals("Add")){
  60.                         if(!database.containsProfile(name.getText())){
  61.                                 FacePamphletProfile profile=new FacePamphletProfile(name.getText());
  62.                                 database.addProfile(profile);
  63.                                 dealingWith=profile;
  64.                                 canvas.displayProfile(dealingWith);
  65.                                 canvas.showMessage("New profile created");
  66.                         }
  67.                         else{
  68.                                 dealingWith=database.getProfile(name.getText());
  69.                                 canvas.displayProfile(dealingWith);
  70.                                 canvas.showMessage("A profile with the name "+dealingWith.getName()+" already exists");
  71.                         }
  72.                 }
  73.                 if(e.getActionCommand().equals("Delete")){
  74.                         dealingWith=database.getProfile(name.getText());
  75.                         if(database.getProfile(name.getText())!=null){                               
  76.                                 Iterator<String> it=dealingWith.getFriends();
  77.                                 while(it.hasNext()){
  78.                                         String nameOfFriend=it.next();
  79.                                         FacePamphletProfile friendProfile=database.getProfile(nameOfFriend);
  80.                                         friendProfile.removeFriend(name.getText());
  81.                                 }
  82.                                 database.deleteProfile(name.getText());
  83.                                 dealingWith=null;
  84.                                 canvas.removeProfile();
  85.                                 canvas.showMessage("Profile of "+name.getText()+" deleted");
  86.                         }
  87.                         else{
  88.                                 canvas.showMessage("A profile with the name "+name.getText()+" does not exist");
  89.                         }
  90.                 }
  91.                 if(e.getActionCommand().equals("Look up")){
  92.                         if(database.containsProfile(name.getText())){
  93.                                 dealingWith=database.getProfile(name.getText());
  94.                                 canvas.displayProfile(dealingWith);
  95.                         }
  96.                         else{
  97.                                 canvas.removeProfile();
  98.                                 canvas.showMessage("A profile with the name "+name.getText()+" does not exist");
  99.                         }
  100.                 }
  101.                 if(e.getActionCommand().equals("Change Status")){
  102.                         if(dealingWith!=null){
  103.                                 dealingWith.setStatus(changeStatus.getText());
  104.                                 canvas.displayProfile(dealingWith);
  105.                                 canvas.showMessage("Status updated to "+changeStatus.getText());
  106.                         }
  107.                         else{
  108.                                 canvas.removeProfile();
  109.                                 canvas.showMessage("Please select a profile to change status");
  110.                                
  111.                         }
  112.                 }
  113.                 if(e.getActionCommand().equals("Change Picture")){
  114.                         if(dealingWith!=null){
  115.                                 try{
  116.                                         dealingWith.setImage(new GImage(changePicture.getText()));
  117.                                         canvas.displayProfile(dealingWith);
  118.                                         canvas.showMessage("Picture updated");
  119.                                        
  120.                                         }
  121.                                 catch(ErrorException ex){
  122.                                         canvas.showMessage("Unable to open image file: "+changePicture.getText());
  123.                                 }
  124.                         }
  125.                         else{
  126.                                 canvas.removeProfile();
  127.                                 canvas.showMessage("Please select a profile to change picture");
  128.                         }
  129.                 }
  130.                 if(e.getActionCommand().equals("Add Friend")){
  131.                         if(dealingWith!=null){
  132.                                 if(database.containsProfile(addFriend.getText())&&!(dealingWith.getName().equals(addFriend.getText()))){
  133.                                         dealingWith.addFriend(addFriend.getText());
  134.                                         FacePamphletProfile profile=database.getProfile(addFriend.getText());
  135.                                         boolean notContainsFriend=profile.addFriend(dealingWith.getName());
  136.                                         canvas.displayProfile(dealingWith);
  137.                                         canvas.showMessage(dealingWith.getName()+" added a friend");
  138.                                         if(!notContainsFriend){
  139.                                                 canvas.showMessage(dealingWith.getName()+" already has "+addFriend.getText()+" as a friend");
  140.                                         }
  141.                                 }
  142.                                 if(!(database.containsProfile(addFriend.getText()))){
  143.                                         canvas.showMessage(addFriend.getText()+" does not exist");
  144.                                 }
  145.                                 if(dealingWith.getName().equals(addFriend.getText())){
  146.                                         canvas.showMessage("Can not add yourself as a friend.");
  147.                                 }
  148.                                
  149.                         }
  150.                         else{
  151.                                 canvas.showMessage("Please select a profile to add friend");
  152.                         }
  153.                        
  154.                        
  155.                 }
  156.                
  157.         }
  158.    
  159.     private JTextField name;
  160.     private JTextField changeStatus;
  161.     private JTextField changePicture;
  162.     private JTextField addFriend;
  163.     private FacePamphletDatabase database;
  164.     private FacePamphletProfile dealingWith;
  165.     private FacePamphletCanvas canvas;

  166. }
复制代码
回复

使用道具 举报

🔗
nprotect 2012-9-18 07:27:21 | 只看该作者
全局:
这次作业依然考察了类之间方法的调用,强调了分解程序的思想,同时也考察了数据库的基本思想,这里就是hashmap。和上一次作业大致相同,不过因为功能更复杂,所以程序更麻烦一些。
回复

使用道具 举报

🔗
nprotect 2012-9-18 07:29:23 | 只看该作者
全局:
FacePamphlet.java:
  1. /*
  2. * File: FacePamphlet.java
  3. * -----------------------
  4. * When it is finished, this program will implement a basic social network
  5. * management system.
  6. */

  7. import acm.program.*;
  8. import acm.graphics.*;
  9. import acm.util.*;
  10. import java.awt.event.*;
  11. import javax.swing.*;

  12. public class FacePamphlet extends Program implements FacePamphletConstants {

  13.         /**
  14.          * This method has the responsibility for initializing the
  15.          * interactors in the application, and taking care of any other
  16.          * initialization that needs to be performed.
  17.          */
  18.        
  19.         public void init() {
  20.                
  21.                 // NORTH
  22.                 name = new JLabel ("Name");
  23.                 add (name, NORTH);
  24.                 textFieldNorth = new JTextField(TEXT_FIELD_SIZE);
  25.                 add (textFieldNorth, NORTH);
  26.                 add = new JButton ("Add");
  27.                 add (add, NORTH);
  28.                 delete = new JButton ("Delete");
  29.                 add (delete, NORTH);
  30.                 lookup = new JButton ("Lookup");
  31.                 add (lookup, NORTH);
  32.                
  33.                 // WEST
  34.                 // Change Status
  35.                 textFieldWest1 = new JTextField (TEXT_FIELD_SIZE);
  36.                 add (textFieldWest1, WEST);
  37.                 status = new JButton ("Change Status");
  38.                 add (status, WEST);
  39.                 add( new JLabel(EMPTY_LABEL_TEXT), WEST);
  40.                 // Change Picture
  41.                 textFieldWest2 = new JTextField (TEXT_FIELD_SIZE);
  42.                 add (textFieldWest2, WEST);
  43.                 picture = new JButton ("Change Picture");
  44.                 add (picture, WEST);
  45.                 add( new JLabel(EMPTY_LABEL_TEXT), WEST);
  46.                 // Add Friend
  47.                 textFieldWest3 = new JTextField (TEXT_FIELD_SIZE);
  48.                 add (textFieldWest3, WEST);
  49.                 friend = new JButton ("Add Friend");
  50.                 add (friend, WEST);
  51.                 add( new JLabel(EMPTY_LABEL_TEXT), WEST);
  52.                
  53.                 // West region need "Enter" function
  54.                 textFieldWest1.addActionListener(this);
  55.                 textFieldWest2.addActionListener(this);  
  56.                 textFieldWest3.addActionListener(this);
  57.                
  58.                 // ActionListerners
  59.                 addActionListeners();
  60.                
  61.                 // canvas
  62.                 add(canvas);
  63.     }
  64.        
  65.     /**
  66.      * This class is responsible for detecting when the buttons are
  67.      * clicked or interactors are used, so you will have to add code
  68.      * to respond to these actions.
  69.      */
  70.    
  71.         public void actionPerformed(ActionEvent e) {
  72.            
  73.                 String cmd = e.getActionCommand();
  74.            
  75.                 // North region
  76.             // Add button
  77.             if (cmd.equals("Add")) {
  78.                     if (data.containsProfile(textFieldNorth.getText())){
  79.                             currentProfile = data.getProfile(textFieldNorth.getText());      // if exist, add = look up
  80.                             canvas.displayProfile(currentProfile);
  81.                             canvas.showMessage("A profile with the name<" + currentProfile.getName() + ">already exists");
  82.                     }
  83.                     else {
  84.                                     currentProfile = new FacePamphletProfile(textFieldNorth.getText());// if do not exist, new a FacePamphletProfile
  85.                                     data.addProfile(currentProfile);
  86.                                     canvas.displayProfile(currentProfile);
  87.                                     canvas.showMessage("New profile created");
  88.                     }
  89.             }
  90.            
  91.             // Delete button
  92.             if (cmd.equals("Delete")) {
  93.                     if (data.containsProfile(textFieldNorth.getText())){
  94.                             data.deleteProfile(textFieldNorth.getText());
  95.                             canvas.removeAll();  // clear any existing profile from the display
  96.                             canvas.showMessage("Profile of <" +textFieldNorth.getText()+ ">deleted");
  97.                             currentProfile = null;       
  98.                     } else {
  99.                             canvas.removeAll();
  100.                             canvas.showMessage("A profile with the name<" + textFieldNorth.getText() + ">does not exist");
  101.                             currentProfile = null;
  102.                     }
  103.             }
  104.            
  105.             // Lookup button
  106.             if (cmd.equals("Lookup")) {
  107.                     if (data.containsProfile(textFieldNorth.getText())) {
  108.                             currentProfile = data.getProfile(textFieldNorth.getText());
  109.                             canvas.displayProfile(currentProfile);
  110.                             canvas.showMessage("Displaying<" + currentProfile.getName() + ">");
  111.                     } else {
  112.                             canvas.removeAll();
  113.                             canvas.showMessage("A profile with the name<" + textFieldNorth.getText() + ">does not exist");
  114.                             currentProfile = null;
  115.                     }
  116.             }
  117.            
  118.             // West region
  119.             // Change Status button
  120.             if ((cmd.equals("Change Status"))||(e.getSource() == textFieldWest1)) {    // Change status button
  121.                     if (currentProfile != null){
  122.                             currentProfile.setStatus(textFieldWest1.getText());
  123.                             canvas.displayProfile(currentProfile);
  124.                             canvas.showMessage("Status updated to<" + currentProfile.getStatus() + ">");
  125.                     }
  126.                     else {
  127.                             canvas.showMessage("Please select a profile to change status");
  128.                     }
  129.             }
  130.            
  131.             // Change Picture button
  132.             if ((cmd.equals("Change Picture"))||(e.getSource() == textFieldWest2)) {    // Change picture button
  133.                     if (currentProfile != null){
  134.                     GImage image = null;
  135.                     try{
  136.                             image = new GImage (textFieldWest2.getText());
  137.                         currentProfile.setImage(image);
  138.                         canvas.displayProfile(currentProfile);
  139.                         canvas.showMessage("Picture updated");
  140.                     }         catch (ErrorException ex) {
  141.                                     canvas.showMessage("Unable to open image file:<" + textFieldWest2.getText() + ">");
  142.                             }

  143.                     } else {
  144.                             canvas.removeAll();
  145.                             canvas.showMessage("Please select a profile to change picture");
  146.                     }
  147.             }
  148.            
  149.             // Add Friend button
  150.             if ((cmd.equals("Add Friend"))||(e.getSource() == textFieldWest3)) {
  151.                     if (currentProfile != null) {
  152.                             if (data.containsProfile(textFieldWest3.getText())){ // judge the status of added profile
  153.                                     boolean b = currentProfile.addFriend(textFieldWest3.getText());
  154.                                             if (b == false) {
  155.                                                     canvas.showMessage("<" + currentProfile.getName() + ">already has " + "<" + textFieldWest3.getText() + ">as a friend");
  156.                                             }
  157.                                             if (b == true) {
  158.                                                     (data.getProfile(textFieldWest3.getText())).addFriend(currentProfile.getName()); // revise adding friend! IMPORTANT
  159.                                                     canvas.displayProfile(currentProfile);
  160.                                                     canvas.showMessage ("<" + textFieldWest3.getText() + ">added as a friend");                                               
  161.                                             }
  162.                             }  else  {
  163.                                     canvas.showMessage("<" + textFieldWest3.getText() + ">does not exist");
  164.                             }
  165.                     }  else {
  166.                             canvas.removeAll();
  167.                             canvas.showMessage("Please select a profile to add friend");
  168.                     }
  169.             }
  170.         }
  171.        
  172.     /** instance variable*/
  173.     // North
  174.     private JLabel name;
  175.     private JTextField textFieldNorth;
  176.     private JButton add;
  177.     private JButton delete;
  178.     private JButton lookup;
  179.     // West
  180.     private JTextField textFieldWest1;
  181.     private JTextField textFieldWest2;
  182.     private JTextField textFieldWest3;
  183.     private JButton status;
  184.     private JButton picture;
  185.     private JButton friend;
  186.     // Current Profile
  187.     private FacePamphletProfile currentProfile;
  188.     private FacePamphletDatabase data = new FacePamphletDatabase();
  189.     // Canvas
  190.     private FacePamphletCanvas canvas = new FacePamphletCanvas();
  191. }
复制代码
FacePamphletProfile.java:
  1. /*
  2. * File: FacePamphletProfile.java
  3. * ------------------------------
  4. * This class keeps track of all the information for one profile
  5. * in the FacePamphlet social network.  Each profile contains a
  6. * name, an image (which may not always be set), a status (what
  7. * the person is currently doing, which may not always be set),
  8. * and a list of friends.
  9. */

  10. import acm.graphics.*;
  11. import java.util.*;

  12. public class FacePamphletProfile implements FacePamphletConstants {
  13.        
  14.         /**
  15.          * Constructor
  16.          * This method takes care of any initialization needed for
  17.          * the profile.
  18.          */
  19.        
  20.         public FacePamphletProfile(String name) {    // input name,
  21.                 this.name = name; // left "name" is defined in this class, right "name" is passed in
  22.                 this.status = "";
  23.                 this.image = null;
  24.         }

  25.         /** This method returns the name associated with the profile. */
  26.         public String getName() {
  27.                 return this.name;
  28.         }

  29.         /**
  30.          * This method returns the image associated with the profile.  
  31.          * If there is no image associated with the profile, the method
  32.          * returns null. */
  33.         public GImage getImage() {
  34.                 return this.image;
  35.         }

  36.         /** This method sets the image associated with the profile. */
  37.         public void setImage(GImage image) {
  38.                 this.image = image;
  39.         }
  40.        
  41.         /**
  42.          * This method returns the status associated with the profile.
  43.          * If there is no status associated with the profile, the method
  44.          * returns the empty string ("").
  45.          */
  46.         public String getStatus() {
  47.                 return this.status;
  48.         }
  49.        
  50.         /** This method sets the status associated with the profile. */
  51.         public void setStatus(String status) {
  52.                 this.status = status;
  53.         }

  54.         /**
  55.          * This method adds the named friend to this profile's list of
  56.          * friends.  It returns true if the friend's name was not already
  57.          * in the list of friends for this profile (and the name is added
  58.          * to the list).  The method returns false if the given friend name
  59.          * was already in the list of friends for this profile (in which
  60.          * case, the given friend name is not added to the list of friends
  61.          * a second time.)
  62.          */
  63.         public boolean addFriend(String friend) {
  64.                 if (friendsList.contains(friend)) return false;
  65.                 friendsList.add(friend);     return true;
  66.         }

  67.         /**
  68.          * This method removes the named friend from this profile's list
  69.          * of friends.  It returns true if the friend's name was in the
  70.          * list of friends for this profile (and the name was removed from
  71.          * the list).  The method returns false if the given friend name
  72.          * was not in the list of friends for this profile (in which case,
  73.          * the given friend name could not be removed.)
  74.          */
  75.         public boolean removeFriend(String friend) {  // remove one friend from the list
  76.         if (friendsList.contains(friend)) {
  77.                 friendsList.remove(friend);
  78.                 return true;
  79.         }
  80.                 return false;
  81.         }

  82.         /**
  83.          * This method returns an iterator over the list of friends
  84.          * associated with the profile.
  85.          */
  86.         public Iterator<String> getFriends() {
  87.                 return friendsList.iterator();  // return an iterator
  88.         }
  89.        
  90.         /**
  91.          * This method returns a string representation of the profile.  
  92.          * This string is of the form: "name (status): list of friends",
  93.          * where name and status are set accordingly and the list of
  94.          * friends is a comma separated list of the names of all of the
  95.          * friends in this profile.
  96.          *
  97.          * For example, in a profile with name "Alice" whose status is
  98.          * "coding" and who has friends Don, Chelsea, and Bob, this method
  99.          * would return the string: "Alice (coding): Don, Chelsea, Bob"
  100.          */
  101.         public String toString() {
  102.                 String line = "";
  103.                 line = "\"" + name + " (" + status + "): ";
  104.                 Iterator<String> it = getFriends();  // object of the Iterator: "it"
  105.                 if (it.hasNext()) line = line + it.next();
  106.                 while (it.hasNext()){
  107.                         line = line + "," + it.next();
  108.                 }
  109.                 line = line + "\"";
  110.                 return line;
  111.         }

  112.         /** instance variable*/
  113.         private String name;
  114.         private String status;
  115.         private GImage image;
  116.         ArrayList<String> friendsList = new ArrayList <String>();   // each people's list of friends store in "friendList"
  117. }
复制代码
FacePamphletDatabase.java:
  1. /*
  2. * File: FacePamphletDatabase.java
  3. * -------------------------------
  4. * This class keeps track of the profiles of all users in the
  5. * FacePamphlet application.  Note that profile names are case
  6. * sensitive, so that "ALICE" and "alice" are NOT the same name.
  7. */

  8. import java.util.*;

  9. /** this class has 4 methods: "add, delete,get, contains" */

  10. public class FacePamphletDatabase implements FacePamphletConstants {

  11.         /**
  12.          * Constructor
  13.          * This method takes care of any initialization needed for
  14.          * the database.
  15.          */
  16.        
  17.         public FacePamphletDatabase() {
  18.                 // null
  19.         }
  20.        
  21.         /**
  22.          * This method adds the given profile to the database.  If the
  23.          * name associated with the profile is the same as an existing
  24.          * name in the database, the existing profile is replaced by
  25.          * the new profile passed in.
  26.          */
  27.        
  28.         public void addProfile(FacePamphletProfile profile) {
  29.                 if (database.containsKey(profile.getName())) database.remove(profile.getName());
  30.                 database.put(profile.getName(), profile);
  31.         }

  32.         /**
  33.          * This method returns the profile associated with the given name
  34.          * in the database.  If there is no profile in the database with
  35.          * the given name, the method returns null.
  36.          */
  37.        
  38.         public FacePamphletProfile getProfile(String name) {
  39.                 if (database.containsKey(name)) return database.get(name);
  40.                 return null;
  41.         }
  42.        
  43.         /**
  44.          * This method removes the profile associated with the given name
  45.          * from the database.  It also updates the list of friends of all
  46.          * other profiles in the database to make sure that this name is
  47.          * removed from the list of friends of any other profile.
  48.          *
  49.          * If there is no profile in the database with the given name, then
  50.          * the database is unchanged after calling this method.
  51.          */
  52.        
  53.         public void deleteProfile(String name) {  // THIS IS IMPORTANT!
  54.                 if (database.containsKey(name)){
  55.                         Iterator<String> it = database.get(name).getFriends(); // set an iterator "it" which represents all of input name's friends
  56.                         while (it.hasNext()){             // it.hasNext() is a String of input name's friend's name
  57.                                 database.get(it.next()).removeFriend(name);
  58.                         }
  59.                 }
  60.                 database.remove(name);
  61.         }

  62.         /**
  63.          * This method returns true if there is a profile in the database
  64.          * that has the given name.  It returns false otherwise.
  65.          */
  66.        
  67.         public boolean containsProfile(String name) {
  68.                 if (database.containsKey(name)) return true;
  69.                 return false;
  70.         }

  71.         /** instance variable*/
  72.        
  73.         private HashMap<String, FacePamphletProfile> database = new HashMap<String, FacePamphletProfile>();   // correspond name & Profile in HashMap
  74. }
复制代码
FacePamphletCanvas.java:
  1. /*
  2. * File: FacePamphletCanvas.java
  3. * -----------------------------
  4. * This class represents the canvas on which the profiles in the social
  5. * network are displayed.  NOTE: This class does NOT need to update the
  6. * display when the window is resized.
  7. */

  8. import acm.graphics.*;
  9. import java.awt.*;
  10. import java.util.*;

  11. public class FacePamphletCanvas extends GCanvas implements FacePamphletConstants {
  12.        
  13.         /**
  14.          * Constructor
  15.          * This method takes care of any initialization needed for
  16.          * the display
  17.          */
  18.         public FacePamphletCanvas() {
  19.                 // null
  20.         }

  21.         /**
  22.          * This method displays a message string near the bottom of the
  23.          * canvas.  Every time this method is called, the previously
  24.          * displayed message (if any) is replaced by the new message text
  25.          * passed in.
  26.          */
  27.        
  28.         public void showMessage(String msg) {   // if called, delete current message and add the new one
  29.                 message = new GLabel(msg,getWidth()/2, getHeight()-BOTTOM_MESSAGE_MARGIN);
  30.                 message.move(-message.getWidth()/2, 0);
  31.                 message.setFont(MESSAGE_FONT);
  32.                 if (getElementAt(getWidth()/2,getHeight()-BOTTOM_MESSAGE_MARGIN) != null) {
  33.                         remove(getElementAt(getWidth()/2,getHeight()-BOTTOM_MESSAGE_MARGIN));
  34.                 }
  35.                 add(message);
  36.         }
  37.        
  38.         /**
  39.          * This method displays the given profile on the canvas.  The
  40.          * canvas is first cleared of all existing items (including
  41.          * messages displayed near the bottom of the screen) and then the
  42.          * given profile is displayed.  The profile display includes the
  43.          * name of the user from the profile, the corresponding image
  44.          * (or an indication that an image does not exist), the status of
  45.          * the user, and a list of the user's friends in the social network.
  46.          */
  47.        
  48.         public void displayProfile(FacePamphletProfile profile) {
  49.                 removeAll();
  50.                
  51.                 // update name
  52.                 name = new GLabel(profile.getName(), LEFT_MARGIN, TOP_MARGIN);
  53.                 name.move(0, name.getHeight());
  54.                 name.setFont(PROFILE_NAME_FONT);
  55.                 name.setColor(Color.blue);
  56.                 add(name);
  57.                
  58.                 // update picture
  59.                 add(new GRect(LEFT_MARGIN, TOP_MARGIN+IMAGE_MARGIN+name.getHeight(), IMAGE_WIDTH, IMAGE_HEIGHT));
  60.                 if (profile.getImage() == null){
  61.                         noPicture = new GLabel ("NO IMAGE!", LEFT_MARGIN+IMAGE_WIDTH/2, TOP_MARGIN+IMAGE_MARGIN+name.getHeight()+IMAGE_HEIGHT/2);
  62.                         noPicture.move(-noPicture.getWidth(), 0);
  63.                         noPicture.setFont(PROFILE_IMAGE_FONT);
  64.                         add(noPicture);
  65.                 }        else {
  66.                         image = profile.getImage();
  67.                         add(image, LEFT_MARGIN, TOP_MARGIN+IMAGE_MARGIN+name.getHeight());
  68.                 }
  69.                
  70.                 // update status
  71.                 if (profile.getStatus()== ""){
  72.                         status = new GLabel("No current status", LEFT_MARGIN, TOP_MARGIN+IMAGE_MARGIN+name.getHeight()+IMAGE_HEIGHT+STATUS_MARGIN);
  73.                         status.move(0, status.getHeight());
  74.                         status.setFont(PROFILE_STATUS_FONT);
  75.                         add(status);
  76.                 } else {
  77.                         status = new GLabel(profile.getName()+" is "+profile.getStatus(), LEFT_MARGIN, TOP_MARGIN+IMAGE_MARGIN+name.getHeight()+IMAGE_HEIGHT+STATUS_MARGIN);
  78.                         status.move(0, status.getHeight());
  79.                         add(status);
  80.                 }
  81.                
  82.                 // Friends Label
  83.                 friendsLabel = new GLabel("Friends: ", getWidth()/2, TOP_MARGIN+IMAGE_MARGIN+name.getHeight());
  84.                 friendsLabel.setFont(PROFILE_FRIEND_LABEL_FONT);
  85.                 add(friendsLabel);
  86.                
  87.                 // update friend
  88.                 Iterator<String> it = profile.getFriends();
  89.                 double y = TOP_MARGIN+IMAGE_MARGIN+name.getHeight();
  90.                         while (it.hasNext()){
  91.                                 friends = new GLabel (it.next());
  92.                                 friends.setFont(PROFILE_FRIEND_FONT);
  93.                                 y = y + friends.getHeight();
  94.                                 add(friends, getWidth()/2,y);
  95.                         }
  96.         }
  97.        
  98.         /** instance variable*/
  99.         private GLabel message;
  100.         private GLabel name;
  101.         private GLabel noPicture;
  102.         private GImage image;
  103.         private GLabel status;
  104.         private GLabel friendsLabel;
  105.         private GLabel friends;
  106. }
复制代码
FacePamphletConstants.java:
  1. /*
  2. * File: FacePamphletConstants.java
  3. * --------------------------------
  4. * This file declares several constants that are shared by the
  5. * different modules in the FacePamphlet application.  Any class
  6. * that implements this interface can use these constants.
  7. */

  8. public interface FacePamphletConstants {

  9.         /** The width of the application window */
  10.         public static final int APPLICATION_WIDTH = 800;

  11.         /** The height of the application window */
  12.         public static final int APPLICATION_HEIGHT = 500;

  13.         /** Number of characters for each of the text input fields */
  14.         public static final int TEXT_FIELD_SIZE = 15;

  15.         /** Text to be used to create an "empty" label to put space
  16.          *  between interactors on EAST border of application.  Note this
  17.          *  label is not actually the empty string, but rather a single space */
  18.         public static final String EMPTY_LABEL_TEXT = " ";

  19.         /** Name of font used to display the application message at the
  20.          *  bottom of the display canvas */
  21.         public static final String MESSAGE_FONT = "Dialog-18";

  22.         /** Name of font used to display the name in a user's profile */
  23.         public static final String PROFILE_NAME_FONT = "Dialog-24";
  24.        
  25.         /** Name of font used to display the text "No Image" in user
  26.          *  profiles that do not contain an actual image */
  27.         public static final String PROFILE_IMAGE_FONT = "Dialog-24";
  28.        
  29.         /** Name of font used to display the status in a user's profile */
  30.         public static final String PROFILE_STATUS_FONT = "Dialog-16-bold";

  31.         /** Name of font used to display the label "Friends" above the
  32.          *  user's list of friends in a profile */
  33.         public static final String PROFILE_FRIEND_LABEL_FONT = "Dialog-16-bold";

  34.         /** Name of font used to display the names from the user's list
  35.          *  of friends in a profile */
  36.         public static final String PROFILE_FRIEND_FONT = "Dialog-16";

  37.         /** The width (in pixels) that profile images should be displayed */
  38.         public static final double IMAGE_WIDTH = 200;

  39.         /** The height (in pixels) that profile images should be displayed */
  40.         public static final double IMAGE_HEIGHT = 200;       

  41.         /** The number of pixels in the vertical margin between the bottom
  42.          *  of the canvas display area and the baseline for the message
  43.          *  text that appears near the bottom of the display */
  44.         public static final double BOTTOM_MESSAGE_MARGIN = 20;

  45.         /** The number of pixels in the hortizontal margin between the
  46.          *  left side of the canvas display area and the Name, Image, and
  47.          *  Status components that are display in the profile */       
  48.         public static final double LEFT_MARGIN = 20;       

  49.         /** The number of pixels in the vertical margin between the top
  50.          *  of the canvas display area and the top (NOT the baseline) of
  51.          *  the Name component that is displayed in the profile */       
  52.         public static final double TOP_MARGIN = 20;       
  53.        
  54.         /** The number of pixels in the vertical margin between the
  55.          *  baseline of the Name component and the top of the Image
  56.          *  displayed in the profile */       
  57.         public static final double IMAGE_MARGIN = 20;

  58.         /** The number of vertical pixels in the vertical margin between
  59.          *  the bottom of the Image and the top of the Status component
  60.          *  in the profile */               
  61.         public static final double STATUS_MARGIN = 20;

  62. }
复制代码


回复

使用道具 举报

全局:
这次写的各种犯智障,时间远远超出预期,一脸血和泪和一股想死的冲动
  1. /*
  2. * File: FacePamphlet.java
  3. * -----------------------
  4. * When it is finished, this program will implement a basic social network
  5. * management system.
  6. */

  7. import acm.program.*;
  8. import acm.graphics.*;
  9. import acm.util.*;

  10. import java.awt.GridLayout;
  11. import java.awt.event.*;
  12. import javax.swing.*;

  13. public class FacePamphlet extends Program
  14.                                         implements FacePamphletConstants {

  15.         /**
  16.          * This method has the responsibility for initializing the
  17.          * interactors in the application, and taking care of any other
  18.          * initialization that needs to be performed.
  19.          */
  20.         public void init() {
  21.                 database=new FacePamphletDatabase();
  22.                 // You fill this in
  23.                 Name=new JTextField(TEXT_FIELD_SIZE);
  24.                 Name.addActionListener(this);
  25.                 add(new JLabel ("Name"),NORTH);
  26.                 add(Name,NORTH);
  27.                 Add=new JButton("Add");
  28.                 Delete=new JButton("Delete");
  29.                 Lookup=new JButton("Lookup");
  30.                 add(Add,NORTH);
  31.                 add(Delete,NORTH);
  32.                 add(Lookup,NORTH);
  33.                 status=new JTextField(TEXT_FIELD_SIZE);
  34.                 status.setActionCommand("Change Status");
  35.                 status.addActionListener(this);
  36.                 picture=new JTextField(TEXT_FIELD_SIZE);
  37.                 picture.setActionCommand("Change Picture");
  38.                 picture.addActionListener(this);
  39.                 addfriend=new JTextField(TEXT_FIELD_SIZE);
  40.                 addfriend.setActionCommand("Add Friend");
  41.                 addfriend.addActionListener(this);
  42.                 Status=new JButton("Change Status");
  43.                 Picture=new JButton("Change Picture");
  44.                 AddFriend=new JButton("Add Friend");
  45.                 add(status,WEST);
  46.                 add(Status,WEST);
  47.                 add(new JLabel(EMPTY_LABEL_TEXT), WEST);
  48.                 add(picture,WEST);
  49.                 add(Picture,WEST);
  50.                 add(new JLabel(EMPTY_LABEL_TEXT), WEST);
  51.                 add(addfriend,WEST);
  52.                 add(AddFriend,WEST);
  53.                 addActionListeners();
  54.                 setLayout(new GridLayout(1,1));
  55.                 canvas=new FacePamphletCanvas();
  56.                 add(canvas);
  57.                
  58.     }
  59.    
  60.   
  61.     /**
  62.      * This class is responsible for detecting when the buttons are
  63.      * clicked or interactors are used, so you will have to add code
  64.      * to respond to these actions.
  65.      */
  66.     public void actionPerformed(ActionEvent e) {
  67.                 // You fill this in as well as add any additional methods
  68.             String cmd=e.getActionCommand();
  69.             if(cmd.equals("Add")){
  70.                     if(Name.getText().equals("")==false){
  71.                             if(!database.containsProfile(Name.getText())){
  72.            
  73.                                     FacePamphletProfile NAME=new FacePamphletProfile(Name.getText());
  74.                                     database.addProfile(NAME);
  75.                                     canvas.displayProfile(NAME);
  76.                                    
  77.                                     canvas.showMessage("User "+Name.getText()+" added");
  78.                                    
  79.                             } else {
  80.                                     canvas.showMessage("User "+Name.getText()+" already exists,try again");
  81.                             }
  82.                     }
  83.             }
  84.             if(cmd.equals("Delete")){
  85.                     if(Name.getText().equals("")==false){
  86.                             if(database.containsProfile(Name.getText())){
  87.                                     database.deleteProfile(Name.getText());
  88.                                     canvas.showMessage("User "+Name.getText()+" deleted");

  89.                                    
  90.                             } else {
  91.                                     canvas.showMessage("User "+Name.getText()+" not exists");
  92.                     }
  93.                     }
  94.             }
  95.             if(cmd.equals("Lookup")){
  96.                     if(Name.getText().equals("")==false){
  97.                             if(database.containsProfile(Name.getText())){
  98.                                     canvas.displayProfile(database.getProfile(Name.getText()));
  99.                                     canvas.showMessage("Displaying "+Name.getText());
  100.                                    
  101.                             //println("lookup:"+Name.getText());
  102.                     }
  103.                     else{
  104.                             canvas.showMessage("User "+Name.getText()+" not exists");
  105.                             }
  106.                     }
  107.             }
  108.             if(cmd.equals("Change Status")){
  109.                     if(status.getText().equals("")==false){
  110.                             if(database.getProfile(Name.getText())!=null){
  111.                                     database.getProfile(Name.getText()).setStatus(status.getText());
  112.                                     canvas.displayProfile(database.getProfile(Name.getText()));
  113.                                     canvas.showMessage("Status changed");
  114.                             } else {
  115.                                     canvas.showMessage("User does not exist");
  116.                             }
  117.                     }
  118.             }
  119.             if(cmd.equals("Change Picture")){
  120.                     GImage image=null;
  121.                     if(picture.getText().equals("")==false){
  122.                             if(database.getProfile(Name.getText())!=null){
  123.                                     //println("changepic:"+picture.getText());
  124.                                    
  125.                                     try{
  126.                                             image=new GImage(picture.getText());
  127.                                            
  128.                                     } catch (ErrorException ex){
  129.                                             canvas.showMessage("image not found");
  130.                                     }
  131.                                     database.getProfile(Name.getText()).setImage(image);
  132.                                         canvas.displayProfile(database.getProfile(Name.getText()));
  133.                                            
  134.                                    
  135.                             } else {
  136.                                     canvas.showMessage("User does not exist");
  137.                             }
  138.                            
  139.                             //since the thing transfer is Gimage, need rewrite
  140.                     }
  141.             }
  142.             if(cmd.equals("Add Friend")){
  143.                     if(addfriend.getText().equals("")==false){
  144.                             //println("addfriend:"+addfriend.getText());
  145.                             if(database.getProfile(Name.getText())!=null && database.getProfile(addfriend.getText()) !=null){
  146.                                     database.getProfile(Name.getText()).addFriend(addfriend.getText());
  147.                                     database.getProfile(addfriend.getText()).addFriend(Name.getText());
  148.                                     canvas.displayProfile(database.getProfile(Name.getText()));
  149.                             }  else {
  150.                                     canvas.showMessage("Either of the names does not exist");
  151.                             }
  152.                            
  153.                     }
  154.            
  155.             }
  156.            
  157.            
  158.            
  159.            
  160.            
  161.            
  162.         }

  163.            
  164.     private FacePamphletCanvas canvas;
  165.     private FacePamphletDatabase database;
  166.     private JTextField status;
  167.     private JButton Status;
  168.     private JTextField picture;
  169.     private JButton Picture;
  170.     private JTextField addfriend;
  171.     private JButton AddFriend;
  172.     private JTextField Name;
  173.     private JButton Add;
  174.     private JButton Delete;
  175.     private JButton Lookup;
  176.    
  177.    
  178. }
复制代码
  1. /*
  2. * File: FacePamphletCanvas.java
  3. * -----------------------------
  4. * This class represents the canvas on which the profiles in the social
  5. * network are displayed.  NOTE: This class does NOT need to update the
  6. * display when the window is resized.
  7. */


  8. import acm.graphics.*;
  9. import java.awt.*;
  10. import java.util.*;

  11. public class FacePamphletCanvas extends GCanvas
  12.                                         implements FacePamphletConstants {
  13.        
  14.         /**
  15.          * Constructor
  16.          * This method takes care of any initialization needed for
  17.          * the display
  18.          */
  19.         public FacePamphletCanvas() {
  20.                 // You fill this in
  21.                 name=new GLabel("");
  22.                 status=new GLabel("");
  23.                 Friends=new GLabel("Friends:");

  24.                
  25.                
  26.         }

  27.        
  28.         /**
  29.          * This method displays a message string near the bottom of the
  30.          * canvas.  Every time this method is called, the previously
  31.          * displayed message (if any) is replaced by the new message text
  32.          * passed in.
  33.          */
  34.         public void showMessage(String msg) {
  35.                 if(message!=null){
  36.                         remove(message);
  37.                 }
  38.                 // You fill this in
  39.                  message=new GLabel(msg);
  40.                  message.setFont(MESSAGE_FONT);
  41.                 add(message,getWidth()/2,getHeight()-message.getAscent()-BOTTOM_MESSAGE_MARGIN);
  42.         }
  43.        
  44.        
  45.         /**
  46.          * This method displays the given profile on the canvas.  The
  47.          * canvas is first cleared of all existing items (including
  48.          * messages displayed near the bottom of the screen) and then the
  49.          * given profile is displayed.  The profile display includes the
  50.          * name of the user from the profile, the corresponding image
  51.          * (or an indication that an image does not exist), the status of
  52.          * the user, and a list of the user's friends in the social network.
  53.          */
  54.         public void displayProfile(FacePamphletProfile profile) {
  55.                 removeAll();
  56.                 name=new GLabel(profile.getName());
  57.                 name.setColor(Color.BLUE);
  58.                 name.setFont(PROFILE_NAME_FONT);
  59.                 add(name,LEFT_MARGIN,TOP_MARGIN+name.getAscent());
  60.                
  61.                 if(profile.getImage()!=null){
  62.                         profile.getImage().scale(IMAGE_WIDTH/profile.getImage().getWidth(),IMAGE_HEIGHT/profile.getImage().getHeight());
  63.                         add(profile.getImage(),LEFT_MARGIN,IMAGE_MARGIN+TOP_MARGIN+name.getAscent());
  64.                 } else {
  65.                 GRect rect=new GRect(LEFT_MARGIN,IMAGE_MARGIN+TOP_MARGIN+name.getAscent(),IMAGE_WIDTH,IMAGE_HEIGHT);
  66.                 add(rect);
  67.                 GLabel noim=new GLabel("No Image");
  68.                 noim.setFont(PROFILE_IMAGE_FONT);
  69.                 add(noim,LEFT_MARGIN+IMAGE_WIDTH/2-noim.getWidth()/2,IMAGE_MARGIN+IMAGE_HEIGHT/2-noim.getHeight()/2+TOP_MARGIN+name.getAscent());
  70.                 }
  71.                
  72.                
  73.                
  74.                 if(profile.getStatus()!=null){
  75.                         status=new GLabel(profile.getName()+ " is "+profile.getStatus());
  76.                 }
  77.                 status.setFont(PROFILE_STATUS_FONT);
  78.                 add(status,LEFT_MARGIN,STATUS_MARGIN+IMAGE_MARGIN+IMAGE_HEIGHT+TOP_MARGIN+name.getAscent());
  79.                
  80.                
  81.                 Friends.setFont(PROFILE_FRIEND_LABEL_FONT);
  82.                 add(Friends, getWidth()/2,IMAGE_MARGIN-Friends.getAscent()+TOP_MARGIN+name.getAscent());
  83.                 int i=0;
  84.                 Iterator <String> it=profile.getFriends();
  85.                 while(it.hasNext()){
  86.                        
  87.                         GLabel friend=new GLabel(it.next());
  88.                         friend.setLocation(getWidth()/2,IMAGE_MARGIN+TOP_MARGIN+name.getAscent()+i*friend.getHeight());
  89.                         i++;
  90.                         add(friend);
  91.                 }
  92.                

  93.                        
  94.                
  95.                
  96.                
  97.                
  98.                 // You fill this in
  99.         }

  100.         private GLabel Friends;
  101.         private GLabel status;
  102.         private GLabel name;
  103.         private GLabel message;
  104. }
复制代码
  1. /*
  2. * File: FacePamphletProfile.java
  3. * ------------------------------
  4. * This class keeps track of all the information for one profile
  5. * in the FacePamphlet social network.  Each profile contains a
  6. * name, an image (which may not always be set), a status (what
  7. * the person is currently doing, which may not always be set),
  8. * and a list of friends.
  9. */

  10. import acm.graphics.*;
  11. import java.util.*;

  12. public class FacePamphletProfile implements FacePamphletConstants {
  13.        
  14.         /**
  15.          * Constructor
  16.          * This method takes care of any initialization needed for
  17.          * the profile.
  18.          */
  19.         public FacePamphletProfile(String Name) {
  20.                 // You fill this in
  21.                 name=Name;
  22.                 status="null";
  23.                 image=null;
  24.                 friends=new ArrayList<String>();
  25.                
  26.         }

  27.         /** This method returns the name associated with the profile. */
  28.         public String getName() {
  29.                 // You fill this in.  Currently always returns the empty string.
  30.                 return name;
  31.         }

  32.         /**
  33.          * This method returns the image associated with the profile.  
  34.          * If there is no image associated with the profile, the method
  35.          * returns null. */
  36.         public GImage getImage() {
  37.                 // You fill this in.  Currently always returns null.
  38.                 return image;
  39.         }

  40.         /** This method sets the image associated with the profile. */
  41.         public void setImage(GImage Image) {
  42.                 // You fill this in
  43.                 image=Image;
  44.         }
  45.        
  46.         /**
  47.          * This method returns the status associated with the profile.
  48.          * If there is no status associated with the profile, the method
  49.          * returns the empty string ("").
  50.          */
  51.         public String getStatus() {
  52.                 // You fill this in.  Currently always returns the empty string.
  53.                 return status;
  54.         }
  55.        
  56.         /** This method sets the status associated with the profile. */
  57.         public void setStatus(String Status) {
  58.                 // You fill this in
  59.                 status=Status;
  60.         }

  61.         /**
  62.          * This method adds the named friend to this profile's list of
  63.          * friends.  It returns true if the friend's name was not already
  64.          * in the list of friends for this profile (and the name is added
  65.          * to the list).  The method returns false if the given friend name
  66.          * was already in the list of friends for this profile (in which
  67.          * case, the given friend name is not added to the list of friends
  68.          * a second time.)
  69.          */
  70.         public boolean addFriend(String friend) {
  71.                 // You fill this in.  Currently always returns true.
  72.                 it=friends.iterator();
  73.                 while(it.hasNext()){
  74.                         String test=it.next();
  75.                         if(test.equals(friend)) return false;
  76.                 }
  77.                
  78.                 friends.add(friend);
  79.                
  80.                 return true;
  81.                 // need to ask whether it will return false for any case, or return false for some point and iterate until
  82.                 //finish, then return true; same for removeFriend()
  83.         }
  84.        
  85.        

  86.         /**
  87.          * This method removes the named friend from this profile's list
  88.          * of friends.  It returns true if the friend's name was in the
  89.          * list of friends for this profile (and the name was removed from
  90.          * the list).  The method returns false if the given friend name
  91.          * was not in the list of friends for this profile (in which case,
  92.          * the given friend name could not be removed.)
  93.          */
  94.         public boolean removeFriend(String friend) {
  95.                 // You fill this in.  Currently always returns false.
  96.                 it=friends.iterator();
  97.                 while(it.hasNext()){
  98.                         String test=it.next();
  99.                         if(friend.equals(test)){
  100.                                 friends.remove(friend);
  101.                                 return true;
  102.                         }
  103.                 }
  104.                 return false;
  105.         }

  106.         /**
  107.          * This method returns an iterator over the list of friends
  108.          * associated with the profile.
  109.          */
  110.         public Iterator<String> getFriends() {
  111.                 // You fill this in.  Currently always returns null.
  112.                
  113.                 return friends.iterator();
  114.         }
  115.        
  116.         /**
  117.          * This method returns a string representation of the profile.  
  118.          * This string is of the form: "name (status): list of friends",
  119.          * where name and status are set accordingly and the list of
  120.          * friends is a comma separated list of the names of all of the
  121.          * friends in this profile.
  122.          *
  123.          * For example, in a profile with name "Alice" whose status is
  124.          * "coding" and who has friends Don, Chelsea, and Bob, this method
  125.          * would return the string: "Alice (coding): Don, Chelsea, Bob"
  126.          */
  127.         public String toString() {
  128.                 // You fill this in.  Currently always returns the empty string.
  129.                 String friendlist="";
  130.                 it=friends.iterator();
  131.                 while(it.hasNext()){
  132.                         String temp=it.next();
  133.                         friendlist=temp+", "+friendlist;
  134.                 }
  135.                 return name+" ("+status+"): "+friendlist;
  136.         }

  137.         public ArrayList<String> friends;
  138.         private Iterator<String> it;
  139.         private GImage image;
  140.         private String name;
  141.         private String status;
  142.        
  143. }
复制代码
  1. /*
  2. * File: FacePamphletDatabase.java
  3. * -------------------------------
  4. * This class keeps track of the profiles of all users in the
  5. * FacePamphlet application.  Note that profile names are case
  6. * sensitive, so that "ALICE" and "alice" are NOT the same name.
  7. */

  8. import java.util.*;

  9. public class FacePamphletDatabase implements FacePamphletConstants {

  10.         /**
  11.          * Constructor
  12.          * This method takes care of any initialization needed for
  13.          * the database.
  14.          */
  15.         public FacePamphletDatabase() {
  16.                 // You fill this in
  17.                 //namelist=new ArrayList<FacePamphletProfile>();
  18.         }
  19.        
  20.        
  21.         /**
  22.          * This method adds the given profile to the database.  If the
  23.          * name associated with the profile is the same as an existing
  24.          * name in the database, the existing profile is replaced by
  25.          * the new profile passed in.
  26.          */
  27.         public void addProfile(FacePamphletProfile Profile) {
  28.                
  29.                 namelist.put(Profile.getName(),Profile);
  30.                
  31.                
  32.                
  33.                 // You fill this in
  34.         }

  35.        
  36.         /**
  37.          * This method returns the profile associated with the given name
  38.          * in the database.  If there is no profile in the database with
  39.          * the given name, the method returns null.
  40.          */
  41.         public FacePamphletProfile getProfile(String name) {

  42.                
  43.                
  44.                 if(namelist.containsKey(name)){
  45.                         return(namelist.get(name));
  46.                                
  47.                
  48.                 }
  49.                 return null;
  50.                
  51.         }
  52.        
  53.        
  54.         /**
  55.          * This method removes the profile associated with the given name
  56.          * from the database.  It also updates the list of friends of all
  57.          * other profiles in the database to make sure that this name is
  58.          * removed from the list of friends of any other profile.
  59.          *
  60.          * If there is no profile in the database with the given name, then
  61.          * the database is unchanged after calling this method.
  62.          */
  63.         public void deleteProfile(String name) {
  64.                        
  65.                 if(namelist.containsKey(name)) namelist.remove(name);
  66.                 it=namelist.keySet().iterator();
  67.                 while(it.hasNext()){
  68.                        
  69.                        
  70.                         String temp=it.next();
  71.                        
  72.                         while(namelist.get(temp).getFriends().hasNext()){
  73.                                 String friend=namelist.get(temp).getFriends().next();
  74.                                 if(name.equals(friend)) namelist.get(temp).removeFriend(name);
  75.                                
  76.                         }
  77.                 }
  78.                                
  79.                        
  80.                
  81.         }

  82.        
  83.         /**
  84.          * This method returns true if there is a profile in the database
  85.          * that has the given name.  It returns false otherwise.
  86.          */
  87.         public boolean containsProfile(String name) {
  88.                
  89.                
  90.        
  91.                 if(namelist.containsKey(name)) return true;
  92.                 else return false;
  93.         }
  94.        
  95.        
  96.        
  97.         private HashMap <String,FacePamphletProfile> namelist=new HashMap<String,FacePamphletProfile>();
  98.         //private ArrayList <FacePamphletProfile> namelist;
  99.         private Iterator<String> it;

  100. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表