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

来自CloudWiki
跳转至: 导航搜索

实训目的

用HashMap重新仓库类Store,实现增删查改功能。

map接口常用方法

方法名	说    明
Object put(Object key,Object value)	插入新的对象,并用key作为其键字
void putAll(Map t)	将另一个Map中的所有对象复制进来
Set entrySet()	返回映射中的关键字-值对的集合
Set keySet()	返回映射中所有关键字的集合
Collection values()	返回映射中所有值的集合
Object remove(Object key)	删除指定的对象
Object get(Object key)	获取与key相联系的对象
boolean containsKey(Object key)	判断是否包含指定的键值
Boolean containsValue(Object value)	判断是否包含指定的对象


分步代码

构造Store类

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

编写数量方法

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

编写添加功能

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

编写删除功能

public void delete(String id) {
		//补充你的代码
	}

编写查询功能

public String find(String id) {//查询某一商品
		//补充你的代码
	}

编写查询所有功能

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

编写修改商品功能

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

编写调用代码

main方法中:

public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Store s = new Store();  
		Goods g1= new Goods("山西刀削面",105.0f,30);
		Goods g2= new Noodles("兰州牛肉面",15.0f,20);
		Goods g3= new Rice("黄焖鸡米饭",12.0f,30,false);
		
		//添加元素		
		System.out.println("执行添加餐品操作...");
		s.add("01", g1);
		s.add("02", g2);
		s.add("03", 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="02";
		String d2= s.find(id);
		System.out.println(id+"号商品详情为:");
		System.out.println(d2);
		
		
        
        
	}

全部代码

package entity;

import java.util.*;

public class Store {
	private  HashMap<String,Goods> gmap;
	private int num ;
	
	public Store() {
		num = 3;
		gmap =  new HashMap<>();
       
	}
	public int getNum() {
		return this.gmap.size();
	}
	
	public void add(String id,Goods g) {		
	   this.gmap.put(id, g);   
		
	}
	
	public void delete(String id) {
		
	}
	public String find(String id) {//查询某一商品
		
	}
	
	public String findAll() {//查询所有商品
		String total="";
		Set<String> keys = this.gmap.keySet();
		System.out.println("编号\t餐品名");
		System.out.println("***********************");
		//遍历keyset,通过map.get(key)方法获得value的值
		for (String key : keys) {
			total +=(key + "\t" + this.gmap.get(key)+"\n");
		}
		return total;
	}
	public void change(String id,Goods g) {
		this.gmap.put(id, g);
	}

	 @Override
	public String toString() {
		String total="";
		Set<String> keys = this.gmap.keySet();
		System.out.println("编号\t餐品名");
		System.out.println("***********************");
		//遍历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
		
		Store s = new Store();  
		Goods g1= new Goods("山西刀削面",105.0f,30);
		Goods g2= new Noodles("兰州牛肉面",15.0f,20);
		Goods g3= new Rice("黄焖鸡米饭",12.0f,30,false);
		
		//添加元素		
		System.out.println("执行添加餐品操作...");
		s.add("01", g1);
		s.add("02", g2);
		s.add("03", 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="02";
		String d2= s.find(id);
		System.out.println(id+"号商品详情为:");
		System.out.println(d2);
		
		
        
        
	}

}