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程序员职场全攻略》有感
从小工到专家 ——读<Java程序员职场全攻略>有感 <Java程序员职场全攻略>是以故事的形式,向读者介绍Java程序员的职场经验.作者牛开复在北京从事软件开发,已经是一 ...
- java多线程实现卖票小程序
package shb.java.demo; /** * 多线程测试卖票小程序. * @Package:shb.java.demo * @Description: * @author shaobn * ...
- java 多线程之卖票两种方式
1.通过extends Thread /* 需求:简单的卖票,多个窗口同时买票 (共用资源) 创建线程的第二种方式:实现Runnable接口 步骤: 1,定义类实现Runnable接口 2,覆盖/重写 ...
- Java 集成 速卖通开发.
一.申请成为开发者 申请入口:http://isvhz.aliexpress.com/isv/index.htm 说明文档:http://activities.aliexpress.com/open/ ...
- Java 集成速卖通开发.
一.申请成为开发者 申请入口:http://isvhz.aliexpress.com/isv/index.htm 说明文档:http://activities.aliexpress.com/open/ ...
- Java之父职场路
Java之父——詹姆斯·高斯林出生于加拿大,是一位计算机编程天才.在卡内基·梅隆大学攻读计算机博士学位时,他编写了多处理器版本的Unix操作系统,是JAVA编程语言的创始人.1991年,在Sun公司工 ...
- 外贸开发,用java调用速卖通api第一步,token的获取。
第一步 定义速卖通api的常量 public String client_id; public String client_key; public String site; 第二步 获取登 ...
- 《Java程序员职场全攻略 从小工到专家》 - 书摘精要
(前言) 学习招式在次,提升内力才是最主要的: (P10) 选择一门编程语言,只是入门的途径.过分依赖编程语言,只会让自己成为代码高手,而不是开发大牛,要知道编程语言只是一种工具,更重要的是编程思想: ...
- 实验11——java线程模拟卖票
package cn.tedu.demo; /** * @author 赵瑞鑫 E-mail:1922250303@qq.com * @version 1.0 * @创建时间:2020年7月31日 下 ...
随机推荐
- Socket编程之Tomcat模拟_采坑汇总
用java.net.Socket来模拟实现Tomcat,碰到了一些坑,大部分是没有想到的,记录下来自查. 直接上代码, public class TomcatDemo { private static ...
- AI关注的网址
中科院计算技术研究所博士招生:http://admission.ucas.ac.cn/info/ZhaoshengDanweiDetail/9adf9e50-424b-44c8-b2dc-900ef9 ...
- 嵌入式开发之davinci--- 8148/8168/8127 中的大屏分布式拼接显示系统
其实在接触从12年接触8127系列ipnc 时看到200w和500w的高清像素,我就萌生了视频拼接的兴趣,没想到今年的安博会就有公司推出产品了,它就是上海的环视科技,从他的主页可以看到,明显的有个只能 ...
- Android插件化(使用Small框架)
github: https://github.com/cayden/MySmall Android插件化(使用Small框架) 框架源代码 1. Create Project File->New ...
- SQL SERVER 2008查看sql执行的时间
set statistics profile onset statistics io onset statistics time ongo<这里写上你的语句...>goset statis ...
- Boxes and Candies(贪心)
Boxes and Candies Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Ther ...
- WCF基础之承载服务和生成客户端
wcf的承载有三种iis.was和自承载,它们如何承载园子里有很多,就不多赘述. 自iis7以后iis支持http和非http的协议,使用iis承载服务与其他两种承载相比,我觉的最明显的是was和自承 ...
- ASP-Dictionary对象-基础用法
1.存在 dim d set d=Server.CreateObject("Scripting.Dictionary") d.Add "c", "Ch ...
- lua2json
lua对象json序列化,很简单,没做中文的unicode编码了 local function bool2json(v) return v and "true" or " ...
- Jeecms 防xss处理原理
Web.xml配置过滤器,并指的要过滤和替换的字符: 过滤器的filter方法,对传入的HttpServletRequest对象进行了修改 具体过滤在XssHttpServletRequestWrap ...