当前位置:   article > 正文

Java设计模式期末——网络商城系统_java设计模式期末网络商城系统

java设计模式期末网络商城系统

写在前面:本人为初学者,该代码仅供参考,如有不对的地方,欢迎各位大佬评论区指正批评。本菜鸟当不胜感激!

题目描述:现假设要实现一个网络商城系统的部分功能

1、商品状态的订阅:网络商城中的每一种商品,供应商和用户都可以对其进行订阅关注。对供应商来说,当商品缺货的时候,提醒供应商补货。对于用户来说,当商品价格变动的时候,或商品由缺货变成有货的时候,可以自动通知用户。请使用观察者模式,实现这种效果。

2、订单状态的管理:当用户在商城中下了订单后,订单存在着几种状态:下单状态(下订单未付款),已付款状态(付款未发货),已发货状态(发货未确认),已完成状态(用户确认或售后完成),已取消状态(用户未付款或用户取消),售后状态(用户收货确认后有退换货问题)。状态之间存在行为转换,请用状态模式,实现订单状态的转换。

3、订单发货的管理:该商城在全国有西南、西北、东南、东北4个物流中心,订单会根据收货地的范围不同,确定应该由那个物流中心发货。请使用代理模式,实现订单发货的物流管理。


具体代码:

商品实例

  1. package FinalAssignment.entity;
  2. /**
  3. * 商品类
  4. */
  5. public class Goods {
  6. private String goodName;//商品名称
  7. private float price;//商品价格
  8. private int count;//商品数量
  9. public Goods(String goodName, float price, int count) {
  10. this.goodName = goodName;
  11. this.price = price;
  12. this.count = count;
  13. }
  14. public String getGoodName() {
  15. return goodName;
  16. }
  17. public void setGoodName(String goodName) {
  18. this.goodName = goodName;
  19. }
  20. public float getPrice() {
  21. return price;
  22. }
  23. public void setPrice(float price) {
  24. this.price = price;
  25. }
  26. public int getCount() {
  27. return count;
  28. }
  29. public void setCount(int count) {
  30. this.count = count;
  31. }
  32. }

