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. Android备份和添加短信

    手机发送成功的,没有成功的,接受的短信都存放在手机自带的数据库中. 现在想要备份一下这个短信,需要访问这个数据库,然后查询所有的短信.就需要内容提供者(短信). 首先要找到内容提供者的uri.

  2. Android程序的打包和安装

    当我们使用Android Studio的时候,这些步骤都交给它去做了. 编译 classes.dex 文件 编译 resources.arsc 文件 生成资源索引表resources.arsc. 把r ...

  3. java.io.IOException: Illegal partition for 67 (-1)

    今天写MapReduce的分区进行排序的功能,自己写了一个Partitioner,然后用的时候就错了 public static class MyPartition extends Partition ...

  4. simple_pool对象池——优化&lt;二&gt;

    本文章由cartzhang编写.转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/55051570 作者:car ...

  5. Mysql----MySQL的mysql_insert_id和LAST_INSERT_ID(转)

    本文介绍的是mysql中last_insert_id和mysql_insert_id的区别 1 mysql_insert_id 一.PHP获取MYSQL新插入数据的ID mysql_insert_id ...

  6. html5小趣味知识点系列(二)tabindex

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 统计输入的单词中有几个长度大于n的,n是自己指定的,用函数对象实现

    #ifndef COUNT_WORD_H #define COUNT_WORD_H #include <string.h> #include <iostream> #inclu ...

  8. spring+struts1

    概括及介绍: 集成原理:在Action 中获得BeanFactory,通过BeanFactory取得业务逻辑对象 本例采用:JDK1.8,tomcat7.0.9  技术点:spring与strut1集 ...

  9. 执行动态的delphi脚本

    相关资料:https://www.cnblogs.com/linyawen/archive/2011/10/01/2196950.html 如何在程序中执行动态生成的Delphi代码 经常发现有人提这 ...

  10. Mysql无法创建函数解决办法

    执行: set global log_bin_trust_function_creators =1; 原文参照:http://www.cnblogs.com/xd502djj/archive/2012 ...