BOOKMain(主类):
package 图书管理系统;
import java.util.Scanner;
import java.io.*;
public class BOOKMain {
public static void main(String[] args) throws IOException{
// TODO 自动生成的方法存根
//@SuppressWarnings("resource")---可要可不要
Scanner sc = new Scanner(System.in);
User user = new User();
Book book = new Book();
user.login();//登录
while(true) {
System.out.println("请输入数字进行操作:");
System.out.println("1.查看全部书籍 \t 2.添加书籍");
System.out.println("3.删除书籍 \t 4.修改书籍");
System.out.println("5.退出图书管理系统");
int i = sc.nextInt();
switch(i) {
case 1:
book.list();//书籍列表
break;
case 2:
book.add();//添加书籍
break;
case 3:
book.list();
System.out.println("------------------");
book.del();//删除书籍
break;
case 4:
book.list();
System.out.println("------------------");
book.update();//更改书籍
break;
case 5:
System.out.println("已退出图书管理系统");
break;
default:
System.out.println("您输入的内容无效");
break;
}
}
}
}
Book类(图书类):
package 图书管理系统;
import java.io.*;
import java.util.Scanner;
public class Book {
private int id;
private String name;
private String aname;
private double price;
private int number;
Scanner in = new Scanner(System.in);
final static int SIZE = 100;
Book[] books = new Book[SIZE];
static int count = 0;//当前图书数量
public Book(int id,String aname,String name,double price,int number) {
super();
this.id = id;
this.aname = aname;
this.name = name;
this.price = price;
this.number = number;
}
public Book() {
// TODO 自动生成的构造函数存根
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
//查看图书
public void list() throws IOException{
books = Service.readData();
if(count == 0) {
System.out.println("图书管理系统还没有图书!!!");
} else {
System.out.println("图书编号 " + "作者 " + "书名 "+"价格 " + "数量");
for(int k = 0;k
System.out.print(books[k].getName() + " ");
System.out.print(books[k].getAname() + " ");
System.out.print(books[k].getPrice() + " ");
System.out.print(books[k].getNumber() + " ");
System.out.println();
}
}
}
//添加图书
public void add() throws IOException{
System.out.println("----------------------------");
if(count
System.out.println("请输入图书名称:");
String name = in.next();
System.out.println("请输入图书作者:");
String aname = in.next();
System.out.println("请输入图书价格:");
Double price = in.nextDouble();
System.out.println("请输入图书数量:");
int number = in.nextInt();
Book book = new Book(id,name,aname,price,number);
books[count]= book;
Service.saveBook(books[count]);
count++;
System.out.println("添加成功!!!");
}else {
System.out.println("添加失败!!!书库已满!!!");
}
}
//删除图书
public void del() throws IOException{
System.out.println("----------------------------");
System.out.println("请输入您要删除图书的编号:");
int id = in.nextInt();
for(int i=id;i
books[i] = books[i+1];
}
books = delUtil(id,books);
count--;
System.out.println("删除成功!!!");
Service scrvice = new Service();
scrvice.saveBook(books);
}
public Book[] delUtil(int index,Book[] array) {
Book[] arr = new Book[array.length-1];
for(int i = 0;i
}else {
arr[i] = array[i+1];
}
}
return arr;
}
//修改图书
public void update() throws IOException{
System.out.println("----------------------------");
System.out.println("请输入您要删除图书的编号:");
int id = in.nextInt();
System.out.println("请输入图书名称:");
String name = in.next();
System.out.println("请输入图书作者:");
String aname = in.next();
System.out.println("请输入图书价格:");
Double price = in.nextDouble();
System.out.println("请输入图书数量:");
int number = in.nextInt();
Book book = new Book(id,name,aname,price,number);
books[id] = book;
Service.saveBook(books);
System.out.println("修改成功!!!");
}
}
User类(主要是用于登录):
package 图书管理系统;
import java.util.Scanner;
public class User {
private String userName ;
// 密码
private String password ;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void login(){
Scanner sc = new Scanner(System.in);
int num = 0;
while(num<3){
User user = new User();
System.out.println("请输入用户名:");
// 从控制台上输入用户名
user.setUserName(sc.nextLine());
System.out.println("请输入密码:");
// 从控制台上输入密码
user.setPassword(sc.nextLine());
if(user.getUserName().equals("admin") && user.getPassword().equals("123123")){
System.out.println("登陆成功!欢迎【" + user.getUserName() + "】使用文波管理系统");
break;
}else{
System.out.println("登录失败!请重新登录:");
num += 1;
}
}
}
}
Service类(实现IO流):
package 图书管理系统;
import java.io.*;
public class Service {
private static final int SIZE = 100;
// 此方法用于从文本文件中读出数据,封装成book对象,存到books中
public static Book[] readData() throws IOException {
// 创建一个Book类型的数组,用于存放读取出来的数据封装成的Book对象
Book[] readBooks = new Book[SIZE];
// 创建一个字符串用于存放读取出来的数据
String data = "";
// 创建一个字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("F:\\bookData.txt"));
// 用于控制存放数组的下标
int id = 0;
// 每次度一行,如果有内容继续读取
while ((data = br.readLine()) != null) {
String[] dataArr = data.split(":");
// 创建Book对象
Book book = new Book();
// 第一个字符串用于设置id
//System.out.println(Integer.parseInt(dataArr[0].trim()));
book.setId(id);
// 第二个字符串用于设置name
book.setName(dataArr[1]);
// 第三个字符串用于设置author
book.setAname(dataArr[2]);
// 第四个字符串用于设置价格
book.setPrice(Double.parseDouble(dataArr[3]));
// 第五个字符串用于设置存量
book.setNumber(Integer.parseInt(dataArr[4]));
// 给数组中的元素赋值
readBooks[id] = book;
id++;
}
Book.count = id;
br.close();
// 返回读取到的数据
return readBooks;
}
public static void saveBook(Book book) throws IOException {
// 创建字符输入缓冲流对象
// BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\bookData.txt",true));
// FileWriter fw = new FileWriter("c:\\bookData.txt",true);
FileOutputStream fos = new FileOutputStream("F:\\bookData.txt",true);
// 将book中的数据拼接成字符串
StringBuffer data = new StringBuffer();
// 获取书籍的id值
data.append(book.getId() + ":");
// 获取书名
data.append(book.getName() + ":");
// 获取作者名
data.append(book.getAname() + ":");
// 获取价格
data.append(book.getPrice() + ":");
// 获取库存数
data.append(book.getNumber() + "\n");
// 创建数据字符串
String dataStr = data.toString();
byte[] buf = dataStr.getBytes();
// 控制写入数据的长度
int len = buf.length;
fos.write(buf, 0, len);
fos.close();
System.out.println("写入成功!");
}
public static void saveBook(Book[] books) throws IOException{
// 创建字符输入缓冲流对象
// BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\bookData.txt",true));
// FileWriter fw = new FileWriter("c:\\bookData.txt",true);
FileOutputStream fos = new FileOutputStream("F:\\bookData.txt");
// 存放数据
StringBuffer data = new StringBuffer();
//String data = "";
for(int i = 0;i < Book.count;i++){
data.append(books[i].getId() + ":");
// 获取书名
data.append(books[i].getName() + ":");
// 获取作者名
data.append(books[i].getAname() + ":");
// 获取价格
data.append(books[i].getPrice() + ":");
// 获取库存数
data.append(books[i].getNumber() + "\n");
//}
}
// 创建数据字符串
String dataStr = data.toString();
byte[] buf = dataStr.getBytes();
// 控制写入数据的长度
int len = buf.length;
fos.write(buf, 0, len);
fos.close();
}
}