[实验任务一]:虚拟聊天室
在“虚拟聊天室”实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:
- 新的具体聊天室中发送的图片大小不得超过20M。
- 新的具体聊天室中发送的文字长度不得超过100个字符。
- 新的具体会员类可以发送图片信息和文本信息。
- 新的具体会员类在发送文本信息时,可以在信息后加上时间。
修改客户端测试类,注意原有系统类代码和客户端代码的修改。
代码
java
AbstractChartroom.java
package test19;
public abstract class AbstractChartroom {
public abstract void register(Member member);
public abstract void sendText(String from,String to,String message);
public abstract void sendImage(String from,String to,String image);
}
Client.java
package test19;
public class Client {
public static void main(String[] args) {
AbstractChartroom happyChat = new NewChatGroup();
Member member1,member2,member3,member4;
member1 = new NewMember("张三");
member2 = new NewMember("李四");
member3 = new NewMember("王五");
member4 = new NewMember("小芳");
happyChat.register(member1);
happyChat.register(member2);
happyChat.register(member3);
happyChat.register(member4);
member1.sendImage("王五","lll");
member1.sendText("王五","pppp");
member3.sendText("小芳","dddd");
member3.sendImage("小芳","oooo");
}
}
Member.java
package test19;
public abstract class Member {
protected AbstractChartroom chartroom;
protected String name;
public Member(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AbstractChartroom getChartroom() {
return chartroom;
}
public void setChartroom(AbstractChartroom chartroom) {
this.chartroom = chartroom;
}
public abstract void sendText(String to,String message);
public abstract void sendImage(String to,String image);
public void receiveText(String from,String message){
System.out.println(from+"发送文本给"+this.name+",内容为"+message);
}
public void receiveImage(String from,String image){
System.out.println(from+"发送图片给"+this.name+",内容为"+image);
}
}
NewChatGroup.java
package test19;
import java.util.Date;
import java.util.Hashtable;
public class NewChatGroup extends AbstractChartroom{
Hashtable members = new Hashtable();
@Override
public void register(Member member) {
if(!members.contains(member)){
members.put(member.getName(),member);
member.setChartroom(this);
}
}
@Override
public void sendText(String from, String to, String message) {
Member o = (Member)members.get(to);
if(message.length()>100){
System.out.println("文字长度不能超过100个字符");
}else {
System.out.println(from+"发消息给"+to+"内容为:"+message+"时间为:"+new Date());
}
}
@Override
public void sendImage(String from, String to, String image) {
Member o = (Member)members.get(to);
if(image.length()>20){
System.out.println("文字长度不能超过100个字符");
}else {
System.out.println(from+"发图片给"+to+"内容为:"+image);
}
}
}
NewMember.java
package test19;
public class NewMember extends Member{
public NewMember(String name) {
super(name);
}
@Override
public void sendText(String to, String message) {
System.out.println("会员发送消息");
chartroom.sendText(name,to,message);
}
@Override
public void sendImage(String to, String image) {
System.out.println("会员发送图片");
chartroom.sendImage(name,to,image);
}
}
C++
#include <iostream>
#include <unordered_map>
using namespace std;
class AbstractChatroom;
class Member{
public:
AbstractChatroom *chatroom;
string name;
Member(string name){
this->name = name;
}
string getName(){return this->name;}
void setName(string name){ this->name = name;}
AbstractChatroom *getChatroom(){return this->chatroom;}
void setChatroom(AbstractChatroom* chatroom){ this->chatroom = chatroom;}
virtual void sendText(string to,string message){}
virtual void sendImage(string to,string image){}
void receiveText(string from,string message){
cout<<from+"给"+name+"发送消息:"+message<<endl;
}
void receiveImage(string from,string image){
cout<<from+"给"+name+"发送图片:"+image<<endl;
}
};
class AbstractChatroom{
public:
virtual void login(Member* member){}
virtual void sendText(string from,string to,string message){}
virtual void sendImage(string from,string to,string image){}
};
class NewGroup: public AbstractChatroom{
private:
unordered_map<string,Member*> members;
public:
void login(Member *member) override{
if(members[member->name]==NULL){
members[member->name] = member;
cout<<member->name<<"已经注册"<<endl;
}
member->setChatroom(this);
cout<<member->name<<"已经登录"<<endl;
}
void sendText(string from,string to,string message){
if (message.length()<100){
Member* m = members[to];
m->receiveText(from,message+"当前时间");
} else{
cout<<"发送失败,文字长度不得超过100"<<endl;
}
}
void sendImage(string from,string to,string image){
if(image.length()<20){
Member* m = members[to];
m->receiveImage(from,image);
}else{
cout<<"发送失败,图片不得超过20M"<<endl;
}
}
};
class NewMember: public Member{
public:
NewMember(string name):Member(name){
}
void sendText(string to,string message){
chatroom->sendText(name,to,message);
}
void sendImage(string to,string image){
chatroom->sendImage(name,to,image);
}
};
int main(){
AbstractChatroom* chatroom = new NewGroup();
Member *m1,*m2,*m3,*m4;
m1 = new NewMember("张三");
m2 = new NewMember("李四");
m3 = new NewMember("王五");
m4 = new NewMember("小芳");
chatroom->login(m1);
chatroom->login(m2);
chatroom->login(m3);
chatroom->login(m4);
m1->sendImage("王五","lll");
m1->sendText("王五","pppp");
m3->sendText("小芳","dddd");
m3->sendImage("小芳","oooo");
return 0;
}
标签:name,19,void,模式,Member,中介,image,public,String
From: https://www.cnblogs.com/java-six/p/16954538.html