赞
踩
写在前面:本人为初学者,该代码仅供参考,如有不对的地方,欢迎各位大佬评论区指正批评。本菜鸟当不胜感激!
题目描述:现假设要实现一个网络商城系统的部分功能
1、商品状态的订阅:网络商城中的每一种商品,供应商和用户都可以对其进行订阅关注。对供应商来说,当商品缺货的时候,提醒供应商补货。对于用户来说,当商品价格变动的时候,或商品由缺货变成有货的时候,可以自动通知用户。请使用观察者模式,实现这种效果。
2、订单状态的管理:当用户在商城中下了订单后,订单存在着几种状态:下单状态(下订单未付款),已付款状态(付款未发货),已发货状态(发货未确认),已完成状态(用户确认或售后完成),已取消状态(用户未付款或用户取消),售后状态(用户收货确认后有退换货问题)。状态之间存在行为转换,请用状态模式,实现订单状态的转换。
3、订单发货的管理:该商城在全国有西南、西北、东南、东北4个物流中心,订单会根据收货地的范围不同,确定应该由那个物流中心发货。请使用代理模式,实现订单发货的物流管理。
具体代码:
商品实例
- package FinalAssignment.entity;
-
- /**
- * 商品类
- */
- public class Goods {
- private String goodName;//商品名称
- private float price;//商品价格
- private int count;//商品数量
-
- public Goods(String goodName, float price, int count) {
- this.goodName = goodName;
- this.price = price;
- this.count = count;
- }
-
- public String getGoodName() {
- return goodName;
- }
-
- public void setGoodName(String goodName) {
- this.goodName = goodName;
- }
-
- public float getPrice() {
- return price;
- }
-
- public void setPrice(float price) {
- this.price = price;
- }
-
- public int getCount() {
- return count;
- }
-
- public void setCount(int count) {
- this.count = count;
- }
- }
观察者模式部分
- package FinalAssignment.observer.observer;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.subject.Subject;
-
- /**
- * 观察者接口
- */
- public interface Observer {
- void upData(Subject subject, Goods good);
- float getMoney();
- void setMoney(float Money);
- String getAddr();
- void setAddr(String addr);
- String getName();
- void setName(String name);
- }
- //-----------------------------------------------------------------------------
-
- package FinalAssignment.observer.observer.impl;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.observer.subject.impl.Food;
-
- /**
- * 消费者(观察者)
- */
- public class Consumer implements Observer {
- private String name;//消费者的姓名
- private float money;//金钱
- private String addr;//顾客收货地址
-
- @Override
- public String getName() {
- return name;
- }
-
- @Override
- public void setName(String name) {
- this.name=name;
- }
-
-
- public Consumer(String name, float money, String addr) {
- this.name = name;
- this.money=money;
- this.addr = addr;
- }
- public float getMoney() {
- return money;
- }
- public void setMoney(float money) {
- this.money = money;
- }
-
- @Override
- public String getAddr() {
- return addr;
- }
-
- @Override
- public void setAddr(String addr) {
- this.addr=addr;
- }
-
- @Override
- public void upData(Subject subject, Goods good) {
- System.out.println(name+subject.getNotice());
- }
- }
- //-----------------------------------------------------------------------------
- package FinalAssignment.observer.observer.impl;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.observer.subject.impl.Food;
-
- /**
- * 供应商(观察者)
- */
- public class Supplier implements Observer {
- private String name;//供应商姓名
-
- public Supplier(String name) {
- this.name = name;
- }
-
- @Override
- public String getName() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void setName(String name) {
-
- }
-
- @Override
- public void upData(Subject subject, Goods good) {
- System.out.println(subject.getNotice());
- System.out.println(name+"收到!");
- good.setCount(100);
- System.out.println("补货完成!");
- //补货完成后通过商城通知顾客
- subject.setNotice("你关注的商品有货了,快来购买吧!");
- subject.notifyObservers();
- }
-
- @Override
- public float getMoney() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void setMoney(float Money) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public String getAddr() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void setAddr(String addr) {
- throw new UnsupportedOperationException();
- }
- }
- //-----------------------------------------------------------------------------
- package FinalAssignment.observer.subject;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
-
- import java.util.List;
-
- /**
- * 主题接口(商城)
- */
- public interface Subject {
- void addObserver(Observer observer);
- void removeObserver(Observer observer);
- void notifyObservers();
- void addGood(Goods good);
- void removeGood(Goods good);
- void addSupplier(Observer observer);
- void removeSupplier(Observer observer);
- List<Goods> getAllGoods();
- void changePrice(String goodName,float discount);
- void notifySuppliers();
- String getNotice();
- void setNotice(String msg);
- }
-
- //-----------------------------------------------------------------------------
- package FinalAssignment.observer.subject.impl;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
-
- import java.util.ArrayList;
- import java.util.List;
-
-
- /**
- * 网络商城蔬菜区
- */
- public class Vegetables implements Subject {
- //存储顾客
- private List<Observer> consumers;
- //存储供应商
- private List<Observer> suppliers;
- //存储该分区里面的商品
- private List<Goods> commodity;
- //存储通知信息
- String notice;
-
- public String getNotice() {
- return notice;
- }
-
- public void setNotice(String notice) {
- this.notice = notice;
- }
-
- public Vegetables() {
- //在构造函数中对集合初始化
- consumers=new ArrayList<>();
- suppliers=new ArrayList<>();
- commodity=new ArrayList<>();
- }
- //添加顾客方法
- @Override
- public void addObserver(Observer observer) {
- consumers.add(observer);
- }
- //移除顾客方法
- @Override
- public void removeObserver(Observer observer) {
- consumers.remove(observer);
- }
- //通知观察者方法
- @Override
- public void notifyObservers() {
- for (Observer consumer:consumers){
- consumer.upData(this,null);
- }
- }
- //通知供应商补货方法
- @Override
- public void notifySuppliers() {
- for(Goods good:commodity){
-
- if(good.getCount()<=0){
- setNotice(good.getGoodName()+"无货了,请快点补货!");
- for (Observer supplier:suppliers){
- supplier.upData(this,good);
- }
- }
- }
- }
- //添加商品方法
- @Override
- public void addGood(Goods good) {
- commodity.add(good);
- }
- //下架商品方法
- @Override
- public void removeGood(Goods good) {
- commodity.remove(good);
- }
- //添加供应商方法
- @Override
- public void addSupplier(Observer observer) {
- suppliers.add(observer);
- }
- //移除供应商方法
- @Override
- public void removeSupplier(Observer observer) {
- suppliers.remove(observer);
- }
- //显示该区域所有商品
- @Override
- public List<Goods> getAllGoods() {
- return commodity;
- }
- //改变商品价格
- public void changePrice(String goodName,float discount){
- for (Goods good:commodity){
- if(good.getGoodName().equals(goodName)){
- float tmp=good.getPrice();
- good.setPrice(good.getPrice()*discount);
- if(tmp>good.getPrice()) {
- setNotice(goodName + "降价了,快来购买");
- notifyObservers();
- }
- }
- }
-
- }
- }
-
- //-----------------------------------------------------------------------------
- package FinalAssignment.observer.subject.impl;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
-
- import java.util.ArrayList;
- import java.util.List;
-
-
- /**
- * 网络商城食品区
- */
- public class Food implements Subject {
- //存储顾客
- private List<Observer> consumers;
- //存储供应商
- private List<Observer> suppliers;
- //存储该分区里面的商品
- private List<Goods> commodity;
- //存储通知信息
- String notice;
- @Override
- public String getNotice() {
- return notice;
- }
-
- @Override
- public void setNotice(String msg) {
- this.notice = msg;
- }
- public Food() {
- //在构造函数中对集合初始化
- consumers=new ArrayList<>();
- suppliers=new ArrayList<>();
- commodity=new ArrayList<>();
- }
- //添加顾客方法
- @Override
- public void addObserver(Observer observer) {
- consumers.add(observer);
- }
- //移除顾客方法
- @Override
- public void removeObserver(Observer observer) {
- consumers.remove(observer);
- }
- //通知消费者方法
- @Override
- public void notifyObservers() {
- for (Observer consumer:consumers){
- consumer.upData(this,null);
- }
- }
- //通知供应商补货方法
- @Override
- public void notifySuppliers() {
- for(Goods good:commodity){
-
- if(good.getCount()<=0){
- setNotice(good.getGoodName()+"无货了,请快点补货!");
- for (Observer supplier:suppliers){
- supplier.upData(this,good);
- }
- }
- }
- }
- //添加商品方法
- @Override
- public void addGood(Goods good) {
- commodity.add(good);
- }
- //下架商品方法
- @Override
- public void removeGood(Goods good) {
- commodity.remove(good);
- }
- //添加供应商方法
- @Override
- public void addSupplier(Observer observer) {
- suppliers.add(observer);
- }
- //移除供应商方法
- @Override
- public void removeSupplier(Observer observer) {
- suppliers.remove(observer);
- }
- //显示该区域所有商品
- @Override
- public List<Goods> getAllGoods() {
- return commodity;
- }
- //改变商品价格
- public void changePrice(String goodName,float discount){
- for (Goods good:commodity){
- if(good.getGoodName().equals(goodName)){
- float tmp=good.getPrice();
- good.setPrice(good.getPrice()*discount);
- if(tmp>good.getPrice()) {
- setNotice(goodName + "降价了,快来购买");
- notifyObservers();
- }
- }
- }
-
- }
- }
-
- //-----------------------------------------------------------------------------
- package FinalAssignment.observer.subject.impl;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
-
- import java.util.ArrayList;
- import java.util.List;
-
-
- /**
- * 网络商城日用品区
- */
-
- public class DailyNecessities implements Subject {
- //存储顾客
- private List<Observer> consumers;
- //存储供应商
- private List<Observer> suppliers;
- //存储该分区里面的商品
- private List<Goods> commodity;
- //存储通知信息
- String notice;
-
- public String getNotice() {
- return notice;
- }
-
- public void setNotice(String notice) {
- this.notice = notice;
- }
-
- public DailyNecessities() {
- //在构造函数中对集合初始化
- consumers=new ArrayList<>();
- suppliers=new ArrayList<>();
- commodity=new ArrayList<>();
- }
- //添加顾客方法
- @Override
- public void addObserver(Observer observer) {
- consumers.add(observer);
- }
- //移除顾客方法
- @Override
- public void removeObserver(Observer observer) {
- consumers.remove(observer);
- }
- //通知观察者方法
- @Override
- public void notifyObservers() {
- for (Observer consumer:consumers){
- consumer.upData(this,null);
- }
- }
- //通知供应商补货方法
- @Override
- public void notifySuppliers() {
- for(Goods good:commodity){
-
- if(good.getCount()<=0){
- setNotice(good.getGoodName()+"无货了,请快点补货!");
- for (Observer supplier:suppliers){
- supplier.upData(this,good);
- }
- }
- }
- }
- //添加商品方法
- @Override
- public void addGood(Goods good) {
- commodity.add(good);
- }
- //下架商品方法
- @Override
- public void removeGood(Goods good) {
- commodity.remove(good);
- }
- //添加供应商方法
- @Override
- public void addSupplier(Observer observer) {
- suppliers.add(observer);
- }
- //移除供应商方法
- @Override
- public void removeSupplier(Observer observer) {
- suppliers.remove(observer);
- }
- //显示该区域所有商品
- @Override
- public List<Goods> getAllGoods() {
- return commodity;
- }
- //改变商品价格
- public void changePrice(String goodName,float discount){
- for (Goods good:commodity){
- if(good.getGoodName().equals(goodName)){
- float tmp=good.getPrice();
- good.setPrice(good.getPrice()*discount);
- if(tmp>good.getPrice()) {
- setNotice(goodName + "降价了,快来购买");
- notifyObservers();
- }
- }
- }
-
- }
- }
-
-
创建一个超市类 将三个分区整合
- package FinalAssignment.observer;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.observer.subject.impl.DailyNecessities;
- import FinalAssignment.observer.subject.impl.Food;
- import FinalAssignment.observer.subject.impl.Vegetables;
-
- public class Supermarket {
- //创建商品分区
- Subject food;
- Subject vegetables;
- Subject dn;//日常生活用品区
-
- public Supermarket() {
- //在构造函数中初始化
- food=new Food();
- vegetables=new Vegetables();
- dn=new DailyNecessities();
- //给每个食品分区补货
- //零食区
- food.addGood(new Goods("火腿肠",1.5f,100));
- food.addGood(new Goods("方便面",3.5f,100));
- food.addGood(new Goods("泡鸡脚",13.5f,30));
- //蔬菜区
- vegetables.addGood(new Goods("西红柿",3f,0));
- vegetables.addGood(new Goods("黄瓜",2.5f,30));
- vegetables.addGood(new Goods("白菜",2f,20));
- //日常生活用品区
- dn.addGood(new Goods("抽纸",10f,100));
- dn.addGood(new Goods("洗衣机",450f,100));
- dn.addGood(new Goods("吹风机",188.88f,80));
- }
- //获取对应商品分区
- public Subject getFood() {
- return food;
- }
-
- public Subject getVegetables() {
- return vegetables;
- }
-
- public Subject getDn() {
- return dn;
- }
- //展示超市所有商品
- public void showAll(){
- System.out.println("欢迎来到Darker商城!");
- System.out.println("======食品区======");
- for (Goods good: food.getAllGoods()){
- System.out.println("商品:"+good.getGoodName()+" 价格:"+good.getPrice()+" 库存:"+good.getCount());
- }
- System.out.println("======蔬菜区======");
- for (Goods good: vegetables.getAllGoods()){
- System.out.println("商品:"+good.getGoodName()+" 价格:"+good.getPrice()+" 库存:"+good.getCount());
- }
- System.out.println("======日常用品区======");
- for (Goods good: dn.getAllGoods()){
- System.out.println("商品:"+good.getGoodName()+" 价格:"+good.getPrice()+" 库存:"+good.getCount());
- }
- }
- //修改商品价格
- public void changePrice(Subject subject,String goodName,float discount){
- subject.changePrice(goodName,discount);
- }
- }
代理模式部分
- package FinalAssignment.proxy;
-
- import FinalAssignment.state.ShopAPP;
-
- public interface Proxy extends Collective{
- String getAddr();
- }
- //------------------------------------------------------------------
- package FinalAssignment.proxy;
-
- /**
- * 物流和状态都要实现的接口
- */
- public interface Collective {
- boolean deliver();//运输方法
- }
-
- //------------------------------------------------------------------
- package FinalAssignment.proxy.impl;
-
- import FinalAssignment.proxy.Proxy;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- /**
- * 西南物流
- */
- public class SouthwestLogistics implements Proxy {
- State state;
- private String[] addr;
-
- public SouthwestLogistics(State state) {
- this.state=state;
- addr=new String[]{"云南","贵州","四川","西藏","重庆"};
- }
- //配送
- @Override
- public boolean deliver() {
- boolean flag = state.deliver();
- if(flag) {
- System.out.println("西南物流中心为你服务!");
- return true;
- }
- else
- return false;
- }
- //获取地址
- @Override
- public String getAddr() {
- StringBuilder sb = new StringBuilder();
- for (int i=0;i<addr.length;i++){
- sb.append(addr[i]);
- }
- return sb.toString();
- }
- }
-
- //------------------------------------------------------------------
- package FinalAssignment.proxy.impl;
-
- import FinalAssignment.proxy.Proxy;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- /**
- * 东南物流
- */
- public class SoutheastLogistics implements Proxy {
- State state;
- private String[] addr;
-
- public SoutheastLogistics(State state) {
- this.state=state;
- addr=new String[]{"江苏","上海","浙江","福建","广东","广西"};
- }
- //配送
- @Override
- public boolean deliver() {
- boolean flag = state.deliver();
- if(flag) {
- System.out.println("东南物流中心为你服务!");
- return true;
- }
- else
- return false;
- }
- //获取地址
- @Override
- public String getAddr() {
- StringBuilder sb = new StringBuilder();
- for (int i=0;i<addr.length;i++){
- sb.append(addr[i]);
- }
- return sb.toString();
- }
- }
-
- //------------------------------------------------------------------
- package FinalAssignment.proxy.impl;
-
- import FinalAssignment.proxy.Proxy;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- /**
- * 西北物流
- */
- public class NorthwestLogistics implements Proxy {
- State state;
- private String[] addr;
-
- public NorthwestLogistics(State state) {
- this.state=state;
- addr=new String[]{"内蒙古","新疆","宁夏","甘肃"};
- }
- //配送
- @Override
- public boolean deliver() {
- boolean flag = state.deliver();
- if(flag) {
- System.out.println("西北物流中心为你服务!");
- return true;
- }
- else
- return false;
- }
- //获取地址
- @Override
- public String getAddr() {
- StringBuilder sb = new StringBuilder();
- for (int i=0;i<addr.length;i++){
- sb.append(addr[i]);
- }
- return sb.toString();
- }
- }
-
- //------------------------------------------------------------------
- package FinalAssignment.proxy.impl;
-
- import FinalAssignment.proxy.Proxy;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- /**
- * 东北物流
- */
- public class NortheastLogistics implements Proxy {
- State state;
- private String[] addr;
-
- public NortheastLogistics(State state) {
- this.state=state;
- addr=new String[]{"黑龙江","吉林","辽宁"};
- }
- //配送
- @Override
- public boolean deliver() {
- boolean flag = state.deliver();
- if(flag) {
- System.out.println("东北物流中心为你服务!");
- return true;
- }
- else
- return false;
- }
- //获取地址
- @Override
- public String getAddr() {
- StringBuilder sb = new StringBuilder();
- for (int i=0;i<addr.length;i++){
- sb.append(addr[i]);
- }
- return sb.toString();
- }
- }
状态模式部分
- package FinalAssignment.state.situation;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.proxy.Collective;
-
- /**
- * 状态接口
- */
- public interface State extends Collective {
- Goods placeOrder(Subject subject,String goodName);//下单方法
- boolean pay(Goods good, Observer consumer);//支付
- void cancel(Goods goods);//取消
- void confirm();//确认收货
- boolean isNeedHelp();//是否需要售后服务
- void service();//售后
- }
- //--------------------------------------------------------------------
- package FinalAssignment.state.situation.impl;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- /**
- * 未下单状态
- */
- public class NoPlaceOrderState implements State {
- ShopAPP app=null;
- public NoPlaceOrderState(ShopAPP app) {
- this.app=app;
- }
- @Override
- public Goods placeOrder(Subject subject,String goodName) {
- boolean flag=false;//标志是否包含欲查找的商品
- for(Goods good:subject.getAllGoods()){
- if(goodName.equals(good.getGoodName())){
- flag=true;//有该商品
- if(good.getCount()<=0){
- //商品库存不足 通知供应商补货
- subject.setNotice(good.getGoodName()+"无货了,请补货!");
- subject.notifySuppliers();
- placeOrder(subject,goodName);
- return good;
- }else{
- good.setCount(good.getCount()-1);//商品数量减少
- System.out.println(good.getGoodName()+"下单成功!");
- //下单成功,修改状态为待付款
- app.setState(app.getCanPayState());
- return good;
- }
- }
- }
- if(!flag){
- System.out.println("暂时没有该商品!");
- }
- return null;
- }
-
- @Override
- public boolean pay(Goods good, Observer consumer) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void cancel(Goods goods) {
- System.out.println("没有订单");
- }
-
- @Override
- public boolean deliver() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void confirm() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean isNeedHelp() {
- return false;
- }
-
- @Override
- public void service() {
- System.out.println("你没有待售后订单");
- }
- }
-
- //--------------------------------------------------------------------
- package FinalAssignment.state.situation.impl;
- /**
- * 已付款状态
- */
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- import java.util.Random;
-
- public class CanPayState implements State {
- private int time=30;//订单付款倒计时
- ShopAPP app=null;
- boolean flag=false;//标记用户是否在规定时间付款
- Random random = new Random();//随机数种子 模拟用户在倒计时时间内付款
- int var;//接受随机产生的数字的临时变量
- public CanPayState(ShopAPP app) {
- this.app=app;
- }
-
-
- @Override
- public Goods placeOrder(Subject subject, String goodName) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean pay(Goods good, Observer consumer) {
- downTime();
- if(flag) {
- if (good.getPrice() > consumer.getMoney()) {
- System.out.println("你的余额不足!");
- return false;
- } else {
- consumer.setMoney(consumer.getMoney() - good.getPrice());
- //付款成功 设置状态为待发货状态
- System.out.println("支付成功!");
- app.setState(app.getWillShippedState());
- }
- return true;
- }else{
- //规定时间未付款 订单取消
- app.setState(app.getCancelledState());
- System.out.print("订单超时!");
- }
- return false;
- }
-
- @Override
- public void cancel(Goods goods) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean deliver() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void confirm() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean isNeedHelp() {
- return false;
- }
-
- @Override
- public void service() {
- System.out.println("你没有待售后订单");
- }
- //倒计时方法
- private void downTime() {
- while (time > 0) {
- time--;
- try {
- var=random.nextInt(3);
- if (var==1){
- flag=true;
- //表示已付款
- break;
- }
- Thread.sleep(1000);
- int hh = time / 60 / 60 % 60;
- int mm = time / 60 % 60;
- int ss = time % 60;
- System.out.println("还剩" + hh + "小时" + mm + "分钟" + ss + "秒将取消订单");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- //--------------------------------------------------------------------
- package FinalAssignment.state.situation.impl;
- /**
- * 待发货状态
- */
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- import java.util.Scanner;
-
- public class WillShippedState implements State {
- ShopAPP app=null;
- public WillShippedState(ShopAPP app) {
- this.app=app;
- }
-
- @Override
- public Goods placeOrder(Subject subject, String goodName) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean pay(Goods good, Observer consumer) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void cancel(Goods good) {
- System.out.println("已发货,无法取消");
- }
-
- @Override
- public boolean deliver() {
- //还未发货之前用户可以选择是否取消订单
- System.out.println("是否取消订单?(0取消1不取消)");
- Scanner sc = new Scanner(System.in);
- if(sc.nextInt()==0){
- app.setState(app.getCancelledState());
- return false;
- }else {
- System.out.println("已发货");
- //改变状态为收货状态
- app.setState(app.getShippedState());
- return true;
- }
- }
-
- @Override
- public void confirm() {
- System.out.println("你还没有待处理订单");
- }
-
- @Override
- public boolean isNeedHelp() {
- return false;
- }
-
- @Override
- public void service() {
- System.out.println("你没有待售后订单");
- }
-
- }
-
- //--------------------------------------------------------------------
- package FinalAssignment.state.situation.impl;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- import java.util.Scanner;
-
- /**
- * 已发货
- */
- public class ShippedState implements State {
- private int time = 3;//订单确认时间 超过该时间自动确认收货
- ShopAPP app=null;
- public ShippedState(ShopAPP app) {
- this.app=app;
- }
- @Override
- public Goods placeOrder(Subject subject,String goodName) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean pay(Goods good, Observer consumer) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void cancel(Goods goods) {
- System.out.println("已发货,无法取消");
- }
-
- @Override
- public boolean deliver() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void confirm() {
- //倒计时开始 结束自动确认收货
- downTime();
- //确认收货
- System.out.println("已确认收货!");
- app.setState(app.getCompletedState());
- }
-
- @Override
- public boolean isNeedHelp() {
- return false;
- }
-
- @Override
- public void service() {
- System.out.println("你没有待售后订单");
- }
- //倒计时方法
- private void downTime() {
- while (time > 0) {
- time--;
- try {
- Thread.sleep(1000);
- int hh = time / 60 / 60 % 60;
- int mm = time / 60 % 60;
- int ss = time % 60;
- System.out.println("还剩" + hh + "小时" + mm + "分钟" + ss + "秒确认收货");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- //--------------------------------------------------------------------
- package FinalAssignment.state.situation.impl;
- /**
- * 售后状态
- */
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- public class AfterSaleState implements State {
- ShopAPP app=null;
- public AfterSaleState(ShopAPP app) {
- this.app=app;
- }
-
-
- @Override
- public Goods placeOrder(Subject subject, String goodName) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean pay(Goods good, Observer consumer) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void cancel(Goods good) {
- System.out.println("已发货,无法取消");
- }
-
- @Override
- public boolean deliver() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void confirm() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean isNeedHelp() {
- return false;
- }
-
- @Override
- public void service() {
- System.out.println("已为你售后处理,请稍后为本次服务做出评价,欢迎你下次光临");
- //售后完成 本次订单完成 将状态设置为初始值
- app.setState(app.getNoPlaceOrderState());
- }
-
- }
-
- //--------------------------------------------------------------------
- package FinalAssignment.state.situation.impl;
- /**
- * 已完成订单状态
- */
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- import java.util.Scanner;
-
- public class CompletedState implements State {
- Scanner sc = new Scanner(System.in);
- ShopAPP app=null;
- public CompletedState(ShopAPP app) {
- this.app=app;
- }
- @Override
- public Goods placeOrder(Subject subject,String goodName) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean pay(Goods good, Observer consumer) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void cancel(Goods goods) {
- System.out.println("已发货,无法取消");
- }
-
- @Override
- public boolean deliver() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void confirm() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean isNeedHelp(){
- System.out.println("是否需要售后服务?(0为取消,1为确认)");
- int flag = sc.nextInt();
- if (flag==0){
- System.out.println("恭喜你购物完成,欢迎你下次光临!");
- //本次订单完成 将状态设置为初始值
- app.setState(app.getNoPlaceOrderState());
- return false;
- }else if(flag==1){
- app.setState(app.getAfterSaleState());
- return true;
- }else {
- throw new UnsupportedOperationException();
- }
- }
-
- @Override
- public void service() {
-
- }
- }
-
- //--------------------------------------------------------------------
- package FinalAssignment.state.situation.impl;
- /**
- * 已取消状态
- */
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.state.ShopAPP;
- import FinalAssignment.state.situation.State;
-
- public class CancelledState implements State {
- ShopAPP app=null;
- public CancelledState(ShopAPP app) {
- this.app=app;
- }
- @Override
- public Goods placeOrder(Subject subject, String goodName) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean pay(Goods good, Observer consumer) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void cancel(Goods good) {
- System.out.println("订单已取消,已为你退款");
- good.setCount(good.getCount()+1);
- app.setState(app.getNoPlaceOrderState());
- }
-
- @Override
- public boolean deliver() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void confirm() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean isNeedHelp() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void service() {
- System.out.println("你没有待售后订单");
- }
- }
创建一个APP类
- package FinalAssignment.state;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.subject.Subject;
- import FinalAssignment.proxy.Proxy;
- import FinalAssignment.proxy.impl.NortheastLogistics;
- import FinalAssignment.proxy.impl.NorthwestLogistics;
- import FinalAssignment.proxy.impl.SoutheastLogistics;
- import FinalAssignment.proxy.impl.SouthwestLogistics;
- import FinalAssignment.state.situation.State;
- import FinalAssignment.state.situation.impl.*;
- import org.junit.Test;
-
- import java.util.Scanner;
-
- /**
- * 网络商城APP
- */
- public class ShopAPP {
- State state;
- Proxy logistics;
- //创建几种状态实例
- State noPlaceOrderState = new NoPlaceOrderState(this);
- State canPayState = new CanPayState(this);
- State cancelledState = new CancelledState(this);
- State willShippedState=new WillShippedState(this);
- State shippedState = new ShippedState(this);
- State completedState = new CompletedState(this);
- State afterSaleState = new AfterSaleState(this);
- //创建物流中心
- Proxy swl = new SouthwestLogistics(willShippedState);//西南
- Proxy nel = new NortheastLogistics(willShippedState);//东南
- Proxy sel = new SoutheastLogistics(willShippedState);//东北
- Proxy nwl = new NorthwestLogistics(willShippedState);//西北
-
- public State getWillShippedState() {
- return willShippedState;
- }
-
- public void setWillShippedState(State willShippedState) {
- this.willShippedState = willShippedState;
- }
-
- public ShopAPP() {
- this.state = noPlaceOrderState;
- }
-
- public State getState() {
- return state;
- }
-
- public void setState(State state){
- this.state=state;
- }
-
- public State getNoPlaceOrderState() {
- return noPlaceOrderState;
- }
-
- public State getCanPayState() {
- return canPayState;
- }
-
- public State getCancelledState() {
- return cancelledState;
- }
-
- public State getCompletedState() {
- return completedState;
- }
-
- public State getShippedState() {
- return shippedState;
- }
-
- public State getAfterSaleState() {
- return afterSaleState;
- }
- //下单的方法
- public Goods placeOrder(Subject subject){
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入您要购买的商品:");
- String goodName=sc.nextLine();
- return this.state.placeOrder(subject,goodName);
- }
- //支付的方法
- public void pay(Goods good, Observer consumer){
- changeMsg(consumer);
- choose(consumer);
- try {
- if(state.pay(good,consumer)){
- if(logistics.deliver()){
- state.confirm();
- if(state.isNeedHelp()){
- state.service();
- }
- }else {
- state.cancel(good);
- }
- }else{
- state.cancel(good);
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
-
- //判断顾客收货地址所属区域
- private void choose(Observer observer){
-
- if(swl.getAddr().contains(observer.getAddr())){
- //西南
- logistics=swl;
- }
- if(nwl.getAddr().contains(observer.getAddr())){
- //西北
- logistics=nwl;
- }
- if(sel.getAddr().contains(observer.getAddr())){
- //东南
- logistics=sel;
- }
- if(nel.getAddr().contains(observer.getAddr())){
- //东北
- logistics=nwl;
- }
- }
- //判断用户是否需要修改信息
- private void changeMsg(Observer observer){
- System.out.println("请确认你的收货信息:");
- System.out.println("姓名:"+observer.getName()+" 收货地址:"+observer.getAddr());
- System.out.println("是否修改信息?(0:取消订单 1:修改信息 2:不做处理)");
- Scanner sc= new Scanner(System.in);
- int num=sc.nextInt();
- if(num==0){
- System.out.println("订单已取消!");
- System.exit(0);
- }else if(num==1){
- System.out.println("请输入收件人:");
- sc.nextLine();//接收缓冲区内的空格
- String name=sc.nextLine();
- observer.setName(name);
- System.out.println("请输入地址:");
- String addr=sc.nextLine();
- observer.setAddr(addr);
- System.out.println("修改完成!");
- changeMsg(observer);
- }else if(num==2){
- System.out.println("请及时付款!");
- }else {
- System.out.println("输入有误!");
- changeMsg(observer);
- }
- }
- }
-
-
-
测试代码
- package FinalAssignment;
-
- import FinalAssignment.entity.Goods;
- import FinalAssignment.observer.Supermarket;
- import FinalAssignment.observer.observer.Observer;
- import FinalAssignment.observer.observer.impl.Consumer;
- import FinalAssignment.observer.observer.impl.Supplier;
- import FinalAssignment.state.ShopAPP;
-
- import java.util.Scanner;
-
- public class Client {
- public static void main(String[] args) {
- //创建app实例1
- ShopAPP app = new ShopAPP();
- //创建商店实例
- Supermarket supermarket = new Supermarket();
- //创建顾客实例
- Observer consumer1=new Consumer("刘备",2000,"四川");
- Observer consumer2=new Consumer("孙权",1888,"广东");
- Observer consumer3=new Consumer("曹操",1000,"宁夏");
- Observer consumer4=new Consumer("纳兰性德",1000,"吉林");
- //顾客注册
- supermarket.getFood().addObserver(consumer1);
- supermarket.getVegetables().addObserver(consumer2);
- supermarket.getDn().addObserver(consumer3);
- supermarket.getVegetables().addObserver(consumer4);
- //创建供货商实例
- Observer supplier = new Supplier("谢华强");
- Observer supplier2 = new Supplier("穿山甲");
- //供货商注册
- supermarket.getFood().addSupplier(supplier);
- supermarket.getVegetables().addSupplier(supplier2);
- supermarket.showAll();
- //商品降价
- supermarket.changePrice(supermarket.getVegetables(),"白菜",0.7f);
- //通过app下单
- Goods good=app.placeOrder(supermarket.getVegetables());
- if(good!=null){
- app.pay(good,consumer1);
- }
- }
- }
谨以此篇文章致敬带我打开设计模式大门的老师。斯人已逝,精神永存!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。