Java任务:用集合和接口实现商品库2

来自CloudWiki
跳转至: 导航搜索

背景知识

Java任务:用集合和接口实现商品库中 我们改写了仓库类,

为了隐藏底层细节,我们应用接口的有关知识,为商品仓库写个接口。

这样外界调用时,直接调用接口就好了 ~

下面我们为仓库类加上接口

项目结构

Ai20-4-52.png

实体类的变动

Goods类及其子类中增添一个成员变量id

同时构造方法及main里面的调用做相应修改。

Ai20-4-49.png

Ai20-4-50.png

Ai20-4-51.png

编写仓库访问接口GoodsDao

public interface GoodsDao {
	  public int getNum();
	  public void add(Goods g);//增加商品
	  
	  public void delete(String id);//删除商品
	  
	  public String findAll();//查询所有商品
	  
	  public String find(String id);//查询某种商品是否存在
	  
	  public void modify(String id,float p);//修改商品
}

实现仓库访问接口GoodsDao

注意:接口的实现类应实现接口的全部功能。

创建GoodsDaoMap类

GoodsDaoMap类就是以前的Store类,为了与接口对接,改了个名字。

public class GoodsDaoMap implements GoodsDao{
	private  HashMap<String,Goods> gmap;
	private int num ;
	
	public GoodsDaoMap() {
		num = 3;
		gmap =  new HashMap<>();
       
	}

实现getNum()方法

public int getNum() {
		return this.gmap.size();
	}
	

实现add() 方法

public void add(Goods g) {
	   String id = g.getId();
	   this.gmap.put(id, g);   
		
	}

实现delete() 方法

public void delete(String id) {
		this.gmap.remove(id);
	}

实现find(String id)方法

public String find(String id) {//查询某一商品
		if(this.gmap.containsKey(id)) {
			return this.gmap.get(id).toString();
		}else {
			return null;
		}
	}

实现findAll() 方法

public String findAll() {//查询所有商品
		String total="";
		Set<String> keys = this.gmap.keySet();
		total +=("编号\t餐品名\n");
		total +=("***********************\n");
		//遍历keyset,通过map.get(key)方法获得value的值
		for (String key : keys) {
			total +=(key + "\t" + this.gmap.get(key)+"\n");
		}
		return total;
	}

实现modify方法

public void modify(String id,float p) {
		
	    Goods h;
		if(this.gmap.containsKey(id)) {
			h = this.gmap.get(id);
			h.setPrice(p);
			this.gmap.put(id, h);
		}
		
	}

编写toString()方法

@Override
	public String toString() {
		String total="";
		Set<String> keys = this.gmap.keySet();
		total +=("编号\t餐品名\n");
		total +=("***********************\n");
		//遍历keyset,通过map.get(key)方法获得value的值
		for (String key : keys) {
			total +=(key + "\t" + this.gmap.get(key)+"\n");
		}
		return total;
	}

完整代码

package dao;

import java.util.*;

import entity.Goods;
import entity.Noodles;
import entity.Rice;

public class GoodsDaoMap implements GoodsDao{
	private  HashMap<String,Goods> gmap;
	private int num ;
	
	public GoodsDaoMap() {
		num = 3;
		gmap =  new HashMap<>();
       
	}
	public int getNum() {
		return this.gmap.size();
	}
	
	public void add(Goods g) {
	   String id = g.getId();
	   this.gmap.put(id, g);   
		
	}
	
	public void delete(String id) {
		this.gmap.remove(id);
	}
	public String find(String id) {//查询某一商品
		if(this.gmap.containsKey(id)) {
			return this.gmap.get(id).toString();
		}else {
			return null;
		}
	}
	
	public String findAll() {//查询所有商品
		String total="";
		Set<String> keys = this.gmap.keySet();
		total +=("编号\t餐品名\n");
		total +=("***********************\n");
		//遍历keyset,通过map.get(key)方法获得value的值
		for (String key : keys) {
			total +=(key + "\t" + this.gmap.get(key)+"\n");
		}
		return total;
	}
	public void modify(String id,float p) {
		
	    Goods h;
		if(this.gmap.containsKey(id)) {
			h = this.gmap.get(id);
			h.setPrice(p);
			this.gmap.put(id, h);
		}
		
	}

	 @Override
	public String toString() {
		String total="";
		Set<String> keys = this.gmap.keySet();
		total +=("编号\t餐品名\n");
		total +=("***********************\n");
		//遍历keyset,通过map.get(key)方法获得value的值
		for (String key : keys) {
			total +=(key + "\t" + this.gmap.get(key)+"\n");
		}
		return total;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		GoodsDaoMap s = new GoodsDaoMap();  
		Goods g1= new Goods("01","山西刀削面",105.0f,30);
		Goods g2= new Noodles("02","兰州牛肉面",15.0f,20);
		Goods g3= new Rice("03","黄焖鸡米饭",12.0f,30,false);
		
		//添加元素		
		System.out.println("执行添加餐品操作...");
		s.add(g1);
		s.add(g2);
		s.add(g3);
		
		System.out.println("仓库的商品总数为:"+s.getNum());
		System.out.println("以下为商品详情信息:");
		System.out.println(s.toString());
		//删除元素
		System.out.println("执行删除餐品操作...");
		s.delete("02");
		//查找所有商品
		String d1 = s.findAll();
		System.out.println("删除后商品信息为:");
		System.out.println(d1);
		
		//查找某一商品
		String id="01";
		String d2= s.find(id);
		System.out.println(id+"号商品详情为:");
		System.out.println(d2);
		
		
        
        
	}

}

编写测试类TestGoods

package main;

import dao.GoodsDao;
import dao.GoodsDaoMap;
import entity.Goods;
import entity.Noodles;
import entity.Rice;

public class TestGoods {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GoodsDao s = new GoodsDaoMap(); //接口回调
		//GoodsDao s = new GoodsDaoSet();
		Goods g1= new Goods("01","山西刀削面",105.0f,30);
		Goods g2= new Noodles("02","兰州牛肉面",15.0f,20);
		Goods g3= new Rice("03","黄焖鸡米饭",12.0f,30,false);
		
		//添加元素		
		System.out.println("执行添加餐品操作...");
		s.add(g1);
		s.add(g2);
		s.add(g3);
		
		System.out.println("仓库的商品总数为:"+s.getNum());
		System.out.println("以下为商品详情信息:");
		System.out.println(s.toString());
		//删除元素
		System.out.println("执行删除餐品操作...");
		s.delete("02");
		//查找所有商品
		String d1 = s.findAll();
		System.out.println("删除后商品信息为:");
		System.out.println(d1);
		
		//查找某一商品
		String id="01";
		String d2= s.find(id);
		System.out.println(id+"号商品详情为:");
		System.out.println(d2);
		
	}

}