第1关:设计Book类
package step1.question;
/**
* 要求定义一个Book类,描述图书的信息。
每一本书都有以下信息:
- 书名(title),String 类型
- 作者(author), String 类型
- 出版社(press ),String 类型
- 书号(ISBN), String 类型
- 定价(price), double 类型
* @author DELL
*
*/
public class Book {
//留意这里的权限是private
private String title;
private String author;
private String press;
private String ISBN;
private double price;
public Book(String title, String author, String press, String ISBN, double price)
{
//***********Begin************
//请在此处完成构造函数的内容
this.title=title;
this.author=author;
this.press=press;
this.ISBN=ISBN;
this.price=price;
//************End*************
}
//获取各个字段的值
//【思考】为什么需要以下5个getter函数
public String getTitle() {
return title;//因为return只能返回一个值
}
public String getAuthor() {
return author;
}
//***********Begin************
//仿造上面两个函数,写出getPress()、getPrice()、getISBN()函数
public String getPress() {
return press;
}
public double getPrice() {
return price;
}
public String getISBN()
{
return ISBN;
}
//************End*************
public void printInfo() {
System.out.println(title + "\t" + author + "\t" + press + "\t"+ ISBN + "\t"+ price);
}
public static void main(String[] args) {
//下面是参考代码,请认真阅读
Book b1 = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
//输出书本信息的方法一:
System.out.println(b1.getTitle()+"\t"+b1.getAuthor()+"\t"
+b1.getPress() + "\t"+b1.getISBN()+"\t"+b1.getPrice());
//输出书本信息的方法二:
b1.printInfo();
System.out.println("-------------我是分割线---------------");
Book[] booklist = new Book[5];
booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社", "9787145513853", 45.6);
booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);
//***********Begin************
//仿造上面方法一,输出5本书的信息(记得使用循环)
for(int i=0;i<=4;i++)
{
System.out.println(booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"+booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
}
//************End*************
System.out.println("-------------我是分割线---------------");
//***********Begin************
//仿造上面方法二,输出5本书的信息(记得使用循环)
for(int i=0;i<=4;i++)
{
booklist[i].printInfo();
}
//************End*************
}
}
第2关:设计Library类
package step2.question;
import java.util.Scanner;
public class Library {
public static final int SIZE = 100; //图书库最大藏书量
Book[] booklist; //图书库
int count; //当前图书库的图书数量
Scanner scan;
public Library() {
booklist = new Book[SIZE];
//初始化图书库,默认里面有5本书,信息如下
booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社", "9787145513853", 45.6);
booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);
count = 5;
scan = new Scanner(System.in);
}
public void run(){
int userChoice = 0;
//循环处理,直到用户选择了“0...退出”
do{
displayMenu();
//读取用户输入
userChoice = scan.nextInt();
switch(userChoice){
case 0:
System.out.println("成功退出系统,欢迎再次使用!");
break;
case 1:
printAllBook();
break;
case 2:
addBook();
break;
case 3:
deleteBook();
break;
case 4:
modifyBook(); //本次任务不要求实现
break;
case 5:
findBook(); //本次任务不要求实现
break;
default:
System.out.println("输入非法,请重新输入!");
}
}while(userChoice != 0);
scan.close();
}
void displayMenu(){
//打印菜单
System.out.println("---------------------");
System.out.println(" 图书管理系统 ");
System.out.println("---------------------");
System.out.println("| 0...退出系统 |");
System.out.println("| 1...显示图书 |");
System.out.println("| 2...增加图书 |");
System.out.println("| 3...删除图书 |");
System.out.println("| 4...修改图书 |");
System.out.println("| 5...查询图书 |");
System.out.println("---------------------");
System.out.print("请输入选项:");
}
void printAllBook(){ //循环打印所有的图书
System.out.println("---------------------------------------------------------------------------");
System.out.println("序号\t书名\t\t作者\t出版社\t\tISBN\t\t单价");
System.out.println("---------------------------------------------------------------------------");
//***********Begin************
//输出booklist数组中count本图书的信息,记得前面需要有个序号(序号从1开始)
//每项信息请用"\t"隔开
for(int i=0;i<count;i++)
{
System.out.println(i+1+"\t"+booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"+booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
}
//************End*************
System.out.println("---------------------------------------------------------------------------");
}
void addBook(){ //增加图书
if (count < SIZE){
System.out.println("----------------------------------------------");
System.out.print("请输入图书名:");
String title = scan.next();
//***********Begin************
//仿造上两行代码,提示用户输入各项信息
//创建一个图书对象,加入到booklist数组中
System.out.print("请输入作者:");
String author=scan.next();
System.out.print("请输入出版社:");
String press=scan.next();
System.out.print("请输入ISBN:");
String ISBN=scan.next();
System.out.print("请输入单价:");
double price=scan.nextDouble();
booklist[count]=new Book(title,author,press,ISBN,price);
//************End*************);
count++;
System.out.println("成功增加1本图书!当前图书库信息如下:");
printAllBook();
}
else{
System.out.println("图书库已满!");
}
}
void deleteBook(){ //删除图书
int id = -1;
while(true){
printAllBook();
System.out.print("请输入序号删除图书,输入0返回主菜单: ");
int userChoice = scan.nextInt();
int a=userChoice;
if(userChoice == 0){ //输入0跳出循环,即返回主菜单
break;
}
else {
//***********Begin************
//删除用户选择的图书,并输出"删除成功!"
//若输入有误,请提示:"输入错误!请重新输入!"
if(userChoice>count)
{
System.out.print("输入错误!请重新输入!");
}
else
{
count--;
for(int i=0;a<=count;i++)
{
booklist[a-1]=booklist[a];
a++;
}
System.out.println("删除成功!");
}
//************End*************);
}
}
}
int findByOrder(int number){ //按序号(1~count)返回数组下标id
if(number > 0 && number <= count ){
return number - 1;
}
else
return -1;
}
void modifyBook() { //修改图书
System.out.println("功能建设中...");
}
void findBook(){ //查询图书
System.out.println("功能建设中...");
}
public static void main(String[] args) {
Library myLibrary = new Library();
//***********Begin************
//调用run()方法
myLibrary.run();
//************End*************);
}
}
第3关:完成“修改图书”功能
package step3.question;
import java.util.Scanner;
public class Library {
public static final int SIZE = 100; //图书库最大藏书量
Book[] booklist; //图书库
int count; //当前图书库的图书数量
Scanner scan;
public Library() {
booklist = new Book[SIZE];
//初始化图书库,默认里面有5本书,信息如下
booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社", "9787145513853", 45.6);
booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);
count = 5;
scan = new Scanner(System.in);
}
public void run(){
int userChoice = 0;
//循环处理,直到用户选择了“0...退出”
do{
displayMenu();
//读取用户输入
userChoice = scan.nextInt();
switch(userChoice){
case 0:
System.out.println("成功退出系统,欢迎再次使用!");
break;
case 1:
printAllBook();
break;
case 2:
addBook();
break;
case 3:
deleteBook();
break;
case 4:
modifyBook();
break;
case 5:
findBook();
break;
default:
System.out.println("输入非法,请重新输入!");
}
}while(userChoice != 0);
scan.close();
}
void displayMenu(){
//打印菜单
System.out.println("---------------------");
System.out.println(" 图书管理系统 ");
System.out.println("---------------------");
System.out.println("| 0...退出系统 |");
System.out.println("| 1...显示图书 |");
System.out.println("| 2...增加图书 |");
System.out.println("| 3...删除图书 |");
System.out.println("| 4...修改图书 |");
System.out.println("| 5...查询图书 |");
System.out.println("---------------------");
System.out.print("请输入选项:");
}
void addBook(){ //增加图书
if (count < SIZE){
System.out.println("----------------------------------------------");
System.out.print("请输入图书名:");
String title = scan.next();
System.out.print("请输入作者:");
String author = scan.next();
System.out.print("请输入出版社:");
String press = scan.next();
System.out.print("请输入ISBN:");
String ISBN = scan.next();
System.out.print("请输入单价:");
double price = scan.nextDouble();
Book book = new Book(title,author,press,ISBN,price);
booklist[count] = book;
count++;
System.out.println("成功增加1本图书!当前图书库信息如下:");
printAllBook();
}
else{
System.out.println("图书库已满!");
}
}
void deleteBook(){ //删除图书
int id = -1;
while(true){
printAllBook();
System.out.print("请输入序号删除图书,输入0返回主菜单: ");
int userChoice = scan.nextInt();
if(userChoice == 0){
break;
}
else {
id = findByOrder(userChoice);
if(id > -1) {
for(int j = id; j < count-1; j++){ //用for循环的形式实现对数组的删除
booklist[j] = booklist[j+1];
}
count --;
System.out.println("删除成功!");
}
else
System.out.println("输入错误!请重新输入!");
}
}
}
void modifyBook() { //修改图书
while(true) {
printAllBook();
System.out.print("请输入序号修改图书,输入0返回主菜单: ");
int userChoice = scan.nextInt();
int id = -1;
if(userChoice == 0){
break;
}
else {
id = findByOrder(userChoice);
//***********Begin************
//调用modifyBookById修改第id本图书
//若修改成功,输出"修改成功!当前图书信息如下:"并打印图书信息
//若输入有误,请提示:"输入错误!请重新输入!"
if(id>-1)
{
modifyBookById(id);
}
else
{
System.out.println("输入错误!请重新输入!");
}
//************End*************);
}
}
}
boolean modifyBookById(int id){ //修改第id本图书(id为数组下标,从0开始)
if(id > -1 && id < count) {
System.out.println("图书信息如下:");
booklist[id].printInfo();
//此处获得图书的原各项信息
String titleOld = booklist[id].getTitle();
String authorOld = booklist[id].getAuthor();
String pressOld = booklist[id].getPress();
String ISBNOld = booklist[id].getISBN();
double priceOld = booklist[id].getPrice();
scan.nextLine(); //释放前一个回车
System.out.println();
System.out.print("请输入新书名(直接回车保持不变):");
String title = scan.nextLine();
title = title.isEmpty() ? titleOld : title;
//***********Begin************
//仿造上面三行代码,提示用户输入其它信息
//变量名请用author,press,ISBN
System.out.print("请输入作者(直接回车保持不变):");
String author=scan.nextLine();
author=author.isEmpty() ? authorOld:author;
System.out.print("请输入出版社(直接回车保持不变):");
String press=scan.nextLine();
press=press.isEmpty()?pressOld:press;
System.out.print("请输入ISBN(直接回车保持不变):");
String ISBN=scan.nextLine();
ISBN=ISBN.isEmpty()?ISBNOld:ISBN;
//************End*************);
System.out.print("请输入单价(直接回车保持不变):");
String strPrice = scan.nextLine();
double price = strPrice.isEmpty()? priceOld : Double.valueOf(strPrice).doubleValue();
System.out.print("修改成功!当前图书信息如下:");
//***********Begin************
//调用Book类的setBook函数,把当前图书的信息重新设置为新信息
booklist[id].setBook(title,author,press,ISBN,price);
System.out.println();
booklist[id].printInfo();
System.out.println();
//************End*************);
return true;
}
else {
return false;
}
}
void printAllBook(){ //循环打印所有的图书
System.out.println("---------------------------------------------------------------------------");
System.out.println("序号\t书名\t\t作者\t出版社\t\tISBN\t\t单价");
System.out.println("---------------------------------------------------------------------------");
for (int i = 0; i < count; i++){
System.out.println((i+1)+"\t"+booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"
+booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
}
System.out.println("---------------------------------------------------------------------------");
}
int findByOrder(int number){ //按序号(1~count)返回数组下标id
if(number > 0 && number <= count ){
return number - 1;
}
else
return -1;
}
void findBook(){ //查询图书
System.out.println("功能建设中...");
}
public static void main(String[] args) {
Library myLibrary = new Library();
myLibrary.run();
}
}
第4关:完成“查询图书”功能
package step4.question;
import java.util.Scanner;
public class Library {
public static final int SIZE = 100; //图书库最大藏书量
Book[] booklist; //图书库
int count; //当前图书库的图书数量
Scanner scan;
public Library() {
booklist = new Book[SIZE];
//初始化图书库,默认里面有5本书,信息如下
booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社", "9787145513853", 45.6);
booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);
count = 5;
scan = new Scanner(System.in);
}
public void run(){
int userChoice = 0;
//循环处理,直到用户选择了“0...退出”
do{
displayMenu();
//读取用户输入
userChoice = scan.nextInt();
switch(userChoice){
case 0:
System.out.println("成功退出系统,欢迎再次使用!");
break;
case 1:
printAllBook();
break;
case 2:
addBook();
break;
case 3:
deleteBook();
break;
case 4:
modifyBook();
break;
case 5:
findBook();
break;
default:
System.out.println("输入非法,请重新输入!");
}
}while(userChoice != 0);
scan.close();
}
void displayMenu(){
//打印菜单
System.out.println("---------------------");
System.out.println(" 图书管理系统 ");
System.out.println("---------------------");
System.out.println("| 0...退出系统 |");
System.out.println("| 1...显示图书 |");
System.out.println("| 2...增加图书 |");
System.out.println("| 3...删除图书 |");
System.out.println("| 4...修改图书 |");
System.out.println("| 5...查询图书 |");
System.out.println("---------------------");
System.out.print("请输入选项:");
}
void printAllBook(){ //循环打印所有的图书
System.out.println("---------------------------------------------------------------------------");
System.out.println("序号\t书名\t\t作者\t出版社\t\tISBN\t\t单价");
System.out.println("---------------------------------------------------------------------------");
for (int i = 0; i < count; i++){
System.out.println((i+1)+"\t"+booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"
+booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
}
System.out.println("---------------------------------------------------------------------------");
}
void addBook(){ //增加图书
if (count < SIZE){
System.out.println("----------------------------------------------");
System.out.print("请输入图书名:");
String title = scan.next();
System.out.print("请输入作者:");
String author = scan.next();
System.out.print("请输入出版社:");
String press = scan.next();
System.out.print("请输入ISBN:");
String ISBN = scan.next();
System.out.print("请输入单价:");
double price = scan.nextDouble();
Book book = new Book(title,author,press,ISBN,price);
booklist[count] = book;
count++;
System.out.println("成功增加1本图书!当前图书库信息如下:");
printAllBook();
}
else{
System.out.println("图书库已满!");
}
}
void deleteBook(){ //删除图书
int id = -1;
while(true){
printAllBook();
System.out.print("请输入序号删除图书,输入0返回主菜单: ");
int userChoice = scan.nextInt();
if(userChoice == 0){
break;
}
else {
id = findByOrder(userChoice);
if(id > -1) {
for(int j = id; j < count-1; j++){ //用for循环的形式实现对数组的删除
booklist[j] = booklist[j+1];
}
count --;
System.out.println("删除成功!");
}
else
System.out.println("输入错误!请重新输入!");
}
}
}
void modifyBook() { //修改图书
while(true) {
printAllBook();
System.out.print("请输入序号修改图书,输入0返回主菜单: ");
int userChoice = scan.nextInt();
int id = -1;
if(userChoice == 0){
break;
}
else {
id = findByOrder(userChoice);
if(modifyBookById(id) == true) {
System.out.println("修改成功!当前图书信息如下:");
booklist[id].printInfo();;
}
else
System.out.println("输入错误!请重新输入!");
}
}
}
boolean modifyBookById(int id){ //修改第id本图书
if(id > -1 && id < count) {
System.out.println("图书信息如下:");
booklist[id].printInfo();
String titleOld = booklist[id].getTitle();
String authorOld = booklist[id].getAuthor();
String pressOld = booklist[id].getPress();
String ISBNOld = booklist[id].getISBN();
double priceOld = booklist[id].getPrice();
scan.nextLine(); //释放前一个回车
System.out.print("请输入新书名(直接回车保持不变):");
String title = scan.nextLine();
title = title.isEmpty() ? titleOld : title;
System.out.print("请输入作者(直接回车保持不变):");
String author = scan.nextLine();
author = author.isEmpty() ? authorOld : author;
System.out.print("请输入出版社(直接回车保持不变):");
String press = scan.nextLine();
press = press.isEmpty() ? pressOld : press;
System.out.print("请输入ISBN(直接回车保持不变):");
String ISBN = scan.nextLine();
ISBN = ISBN.isEmpty() ? ISBNOld : ISBN;
System.out.print("请输入单价(直接回车保持不变):");
String strPrice = scan.nextLine();
double price = strPrice.isEmpty()? priceOld : Double.valueOf(strPrice).doubleValue();
booklist[id].setBook(title,author,press,ISBN,price);
return true;
}
else {
return false;
}
}
void findBook(){ //查询图书
while(true){
System.out.println("----------------------------------------------");
System.out.println("请输入按哪种方法查找图书:0、返回主菜单 1、书名 2、作者名 ");
int userChoice = scan.nextInt();
if(userChoice == 0) {
break;
}
else if(userChoice >= 3) {
System.out.println("输入错误,请重新输入!");
}
else {
if(userChoice == 1) {
System.out.println("请输入您要查找的书名:");
String title = scan.next();
findByTitle(title);
}
else if(userChoice == 2) {
System.out.println("请输入您要查找的作者名:");
String author = scan.next();
findByAuthor(author);
}
}
}
}
int findByOrder(int number){ //按序号(1~count)返回数组下标id
if(number > 0 && number <= count ){
return number - 1;
}
else
return -1;
}
void findByTitle(String title){//按书名查找图书,返回id
//***********Begin************
//遍历booklist数组中进行查找,输出所有包含title的图书信息
//如果一本都没找到,请输出"查找失败!"
int flag=0;
for(int i=0;i<count;i++){
if(booklist[i].getTitle().contains(title))
{
System.out.println(booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"+booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
flag=1;
}
}
if(flag==0)
{
System.out.println("查找失败!");
}
//************End*************
}
void findByAuthor(String author) {//按作者名查找图书,返回id
//***********Begin************
//遍历booklist数组中进行查找,输出所有作者名是author的图书信息
//如果一本都没找到,请输出"查找失败!"
int flag=0;
for(int i=0;i<count;i++){
if(booklist[i].getAuthor().equals(author))
{
System.out.println(booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"+booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
flag=1;
}
}
if(flag==0)
{
System.out.println("查找失败!");
}
//************End*************
}
public static void main(String[] args) {
Library myLibrary = new Library();
myLibrary.run();
}
}