基于Java的简单图书管理系统的实现(增删改查)
package com.situ.lib;public class Book {//对象:书-----定义书的属性:private String name;private String isbn;private String author;private double price;//无参构造方法:public Book(){}//get,set方法:public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getIsbn() {return isbn;}public void setIsbn(String isbn) {this.isbn = isbn;}public String getName() {return name;}public void setName(String name) {this.name = name;}//功能方法:打印图书信息:public void print(){System.out.println("书名:" + this.name +"书号:" + this.isbn +"作者:" + this.author +"价格:" + this.price);}}
package com.situ.lib;import java.util.Scanner;public class Manager {//用来保存所有的图书信息:private Book[] books = new Book[10];private int index = 0;public static void main(String[] args) {Manager manager = new Manager();manager.menu();}public void menu() {//提供一个主菜单:menu:while (true) {System.out.println("--------欢迎使用图书管理系统--------");System.out.println("1.添加图书");System.out.println("2.删除图书");System.out.println("3.修改图书");System.out.println("4.查询图书");System.out.println("5.退出系统");System.out.println("请输入您的操作序号:");//用户进行输入选择对应的操作:Scanner sc = new Scanner(System.in);int option = sc.nextInt();switch (option) {case 1:add();break;case 2:delete();break;case 3:update();break;case 4:query();break;case 5://退出 break;单纯写一个break只会退出switch还会在while里面//break menu;System.out.println("退出系统,感谢您的使用!!!");return;//System.exit(0);default:System.out.println("您的输入有误,请重新输入");break;}}}/*这作为功能类,功能是:增,删,改,查---封装成不同的方法;可以定义成静态方法,也可以定义成非静态的方法,但是对于功能性方法,尽量不要定义成静态方法*///增public void add() {System.out.println("添加图书");//输入图书信息:Scanner sc = new Scanner(System.in);System.out.println("请输入图书编号:");String isbn = sc.next();System.out.println("请输入图书名称:");String name = sc.next();System.out.println("请输入作者:");String author = sc.next();System.out.println("请输入图书价格:");double price = sc.nextDouble();//创建对象:Book book = new Book();book.setIsbn(isbn);book.setName(name);book.setAuthor(author);book.setPrice(price);//将对象保存到数组中:if(index>=books.length){//数组已经放满了,扩容:Book[] newBooks = new Book[books.length*2];//将老数组中的元素拷贝到新数组里面:for(int i=0;i<books.length;i++){newBooks[i] = books[i];}books = newBooks;}books[index] = book;index++;}//删public void delete() {//输入要删除的书号:System.out.println("删除图书");Scanner sc = new Scanner(System.in);System.out.println("请输入要删除的书号");String isbn = sc.next();//如何删除?//先找到这本书的下标int index = -1;for (int i = 0; i < this.index; i++) {//字符串的比较相等要用.equals方法比较if (books[i].getIsbn().equals(isbn)) {index = i;break;}}if (index >= 0) {//删除:books[index] = null;//后面的书往前移动一个for (int i = index; i < this.index - 1; i++) {books[i] = books[i + 1];}this.index--;System.out.println("删除成功");} else {System.out.println("未找到指定图书!");}}//改public void update() {System.out.println("修改图书:");System.out.println("请输入要修改的图书的编号:");Scanner sc = new Scanner(System.in);String isbn = sc.next();//先找到这本书的下标int index = -1;for (int i = 0; i < this.index; i++) {//字符串的比较相等要用.equals方法比较if (books[i].getIsbn().equals(isbn)) {index = i;break;}}if (index >= 0) {//才能修改books[index];书名,作者,价格//书名:System.out.println("请输入新的书名:");books[index].setName(sc.next());//作者:System.out.println("请输入新的作者");books[index].setAuthor(sc.next());//价格:System.out.println("请输入新的价格:");books[index].setPrice(sc.nextDouble());System.out.println("修改成功");} else {//找不到System.out.println("未找到指定的图书!");}}//查public void query() {// //遍历打印:
// for(int i=0;i<index;i++){
// //System.out.println(books[i]);
// books[i].print();
// }while (true) {System.out.println("查询图书:");System.out.println("1.查询所有图书");System.out.println("2.根据书名查询");System.out.println("3.返回主菜单");System.out.println("请输入您的操作");Scanner sc = new Scanner(System.in);int option = sc.nextInt();switch (option) {case 1:queryAll();break;case 2:quertByName();break;case 3:return ;default:System.out.println("输入有误,请重新输入:");break;}}}private void quertByName() {System.out.println("请输入要查询的书名");Scanner sc = new Scanner(System.in);String name = sc.next();for(int i=00;i<index;i++){if(books[i].getName().equals(name)){books[i].print();}}}private void queryAll() {for(int i=0;i<index;i++){books[i].print();}}
}
补充:关于数组的扩充可以使用现成的Arrays实现(可以参考JDK API 1.6中文版)