观察者模式部分

  1. package FinalAssignment.observer.observer;
  2. import FinalAssignment.entity.Goods;
  3. import FinalAssignment.observer.subject.Subject;
  4. /**
  5. * 观察者接口
  6. */
  7. public interface Observer {
  8. void upData(Subject subject, Goods good);
  9. float getMoney();
  10. void setMoney(float Money);
  11. String getAddr();
  12. void setAddr(String addr);
  13. String getName();
  14. void setName(String name);
  15. }
  16. //-----------------------------------------------------------------------------
  17. package FinalAssignment.observer.observer.impl;
  18. import FinalAssignment.entity.Goods;
  19. import FinalAssignment.observer.observer.Observer;
  20. import FinalAssignment.observer.subject.Subject;
  21. import FinalAssignment.observer.subject.impl.Food;
  22. /**
  23. * 消费者(观察者)
  24. */
  25. public class Consumer implements Observer {
  26. private String name;//消费者的姓名
  27. private float money;//金钱
  28. private String addr;//顾客收货地址
  29. @Override
  30. public String getName() {
  31. return name;
  32. }
  33. @Override
  34. public void setName(String name) {
  35. this.name=name;
  36. }
  37. public Consumer(String name, float money, String addr) {
  38. this.name = name;
  39. this.money=money;
  40. this.addr = addr;
  41. }
  42. public float getMoney() {
  43. return money;
  44. }
  45. public void setMoney(float money) {
  46. this.money = money;
  47. }
  48. @Override
  49. public String getAddr() {
  50. return addr;
  51. }
  52. @Override
  53. public void setAddr(String addr) {
  54. this.addr=addr;
  55. }
  56. @Override
  57. public void upData(Subject subject, Goods good) {
  58. System.out.println(name+subject.getNotice());
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. package FinalAssignment.observer.observer.impl;
  63. import FinalAssignment.entity.Goods;
  64. import FinalAssignment.observer.observer.Observer;
  65. import FinalAssignment.observer.subject.Subject;
  66. import FinalAssignment.observer.subject.impl.Food;
  67. /**
  68. * 供应商(观察者)
  69. */
  70. public class Supplier implements Observer {
  71. private String name;//供应商姓名
  72. public Supplier(String name) {
  73. this.name = name;
  74. }
  75. @Override
  76. public String getName() {
  77. throw new UnsupportedOperationException();
  78. }
  79. @Override
  80. public void setName(String name) {
  81. }
  82. @Override
  83. public void upData(Subject subject, Goods good) {
  84. System.out.println(subject.getNotice());
  85. System.out.println(name+"收到!");
  86. good.setCount(100);
  87. System.out.println("补货完成!");
  88. //补货完成后通过商城通知顾客
  89. subject.setNotice("你关注的商品有货了,快来购买吧!");
  90. subject.notifyObservers();
  91. }
  92. @Override
  93. public float getMoney() {
  94. throw new UnsupportedOperationException();
  95. }
  96. @Override
  97. public void setMoney(float Money) {
  98. throw new UnsupportedOperationException();
  99. }
  100. @Override
  101. public String getAddr() {
  102. throw new UnsupportedOperationException();
  103. }
  104. @Override
  105. public void setAddr(String addr) {
  106. throw new UnsupportedOperationException();
  107. }
  108. }
  109. //-----------------------------------------------------------------------------
  110. package FinalAssignment.observer.subject;
  111. import FinalAssignment.entity.Goods;
  112. import FinalAssignment.observer.observer.Observer;
  113. import java.util.List;
  114. /**
  115. * 主题接口(商城)
  116. */
  117. public interface Subject {
  118. void addObserver(Observer observer);
  119. void removeObserver(Observer observer);
  120. void notifyObservers();
  121. void addGood(Goods good);
  122. void removeGood(Goods good);
  123. void addSupplier(Observer observer);
  124. void removeSupplier(Observer observer);
  125. List<Goods> getAllGoods();
  126. void changePrice(String goodName,float discount);
  127. void notifySuppliers();
  128. String getNotice();
  129. void setNotice(String msg);
  130. }
  131. //-----------------------------------------------------------------------------
  132. package FinalAssignment.observer.subject.impl;
  133. import FinalAssignment.entity.Goods;
  134. import FinalAssignment.observer.observer.Observer;
  135. import FinalAssignment.observer.subject.Subject;
  136. import java.util.ArrayList;
  137. import java.util.List;
  138. /**
  139. * 网络商城蔬菜区
  140. */
  141. public class Vegetables implements Subject {
  142. //存储顾客
  143. private List<Observer> consumers;
  144. //存储供应商
  145. private List<Observer> suppliers;
  146. //存储该分区里面的商品
  147. private List<Goods> commodity;
  148. //存储通知信息
  149. String notice;
  150. public String getNotice() {
  151. return notice;
  152. }
  153. public void setNotice(String notice) {
  154. this.notice = notice;
  155. }
  156. public Vegetables() {
  157. //在构造函数中对集合初始化
  158. consumers=new ArrayList<>();
  159. suppliers=new ArrayList<>();
  160. commodity=new ArrayList<>();
  161. }
  162. //添加顾客方法
  163. @Override
  164. public void addObserver(Observer observer) {
  165. consumers.add(observer);
  166. }
  167. //移除顾客方法
  168. @Override
  169. public void removeObserver(Observer observer) {
  170. consumers.remove(observer);
  171. }
  172. //通知观察者方法
  173. @Override
  174. public void notifyObservers() {
  175. for (Observer consumer:consumers){
  176. consumer.upData(this,null);
  177. }
  178. }
  179. //通知供应商补货方法
  180. @Override
  181. public void notifySuppliers() {
  182. for(Goods good:commodity){
  183. if(good.getCount()<=0){
  184. setNotice(good.getGoodName()+"无货了,请快点补货!");
  185. for (Observer supplier:suppliers){
  186. supplier.upData(this,good);
  187. }
  188. }
  189. }
  190. }
  191. //添加商品方法
  192. @Override
  193. public void addGood(Goods good) {
  194. commodity.add(good);
  195. }
  196. //下架商品方法
  197. @Override
  198. public void removeGood(Goods good) {
  199. commodity.remove(good);
  200. }
  201. //添加供应商方法
  202. @Override
  203. public void addSupplier(Observer observer) {
  204. suppliers.add(observer);
  205. }
  206. //移除供应商方法
  207. @Override
  208. public void removeSupplier(Observer observer) {
  209. suppliers.remove(observer);
  210. }
  211. //显示该区域所有商品
  212. @Override
  213. public List<Goods> getAllGoods() {
  214. return commodity;
  215. }
  216. //改变商品价格
  217. public void changePrice(String goodName,float discount){
  218. for (Goods good:commodity){
  219. if(good.getGoodName().equals(goodName)){
  220. float tmp=good.getPrice();
  221. good.setPrice(good.getPrice()*discount);
  222. if(tmp>good.getPrice()) {
  223. setNotice(goodName + "降价了,快来购买");
  224. notifyObservers();
  225. }
  226. }
  227. }
  228. }
  229. }
  230. //-----------------------------------------------------------------------------
  231. package FinalAssignment.observer.subject.impl;
  232. import FinalAssignment.entity.Goods;
  233. import FinalAssignment.observer.observer.Observer;
  234. import FinalAssignment.observer.subject.Subject;
  235. import java.util.ArrayList;
  236. import java.util.List;
  237. /**
  238. * 网络商城食品区
  239. */
  240. public class Food implements Subject {
  241. //存储顾客
  242. private List<Observer> consumers;
  243. //存储供应商
  244. private List<Observer> suppliers;
  245. //存储该分区里面的商品
  246. private List<Goods> commodity;
  247. //存储通知信息
  248. String notice;
  249. @Override
  250. public String getNotice() {
  251. return notice;
  252. }
  253. @Override
  254. public void setNotice(String msg) {
  255. this.notice = msg;
  256. }
  257. public Food() {
  258. //在构造函数中对集合初始化
  259. consumers=new ArrayList<>();
  260. suppliers=new ArrayList<>();
  261. commodity=new ArrayList<>();
  262. }
  263. //添加顾客方法
  264. @Override
  265. public void addObserver(Observer observer) {
  266. consumers.add(observer);
  267. }
  268. //移除顾客方法
  269. @Override
  270. public void removeObserver(Observer observer) {
  271. consumers.remove(observer);
  272. }
  273. //通知消费者方法
  274. @Override
  275. public void notifyObservers() {
  276. for (Observer consumer:consumers){
  277. consumer.upData(this,null);
  278. }
  279. }
  280. //通知供应商补货方法
  281. @Override
  282. public void notifySuppliers() {
  283. for(Goods good:commodity){
  284. if(good.getCount()<=0){
  285. setNotice(good.getGoodName()+"无货了,请快点补货!");
  286. for (Observer supplier:suppliers){
  287. supplier.upData(this,good);
  288. }
  289. }
  290. }
  291. }
  292. //添加商品方法
  293. @Override
  294. public void addGood(Goods good) {
  295. commodity.add(good);
  296. }
  297. //下架商品方法
  298. @Override
  299. public void removeGood(Goods good) {
  300. commodity.remove(good);
  301. }
  302. //添加供应商方法
  303. @Override
  304. public void addSupplier(Observer observer) {
  305. suppliers.add(observer);
  306. }
  307. //移除供应商方法
  308. @Override
  309. public void removeSupplier(Observer observer) {
  310. suppliers.remove(observer);
  311. }
  312. //显示该区域所有商品
  313. @Override
  314. public List<Goods> getAllGoods() {
  315. return commodity;
  316. }
  317. //改变商品价格
  318. public void changePrice(String goodName,float discount){
  319. for (Goods good:commodity){
  320. if(good.getGoodName().equals(goodName)){
  321. float tmp=good.getPrice();
  322. good.setPrice(good.getPrice()*discount);
  323. if(tmp>good.getPrice()) {
  324. setNotice(goodName + "降价了,快来购买");
  325. notifyObservers();
  326. }
  327. }
  328. }
  329. }
  330. }
  331. //-----------------------------------------------------------------------------
  332. package FinalAssignment.observer.subject.impl;
  333. import FinalAssignment.entity.Goods;
  334. import FinalAssignment.observer.observer.Observer;
  335. import FinalAssignment.observer.subject.Subject;
  336. import java.util.ArrayList;
  337. import java.util.List;
  338. /**
  339. * 网络商城日用品区
  340. */
  341. public class DailyNecessities implements Subject {
  342. //存储顾客
  343. private List<Observer> consumers;
  344. //存储供应商
  345. private List<Observer> suppliers;
  346. //存储该分区里面的商品
  347. private List<Goods> commodity;
  348. //存储通知信息
  349. String notice;
  350. public String getNotice() {
  351. return notice;
  352. }
  353. public void setNotice(String notice) {
  354. this.notice = notice;
  355. }
  356. public DailyNecessities() {
  357. //在构造函数中对集合初始化
  358. consumers=new ArrayList<>();
  359. suppliers=new ArrayList<>();
  360. commodity=new ArrayList<>();
  361. }
  362. //添加顾客方法
  363. @Override
  364. public void addObserver(Observer observer) {
  365. consumers.add(observer);
  366. }
  367. //移除顾客方法
  368. @Override
  369. public void removeObserver(Observer observer) {
  370. consumers.remove(observer);
  371. }
  372. //通知观察者方法
  373. @Override
  374. public void notifyObservers() {
  375. for (Observer consumer:consumers){
  376. consumer.upData(this,null);
  377. }
  378. }
  379. //通知供应商补货方法
  380. @Override
  381. public void notifySuppliers() {
  382. for(Goods good:commodity){
  383. if(good.getCount()<=0){
  384. setNotice(good.getGoodName()+"无货了,请快点补货!");
  385. for (Observer supplier:suppliers){
  386. supplier.upData(this,good);
  387. }
  388. }
  389. }
  390. }
  391. //添加商品方法
  392. @Override
  393. public void addGood(Goods good) {
  394. commodity.add(good);
  395. }
  396. //下架商品方法
  397. @Override
  398. public void removeGood(Goods good) {
  399. commodity.remove(good);
  400. }
  401. //添加供应商方法
  402. @Override
  403. public void addSupplier(Observer observer) {
  404. suppliers.add(observer);
  405. }
  406. //移除供应商方法
  407. @Override
  408. public void removeSupplier(Observer observer) {
  409. suppliers.remove(observer);
  410. }
  411. //显示该区域所有商品
  412. @Override
  413. public List<Goods> getAllGoods() {
  414. return commodity;
  415. }
  416. //改变商品价格
  417. public void changePrice(String goodName,float discount){
  418. for (Goods good:commodity){
  419. if(good.getGoodName().equals(goodName)){
  420. float tmp=good.getPrice();
  421. good.setPrice(good.getPrice()*discount);
  422. if(tmp>good.getPrice()) {
  423. setNotice(goodName + "降价了,快来购买");
  424. notifyObservers();
  425. }
  426. }
  427. }
  428. }
  429. }

创建一个超市类 将三个分区整合

  1. package FinalAssignment.observer;
  2. import FinalAssignment.entity.Goods;
  3. import FinalAssignment.observer.subject.Subject;
  4. import FinalAssignment.observer.subject.impl.DailyNecessities;
  5. import FinalAssignment.observer.subject.impl.Food;
  6. import FinalAssignment.observer.subject.impl.Vegetables;
  7. public class Supermarket {
  8. //创建商品分区
  9. Subject food;
  10. Subject vegetables;
  11. Subject dn;//日常生活用品区
  12. public Supermarket() {
  13. //在构造函数中初始化
  14. food=new Food();
  15. vegetables=new Vegetables();
  16. dn=new DailyNecessities();
  17. //给每个食品分区补货
  18. //零食区
  19. food.addGood(new Goods("火腿肠",1.5f,100));
  20. food.addGood(new Goods("方便面",3.5f,100));
  21. food.addGood(new Goods("泡鸡脚",13.5f,30));
  22. //蔬菜区
  23. vegetables.addGood(new Goods("西红柿",3f,0));
  24. vegetables.addGood(new Goods("黄瓜",2.5f,30));
  25. vegetables.addGood(new Goods("白菜",2f,20));
  26. //日常生活用品区
  27. dn.addGood(new Goods("抽纸",10f,100));
  28. dn.addGood(new Goods("洗衣机",450f,100));
  29. dn.addGood(new Goods("吹风机",188.88f,80));
  30. }
  31. //获取对应商品分区
  32. public Subject getFood() {
  33. return food;
  34. }
  35. public Subject getVegetables() {
  36. return vegetables;
  37. }
  38. public Subject getDn() {
  39. return dn;
  40. }
  41. //展示超市所有商品
  42. public void showAll(){
  43. System.out.println("欢迎来到Darker商城!");
  44. System.out.println("======食品区======");
  45. for (Goods good: food.getAllGoods()){
  46. System.out.println("商品:"+good.getGoodName()+" 价格:"+good.getPrice()+" 库存:"+good.getCount());
  47. }
  48. System.out.println("======蔬菜区======");
  49. for (Goods good: vegetables.getAllGoods()){
  50. System.out.println("商品:"+good.getGoodName()+" 价格:"+good.getPrice()+" 库存:"+good.getCount());
  51. }
  52. System.out.println("======日常用品区======");
  53. for (Goods good: dn.getAllGoods()){
  54. System.out.println("商品:"+good.getGoodName()+" 价格:"+good.getPrice()+" 库存:"+good.getCount());
  55. }
  56. }
  57. //修改商品价格
  58. public void changePrice(Subject subject,String goodName,float discount){
  59. subject.changePrice(goodName,discount);
  60. }
  61. }

代理模式部分

  1. package FinalAssignment.proxy;
  2. import FinalAssignment.state.ShopAPP;
  3. public interface Proxy extends Collective{
  4. String getAddr();
  5. }
  6. //------------------------------------------------------------------
  7. package FinalAssignment.proxy;
  8. /**
  9. * 物流和状态都要实现的接口
  10. */
  11. public interface Collective {
  12. boolean deliver();//运输方法
  13. }
  14. //------------------------------------------------------------------
  15. package FinalAssignment.proxy.impl;
  16. import FinalAssignment.proxy.Proxy;
  17. import FinalAssignment.state.ShopAPP;
  18. import FinalAssignment.state.situation.State;
  19. /**
  20. * 西南物流
  21. */
  22. public class SouthwestLogistics implements Proxy {
  23. State state;
  24. private String[] addr;
  25. public SouthwestLogistics(State state) {
  26. this.state=state;
  27. addr=new String[]{"云南","贵州","四川","西藏","重庆"};
  28. }
  29. //配送
  30. @Override
  31. public boolean deliver() {
  32. boolean flag = state.deliver();
  33. if(flag) {
  34. System.out.println("西南物流中心为你服务!");
  35. return true;
  36. }
  37. else
  38. return false;
  39. }
  40. //获取地址
  41. @Override
  42. public String getAddr() {
  43. StringBuilder sb = new StringBuilder();
  44. for (int i=0;i<addr.length;i++){
  45. sb.append(addr[i]);
  46. }
  47. return sb.toString();
  48. }
  49. }
  50. //------------------------------------------------------------------
  51. package FinalAssignment.proxy.impl;
  52. import FinalAssignment.proxy.Proxy;
  53. import FinalAssignment.state.ShopAPP;
  54. import FinalAssignment.state.situation.State;
  55. /**
  56. * 东南物流
  57. */
  58. public class SoutheastLogistics implements Proxy {
  59. State state;
  60. private String[] addr;
  61. public SoutheastLogistics(State state) {
  62. this.state=state;
  63. addr=new String[]{"江苏","上海","浙江","福建","广东","广西"};
  64. }
  65. //配送
  66. @Override
  67. public boolean deliver() {
  68. boolean flag = state.deliver();
  69. if(flag) {
  70. System.out.println("东南物流中心为你服务!");
  71. return true;
  72. }
  73. else
  74. return false;
  75. }
  76. //获取地址
  77. @Override
  78. public String getAddr() {
  79. StringBuilder sb = new StringBuilder();
  80. for (int i=0;i<addr.length;i++){
  81. sb.append(addr[i]);
  82. }
  83. return sb.toString();
  84. }
  85. }
  86. //------------------------------------------------------------------
  87. package FinalAssignment.proxy.impl;
  88. import FinalAssignment.proxy.Proxy;
  89. import FinalAssignment.state.ShopAPP;
  90. import FinalAssignment.state.situation.State;
  91. /**
  92. * 西北物流
  93. */
  94. public class NorthwestLogistics implements Proxy {
  95. State state;
  96. private String[] addr;
  97. public NorthwestLogistics(State state) {
  98. this.state=state;
  99. addr=new String[]{"内蒙古","新疆","宁夏","甘肃"};
  100. }
  101. //配送
  102. @Override
  103. public boolean deliver() {
  104. boolean flag = state.deliver();
  105. if(flag) {
  106. System.out.println("西北物流中心为你服务!");
  107. return true;
  108. }
  109. else
  110. return false;
  111. }
  112. //获取地址
  113. @Override
  114. public String getAddr() {
  115. StringBuilder sb = new StringBuilder();
  116. for (int i=0;i<addr.length;i++){
  117. sb.append(addr[i]);
  118. }
  119. return sb.toString();
  120. }
  121. }
  122. //------------------------------------------------------------------
  123. package FinalAssignment.proxy.impl;
  124. import FinalAssignment.proxy.Proxy;
  125. import FinalAssignment.state.ShopAPP;
  126. import FinalAssignment.state.situation.State;
  127. /**
  128. * 东北物流
  129. */
  130. public class NortheastLogistics implements Proxy {
  131. State state;
  132. private String[] addr;
  133. public NortheastLogistics(State state) {
  134. this.state=state;
  135. addr=new String[]{"黑龙江","吉林","辽宁"};
  136. }
  137. //配送
  138. @Override
  139. public boolean deliver() {
  140. boolean flag = state.deliver();
  141. if(flag) {
  142. System.out.println("东北物流中心为你服务!");
  143. return true;
  144. }
  145. else
  146. return false;
  147. }
  148. //获取地址
  149. @Override
  150. public String getAddr() {
  151. StringBuilder sb = new StringBuilder();
  152. for (int i=0;i<addr.length;i++){
  153. sb.append(addr[i]);
  154. }
  155. return sb.toString();
  156. }
  157. }

状态模式部分 

  1. package FinalAssignment.state.situation;
  2. import FinalAssignment.entity.Goods;
  3. import FinalAssignment.observer.observer.Observer;
  4. import FinalAssignment.observer.subject.Subject;
  5. import FinalAssignment.proxy.Collective;
  6. /**
  7. * 状态接口
  8. */
  9. public interface State extends Collective {
  10. Goods placeOrder(Subject subject,String goodName);//下单方法
  11. boolean pay(Goods good, Observer consumer);//支付
  12. void cancel(Goods goods);//取消
  13. void confirm();//确认收货
  14. boolean isNeedHelp();//是否需要售后服务
  15. void service();//售后
  16. }
  17. //--------------------------------------------------------------------
  18. package FinalAssignment.state.situation.impl;
  19. import FinalAssignment.entity.Goods;
  20. import FinalAssignment.observer.observer.Observer;
  21. import FinalAssignment.observer.subject.Subject;
  22. import FinalAssignment.state.ShopAPP;
  23. import FinalAssignment.state.situation.State;
  24. /**
  25. * 未下单状态
  26. */
  27. public class NoPlaceOrderState implements State {
  28. ShopAPP app=null;
  29. public NoPlaceOrderState(ShopAPP app) {
  30. this.app=app;
  31. }
  32. @Override
  33. public Goods placeOrder(Subject subject,String goodName) {
  34. boolean flag=false;//标志是否包含欲查找的商品
  35. for(Goods good:subject.getAllGoods()){
  36. if(goodName.equals(good.getGoodName())){
  37. flag=true;//有该商品
  38. if(good.getCount()<=0){
  39. //商品库存不足 通知供应商补货
  40. subject.setNotice(good.getGoodName()+"无货了,请补货!");
  41. subject.notifySuppliers();
  42. placeOrder(subject,goodName);
  43. return good;
  44. }else{
  45. good.setCount(good.getCount()-1);//商品数量减少
  46. System.out.println(good.getGoodName()+"下单成功!");
  47. //下单成功,修改状态为待付款
  48. app.setState(app.getCanPayState());
  49. return good;
  50. }
  51. }
  52. }
  53. if(!flag){
  54. System.out.println("暂时没有该商品!");
  55. }
  56. return null;
  57. }
  58. @Override
  59. public boolean pay(Goods good, Observer consumer) {
  60. throw new UnsupportedOperationException();
  61. }
  62. @Override
  63. public void cancel(Goods goods) {
  64. System.out.println("没有订单");
  65. }
  66. @Override
  67. public boolean deliver() {
  68. throw new UnsupportedOperationException();
  69. }
  70. @Override
  71. public void confirm() {
  72. throw new UnsupportedOperationException();
  73. }
  74. @Override
  75. public boolean isNeedHelp() {
  76. return false;
  77. }
  78. @Override
  79. public void service() {
  80. System.out.println("你没有待售后订单");
  81. }
  82. }
  83. //--------------------------------------------------------------------
  84. package FinalAssignment.state.situation.impl;
  85. /**
  86. * 已付款状态
  87. */
  88. import FinalAssignment.entity.Goods;
  89. import FinalAssignment.observer.observer.Observer;
  90. import FinalAssignment.observer.subject.Subject;
  91. import FinalAssignment.state.ShopAPP;
  92. import FinalAssignment.state.situation.State;
  93. import java.util.Random;
  94. public class CanPayState implements State {
  95. private int time=30;//订单付款倒计时
  96. ShopAPP app=null;
  97. boolean flag=false;//标记用户是否在规定时间付款
  98. Random random = new Random();//随机数种子 模拟用户在倒计时时间内付款
  99. int var;//接受随机产生的数字的临时变量
  100. public CanPayState(ShopAPP app) {
  101. this.app=app;
  102. }
  103. @Override
  104. public Goods placeOrder(Subject subject, String goodName) {
  105. throw new UnsupportedOperationException();
  106. }
  107. @Override
  108. public boolean pay(Goods good, Observer consumer) {
  109. downTime();
  110. if(flag) {
  111. if (good.getPrice() > consumer.getMoney()) {
  112. System.out.println("你的余额不足!");
  113. return false;
  114. } else {
  115. consumer.setMoney(consumer.getMoney() - good.getPrice());
  116. //付款成功 设置状态为待发货状态
  117. System.out.println("支付成功!");
  118. app.setState(app.getWillShippedState());
  119. }
  120. return true;
  121. }else{
  122. //规定时间未付款 订单取消
  123. app.setState(app.getCancelledState());
  124. System.out.print("订单超时!");
  125. }
  126. return false;
  127. }
  128. @Override
  129. public void cancel(Goods goods) {
  130. throw new UnsupportedOperationException();
  131. }
  132. @Override
  133. public boolean deliver() {
  134. throw new UnsupportedOperationException();
  135. }
  136. @Override
  137. public void confirm() {
  138. throw new UnsupportedOperationException();
  139. }
  140. @Override
  141. public boolean isNeedHelp() {
  142. return false;
  143. }
  144. @Override
  145. public void service() {
  146. System.out.println("你没有待售后订单");
  147. }
  148. //倒计时方法
  149. private void downTime() {
  150. while (time > 0) {
  151. time--;
  152. try {
  153. var=random.nextInt(3);
  154. if (var==1){
  155. flag=true;
  156. //表示已付款
  157. break;
  158. }
  159. Thread.sleep(1000);
  160. int hh = time / 60 / 60 % 60;
  161. int mm = time / 60 % 60;
  162. int ss = time % 60;
  163. System.out.println("还剩" + hh + "小时" + mm + "分钟" + ss + "秒将取消订单");
  164. } catch (InterruptedException e) {
  165. e.printStackTrace();
  166. }
  167. }
  168. }
  169. }
  170. //--------------------------------------------------------------------
  171. package FinalAssignment.state.situation.impl;
  172. /**
  173. * 待发货状态
  174. */
  175. import FinalAssignment.entity.Goods;
  176. import FinalAssignment.observer.observer.Observer;
  177. import FinalAssignment.observer.subject.Subject;
  178. import FinalAssignment.state.ShopAPP;
  179. import FinalAssignment.state.situation.State;
  180. import java.util.Scanner;
  181. public class WillShippedState implements State {
  182. ShopAPP app=null;
  183. public WillShippedState(ShopAPP app) {
  184. this.app=app;
  185. }
  186. @Override
  187. public Goods placeOrder(Subject subject, String goodName) {
  188. throw new UnsupportedOperationException();
  189. }
  190. @Override
  191. public boolean pay(Goods good, Observer consumer) {
  192. throw new UnsupportedOperationException();
  193. }
  194. @Override
  195. public void cancel(Goods good) {
  196. System.out.println("已发货,无法取消");
  197. }
  198. @Override
  199. public boolean deliver() {
  200. //还未发货之前用户可以选择是否取消订单
  201. System.out.println("是否取消订单?(0取消1不取消)");
  202. Scanner sc = new Scanner(System.in);
  203. if(sc.nextInt()==0){
  204. app.setState(app.getCancelledState());
  205. return false;
  206. }else {
  207. System.out.println("已发货");
  208. //改变状态为收货状态
  209. app.setState(app.getShippedState());
  210. return true;
  211. }
  212. }
  213. @Override
  214. public void confirm() {
  215. System.out.println("你还没有待处理订单");
  216. }
  217. @Override
  218. public boolean isNeedHelp() {
  219. return false;
  220. }
  221. @Override
  222. public void service() {
  223. System.out.println("你没有待售后订单");
  224. }
  225. }
  226. //--------------------------------------------------------------------
  227. package FinalAssignment.state.situation.impl;
  228. import FinalAssignment.entity.Goods;
  229. import FinalAssignment.observer.observer.Observer;
  230. import FinalAssignment.observer.subject.Subject;
  231. import FinalAssignment.state.ShopAPP;
  232. import FinalAssignment.state.situation.State;
  233. import java.util.Scanner;
  234. /**
  235. * 已发货
  236. */
  237. public class ShippedState implements State {
  238. private int time = 3;//订单确认时间 超过该时间自动确认收货
  239. ShopAPP app=null;
  240. public ShippedState(ShopAPP app) {
  241. this.app=app;
  242. }
  243. @Override
  244. public Goods placeOrder(Subject subject,String goodName) {
  245. throw new UnsupportedOperationException();
  246. }
  247. @Override
  248. public boolean pay(Goods good, Observer consumer) {
  249. throw new UnsupportedOperationException();
  250. }
  251. @Override
  252. public void cancel(Goods goods) {
  253. System.out.println("已发货,无法取消");
  254. }
  255. @Override
  256. public boolean deliver() {
  257. throw new UnsupportedOperationException();
  258. }
  259. @Override
  260. public void confirm() {
  261. //倒计时开始 结束自动确认收货
  262. downTime();
  263. //确认收货
  264. System.out.println("已确认收货!");
  265. app.setState(app.getCompletedState());
  266. }
  267. @Override
  268. public boolean isNeedHelp() {
  269. return false;
  270. }
  271. @Override
  272. public void service() {
  273. System.out.println("你没有待售后订单");
  274. }
  275. //倒计时方法
  276. private void downTime() {
  277. while (time > 0) {
  278. time--;
  279. try {
  280. Thread.sleep(1000);
  281. int hh = time / 60 / 60 % 60;
  282. int mm = time / 60 % 60;
  283. int ss = time % 60;
  284. System.out.println("还剩" + hh + "小时" + mm + "分钟" + ss + "秒确认收货");
  285. } catch (InterruptedException e) {
  286. e.printStackTrace();
  287. }
  288. }
  289. }
  290. }
  291. //--------------------------------------------------------------------
  292. package FinalAssignment.state.situation.impl;
  293. /**
  294. * 售后状态
  295. */
  296. import FinalAssignment.entity.Goods;
  297. import FinalAssignment.observer.observer.Observer;
  298. import FinalAssignment.observer.subject.Subject;
  299. import FinalAssignment.state.ShopAPP;
  300. import FinalAssignment.state.situation.State;
  301. public class AfterSaleState implements State {
  302. ShopAPP app=null;
  303. public AfterSaleState(ShopAPP app) {
  304. this.app=app;
  305. }
  306. @Override
  307. public Goods placeOrder(Subject subject, String goodName) {
  308. throw new UnsupportedOperationException();
  309. }
  310. @Override
  311. public boolean pay(Goods good, Observer consumer) {
  312. throw new UnsupportedOperationException();
  313. }
  314. @Override
  315. public void cancel(Goods good) {
  316. System.out.println("已发货,无法取消");
  317. }
  318. @Override
  319. public boolean deliver() {
  320. throw new UnsupportedOperationException();
  321. }
  322. @Override
  323. public void confirm() {
  324. throw new UnsupportedOperationException();
  325. }
  326. @Override
  327. public boolean isNeedHelp() {
  328. return false;
  329. }
  330. @Override
  331. public void service() {
  332. System.out.println("已为你售后处理,请稍后为本次服务做出评价,欢迎你下次光临");
  333. //售后完成 本次订单完成 将状态设置为初始值
  334. app.setState(app.getNoPlaceOrderState());
  335. }
  336. }
  337. //--------------------------------------------------------------------
  338. package FinalAssignment.state.situation.impl;
  339. /**
  340. * 已完成订单状态
  341. */
  342. import FinalAssignment.entity.Goods;
  343. import FinalAssignment.observer.observer.Observer;
  344. import FinalAssignment.observer.subject.Subject;
  345. import FinalAssignment.state.ShopAPP;
  346. import FinalAssignment.state.situation.State;
  347. import java.util.Scanner;
  348. public class CompletedState implements State {
  349. Scanner sc = new Scanner(System.in);
  350. ShopAPP app=null;
  351. public CompletedState(ShopAPP app) {
  352. this.app=app;
  353. }
  354. @Override
  355. public Goods placeOrder(Subject subject,String goodName) {
  356. throw new UnsupportedOperationException();
  357. }
  358. @Override
  359. public boolean pay(Goods good, Observer consumer) {
  360. throw new UnsupportedOperationException();
  361. }
  362. @Override
  363. public void cancel(Goods goods) {
  364. System.out.println("已发货,无法取消");
  365. }
  366. @Override
  367. public boolean deliver() {
  368. throw new UnsupportedOperationException();
  369. }
  370. @Override
  371. public void confirm() {
  372. throw new UnsupportedOperationException();
  373. }
  374. @Override
  375. public boolean isNeedHelp(){
  376. System.out.println("是否需要售后服务?(0为取消,1为确认)");
  377. int flag = sc.nextInt();
  378. if (flag==0){
  379. System.out.println("恭喜你购物完成,欢迎你下次光临!");
  380. //本次订单完成 将状态设置为初始值
  381. app.setState(app.getNoPlaceOrderState());
  382. return false;
  383. }else if(flag==1){
  384. app.setState(app.getAfterSaleState());
  385. return true;
  386. }else {
  387. throw new UnsupportedOperationException();
  388. }
  389. }
  390. @Override
  391. public void service() {
  392. }
  393. }
  394. //--------------------------------------------------------------------
  395. package FinalAssignment.state.situation.impl;
  396. /**
  397. * 已取消状态
  398. */
  399. import FinalAssignment.entity.Goods;
  400. import FinalAssignment.observer.observer.Observer;
  401. import FinalAssignment.observer.subject.Subject;
  402. import FinalAssignment.state.ShopAPP;
  403. import FinalAssignment.state.situation.State;
  404. public class CancelledState implements State {
  405. ShopAPP app=null;
  406. public CancelledState(ShopAPP app) {
  407. this.app=app;
  408. }
  409. @Override
  410. public Goods placeOrder(Subject subject, String goodName) {
  411. throw new UnsupportedOperationException();
  412. }
  413. @Override
  414. public boolean pay(Goods good, Observer consumer) {
  415. throw new UnsupportedOperationException();
  416. }
  417. @Override
  418. public void cancel(Goods good) {
  419. System.out.println("订单已取消,已为你退款");
  420. good.setCount(good.getCount()+1);
  421. app.setState(app.getNoPlaceOrderState());
  422. }
  423. @Override
  424. public boolean deliver() {
  425. throw new UnsupportedOperationException();
  426. }
  427. @Override
  428. public void confirm() {
  429. throw new UnsupportedOperationException();
  430. }
  431. @Override
  432. public boolean isNeedHelp() {
  433. throw new UnsupportedOperationException();
  434. }
  435. @Override
  436. public void service() {
  437. System.out.println("你没有待售后订单");
  438. }
  439. }

创建一个APP类

  1. package FinalAssignment.state;
  2. import FinalAssignment.entity.Goods;
  3. import FinalAssignment.observer.observer.Observer;
  4. import FinalAssignment.observer.subject.Subject;
  5. import FinalAssignment.proxy.Proxy;
  6. import FinalAssignment.proxy.impl.NortheastLogistics;
  7. import FinalAssignment.proxy.impl.NorthwestLogistics;
  8. import FinalAssignment.proxy.impl.SoutheastLogistics;
  9. import FinalAssignment.proxy.impl.SouthwestLogistics;
  10. import FinalAssignment.state.situation.State;
  11. import FinalAssignment.state.situation.impl.*;
  12. import org.junit.Test;
  13. import java.util.Scanner;
  14. /**
  15. * 网络商城APP
  16. */
  17. public class ShopAPP {
  18. State state;
  19. Proxy logistics;
  20. //创建几种状态实例
  21. State noPlaceOrderState = new NoPlaceOrderState(this);
  22. State canPayState = new CanPayState(this);
  23. State cancelledState = new CancelledState(this);
  24. State willShippedState=new WillShippedState(this);
  25. State shippedState = new ShippedState(this);
  26. State completedState = new CompletedState(this);
  27. State afterSaleState = new AfterSaleState(this);
  28. //创建物流中心
  29. Proxy swl = new SouthwestLogistics(willShippedState);//西南
  30. Proxy nel = new NortheastLogistics(willShippedState);//东南
  31. Proxy sel = new SoutheastLogistics(willShippedState);//东北
  32. Proxy nwl = new NorthwestLogistics(willShippedState);//西北
  33. public State getWillShippedState() {
  34. return willShippedState;
  35. }
  36. public void setWillShippedState(State willShippedState) {
  37. this.willShippedState = willShippedState;
  38. }
  39. public ShopAPP() {
  40. this.state = noPlaceOrderState;
  41. }
  42. public State getState() {
  43. return state;
  44. }
  45. public void setState(State state){
  46. this.state=state;
  47. }
  48. public State getNoPlaceOrderState() {
  49. return noPlaceOrderState;
  50. }
  51. public State getCanPayState() {
  52. return canPayState;
  53. }
  54. public State getCancelledState() {
  55. return cancelledState;
  56. }
  57. public State getCompletedState() {
  58. return completedState;
  59. }
  60. public State getShippedState() {
  61. return shippedState;
  62. }
  63. public State getAfterSaleState() {
  64. return afterSaleState;
  65. }
  66. //下单的方法
  67. public Goods placeOrder(Subject subject){
  68. Scanner sc = new Scanner(System.in);
  69. System.out.println("请输入您要购买的商品:");
  70. String goodName=sc.nextLine();
  71. return this.state.placeOrder(subject,goodName);
  72. }
  73. //支付的方法
  74. public void pay(Goods good, Observer consumer){
  75. changeMsg(consumer);
  76. choose(consumer);
  77. try {
  78. if(state.pay(good,consumer)){
  79. if(logistics.deliver()){
  80. state.confirm();
  81. if(state.isNeedHelp()){
  82. state.service();
  83. }
  84. }else {
  85. state.cancel(good);
  86. }
  87. }else{
  88. state.cancel(good);
  89. }
  90. }catch (Exception e){
  91. e.printStackTrace();
  92. }
  93. }
  94. //判断顾客收货地址所属区域
  95. private void choose(Observer observer){
  96. if(swl.getAddr().contains(observer.getAddr())){
  97. //西南
  98. logistics=swl;
  99. }
  100. if(nwl.getAddr().contains(observer.getAddr())){
  101. //西北
  102. logistics=nwl;
  103. }
  104. if(sel.getAddr().contains(observer.getAddr())){
  105. //东南
  106. logistics=sel;
  107. }
  108. if(nel.getAddr().contains(observer.getAddr())){
  109. //东北
  110. logistics=nwl;
  111. }
  112. }
  113. //判断用户是否需要修改信息
  114. private void changeMsg(Observer observer){
  115. System.out.println("请确认你的收货信息:");
  116. System.out.println("姓名:"+observer.getName()+" 收货地址:"+observer.getAddr());
  117. System.out.println("是否修改信息?(0:取消订单 1:修改信息 2:不做处理)");
  118. Scanner sc= new Scanner(System.in);
  119. int num=sc.nextInt();
  120. if(num==0){
  121. System.out.println("订单已取消!");
  122. System.exit(0);
  123. }else if(num==1){
  124. System.out.println("请输入收件人:");
  125. sc.nextLine();//接收缓冲区内的空格
  126. String name=sc.nextLine();
  127. observer.setName(name);
  128. System.out.println("请输入地址:");
  129. String addr=sc.nextLine();
  130. observer.setAddr(addr);
  131. System.out.println("修改完成!");
  132. changeMsg(observer);
  133. }else if(num==2){
  134. System.out.println("请及时付款!");
  135. }else {
  136. System.out.println("输入有误!");
  137. changeMsg(observer);
  138. }
  139. }
  140. }

测试代码 

  1. package FinalAssignment;
  2. import FinalAssignment.entity.Goods;
  3. import FinalAssignment.observer.Supermarket;
  4. import FinalAssignment.observer.observer.Observer;
  5. import FinalAssignment.observer.observer.impl.Consumer;
  6. import FinalAssignment.observer.observer.impl.Supplier;
  7. import FinalAssignment.state.ShopAPP;
  8. import java.util.Scanner;
  9. public class Client {
  10. public static void main(String[] args) {
  11. //创建app实例1
  12. ShopAPP app = new ShopAPP();
  13. //创建商店实例
  14. Supermarket supermarket = new Supermarket();
  15. //创建顾客实例
  16. Observer consumer1=new Consumer("刘备",2000,"四川");
  17. Observer consumer2=new Consumer("孙权",1888,"广东");
  18. Observer consumer3=new Consumer("曹操",1000,"宁夏");
  19. Observer consumer4=new Consumer("纳兰性德",1000,"吉林");
  20. //顾客注册
  21. supermarket.getFood().addObserver(consumer1);
  22. supermarket.getVegetables().addObserver(consumer2);
  23. supermarket.getDn().addObserver(consumer3);
  24. supermarket.getVegetables().addObserver(consumer4);
  25. //创建供货商实例
  26. Observer supplier = new Supplier("谢华强");
  27. Observer supplier2 = new Supplier("穿山甲");
  28. //供货商注册
  29. supermarket.getFood().addSupplier(supplier);
  30. supermarket.getVegetables().addSupplier(supplier2);
  31. supermarket.showAll();
  32. //商品降价
  33. supermarket.changePrice(supermarket.getVegetables(),"白菜",0.7f);
  34. //通过app下单
  35. Goods good=app.placeOrder(supermarket.getVegetables());
  36. if(good!=null){
  37. app.pay(good,consumer1);
  38. }
  39. }
  40. }

谨以此篇文章致敬带我打开设计模式大门的老师。斯人已逝,精神永存!


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/894248
推荐阅读
相关标签
  

闽ICP备14008679号