商苑面馆:Java命令行版之 FoodDao

来自CloudWiki
跳转至: 导航搜索

Dao层

Java8-1.png

前期准备

在entity包下建立Food类:

package entity;

public class Food {
	//成员变量
	//名称
	private String name;
	//价格
	private double price;
	//数量
	private int num;
	
	/*构造方法*/
	public Food(){
		
	}
	public Food(String name,double price,int num){
		this.name = name;
		this.price = price;
		this.num = num;
	}
	
	//成员方法
	public String getName(){
		return this.name;
	}
	public double getPrice(){
		return this.price;
	}
	public int getNum(){
		return this.num;
	}
	//写方法
	public boolean setName(String s){
		this.name = s;
		return true;
	}
	public boolean setPrice(double p){
	    this.price = p;
	    return true;
	}
	public boolean setNum(int n){
	    this.num = n;
	    return true;
	}
	   
	public String toString(){
	    return "名称:" +this.name+"价格:"+
	               this.price + "剩余数量: " + this.num;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Food [] f = new Food[3];
		f[0] =  new Noodle("炸酱面",12,3,"不辣");
		f[1] =  new Rice("牛肉盖浇饭",3,2,"盖浇饭");
		f[2] = new SmallEat("鹌鹑蛋",3,2,"微辣");
		
		
		for(int i =0;i<f.length;i++){
			System.out.println(f[i].toString());
		}
		

	}
	public void buy() {
		// TODO Auto-generated method stub
		
	}

}

实现步骤

建立接口FoodDao

在包dao下建立抽象接口FoodDao

package dao;

import entity.Food;

public interface FoodDao {
	public boolean addFoods(Food goods);//增加商品
	  
	public Food search(String s);
	public String toString(); 
	
	
}

建立接口的实现类

接口实现类可以用多种方式实现,ArrayList,LinkedList, MySQL等

这里我们用ArrayList实现

接口实现类FoodDaoArray

package dao;
import java.util.*;

//import entity.*;
import entity.Food;
import entity.Noodle;
public class FoodDaoArray implements FoodDao{
    //private Food[] n;//食物数组
    private ArrayList<Food> n;//食物数组
    
    public FoodDaoArray( ){    	
    	this.n=new ArrayList<>();
    	Food f1 = new Noodle("炸酱面",12.0,3,"不辣");
    	Food f2 =new Noodle("臊子面",14.0,2,"麻辣");
    	Food f3 = new Noodle("刀削面",10.0,5,"微辣");
    	n.add(f1);n.add(f2);n.add(f3);
    }
    public FoodDaoArray(ArrayList<Food> n ){
    	this.n = n;
    	
    }
    public boolean addFoods(Food f){
    	//this.n.add(f);
    	this.n.add(f);
    	return true;
    }
    //按名称搜
    public Food search(String s){
    	for(int i=0;i<this.n.size();i++){
    		if(n.get(i).getName().equals(s)){
    			return n.get(i);
    		}
    	}
    	return null;
    	//return new Noodle();
    }
 
    public String toString(){
    	String s="";
    	for(int i=0;i<n.size();i++){
    		s += n.get(i).toString()+"\r\n";
    	}
    	return s;
    }
   
    public static void main(String[] args) {
		// TODO Auto-generated method stub
    	 
    	
    }

}

编写测试代码

在FoodDaoArray类的main方法中编写测试代码:

 public static void main(String[] args) {
		// TODO Auto-generated method stub
    	 
    	FoodDao  f = new FoodDaoArray();//用数组列表f初始化食物仓库foodset
       
        System.out.println(f.toString());//调用toString方法
        
        //接收用户输入
        Scanner scan = new Scanner(System.in);
        System.out.println("您想点什么餐品?");               
        String meal = scan.next();
        System.out.println("输入的数据为:" + meal);        
        
        //按名称搜索,调用search方法
        Food n01 = f.search(meal);
       
        if(n01 != null) {
           System.out.println("餐品详细信息为:"+n01.toString());
           System.out.println("请付钱!");
	    }else {
	    	System.out.println("对不起,我们店现在没有这种面");
	    }
        
        scan.close();
        
        
    }

Java5-5.png