Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

MessageClypeData.

java
1 package data;
2
3 public class MessageClypeData extends ClypeData {
4
5 /**
6 *
7 */
8 private static final long serialVersionUID = -8426073677763852305L;
9 private String message;
10
11 /**
12 *
13 * Constructor that takes a username, message, and type.
14 *
15 * @param userName username associated with the data
16 * @param message message associated with the data
17 * @param type type of the data, defined in ClypeData.
18 */
19 public MessageClypeData(String userName, String message, int type, int clientID) {
20 super(userName, type, clientID);
21
22 this.message = message;
23
24 }
25
26 /**
27 *
28 * Constructor that encrypts a message with a key using the vigenere cypher.
29 *
30 * @param userName username associated with the data
31 * @param message message associated with the data
32 * @param key a String that is the key used for encryption of the message
33 * @param type type of the data, defined in ClypeData.
34 */
35 public MessageClypeData(String userName, String message, String key, int type, int
clientID) {
36 super(userName, type, clientID);
37 this.message = encrypt(message, key);
38 }
39
40 /**
41 * Default constructor that sets username to "Anonymous", the message is defaultd to "",
and the type is defaulted to 3.
42 */
43 public MessageClypeData(int clientID) {
44 this("Anonymous","",ClypeData.SEND_MESSAGE, clientID);
45 }
46
47 /**
48 * Overriden method of getData() which returns the message
49 * @return the message
50 */
51 @Override
52 public String getData() {
53 return message;
54 }
55
56 /**
57 * Overriden method of getData(String key) which returns the decrypted message
MessageClypeData.java
58 *
59 * @param key the String used as a key for encryption and decryption
60 * @return the decrypted message using the key
61 */
62 @Override
63 public String getData(String key) {
64 return decrypt(message, key);
65 }
66
67 /**
68 * Overriden method of equals(Object obj), which makes sure the object passed in is not
null and is the correct type.
69 * Then checks if the following are equal: userName, message, and type
70 */
71 @Override
72 public boolean equals(Object obj) {
73 if(this == obj)
74 return true;
75 if(obj == null || !(obj instanceof MessageClypeData))
76 return false;
77
78 MessageClypeData m = (MessageClypeData) obj;
79 if(m.getUserName() == this.getUserName() && m.getData() == this.getData() &&
m.getType() == this.getType())
80 return true;
81 else
82 return false;
83 }
84
85 /**
86 * Overriden hashCode() method.
87 */
88 @SuppressWarnings("deprecation")
89 @Override
90 public int hashCode() {
91 //from lecture notes
92 int result = 17;
93 if(this.getUserName() != null)
94 result = 37*result + this.getUserName().hashCode();
95 if(this.message != null)
96 result = 37*result + this.message.hashCode();
97 result = 37*result + this.getType();
98 result = 37*result + this.getDate().getDate();
99 return result;
100 }
101
102 /**
103 * @return useful data that gets printed to the console for the user to view
104 */
105 @Override
106 public String getDataToPrint() {
107 return this.getUserName() + ": " + this.getData();
108 }
109
110 /**
111 * Overriden toString() method which returns/prints the following: userName, message,
type,
112 * and the date of creation.
113 */
MessageClypeData.java
114 @Override
115 public String toString() {
116
117 return "Username: " + this.getUserName() + "\n"
118 + "Message: " + this.getData() + "\n"
119 + "Type: Message (" + this.getType() + ")\n"
120 + "Date Created: " + this.getDate() + "\n";
121
122 }
123
124 }
125

You might also like