java:练习超市卖场

Posted 穆晟铭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java:练习超市卖场相关的知识,希望对你有一定的参考价值。

java:练习超市卖场

涉及到:大商品类,具体商品(以书为例),卖场类

Goods,Book,superMart, 

商品类Goods:

public interface Goods {

	//商品类
	public String getName();
	public int getCount();
	public float getPrice();
	
	
	
	
}

 书:

注意:复写hashCode,和equals是为了实现删除按钮

package abc;

public class Book implements Goods {

	private String name;
	private int count;
	private float price;
	public String getName() {
		return name;
	}
	
	
	
	
	public Book() {
		super();
	}




	public Book(String name, int count, float price) {
		super();
		this.name = name;
		this.count = count;
		this.price = price;
	}

	
	



	public void setName(String name) {
		this.name = name;
	}
	
	public int getCount() {
		return count;
	}
	
	public void setCount(int count) {
		this.count = count;
	}
	
	public float getPrice() {
		return price;
	}
	
	public void setPrice(float price) {
		this.price = price;
	}

	
	


	//复写hashCode,和equals是为了实现删除按钮
	@Override
	public int hashCode() {
		return this.name.hashCode() + 
				new Integer(this.count).hashCode() +
				 new Float(this.price).hashCode();
	}



	//复写hashCode,和equals是为了实现删除按钮
	@Override
	public boolean equals(Object obj) {
		if(obj == this)
		{
			return true;
		}
		if(!(obj instanceof Book))
		{
			return false;
		}
		Book b = (Book) obj;
		if( b.name.equals(this.name) && b.count == this.count && b.price == this.price)
		{
			return true;
		}else {
			return false;
		}
	}




	@Override
	public String toString() {
		return "书名:" + name + ", 数量:" + count + ", 价格:" + price ;
	}
	
	
	
	
	
	
	
	
	
}

  

超级市场:

需要注意remove删除方法,必须在BOOK中定义相关的equals,hashCode方法才能删除

//删除,需要复写book里面的equals和hasCode
remove(Goods good)
package abc;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SuperMark {

	private List<Goods> allGoods;


	public SuperMark()
	{
		this.allGoods = new ArrayList<Goods>();
	}
	
	
	public void add(Goods good)
	{
		this.allGoods.add(good);
	}
	
	//删除,需要复写book里面的equals和hasCode
	public void remove(Goods good)
	{
		this.allGoods.remove(good);
	}
	
	public List<Goods> search(String keyword)
	{
		List<Goods> temp = new ArrayList<Goods>();
		Iterator<Goods> iter = this.allGoods.iterator();
		while(iter.hasNext())
		{
			Goods g = iter.next();
			if(g.getName().indexOf(keyword) != -1)
			{
				temp.add(g);
			}
		}
		return temp;
	}
	
	
	public List<Goods> getAllGoods()
	{
		return this.allGoods;
	}
	
	
	
}

  

测试:

 

public class Demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		System.out.println("gaga");
		
		
		
		SuperMark sm = new SuperMark();
		sm.add(new Book("java",5,10.4f));
		sm.add(new Book("net",6,22.f));
		sm.add(new Book("php",6,10f));
		
		print(sm.search("j"));
		
		
		
	}
	
	
	public static void print(List all)
	{
		Iterator iter = all.iterator();
		while(iter.hasNext())
		{
			System.out.println(iter.next());
		}
	}

}

  

 

 

 

 

以上是关于java:练习超市卖场的主要内容,如果未能解决你的问题,请参考以下文章

无人超市系统开发的流程

java面向对象复习之一

当新零售遇上 Serverless

当新零售遇上 Serverless

新零售:从上云到云原生 Serverless

存储过程练习 超市管理系统