问题描述:
在“虚拟聊天室”实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:
1. 新的具体聊天室中发送的图片大小不得超过20M。
2. 新的具体聊天室中发送的文字长度不得超过100个字符。
3. 新的具体会员类可以发送图片信息和文本信息。
4. 新的具体会员类在发送文本信息时,可以在信息后加上时间。
修改客户端测试类,注意原有系统类代码和客户端代码的修改。
Java源代码:
1 //Member.java 2 package shiyan19; 3 4 public abstract class Member { 5 protected AbstractChatroom room; 6 protected String name; 7 public Member(String a){ 8 name=a; 9 } 10 public String getname(){ 11 return name; 12 } 13 public void setname(String a){ 14 name=a; 15 } 16 public AbstractChatroom getroom(){ 17 return room; 18 } 19 public void setroom(AbstractChatroom a){ 20 room=a; 21 } 22 public abstract void sendtext(String a,String mess); 23 public abstract void sendimage(String a,String image); 24 public void receivetext(String a,String mess){ 25 System.out.println(a+"发送的给"+name+"内容是"+mess); 26 } 27 public void receiveimage(String a,String image){ 28 System.out.println(a+"发送的给"+name+"内容是"+image); 29 } 30 } 31 //CommonMember.java 32 package shiyan19; 33 34 35 public class CommonMember extends Member{ 36 public CommonMember(String a){ 37 super(a); 38 } 39 public void sendtext(String a,String c){ 40 System.out.println("普通会员发送消息"); 41 room.sendtext(name,a,c); 42 } 43 public void sendimage(String a,String c){ 44 System.out.println("普通会员不能发送图片"); 45 } 46 } 47 //DiamondMember.java 48 package shiyan19; 49 50 public class DiamondMember extends Member{ 51 public DiamondMember(String a){ 52 super(a); 53 } 54 public void sendtext(String a,String c){ 55 System.out.println("钻石会员发送消息"); 56 room.sendtext(name,a,c); 57 } 58 public void sendimage(String a,String c){ 59 System.out.println("钻石会员发送图片"); 60 room.sendimage(name,a,c); 61 } 62 } 63 //NewGroup.java 64 package shiyan19; 65 66 import java.util.HashMap; 67 import java.util.Map; 68 public class NewGroup extends AbstractChatroom{ 69 70 Map<String,Member> table = new HashMap<>(); 71 public void register(Member member) { 72 table.put(member.getname(), member); 73 member.setroom(this); 74 } 75 76 public void sendtext(String from, String to, String message) { 77 if(message.length()>=100){ 78 System.out.println("超过100个字符,发送失败"); 79 }else{ 80 Member member = table.get(to); 81 if(member!=null) 82 member.receivetext(from, message); 83 } 84 } 85 86 public void sendimage(String from, String to, String message) { 87 if(message.length()>=20){ 88 System.out.println("图片超过20M,发送失败"); 89 }else{ 90 Member member = table.get(to); 91 if(member!=null) 92 member.receiveimage(from, message); 93 } 94 } 95 96 } 97 //NewMember.java 98 package shiyan19; 99 100 public class NewMember extends Member{ 101 public NewMember(String a){ 102 super(a); 103 } 104 public void sendtext(String a,String c){ 105 System.out.println("新会员发送消息:"); 106 room.sendtext(name,a,c); 107 } 108 public void sendimage(String a,String c){ 109 System.out.println("新会员发送图片:"); 110 room.sendimage(name,a,c); 111 } 112 } 113 //AbstractChatroom.java 114 package shiyan19; 115 116 public abstract class AbstractChatroom { 117 public abstract void register(Member m); 118 public abstract void sendtext(String a,String b,String mess); 119 public abstract void sendimage(String a,String b,String mess); 120 } 121 //ChatGroup.java 122 package shiyan19; 123 124 import java.util.*; 125 126 public class ChatGroup extends AbstractChatroom{ 127 private Hashtable ms=new Hashtable(); 128 public void register(Member m) 129 { 130 if(!ms.contains(m)){ 131 ms.put(m.getname(),m); 132 m.setroom(this); 133 } 134 } 135 public void sendtext(String a,String b,String c){ 136 Member m=(Member)ms.get(b); 137 String newm=c; 138 newm=c.replaceAll("日","*"); 139 m.receivetext(a,newm); 140 } 141 public void sendimage(String a,String b,String c){ 142 Member m=(Member)ms.get(b); 143 if(c.length()>5){ 144 System.out.println("图片较大,发送失败"); 145 }else{ 146 m.receiveimage(a,c); 147 } 148 } 149 } 150 //main.java 151 package shiyan19; 152 153 public class main { 154 public static void main(String args[]){ 155 AbstractChatroom r1=new NewGroup(); 156 Member m1,m2,m3; 157 m1=new NewMember("小李"); 158 m2=new NewMember("小红"); 159 m3=new NewMember("张三"); 160 r1.register(m1); 161 r1.register(m2); 162 r1.register(m3); 163 m1.sendtext("小红",":小红,你好"); 164 m2.sendtext("小李",":小李,你好"); 165 m1.sendtext("小红",":吃饭了吗?"); 166 m2.sendtext("小李",":吃了"); 167 m1.sendtext("张三",":---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"); 168 m3.sendimage("小李",":哈哈.jpg"); 169 m2.sendimage("张三",":哈哈.jpg--------------------"); 170 } 171 }
运行结果: