Java实战-基于I/O流设计的图书馆管理系统项目总结
Posted 没谱的曲
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实战-基于I/O流设计的图书馆管理系统项目总结相关的知识,希望对你有一定的参考价值。
前言
当前,我还没有学到数据库相关的知识,想要完全通过Java来完成一个可以存储数据的图书馆管理系统就只能使用I/O流的知识,将内容都保存到文件中,再进行增删改查的操作,这就很考验我们的Java基础知识掌握能力。
项目介绍
该项目重点考察了:
- Java基本语法的掌握
- 流程控制语句的熟练使用
- 面向对象思想的理解
- 能否熟练使用封装、继承、多态
- 接口、异常的熟悉
- 集合的使用
- 是否熟悉掌握I/O流相关操作
项目说明
- 传统的人工管理图书馆流程繁琐,我们需求设计一个图书馆管理系统来方便学生借书以及管理员管理书籍
- 系统功能分为:读者信息管理模块、图书信息管理模块、图书借阅管理模块、基础信息维护模块和用户管理模块。
- 读者信息管理:能对读者基本信息管理,如:新增读者、信息修改、查询
- 图书信息管理:能管理图书基本信息,如新增图书、删除图书、查询图书等
- 图书借阅信息管理:能对借阅信息进行记录,包括读者信息、图书信息、借阅时间等。
- 图书归还信息管理:能对图书归还信息进行判断,是否超出借阅时间,罚金为多少
项目的实现
代码部分如下:
Person类:
package User;
public class Person
public String name;//姓名
public String sex;//性别
public int age;//年龄
public Person()
public Person(String name, String sex, int age)
this.name = name;
this.sex = sex;
this.age = age;
public void setName(String name)
this.name = name;
public String getName()
return name;
public void setSex(String sex)
this.sex = sex;
public String getSex()
return sex;
public void setAge(int age)
this.age = age;
public int getAge()
return age;
Book类:
package Book;
public class Book
private String bookName; //书名
private String author; //作者
public double price; //价格
private String category; //分类
public String press;//出版社
private String state; //状态 true-已借出 false-未借出
private String borrowName;
private String borrowTime;
public Book()
public Book(String bookName, String author, double price, String category, String press, String state, String borrowName, String borrowTime)
this.bookName = bookName;
this.author = author;
this.price = price;
this.category = category;
this.press = press;
this.state = state;
this.borrowName = borrowName;
this.borrowTime = borrowTime;
public String getName()
return bookName;
public void setName(String bookName)
this.bookName = bookName;
public String getAuthor()
return author;
public void setAuthor(String author)
this.author = author;
public double getPrice()
return price;
public void setPrice(double price)
this.price = price;
public String getCategory()
return category;
public void setCategory(String category)
this.category = category;
public String getPress()
return press;
public void setPress(String press)
this.press = press;
public String getState()
return state;
public void setState(String state)
this.state = state;
public String getBorrowName()
return borrowName;
public void setBorrowName(String borrowName)
this.borrowName = borrowName;
public String getBorrowTime()
return borrowTime;
public void setBorrowTime(String borrowTime)
this.borrowTime = borrowTime;
@Override
public String toString()
return bookName + "," + author + "," + price + "," + category + "," + press + "," + state + "," + borrowName + "," + borrowTime;
Library类:
package Library;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import User.User;
import User.Admin;
public class Library
Scanner sc = new Scanner(System.in);
private String operator;
//用户注册登录
public void Interface() throws IOException, ParseException
boolean a = true;
File file = new File("用户信息.txt");
try
while (a)
System.out.println("==================图书管理系统==================");
System.out.println("请登录:1.普通用户 2.管理员登录 3.注册 4.退出系统");
int i = sc.nextInt();
if (i < 1 || i > 4)
System.out.println("到底能不能行?能不能看明白了再输!");
else if (i == 4)
a = false;
else if (i == 1)
//普通用户登录
System.out.println("请输入您的用户名:");
operator = sc.next();
System.out.println("请输入您的密码:");
String password = sc.next();
FileInputStream intput = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null)
list.add(tempString);
//判断输入的用户名是否存在
boolean flag = false;
String[] userinf = new String[5];
for (String user : list)
userinf = user.split(",");
if (operator.equals(userinf[0])) //判断用户名是否存在
flag = true;
break;
//如果存在,判断密码是否正确
if (flag == true)
if (password.equals(userinf[1]))
System.out.println("登陆成功");
//写日志
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String returnTime = sdf.format(nowDate);
File file2 = new File("日志.txt");
FileWriter fw1 = new FileWriter(file2, true);
fw1.write(returnTime + " " + operator + "登录了");
fw1.write(System.getProperty("line.separator"));//在段落后添加一个换行符
fw1.close();
//登录
try
User user = new User();
user.operation(userinf, operator);
catch (Exception e)
System.out.println("到底能不能行?能不能看明白了再输!");
User user = new User();
user.operation(userinf, operator);
else
System.out.println("密码错误");
else
System.out.println("用户名不存在");
intput.close();
reader.close();
else if (i == 2)
//管理员登录
System.out.println("请输入管理员密码!");
String rootPassword = sc.next();
if (rootPassword.equals("88888888"))
//管理员登录成功
System.out.println("管理员登陆成功!");
try
Admin admin = new Admin();
admin.admin();
catch (Exception e)
System.out.println("到底能不能行?能不能看明白了再输!");
Admin admin = new Admin();
admin.admin();
else
System.out.println("你是不是管理员心里没个ACD数吗?");
else if (i == 3)
//注册
System.out.println("请输入用户名:");
String name = sc.next();
//查找是否有同名用户
FileInputStream intput = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null)
list.add(tempString);
String[] userinf = new String[5];
boolean flag = false;
for (String user : list)
userinf = user.split(",");
if (name.equals(userinf[0])) //判断用户名是否存在
flag = true;
System.out.println("该用户名已存在,请重新拟取名称");
break;
if (flag == false)
System.out.println("请输入密码:");
String password = sc.next();
String sex;
while (true)
System.out.println("请输入您的性别:");
sex = sc.next();
if (sex.equals("男") || sex.equals("女"))
break;
else
System.out.println("请输入正确的性别!");
System.out.println("请输入您的年龄:");
int age = sc.nextInt();
if (age < 10)
System.out.println("您的年龄太小啦!请让家长使用账号帮您借阅书籍!");
break;
long phone;
while (true)
System.out.println("请输入您的电话号码:");
phone = sc.nextInt();
if (phone < 0)
System.out.println("请输入正确的电话号码!");
else
break;
User u = new User(name, sex, age, phone, password);
file.createNewFile();
FileWriter fw = new FileWriter(file, true);
fw.write(u.toString());
fw.write(System.getProperty("line.separator"));//在段落后添加一个换行符
fw.close();
System.out.println("恭喜您,注册成功!");
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String returnTime = sdf.format(nowDate);
File file1 = new File("日志.txt");
FileWriter fileWriter = new FileWriter(file1, true);
fileWriter.write(returnTime + " 注册了新用户:" + name);
fileWriter.write(System.getProperty("line.separator"));//在段落后添加一个换行符
fileWriter.close();
catch (Exception e)
System.out.println("到底能不能行?能不能看明白了再输!");
Library library = new Library();
library.Interface();
return;
添加书籍AddBook:
package Book;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AddBook
public void addBook() throws IOException
Scanner sc = new Scanner(System.in);
File file = new File("书籍.txt");
System.out.println("请输入书名:");
String bookName = sc.next();
//查找是否有同名书籍
FileInputStream intput = new FileInputStream(file图书馆管理系统重构(数据库版)