三个类,打电话,核对身份证,发出包裹

纯属借口 2014-04-12 08:54:57
有一个快递员,负责在校门口派送包裹,首先会打电话给同学,然后由同学带着身份证领取包裹,快递员核对身份证,正确的话发出包裹,不正确的话不发出包裹。
请分析该过程,设计相应的类,并为快递员编写程序实现整个过程。
...全文
293 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
huoxu 2014-04-14
  • 打赏
  • 举报
回复
引用 6 楼 u011461314 的回复:
刚学,想问个问题:类有动作的吗?
类 包含 属性 和 方法 (动作)
_serendipity_ 2014-04-14
  • 打赏
  • 举报
回复





public interface Call {
public void makdPhoneCall(String phoneNumber);
}

public class CallBean implements Call {
@Override
public void makdPhoneCall(String phoneNumber) {
throw new UnsupportedOperationException("Not supported yet.");
}
}

public interface CheckId {
public Boolean checkId(IdCard idCard);
}

public class CheckIdBean implements CheckId {
@Override
public Boolean checkId(IdCard idCard) {
throw new UnsupportedOperationException("Not supported yet.");
}
}

public interface SendParcel {
public Boolean sendParcel(Parcel parcel);
}

public class SendParcelBean implements SendParcel {
@Override
public Boolean sendParcel(Parcel parcel) {
throw new UnsupportedOperationException("Not supported yet.");
}
}

public class IdCard {
private String idNumber;
public IdCard(String idNumber) {
this.idNumber = idNumber;
}
public String getIdNumber() {
return idNumber;
}
}

public class Parcel {
}

public class Courier {

@Inject private Call call;
@Inject private CheckId checkId;
@Inject private SendParcel sendParcel;

private IdCard idCard;
private Parcel parcel;

public Courier(IdCard idCard, Parcel parcel) {
this.idCard = idCard;
this.parcel = parcel;
}

public void send() {
call.makdPhoneCall("12312312312");
if (checkId.checkId(idCard)) {
sendParcel.sendParcel(parcel);
}
}

public static void main(String[] args) {
IdCard idCard = new IdCard("111111111111111111");
Parcel parcel = new Parcel();
Courier courier = new Courier(idCard, parcel);
courier.send();
}
}
java_liyi 2014-04-14
  • 打赏
  • 举报
回复
抛块砖先


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运行环境包含兩部份組成
 * <ul>
 * <li>1.可以接收邮件的人</li>
 * <li>2.邮递员</li>
 * </ul>
 */
public class Environment {
	static private List<Student> personList;
	static private Map<String, Courier> courierList;

	public Environment(Map<String, Courier> courierList,
		List<Student> personList) {
		Environment.courierList = courierList;
		Environment.personList = personList;
	}

	// 开始执行
	public void run() {
		Thread thread;
		for (Courier c : courierList.values()) {
			thread = new Thread(c);
			thread.start();
		}
	}

	public static List<Student> getPersonList() {
		return personList;
	}

	public static Courier getCourier(String courierid) {
		return courierList.get(courierid);
	}

	// 通过电话号码查找收件人
	public static Person getPerson(String phoneNumber) {
		if (phoneNumber == null)
			throw new RuntimeException("请输入电话号码!");
		if (personList == null || personList.size() == 0)
			return null;
		for (Person person : personList) {
			if (phoneNumber.equals(person.getPhone()))
				return person;
		}
		return null;
	}

	// 准备多个邮递员
	public static Map<String, Courier> prepareCouriers() {
		Map<String, Courier> map = new HashMap();
		Courier s1 = new Courier("申通", "小王", "026-01");
		s1.addPacks(preparePacks());
		Courier s2 = new Courier("中通", "小赵", "026-02");
		s2.addPacks(preparePacks());
		map.put(s1.getId(), s1);
		map.put(s2.getId(), s2);
		return map;
	}

	// 准备多个收件人对象数据
	public static List<Student> prepareReceivers() {
		List<Student> l = new ArrayList();
		Student s1 = new Student("s1", "李雷", "026-01");
		Student s2 = new Student("s2", "韩梅梅", "026-02");
		Student s3 = new Student("s3", "Lucy", "026-03");
		Student s4 = new Student("s4", "Lily", "026-04");
		l.add(s1);
		l.add(s2);
		l.add(s3);
		l.add(s4);
		return l;
	}

