博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Java]完成Book的管理(entity/dao/test)增删改查操作[ArrayList]
阅读量:3942 次
发布时间:2019-05-24

本文共 3031 字,大约阅读时间需要 10 分钟。

 

1.创建Entity

 

/** * 完成Book的管理 * 要求:有包entity/dao/test→查询,修改,删除,增加操作(ArrayList来存储) * @author Administrator * */public class Books {	private String id;	private String name;	private double price;	public String getId() {		return id;	}	public void setId(String id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public double getPrice() {		return price;	}	public void setPrice(double price) {		this.price = price;	}	@Override	public String toString() {		return "BooksEntity [id=" + id + ", name=" + name + ", price=" + price + "]";	}	public Books(String id, String name, double price) {		super();		this.id = id;		this.name = name;		this.price = price;	}		}

2.创建dao(方法)

import java.util.ArrayList;import java.util.Iterator;public class Booksdao {	//创建集合来存储Books	private ArrayList
List = new ArrayList
(); //增 public boolean add(Books b) { //判断b是否为空 if (b == null) { //为空返回false return false; } //否则add到List List.add(b); //返回true return true; } //删 public boolean deleteById(String id) { for (int i = 0; i < List.size(); i++) { //集合中的元素赋值给b来存储 Books b = List.get(i); if (id.equals(b.getId())) { //判断值相等做删除 List.remove(i); return true; } }return false; } public boolean updateBooks(Books s) { for (int i = 0; i < List.size(); i++) { //集合中的元素赋值给b来存储 Books b = List.get(i); if (s.getId().equals(b.getId())) { //将新对象s替换掉原本的b List.set(i, s); return true; } }return false; } /** * * @param id 根据id找到对象 * @return 如果为null则没有找到 */ public Books getBooksById(String id) { for (int i = 0; i < List.size(); i++) { //集合中的元素赋值给b来存储 Books b = List.get(i); if (id.equals(b.getId())) { return b; } }return null; } //查询所有数据 public ArrayList
getBooks(){ return List; }}

3.测试

package com.cs.work01;import java.util.ArrayList;public class TestBooks {	public static void main(String[] args) {		Booksdao bd = new Booksdao();				//测试增,增加新对象		Books b1 = new Books("156484", "《百年孤独》", 34.40);		Books b2 = new Books("187916", "《美国大城市的生与死》", 9.9);		Books b3 = new Books("354891", "《城市发展史》", 24.90);		Books b4 = new Books("008763", "《理解》", 199);				bd.add(b1);		bd.add(b2);		bd.add(b3);		bd.add(b4);		//输出测试		ArrayList
List = bd.getBooks(); if (List.isEmpty()) { System.out.println("集合无数据"); }else List.forEach(System.out::println); //测试删除 System.out.println("*****************删除后*************"); bd.deleteById("008763"); if (List.isEmpty()) { System.out.println("集合无数据"); }else List.forEach(System.out::println); //测试改 //id不变,修改name和price System.out.println("*****************修改后*************"); Books b5 = new Books("354891", "《少年派克漂流记》", 79.8); //返回修改结果flag boolean flag = bd.updateBooks(b5); System.out.println(flag); //输出 if (List.isEmpty()) { System.out.println("集合无数据"); }else List.forEach(System.out::println); //查 System.out.println("*****************查询结果*************"); System.out.println(bd.getBooksById("156484")); }}

结果:

转载地址:http://utnwi.baihongyu.com/

你可能感兴趣的文章
read obj in matlab
查看>>
find out the neighbour matrix of a mesh
查看>>
Operators and special characters in matlab
查看>>
As-Conformal-As-Possible Surface Registration
查看>>
qmake Variable Reference
查看>>
Lesson 2 Gradient Desent
查看>>
find border vertex
查看>>
matlab sliced variable
查看>>
create symbolic array
查看>>
TAUCS库的编译(vs2010)
查看>>
color vector using in plotting example points and lines between corresponding vertices
查看>>
mex 里面调用matlab函数
查看>>
matlab中cuda编程中分配grid和block dimension的时候的注意事项
查看>>
GPU CUDA and MEX Programming
查看>>
arrayfun用法
查看>>
矩阵积分
查看>>
optimization on macOS
查看>>
Template-Based 3D Model Fitting Using Dual-Domain Relaxation
查看>>
install libfreenect2 on ubuntu 16.04
查看>>
how to use automake to build files
查看>>