“商苑面馆:Java命令行版之 FoodModel”的版本间的差异
来自CloudWiki
(→与View层、Dao层联调联试) |
|||
第39行: | 第39行: | ||
}</nowiki> | }</nowiki> | ||
===搜索特定商品:findFood( )=== | ===搜索特定商品:findFood( )=== | ||
− | <nowiki>public | + | <nowiki>public Food findFood(String s){ |
//按名称搜索,调用search方法 | //按名称搜索,调用search方法 | ||
Food n01 = foodset.search(s); | Food n01 = foodset.search(s); | ||
第46行: | 第46行: | ||
System.out.println("餐品详细信息为:"+n01.toString()); | System.out.println("餐品详细信息为:"+n01.toString()); | ||
System.out.println("请加入购物车"); | System.out.println("请加入购物车"); | ||
+ | return n01; | ||
}else { | }else { | ||
System.out.println("对不起,我们店现在没有这种面"); | System.out.println("对不起,我们店现在没有这种面"); | ||
+ | return null; | ||
} | } | ||
}</nowiki> | }</nowiki> | ||
第96行: | 第98行: | ||
import dao.*; | import dao.*; | ||
− | import entity. | + | import entity.Food; |
− | + | ||
− | public class | + | public class FoodService { |
− | + | private FoodDao foodset; | |
− | |||
− | |||
− | private | ||
− | + | public FoodService() { | |
− | public | + | this.foodset = new FoodDaoArray(); |
− | + | } | |
+ | public void listFood(){ | ||
+ | System.out.println("本店现有如下餐品:"); | ||
+ | System.out.println(foodset.toString()); | ||
+ | System.out.println("如需打包,另加打包费1元"); | ||
+ | } | ||
+ | public Food findFood(String s){ | ||
+ | //按名称搜索,调用search方法 | ||
+ | Food n01 = foodset.search(s); | ||
+ | |||
+ | if(n01 != null) { | ||
+ | System.out.println("餐品详细信息为:"+n01.toString()); | ||
+ | System.out.println("请加入购物车"); | ||
+ | return n01; | ||
+ | }else { | ||
+ | System.out.println("对不起,我们店现在没有这种面"); | ||
+ | return null; | ||
+ | } | ||
} | } | ||
− | + | public static void main(String[] args) { | |
− | + | // TODO Auto-generated method stub | |
− | + | FoodService fs = new FoodService(); | |
− | + | fs.listFood(); | |
− | |||
− | |||
− | |||
System.out.println(); | System.out.println(); | ||
− | } | + | System.out.println("现在我们来看一下‘炸酱面'"); |
+ | fs.findFood("炸酱面"); | ||
+ | } | ||
− | + | ||
− | + | } | |
− | + | </nowiki> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
2019年5月12日 (日) 07:23的最新版本
目录
Model层的作用
- M即model模型是指模型表示业务规则。在MVC的三个部件中,模型拥有最多的处理任务。被模型返回的数据是中立的,模型与数据格式无关,这样一个模型能为多个视图提供数据,由于应用于模型的代码只需写一次就可以被多个视图重用,所以减少了代码的重复性。
客户端功能
客户端Model层的编写
新建类FoodService
新建包service,在其中新建此类。
package service; import dao.*; public class FoodService { private FoodDao foodset; public FoodService() { this.foodset = new FoodDaoArray(); } public static void main(String[] args) { // TODO Auto-generated method stub } }
列出全部商品:listFood( )
public void listFood(){ System.out.println("本店现有如下餐品:"); System.out.println(foodset.toString()); System.out.println("如需打包,另加打包费1元"); }
搜索特定商品:findFood( )
public Food findFood(String s){ //按名称搜索,调用search方法 Food n01 = foodset.search(s); if(n01 != null) { System.out.println("餐品详细信息为:"+n01.toString()); System.out.println("请加入购物车"); return n01; }else { System.out.println("对不起,我们店现在没有这种面"); return null; } }
在main方法中编写测试代码
public static void main(String[] args) { FoodService fs = new FoodService(); fs.listFood();//测试listFood方法 System.out.println(); System.out.println("现在我们来看一下‘炸酱面'"); fs.findFood("炸酱面");//测试findFood方法 }
与View层、Dao层联调联试
重写View层FoodShow的main方法,主要在方法中调用了一下FoodService.
这样就实现了View 层 调用 Model层 ,Model层又调用 Dao层, MVC三层架构基本建立成功!
public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); FoodShow fs = new FoodShow(); FoodService fservice = new FoodService(); fs.showMenu();//视图层列出菜单 int option = fs.getChoise();//视图层让顾客选择 if(option ==1){ fservice.listFood();//调用业务逻辑层 列出全部食物 } //调用业务逻辑层搜索商品 System.out.println("您想点什么餐品?"); String meal = scan.next();//meal:饭的意思,请替换为你的商品类别 fservice.findFood(meal); scan.close(); }
然后鼠标右击FoodShow类,RUN AS -> JAVA APPLICATION :
完整代码
package service; import dao.*; import entity.Food; public class FoodService { private FoodDao foodset; public FoodService() { this.foodset = new FoodDaoArray(); } public void listFood(){ System.out.println("本店现有如下餐品:"); System.out.println(foodset.toString()); System.out.println("如需打包,另加打包费1元"); } public Food findFood(String s){ //按名称搜索,调用search方法 Food n01 = foodset.search(s); if(n01 != null) { System.out.println("餐品详细信息为:"+n01.toString()); System.out.println("请加入购物车"); return n01; }else { System.out.println("对不起,我们店现在没有这种面"); return null; } } public static void main(String[] args) { // TODO Auto-generated method stub FoodService fs = new FoodService(); fs.listFood(); System.out.println(); System.out.println("现在我们来看一下‘炸酱面'"); fs.findFood("炸酱面"); } }