	// 准备多个要派发的包裹
	// 从理论上说,多个邮递员间的包裹可以是重复的,但是每个邮递员领取的包裹id是不允许重复的
	public static List<Pack> preparePacks() {
		List<Pack> l = new ArrayList<Pack>();
		Pack s1 = new Pack("p1", "s1", "李雷", "026-01");// 正确
		Pack s2 = new Pack("p2", "s2", "韩梅梅", "026-02");// 正确
		Pack s3 = new Pack("p3", "s3", "Lucy", "026-03");// 正确
		Pack s4 = new Pack("p4", "s4", "Lily", "026-08");// 无人应答
		Pack s5 = new Pack("p5", "s3", "小明", "026-04");// 包裹与电话收件人不符
		Pack s6 = new Pack("p6", "s5", "韩梅梅", "026-03");// 包裹与电话收件人不符
		Pack s7 = new Pack("p7", "s5", "韩梅梅", "026-02");// 包裹与电话收件人不符
		l.add(s1);
		l.add(s2);
		l.add(s3);
		l.add(s4);
		l.add(s5);
		l.add(s6);
		l.add(s7);
		return l;
	}

	/**
	 * 入口
	 * @param args
	 */
	public static void main(String[] args) {
		// 加入运行场景
		new Environment(prepareCouriers(),
			prepareReceivers()).run();
	}
}




import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 快递员
 */
public class Courier extends Person implements Runnable{
	private Map<String, Pack> packList;

	public Map<String, Pack> getPackList() {
		return packList;
	}

	public Courier(String id, String name, String phone) {
		super(id, name, phone);
	}
	
	

	// 一次领取一个要派发的包裹
	public void addPack(Pack pack) {
		if (this.packList == null)
			this.packList = new HashMap<String, Pack>();
		packList.put(pack.getId(), pack);
	}

	// 一次领取多个要派发的包裹
	public void addPacks(List<Pack> packList) {
		for (Pack pack : packList) {
			addPack(pack);
		}
	}

	// 派发包裹
	public void distribute() {
		Iterator<Pack> itor =  packList.values().iterator();
		for (Iterator<Pack> it =itor;it.hasNext();) {
			distribute(it.next());
		}
	}

	// 派发包裹
	private void distribute(Pack pack) {
		// 打电话给收件人
		// 如果收件人应答,则继续发件,不在则返回
		Person p;
		System.out.println(this.toString()+":翻查了包裹"+pack.getId()+"的收件人信息。");
		if ((p = this.phone(pack.getPhone())) != null) {
			// 收件人存在,先将包裹id标记给收件人
			System.out.println(this.toString()+":非常幸运,电话接通了!接电话的人是"+p.getName());
			if (!p.getName().equals(pack.getPname())) {
				System.out.println(this.toString()+":对不起,我要找的人是"+pack.getPname()+",打扰了!");
				return;
			}
			p.receive(this.getId(), pack.getId());//通知P来收包裹
			return;
		}System.out.println(this.toString()+":没有人应答!");
	}

	/**
	 * 快递员拣取与人员提供信息相符的包裹
	 * @param packid 包裹ID
	 * @param person 收件人
	 */
	public Pack getPack(String packid, Person person) {
		Pack pack = getPack(packid);
		if (pack == null) {
			System.out.println(this.toString()
				+ ":非常遗憾,我找寻不到你说的包裹!");
			return null;
		}
		if( person == null
			|| person.getId() == null){
			System.out.println(this.toString()
				+ ":鬼啊!");
			return null;
		}
		if (!pack.getPid().equals(person.getId()) || !pack.getPname().equals(person.getName())) {
			System.out.println(this.toString()
				+ ":对不起,你不是收件人!我不能将包裹拿给你!");
			return null;
		}
		System.out
			.println(this.toString() + ":请签名!");
		// 将包裹从自身的列表中剔除
		return pack;
	}

	/**
	 * 根据包裹id查找包裹
	 */
	private Pack getPack(String packid) {
		if (packList == null || packList.size() == 0)
			return null;
		Pack p = packList.get(packid);
		return p.isDirty()?null:p;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		this.distribute();
	}
	
	public @Override String toString(){
		return this.getId()+super.toString();
	}
	
	@Override
	public String getType(){
		return "快递员";
	}
}




/**
 * 包裹类
 *
 */
public class Pack {
	private String id;//包裹id
	private String pid;//收件人Id
	private String pname;//收件人姓名
	private String phone;//收件人电话
	private boolean dirty = false;//包裹的状态,未领取时为false,领取后未true
	private String signature;
	public Pack(String id,String pid,String pname,String phone){
		this.id = id;
		this.pid = pid;
		this.pname = pname;
		this.phone = phone;
	}
	
