Java任务:图书的接口处理
来自CloudWiki
目录
任务描述:图书的业务处理
图书的业务处理需要对图书进行增加、删除、修改、查询等处理。因为图书信息可以保存在内存中、文件中、数据库中,所以增删查改操作方法可能会发生变化。为了避免因信息保存方式变化而修改上层业务代码,本次任务是通过接口描述图书增删查改处理,通过接口调用保持业务代码的稳定。
图5-1 业务处理流程
任务实现
数据库查询的细节会在后面的任务详细讲解。这里大家只要理解通过接口将改变数据存储方式对上层代码的影响降到了最低(只有一行代码用于创建接口实现类对象)即可。
第一步,定义书籍数据访问接口BookDao
DAO是什么
DAO(Data Access Object)是一个数据访问接口,数据访问:顾名思义就是与数据打交道。夹在业务逻辑与数据资源中间。 数据的储存形式是多种多样的,我们现在是把数据暂时储存在内存的数组当中,以后我们也可能把数据存储在文件当中,或是存储在数据库当中。
在内部,数据的存储方式是多种多样的、不断迭代的,但是给外部提供的接口始终相同,这就大大提高了外部人员编程的方便性,也体现了接口的巨大作用
源代码
package dao; import entity.*; public interface BookDao { /** * 向数据仓库中添加商品 * @param goods 向仓库中添加的商品(数组) * @return 值为空 */ public void addGoods(Goods [] goods); /** * 查询数据库中的商品 * @param 值为空 * @return 值为空 */ public void queryGoods(); }}
第二步:编写书籍数据访问实现类BookArrayImpl
(数据存储在数组中),实现接口BookDao。
package dao; import entity.*; public class BookArrayImpl implements BookDao{ /** * 图书仓库类(用于存储图书和其他商品 */ private BookDataSet dbset = null; public BookArrayImpl() { dbset = new BookDataSet(); } /** * 用于向图书仓库添加商品 */ public void addGoods(Goods [] goods){ for(int i=0;i<goods.length;i++){ dbset.addGoods(goods[i]); } } /** * 查询仓库中的商品 */ public void queryGoods( ){ System.out.println("本店现有如下商品:"); dbset.getGoods(); } }
第三步,编写测试类(主类)
package main; import entity.*; import dao.*; /*本类对应实训: */ public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub Goods[] goods=new Goods[5]; goods[0]=new Book("01","think in java","Bruce Eckel",50,"工具类",50.0f); goods[1]=new Book("02","JAVA核心技术","Cay S.Horstmann",40,"工具类",50.0f); goods[2]=new Book("03","射雕英雄传","金庸",30,"文学类",50.0f); goods[3]= new LanguageRepeator("lr01","复读机", "清华同方",151f,50,"tf-001",180); goods[4]= new LanguageRepeator("lr02","复读机", "小米",151f,50,"tf-001",180); BookDao bd = new BookArrayImpl(); bd.addGoods(goods); bd.queryGoods(); } }
项目中的其他类
商品父类Goods
无变动
package entity; public class Goods { protected String cid; //商品编号 protected String name; //商品名称 protected String provider; //商品供应商 protected int number; //商品库存 protected float price; //商品价格 public Goods(){} public Goods(String cid,String name,float price,int number){ this.cid = cid; this.name = name; this.number = number; this.price = price; } public Goods(String cid, String name, String provider, float price, int number) { // TODO Auto-generated constructor stub this.cid = cid; this.name = name; this.provider = provider; this.number = number; this.price = price; } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getProvider() { return provider; } public void setProvider(String provider) { this.provider = provider; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "Goods [cid=" + cid + ", name=" + name + ", provider=" + provider + ", number=" + number + ", price=" + price + "]"; } }
图书类 Book
无变动
package entity; public class Book extends Goods{ String author; // 书籍作者 String category; // 书籍详细分类(类目) public Book() { } public Book(String cid, String name, int number,float price) { super(cid, name, price, number); } public Book(String cid, String name, String author,int number,String category, float price) { super(cid, name, price, number); this.author = author; this.category = category; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } @Override public String toString() { return "商品编号:" + this.cid + " | 商品名:" + this.name +" | 作者:" + this.author+ " | 类目:" + this.category + " | 商品总数:" + this.number+ " |商品价格:"+this.price; } }
复读机类LanguageRepeator
无变动
package entity; public class LanguageRepeator extends Goods { private String model; // 复读机型号 private int duration; //复读时长(秒) public String getModel() { return model; } public void setModel(String model) { this.model = model; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } public LanguageRepeator() { } public LanguageRepeator(String cid, String name, String provider, float price, int number) { super(cid, name, provider, price, number); } public LanguageRepeator(String cid, String name, String provider, float price, int number, String model, int duration) { // TODO Auto-generated constructor stub super(cid, name, provider, price, number); this.model = model; this.duration = duration; } @Override public String toString() { return "商品编号:" + this.cid + " | 商品名:" + this.name +" | 型号:" + this.model+ " | 复读时长:" + this.duration + " | 商品总数:" + this.number+ " |商品价格:"+this.price; } }
图书仓库类BookDataSet
类的成员变量有变动
package entity; public class BookDataSet { /*有商品的品种数,如num=5,代表目前经营5个品种的商品 */ private int num=0; /*存放的商品*/ final static int size = 1000; private Goods [] goods = new Goods[size]; public BookDataSet(){ } public BookDataSet(int num,Goods [] goods){ if(num >=goods.length){ this.num =goods.length; }else { this.num =num; } this.goods=goods; } public void getGoods(){ System.out.println("站点商品明细:"); //显示所有书籍 for(int i=0;i<this.num;i++) { System.out.println(goods[i].toString()); } } public Goods getGoods(String id){ // 遍历所有的书籍信息 for(int i=0;i<this.num;i++){ if (this.goods[i].cid.equals(id)) { return this.goods[i]; } } // 没有找到任何书籍信息抛出null return null; } public void addGoods(Goods mygoods){ if(num==this.goods.length){ System.out.println("商品品种已满"); }else { goods[num] = mygoods; num += 1; } } public void delGoods(Goods myGoods){ //将数组中myGoods条目对应的商品数量减为0 } public void ModifyGoods(Goods myGoods){ //将数组中myGoods条目对应的信息修改一下 } @Override public String toString() { int i; String s=""; for(i=0;i<this.num;i++){ s +=this.goods[i].toString()+"\n"; } return s; } public void adddGoods(Book newbook) { // TODO Auto-generated method stub } }
类别类Category
无变动
package entity; public class Category { private int id = 0; //商品一级类目 private String firstLevel; //商品二级类目 private String secondLevel; /* * 无参构造函数 */ public Category() {} /* * 带三个参数的构造函数 */ public Category(int id, String firstLevel, String secondLevel) { this.id = id; this.firstLevel = firstLevel; this.secondLevel = secondLevel; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstLevel() { return firstLevel; } public void setFirstLevel(String firstLevel) { this.firstLevel = firstLevel; } public String getSecondLevel() { return secondLevel; } public void setSecondLevel(String secondLevel) { this.secondLevel = secondLevel; } @Override public String toString() { return this.firstLevel + ">" + this.secondLevel; } }
返回 Java程序设计