@[toc]
前言
之前用Python写了三个版本的图书管理系统,但都没有用到类和对象的思想,全是定义的函数。Java我学了一个月了,刚接触到类和对象的封装性(学校事太多,进度有点慢),那么我就用我脑子里为数不多的知识写出自己的第一个Java项目 —— 图书管理系统之Java版。
先看整个项目结构
有三个类:Book类定义了书的结构,BookArray类用于存储book信息,BookView类用于用户与程序交互及操作可视化。
其中,最重要的是BookArray类,要把这里面的功能好好写写。
话不多说,上代码!!!
Book.java
package 封装继承多态.封装性练习基本使用.图书管理系统;
public class Book {
private String number; // 编号(从1开始依次递增)
private String title; // 书名
private String author; // 作者
private String type; // 种类
private double price; // 价格
// 构造器
public Book() {
}
public Book(String number, String title, String author, String type, double price) {
this.number = number;
this.title = title;
this.author = author;
this.type = type;
this.price = price;
}
// 方法
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
BookArray.java
package 封装继承多态.封装性练习基本使用.图书管理系统;
public class BookArray {
private Book[] books; // 存放书信息的数组
private int total; // 记录书的数量
// 构造器
public BookArray(int length) { // length指定数组长度
books = new Book[length];
}
// 方法
// 增
public boolean addBook(Book book){
if(total < books.length && total >= 0){
books[total] = book;
total += 1;
return true;
}else {
return false;
}
}
// 删
public boolean deleteBook(int index) {
if(index > 0 && index <= total){
for (int i = index-1 ; i < total-1; i++) {
books[index - 1] = books[index];
}
books[total-1] = null;
total -= 1;
return true;
}else {
return false;
}
}
// 改
public boolean modifyBook(int index,Book book) {
if(index > 0 && index <= total){
books[index - 1] = book;
return true;
}else {
return false;
}
}
// 查
public Book getBook(int index) {
if(index > 0 && index <= total){
return books[index - 1];
}else {
return null;
}
}
// 展示所有
public Book[] getAllBooks() {
Book[] books1 = new Book[total];
for (int i = 0; i < total; i++) {
books1[i] = books[i];
}
return books1;
}
// 存了多少本书
public int getTotal(){
return total;
}
}
BookView.java
package 封装继承多态.封装性练习基本使用.图书管理系统;
import java.util.Scanner;
public class BookView {
private BookArray bookList = new BookArray(20); // 20长度的数组
public void Menu(){
System.out.println("------------------------功能菜单------------------------");
System.out.println("\t\t\t\t1.新增图书信息");
System.out.println("\t\t\t\t2.删除图书信息");
System.out.println("\t\t\t\t3.修改图书信息");
System.out.println("\t\t\t\t4.查询图书信息");
System.out.println("\t\t\t\t5.展示所有图书信息");
System.out.println("\t\t\t\t0.退出");
System.out.println("------------------------------------------------------");
}
public void add(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入编号:");
String number = scanner.next();
System.out.println("请输入书名:");
String title = scanner.next();
System.out.println("请输入作者:");
String author = scanner.next();
System.out.println("请输入种类:");
String type = scanner.next();
System.out.println("请输入价格:");
double price = scanner.nextDouble();
Book book = new Book(number,title,author,type,price);
bookList.addBook(book);
}
public void delete(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入想要删除图书信息的位置:");
int index = scanner.nextInt();
bookList.deleteBook(index);
}
public void modify(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入想要修改的图书信息的位置:"); // 第一个位置输入 1
int index = scanner.nextInt();
System.out.println("编号" + "(" + bookList.getBook(index).getNumber() + ")");
System.out.println("修改为:");
String number = scanner.next();
System.out.println("书名" + "(" + bookList.getBook(index).getTitle() + ")");
System.out.println("修改为:");
String title = scanner.next();
System.out.println("作者" + "(" + bookList.getBook(index).getAuthor() + ")");
System.out.println("修改为:");
String author = scanner.next();
System.out.println("种类" + "(" + bookList.getBook(index).getType() + ")");
System.out.println("修改为:");
String type = scanner.next();
System.out.println("价格" + "(" + bookList.getBook(index).getPrice() + ")");
System.out.println("修改为:");
double price = scanner.nextDouble();
Book book = new Book(number,title,author,type,price);
bookList.modifyBook(index,book);
}
public void search(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入想要查找的图书信息的位置:"); // 第一个位置输入 1
int index = scanner.nextInt();
Book result = bookList.getBook(index);
System.out.println(" 编号\t\t" + "书名\t\t\t\t" + "作者\t\t\t" + "种类\t\t\t" + "价格" );
System.out.println(" " + result.getNumber() + "\t\t\t" + result.getTitle() + "\t\t" + result.getAuthor() + "\t\t" + result.getType() + "\t\t" + result.getPrice());
}
public void show(){
Book[] result = bookList.getAllBooks();
int total = bookList.getTotal();
System.out.println("--------------------------客户列表--------------------------");
if (total == 0){
System.out.println("没有图书信息!");
}else{
System.out.println(" 编号\t\t" + "书名\t\t\t\t" + "作者\t\t\t" + "种类\t\t\t" + "价格" );
for (Book book : result) {
System.out.println(" " + book.getNumber() + "\t\t\t" + book.getTitle() + "\t\t" + book.getAuthor() + "\t\t" + book.getType() + "\t\t" + book.getPrice());
}
}
System.out.println("\n");
}
public static void main(String[] args) {
BookView bookView = new BookView();
boolean judge = true;
while(judge){
bookView.Menu();
Scanner scanner = new Scanner(System.in);
System.out.println("请选择功能:");
int select = scanner.nextInt();
switch (select){
case 1:
bookView.add();
System.out.println("新增信息完成!");
break;
case 2:
bookView.delete();
System.out.println("删除信息完成");
break;
case 3:
bookView.modify();
System.out.println("修改信息完成");
break;
case 4:
bookView.search();
break;
case 5:
bookView.show();
break;
case 0:
judge = false;
System.out.println("谢谢使用!");
break;
}
}
}
}
效果展示
先新增两个信息
再展示所有图书,看是否新增成功
接着试试查找功能
然后试验修改功能
检验修改功能是否成功
最后尝试删除功能
删除成功
退出
项目总结
最后再来说说这个项目吧,它是一个阶段性学习练手的小项目,并不是什么厉害的项目。同时也是我的第一个Java小项目,所以写一篇博客记录一下。接下来还有很多要学的,任重而道远啊!