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:练习超市卖场的更多相关文章

  1. 从小工到专家 ——读《Java程序员职场全攻略》有感

    从小工到专家 ——读<Java程序员职场全攻略>有感   <Java程序员职场全攻略>是以故事的形式,向读者介绍Java程序员的职场经验.作者牛开复在北京从事软件开发,已经是一 ...

  2. java多线程实现卖票小程序

    package shb.java.demo; /** * 多线程测试卖票小程序. * @Package:shb.java.demo * @Description: * @author shaobn * ...

  3. java 多线程之卖票两种方式

    1.通过extends Thread /* 需求:简单的卖票,多个窗口同时买票 (共用资源) 创建线程的第二种方式:实现Runnable接口 步骤: 1,定义类实现Runnable接口 2,覆盖/重写 ...

  4. Java 集成 速卖通开发.

    一.申请成为开发者 申请入口:http://isvhz.aliexpress.com/isv/index.htm 说明文档:http://activities.aliexpress.com/open/ ...

  5. Java 集成速卖通开发.

    一.申请成为开发者 申请入口:http://isvhz.aliexpress.com/isv/index.htm 说明文档:http://activities.aliexpress.com/open/ ...

  6. Java之父职场路

    Java之父——詹姆斯·高斯林出生于加拿大,是一位计算机编程天才.在卡内基·梅隆大学攻读计算机博士学位时,他编写了多处理器版本的Unix操作系统,是JAVA编程语言的创始人.1991年,在Sun公司工 ...

  7. 外贸开发,用java调用速卖通api第一步,token的获取。

     第一步 定义速卖通api的常量  public String client_id;  public String client_key;  public String site;   第二步 获取登 ...

  8. 《Java程序员职场全攻略 从小工到专家》 - 书摘精要

    (前言) 学习招式在次,提升内力才是最主要的: (P10) 选择一门编程语言,只是入门的途径.过分依赖编程语言,只会让自己成为代码高手,而不是开发大牛,要知道编程语言只是一种工具,更重要的是编程思想: ...

  9. 实验11——java线程模拟卖票

    package cn.tedu.demo; /** * @author 赵瑞鑫 E-mail:1922250303@qq.com * @version 1.0 * @创建时间:2020年7月31日 下 ...

随机推荐

  1. oracle高性能的SQL语句的写法

    1.当多表查询的时候,把数据量小的表放在最后面,ORACLE会把最后面的表当作基础表,因为表间连接时,最右边的表会被放到嵌套循环的最外层.最外层的循环次数越少,效率越高. 2.Oracle采用自下而上 ...

  2. IE 下 log 调试的一点不同

    介绍一点IE下控制台调试与chrome的区别 数组 alert([1,2]) console.log([1,2]) console.log([1,2],"1,2") alert I ...

  3. 调用http接口耗时过长。

    利用CRUL命令简单分析请求细节所占用的时间吧 curl -o /dev/null -s -w %{http_code}:%{time_namelookup}:%{time_redirect}:%{t ...

  4. php 在linux 用file_exists() 函数判断 另外一台服务器映射过来的文件是否存在 总是返回false

    php 在linux 用file_exists() 函数判断 另外一台服务器映射过来的文件是否存在 总是返回false .如下案例 $type="android"; $url=&q ...

  5. 我在面试.NET/C#程序员时会提出的问题(转载)

    转自:http://blog.zhaojie.me/2011/03/my-interview-questions-for-dotnet-programmers.html 说起来我也面试过相当数量的.N ...

  6. python selenium - SSL处理(https)

    在实际的自动化测试实践中,因为越来越多的站点接入https,使得我们原有的python selenium2自动化测试代码进行测试时,浏览器总是报安全问题,即便在浏览器选项中将被测网址加入信任网址也没用 ...

  7. mxnet编译问题手记

    MXNet在64位Win7下的编译安装:https://www.cnblogs.com/noahzn/p/5506086.html http://blog.csdn.net/Jarvis_wxy/ar ...

  8. Spring @Configuration注解

    1 该注解的用途 这个注解表示这个类可以作为spring ioc容器bean的来源,其本质上它是对xml文件中创建bean的一种替换.有了这个注释,Spring framework就能在需要的时候构造 ...

  9. swift 一句代码补全tableView分割线

    1.swift实现分割线补全 swift一个大进步,只要设置tableView.separatorInset = UIEdgeInsets.zero即可补全分割线, 2.OC实现分割线补全 而在OC中 ...

  10. Zookeeper Curator 事件监听 - 秒懂

    目录 写在前面 1.1. Curator 事件监听 1.1.1. Watcher 标准的事件处理器 1.1.2. NodeCache 节点缓存的监听 1.1.3. PathChildrenCache ...