“Java任务:方法重载之查找商品”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
第54行: 第54行:
 
}
 
}
 
</nowiki>
 
</nowiki>
 +
 +
==项目的其他方法==
 +
Book类:
 +
<nowiki>package entity;
 +
 +
public class Book {
 +
    String bid; // 书籍编号
 +
    String name; // 书籍名字
 +
    String author; // 书籍作者
 +
    int number; // 书籍库存
 +
    float price;//书籍价格
 +
    String category; // 书籍详细分类(类目)
 +
 +
/*
 +
* 空构造函数
 +
*/
 +
public Book() {
 +
 +
}
 +
 +
/*
 +
* 含六个参数的构造函数
 +
*/
 +
public Book(String bid, String name, String author, int number, String category,float price) {
 +
               
 +
this.bid = bid;//这里的this表示本类的bid属性,也可以不加
 +
this.name = name;
 +
this.author = author;
 +
this.number = number;
 +
this.category = category;
 +
        this.price = price;
 +
}
 +
 +
public  float getPrice(){
 +
return this.price;
 +
}
 +
public void setPrice(float p){
 +
this.price=p;
 +
}
 +
public void ShowInfo()
 +
{
 +
 +
System.out.println( "商品编号:" + this.bid + " | 商品名:" + this.name
 +
+" | 作者:" + this.author+ " | 类目:" + this.category + " | 商品总数:" + this.number+
 +
" |商品价格:"+this.price);
 +
}
 +
 +
@Override
 +
public String toString()
 +
{
 +
return "商品编号:" + this.bid + " | 商品名:" + this.name
 +
+" | 作者:" + this.author+ " | 类目:" + this.category + " | 商品总数:" + this.number+
 +
" |商品价格:"+this.price+"\n";
 +
}
 +
 +
}</nowiki>
 +
MainClass类:
 +
<nowiki>package main;
 +
import entity.Book;
 +
import entity.Category;
 +
import entity.BookDataSet;
 +
public class MainClass {
 +
 +
public static void main(String[] args) {
 +
// TODO Auto-generated method stub
 +
 +
Category c1 = new Category(1, "工具类", "软件编程");
 +
Category c2 = new Category(1, "文学类", "小说");
 +
System.out.println("商品的类别有:");
 +
System.out.println(c1.toString());
 +
System.out.println(c2.toString());
 +
System.out.println();
 +
 +
int size = 5;    
 +
    Book[] books=new Book[size];
 +
    books[0]=new Book("01","think in java","Bruce Eckel",50,"工具类",50.0f);
 +
    books[1]=new Book("02","JAVA核心技术","Cay S.Horstmann",40,"工具类",50.0f);
 +
    books[2]=new Book("03","射雕英雄传","金庸",30,"文学类",50.0f);
 +
    books[3]=new Book("04","平凡的世界","路遥",20,"文学类",50.0f);
 +
    books[4]=new Book("05","心理罪","雷米",60,"文学类",50.0f);
 +
   
 +
BookDataSet dbset = new BookDataSet(size,books);
 +
System.out.println("图书的具体信息是:");
 +
System.out.println(dbset.toString());
 +
}
 +
 +
 +
}</nowiki>
  
 
返回 [[Java程序设计]]
 
返回 [[Java程序设计]]

2018年4月17日 (二) 02:57的版本

任务描述:查找图书

用户有时需要根据不同的条件来查询图书,比如编号、书名、作者、分类等。项目goodsmanage_console中的BookDaoImpl类提供了查询图书的方法。类中已经提供了两个版本的queryBook()方法,分别用于查询所有图书和指定编号的图书。你的任务是再提供一个queryBook()方法,根据指定书名、作者或分类来查询图书。

任务实现

数据库查询的细节会在后面的任务详细讲解。这里使用测试数据演示方法重载功能。

package entity;

public class BookDataSet {
     /*书籍的总数*/
	private int num;
	/*存放的书籍*/
	private Book [] books;
	
	
    public BookDataSet(){
    	
    }
     public BookDataSet(int num,Book [] books){
    	this.num =num;
    	this.books=books;
     }
    public void getBooks(){
    	System.out.println("站点商品明细:");
		//显示所有书籍
		for(int i=0;i<books.length;i++) {
			System.out.println(books[i]);
		}
    }
    public Book getBooks(String id){
    	// 遍历所有的书籍信息
    	for(int i=0;i<this.books.length;i++){
    		if (this.books[i].cid.equals(id)) {
    			return this.books[i];
    		}
    	}
    	// 没有找到任何书籍信息抛出null
    	return null;
    }
    
    @Override
 	public String toString() {
    	 int i;
		 String s="";
		 for(i=0;i<this.books.length;i++){
			 s +=this.books[i].toString();
		 }
		 return s;
 	}

}

项目的其他方法

Book类:

package entity;

public class Book {
	    String bid; // 书籍编号
	    String name; // 书籍名字
	    String author; // 书籍作者
	     int number; // 书籍库存
	    float price;//书籍价格
	    String category; // 书籍详细分类(类目)
		
		/*
		 * 空构造函数
		 */
		public Book() {
			
		}
		
		/*
		 * 含六个参数的构造函数
		 */
		public Book(String bid, String name, String author, int number, String category,float price) {
	                
			this.bid = bid;//这里的this表示本类的bid属性,也可以不加
			this.name = name;
			this.author = author;
			this.number = number;
			this.category = category;
	        this.price = price;
		}
		
		public  float getPrice(){
			return this.price;			
		}
		public void setPrice(float p){
			this.price=p;
		}
		public void ShowInfo() 
		 {
					
			 System.out.println( "商品编号:" + this.bid + " | 商品名:" + this.name 
					 +" | 作者:" + this.author+ " | 类目:" + this.category + " | 商品总数:" + this.number+ 
					 " |商品价格:"+this.price);
		}
		
		 @Override
		public String toString() 
		 {
				return "商品编号:" + this.bid + " | 商品名:" + this.name 
						+" | 作者:" + this.author+ " | 类目:" + this.category + " | 商品总数:" + this.number+
						" |商品价格:"+this.price+"\n";
		 }

}

MainClass类:

package main;
import entity.Book;
import entity.Category;
import entity.BookDataSet;
public class MainClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Category c1 = new Category(1, "工具类", "软件编程");
		Category c2 = new Category(1, "文学类", "小说");
		System.out.println("商品的类别有:");
		System.out.println(c1.toString());
		System.out.println(c2.toString());
		System.out.println();
		
		int size = 5;	    
	    Book[] books=new Book[size];
	    books[0]=new Book("01","think in java","Bruce Eckel",50,"工具类",50.0f);
	    books[1]=new Book("02","JAVA核心技术","Cay S.Horstmann",40,"工具类",50.0f);
	    books[2]=new Book("03","射雕英雄传","金庸",30,"文学类",50.0f);
	    books[3]=new Book("04","平凡的世界","路遥",20,"文学类",50.0f);
	    books[4]=new Book("05","心理罪","雷米",60,"文学类",50.0f);
	    
		BookDataSet dbset = new BookDataSet(size,books);
		System.out.println("图书的具体信息是:");		
		System.out.println(dbset.toString());
	}
	

}

返回 Java程序设计