	public String getId() {
		return id;
	}
	public String getPhone() {
		return phone;
	}
	public String getPid() {
		return pid;
	}
	public String getPname() {
		return pname;
	}

	public boolean isDirty() {
		return dirty;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.dirty=true;
		this.signature = signature;
	}
}




import java.util.ArrayList;
import java.util.List;

/**
 * 人员类
 *
 */
public class Person{
	private String id;
	private String name;
	private String phone;
	
	public Person(String id,String name,String phone){
		this.id = id;
		this.name = name;
		this.phone = phone;
	}
	
	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getPhone() {
		return phone;
	}
	public Person phone(String phoneNumber){
		//从上下文中查询是否存在用户
		System.out.println(this.toString()+":拨打了电话:"+phoneNumber);
		return Environment.getPerson(phoneNumber);
	}
	
	public void receive(String courierid,String packid){
		System.out.println(this.toString() + ":我马上就赶过来接收包裹!");
		Thread thread = new Thread(new PersonPack(this,courierid,packid));
		thread.start();
	}
	
	/**
	 * 签名
	 */
	public void signature(String courier,Pack pack){
		pack.setSignature(this.getName());
		System.out.println(this.toString()+"已经了签收由"+Environment.getCourier(courier).toString()+"派出的邮件"+pack.getId());
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return getType()+this.getName();
	}
	
	public String getType(){
		return "人";
	}
	
	/**
	 * Person的包裹 *
	 */
	public class PersonPack implements Runnable{
		Person person;
		String courier;
		String packid;
		
		public PersonPack(Person person,String courier,String packid){
			this.person = person;
			this.courier = courier;
			this.packid = packid;
			
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			System.out.println(person.toString()+":呼呼....,久等了");
			System.out.println(String.format(person.toString()+":你好!我要收取一个id为%s的包裹",packid));
			Pack p = Environment.getCourier(courier).getPack(packid, person);
			if(p!=null){
				person.signature(courier,p);//签收
				System.out.println(person.toString()+":非常感谢,我已经收到了包裹");return;
			}	
			System.out.println(person.toString()+":我的包裹去哪了....");
		}
	}
}



public class Student extends Person{
	public Student(String id, String name, String phone) {
		super(id, name, phone);
	}
	@Override
	public String getType(){
		return "学生";
	}
}

_serendipity_ 2014-04-14
  • 打赏
  • 举报
回复
画个顺序图,一清二楚。
tony4geek 2014-04-13
  • 打赏
  • 举报
回复
按照 面向对象的思想做,来看看回复。
-江沐风- 2014-04-13
  • 打赏
  • 举报
回复
学生算是个类,身份证信息id可以作为属性; 快递员也可以作为一个类,打电话可以通过方法来实现;
-江沐风- 2014-04-13
  • 打赏
  • 举报
回复
引用 6 楼 u011461314 的回复:
刚学,想问个问题:类有动作的吗?
类是对象,动作可以通过方法来实现;
纯属借口 2014-04-13
  • 打赏
  • 举报
回复
引用 6 楼 u011461314 的回复:
刚学,想问个问题:类有动作的吗?
我也刚学 什么动作?
zhjdg 2014-04-13
  • 打赏
  • 举报
回复
刚学,想问个问题:类有动作的吗?
纯属借口 2014-04-13
  • 打赏
  • 举报
回复
引用 4 楼 gagewang1 的回复:
我认为是要写出大概的设计思路,而不是具体的实现。 有哪几个class,class有哪些method

class Person{

}

class Courier extends Person{

}







class 有打电话 核实身份证 发出包裹
纯属借口 2014-04-13
  • 打赏
  • 举报
回复
引用 9 楼 u012724379 的回复:
学生算是个类,身份证信息id可以作为属性; 快递员也可以作为一个类,打电话可以通过方法来实现;
打电话咋用方法实现
中华雪碧 2014-04-12
  • 打赏
  • 举报
回复
我认为是要写出大概的设计思路,而不是具体的实现。 有哪几个class,class有哪些method

class Person{

}

class Courier extends Person{

}







纯属借口 2014-04-12
  • 打赏
  • 举报
回复
引用 1 楼 u012724379 的回复:
这个,可以看看设计模式; 你可以先把你的意思给表达出来,让大家在你的理解的基础上,帮你分析分析;
我想问那个打电话类 怎么实现?
-江沐风- 2014-04-12
  • 打赏
  • 举报
回复
这个,可以看看设计模式; 你可以先把你的意思给表达出来,让大家在你的理解的基础上,帮你分析分析;

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