Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. public class Message {
  2. // the properties of a message
  3. private String sender;
  4. private String receiver;
  5. private String subject;
  6. private String body;
  7.  
  8. // all property values are known at creation of the message; so initialize
  9. public Message (String s, String r, String sub, String b)
  10. {
  11. sender = s;
  12. receiver = r;
  13. subject = sub;
  14. body = b;
  15. }
  16.  
  17. // any nice format of printing the names and the values of the properties will do
  18. public void printMsg()
  19. {
  20. System.out.println("Sender: " + sender);
  21. System.out.println("Receiver: " + receiver);
  22. System.out.println("Subject: " + subject);
  23. System.out.println("Message: " + body);
  24. }
  25.  
  26. // what follows are basic getter methods
  27.  
  28. public String getSender()
  29. {
  30. return sender;
  31. }
  32.  
  33. public String getReceiver()
  34. {
  35. return receiver;
  36. }
  37.  
  38. public String getSubject()
  39. {
  40. return subject;
  41. }
  42.  
  43. public String getBody()
  44. {
  45. return body;
  46. }
  47.  
  48. }
  49.  
  50. import java.util.ArrayList;
  51.  
  52. public class Userlist
  53. {
  54.  
  55. private ArrayList<User> users;
  56. private int numUsers;
  57.  
  58. public Userlist()
  59. {
  60. users = new ArrayList<User>();
  61. }
  62.  
  63. public User findUser(String username)
  64. {
  65. for (User i : users)
  66. {
  67. if (i.userName.equals(username))
  68. return i;
  69. }
  70. return null;
  71. }
  72.  
  73. public void addUser(User u)
  74. {
  75. if (findUser(u.userName) != null)
  76. System.out.println("User already exists");
  77. else
  78. {
  79. users.add(u);
  80. numUsers++;
  81. }
  82. }
  83.  
  84. public int getNumUsers()
  85. {
  86. return this.numUsers;
  87. }
  88.  
  89. public User getUser(int i)
  90. {
  91. if (i>=users.length)
  92. return null;
  93. else
  94. return users.get(i-1);
  95. }
  96. }
  97.  
  98. public class UserList implements Serializable
  99. {
  100. }
  101.  
  102. public class Message implements Serializable
  103. {
  104. }
  105.  
  106. FileOutputStream fos = new FileOutputStream(filename);
  107. ObjectOutputStream out = new ObjectOutputStream(fos);
  108.  
  109. UserList userList ; // assume this is the object to be serialized
  110. out.writeObject(userList); // serializes the object
  111.  
  112. FileInputStream fis = new FileInputStream(filename);
  113. ObjectInputStream in = new ObjectInputStream(fis);
  114. userList = (UserList) in.readObject();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement