资源描述
精品文档
Java实验报告
图书管理系统
1. 管理员界面
<<密码验证
<<展示所有图书
<<查找图书
<<添加书籍
<<删除书籍
<<修改密码
<<返回上一层
<<结束
2. 学生界面
<<身份识别
<<查找图书
<<借阅图书
<<归还书籍
<<返回上一层
<<结束
3. 退出程序
源代码:
Main.java
package pany;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
User U;
Library L=new Library("东北大学图书馆");
System.out.println("欢迎来到东北大学图书馆");
Book b1=new Book("java编程思想","sdhf","554532",new Date(2016,12,13),15);
Book b2=new Book("C++编程","nimei","15654",new Date(2017,5,13),5);
Students s1=new Undergraduate("黎明","20175821");
Students s2=new Master("狭隘","20164545");
b1.BookUser.add(s2);
b1.BookUser.add(s1);
b2.BookUser.add(s2);
b2.BookUser.add(s1);
L.addBook(b1);
L.addBook(b2);
//在以上的代码中都是为了增加图书馆中的藏书
Scanner sc=new Scanner(System.in);
String order;
//命令操作板
while(true){
System.out.println("命令操作符\n<<AccessADInterface\n<<AccessSTInterface\n<<over");
System.out.print("<<");
order=sc.next();
if(order.equals("AccessADInterface")){
Administrators A=new Administrators();
if (A.getPassWord() == 1){
while(true) {
System.out.println("命令操作符\n<<ShowAllBook\n<<SearchBook\n<<AddBook\n<<DeleteBook\n<<ChangePassWord\n<<GoBack\n<<over");
System.out.print("<<");
order = sc.next();
if (order.equals("ShowAllBook")) {
L.ShowBook();
} else if (order .equals( "SearchBook")) {
L.ShowBook(L.SearchBook());
} else if (order.equals( "AddBook") ){
L.addBook();
} else if (order .equals( "DeleteBook")) {
L.DeleteBook();
} else if(order.equals("ChangePassWord")){
A.changePassWord();
} else if (order .equals( "GoBack")) {
break;
} else if (order .equals( "over")) {
System.out.println("已退出图书管理系统");
System.exit(0);
}
}
}
else{
System.exit(0);
}
} else if(order.equals("AccessSTInterface")){
U=L.UserComing();
while (true){
System.out.println("<<SearchBook\n<<BorrowBook\n<<ReturnBook\n<<GoBack\n<<over");
System.out.print("<<");
order=sc.next();
if(order.equals("SearchBook")) {
L.ShowBook(L.SearchBook());
}
else if(order.equals("BorrowBook")){
L.SearchBook().BookBorrow(U);
}
else if(order.equals("ReturnBook")){
L.ReturnBook(U);
}
else if(order.equals("GoBack")){
break;
}
}
}
else if(order.equals("over")){
System.out.println("已退出图书管理系统");
System.exit(0);
}
else {
System.out.println("输入了错误的指令");
continue;
}
}
}
}
Library.java
package pany;
import java.util.Scanner;
import java.util.Vector;
public class Library {
public static final int OK=1;
public static final int ERROR=1;
Vector<Object> book= new Vector();//用来存储书类 丹书类中也存有用户类
private String Libraryname;
Scanner sc=new Scanner(System.in);
Library (String name){
this.Libraryname=name;
}
public User UserComing() {
int kind;
User U;
Scanner sc = new Scanner(System.in);
System.out.println("如果你是老师请输入1,若是学生请输入2");
while (true) {
while(true){
kind = sc.nextInt();
if (kind == 1) {
U = new Teacher();
return U;
} else if (kind == 2) {
System.out.println("如果你是本科生请输入1,若是硕士研究生请输入2");
while (true){
kind=sc.nextInt();
if(kind==1){
U = new Undergraduate();
return U;
}
else if(kind==2){
U=new Master();
return U;
}else{
System.out.println("输入错误");
break;
}
}
} else {
System.out.println("输入错误");
System.out.println("请重新输入:");
break;
}
}
}
}
public void addBook(Book b){
book.add(b);
}
public void addBook(){
int i;
i=1;
Book b;
while(i==1){
b=new Book();
this.addBook(b);
System.out.println("如果要继续输入书本信息就输入1,否则就输入2");
i=sc.nextInt();
}
}
public Book SearchBook(){
String BookName;
Book b;
System.out.println("请输入你要借查询的书");
BookName=sc.next();
for(int i=0;i<this.book.size();i++){
b=(Book)this.book.elementAt(i);
if(b.bookName.equals(BookName)){
return b;
}
}
return null;
}
public void ReturnBook(User U){
User u;
Book b;
b=this.SearchBook();
for(int i=0;i<b.BookUser.size();i++){
u=(User) b.BookUser.elementAt(i);
if(u==U){
U.numBookBor--;
b.BookUser.remove(i);
b.surplus++;
System.out.println("还书成功");
return;
}
else {
System.out.println("您不是这本书的借阅者,不能还书");
return;
}
}
System.out.println("找不到你要的书");
}
public void DeleteBook(){
String BookName;
Book b;
System.out.println("请输入你要删除的书");
BookName=sc.next();
for(int i=0;i<this.book.size();i++){
b=(Book)this.book.elementAt(i);
if(b.bookName.equals(BookName)){
if(b.BookUser.size()==0){
this.book.remove(i);
System.out.println("已经成功的删除书籍");
}
else {
System.out.println("还有人尚未还书,不能够删除书籍");
}
}
}
}
public void ShowBook(){
Book b;
for(int i=0;i<this.book.size();i++){
b=(Book)book.elementAt(i);
System.out.println((i+1)+"\t"+b.bookName+"\t"+b.auther+"\t"+b.bookNum+"\t"+b.depositTime.getYear()+"\t"+b.depositTime.getMonth()+"\t"+b.depositTime.getDay()+"\t"+b.surplus);
}
}
public void ShowBook(Book b){
System.out.println("\t"+b.bookName+"\t"+b.auther+"\t"+b.bookNum+"\t"+b.depositTime.getYear()+"\t"+b.depositTime.getMonth()+"\t"+b.depositTime.getDay()+"\t"+b.surplus);
}
}
Book.java
package pany;
import java.util.Scanner;
import java.util.Vector;
public class Library {
public static final int OK=1;
public static final int ERROR=1;
Vector<Object> book= new Vector();//用来存储书类 丹书类中也存有用户类
private String Libraryname;
Scanner sc=new Scanner(System.in);
Library (String name){
this.Libraryname=name;
}
public User UserComing() {
int kind;
User U;
Scanner sc = new Scanner(System.in);
System.out.println("如果你是老师请输入1,若是学生请输入2");
while (true) {
while(true){
kind = sc.nextInt();
if (kind == 1) {
U = new Teacher();
return U;
} else if (kind == 2) {
System.out.println("如果你是本科生请输入1,若是硕士研究生请输入2");
while (true){
kind=sc.nextInt();
if(kind==1){
U = new Undergraduate();
return U;
}
else if(kind==2){
U=new Master();
return U;
}else{
System.out.println("输入错误");
break;
}
}
} else {
System.out.println("输入错误");
System.out.println("请重新输入:");
break;
}
}
}
}
public void addBook(Book b){
book.add(b);
}
public void addBook(){
int i;
i=1;
Book b;
while(i==1){
b=new Book();
this.addBook(b);
System.out.println("如果要继续输入书本信息就输入1,否则就输入2");
i=sc.nextInt();
}
}
public Book SearchBook(){
String BookName;
Book b;
System.out.println("请输入你要借查询的书");
BookName=sc.next();
for(int i=0;i<this.book.size();i++){
b=(Book)this.book.elementAt(i);
if(b.bookName.equals(BookName)){
return b;
}
}
return null;
}
public void ReturnBook(User U){
User u;
Book b;
b=this.SearchBook();
for(int i=0;i<b.BookUser.size();i++){
u=(User) b.BookUser.elementAt(i);
if(u==U){
U.numBookBor--;
b.BookUser.remove(i);
b.surplus++;
System.out.println("还书成功");
return;
}
else {
System.out.println("您不是这本书的借阅者,不能还书");
return;
}
}
System.out.println("找不到你要的书");
}
public void DeleteBook(){
String BookName;
Book b;
System.out.println("请输入你要删除的书");
BookName=sc.next();
for(int i=0;i<this.book.size();i++){
b=(Book)this.book.elementAt(i);
if(b.bookName.equals(BookName)){
if(b.BookUser.size()==0){
this.book.remove(i);
System.out.println("已经成功的删除书籍");
}
else {
System.out.println("还有人尚未还书,不能够删除书籍");
}
}
}
}
public void ShowBook(){
Book b;
for(int i=0;i<this.book.size();i++){
b=(Book)book.elementAt(i);
System.out.println((i+1)+"\t"+b.bookName+"\t"+b.auther+"\t"+b.bookNum+"\t"+b.depositTime.getYear()+"\t"+b.depositTime.getMonth()+"\t"+b.depositTime.getDay()+"\t"+b.surplus);
}
}
public void ShowBook(Book b){
System.out.println("\t"+b.bookName+"\t"+b.auther+"\t"+b.bookNum+"\t"+b.depositTime.getYear()+"\t"+b.depositTime.getMonth()+"\t"+b.depositTime.getDay()+"\t"+b.surplus);
}
}
User.java
package pany;
import java.util.Date;
import java.util.Scanner;
class User{
public String name;
public int numBookBor;//借书的数量
//public Date borrowTime;
public String Id;
public Scanner sc=new Scanner(System.in);
User(){}
}
class Students extends User{
}
//想想其实也不过如此
class Undergraduate extends Students{
Undergraduate(String sName,String id){
this.name=sName;
this.Id=id;
}
Undergraduate(){
System.out.println("请输入你的姓名:");
this.name=sc.next();
System.out.println("请输入你的学号:");
this.Id=sc.next();
this.numBookBor=0;
}
}
class Master extends Students{
Master(String sName,String id){
this.name=sName;
this.Id=id;
}
Master(){
System.out.println("请输入你的姓名:");
this.name=sc.next();
System.out.println("请输入你的学号:");
this.Id=sc.next();
this.numBookBor=0;
}
}
class Teacher extends User{
Teacher(String tName,String id){
this.name=tName;
this.Id=id;
}
Teacher(){
System.out.println("请输入你的姓名:");
this.name=sc.next();
System.out.println("请输入你的工号:");
this.Id=sc.next();
this.numBookBor=0;
}
}
Administrators/java
package pany;
import java.util.Scanner;
public class Administrators {
; private String passWod;
Scanner sc=new Scanner(System.in);
Administrators(){
this.passWod="neu123";
}
public int getPassWord(){
String passWord;
for(int i=0;i<3;i++){
System.out.println("请输入密码:");
System.out.print("<<");
passWord=sc.next();
if( passWord.equals(this.passWod)){
return 1;
}
else {
System.out.println("密码错误,您还有"+(2-i)+"机会");
}
}
return 0;
}
public void changePassWord(){
String passWord;
for(int i=0;i<3;i++){
System.out.println("请输入原密码:\n<<");
passWord=sc.next();
if( passWord.equals(this.passWod)){
System.out.println("请输入新密码:\n<<");
this.passWod=sc.next();
break;
}
else {
System.out.println("密码错误,您还有"+(2-i)+"机会");
}
}
}
}
可修改
展开阅读全文