java 图书馆管理系统(超低配版)

目录

图书管理系统要求

思路

show()方法

add()方法

search方法

delete()方法

代码

接口

Book类

 Reader类

User类

Informatica类

BookList类

ReaderList类

UserList类

IFList类

main

收获

调试时出现的错误


 

图书管理系统要求

实现图书管理系统的用户交互功能,包括用户菜单的显示与用户选择菜单后对数据的新增、删除、查询、列表等功能(分别实现对图书信息、读者信息、借阅信息和用户信息的操作)。具体实现的功能菜单如下:

0:系统退出

1:图书信息列表显示 

2:图书信息新增

3:图书信息删除

4:图书信息查找(按书名查找)

5:读者信息列表显示

6:读者信息新增

7:读者信息删除

8:读者信息查找(按姓名查找)

9:借阅信息列表显示

10:借阅信息新增

11:借阅信息删除

12:借阅信息查找(按书名查找)

13:用户信息列表显示

14:用户信息新增

15:用户信息删除

16:用户信息查找(按用户名查找)

17:要求使用io流操作文件进行信息的删改

思路

创建四个文件:读者信息.txt,借阅信息.txt,图书信息.txt,用户信息.txt   。当做数据库。用io输入输出。

对于每个信息库的操作都是四个:show-显示,add-增添,delete-删除,search-查找。所以可以用接口把他们串联起来。

show()方法

将文件里的所有信息都读出来。

public void show() {
    //创建文件读取对象和字符流对象
    File file=new File("文件");
    FileReader f=null;		
    BufferedReader f1=null;	
	f=new FileReader(file);
	f1=new BufferedReader(f);
	String str=null;
    //按读写,输出。
	while((str=f1.readLine())!=null) 
	    System.out.println(str);
	//关闭			
	f1.close();
	f.close();
}			
				

add()方法

输入要添加到文本文件中的字符串。

将字符串添加至文件的末尾。

public void add() {
    Scanner in = new Scanner(System.in);
    //输入想要添加的信息
	String s1=in.nextLine();
    //打开
    File file=new File("文件");
	FileWriter f=null;
	BufferedWriter f1=null;
	f=new FileWriter("文件",true);//追加
	f1=new BufferedWriter(f);
	//添加到文本
	f1.write(s1);
    //添加换行
	f1.newLine();
	//关闭
    f1.close();
	f.close();
}

search方法

将文本文件按行提取,因为我们提前知道类的成员数据的个数n,所以按照每n行(每n个字符串)构造一个该类对象,然后用插入list中。

输入 相应的数值 ,在list中搜索,输出。

public void search() {
	Scanner in = new Scanner(System.in);
	//输入需要查询的信息
    //打开
    String s=in.nextLine();
	File file=new File("文件");
	FileReader f=null;		//文件读取对象
	BufferedReader f1=null;	//字符流对象
	f=new FileReader(file);
	f1=new BufferedReader(f);
	String str=null;			
    long js=1;//记录第几个字符串
	//假设该类有三个数据成员    n=3
    String s1=null;
	String s2=null;
	String s3=null;
    //逐行提取
	while((str=f1.readLine())!=null) {
		if(js%3==1)
			s1=str;
		if(js%3==2)
			s2=str;
		if(js%3==0) {
			s3=str;
			Book _book=new Book(s1,s2,s3);
			booklist.add(_book);
		}
		js++;
	}
    //关闭
	f1.close();
	f.close();
    //遍历list
	for (Book i : booklist) 
		if(i.getName().equals(s)) 
			i.show();   
}

delete()方法

和search()方法一样,从文本文件中提取,构造类,插入list 。

然后遍历,找相关的信息,从list中删除。

全部重新写入文本文件。

public void delete() {
	Scanner in = new Scanner(System.in);
	//输入要删除的信息
	String s11=in.nextLine();
    //打开
    File file=new File("文件");
	FileReader f=null;		//文件读取对象
	BufferedReader f1=null;	//字符流对象
	f=new FileReader(file);
	f1=new BufferedReader(f);
	String str=null;
	long js=1;//记录第几个字符串 假设有三个 n=3
	String s1=null;
	String s2=null;
	String s3=null;
    //从文件中读取数据
	while((str=f1.readLine())!=null) {
		if(js%3==1)
			s1=str;
		if(js%3==2)
			s2=str;
		if(js%3==0) {
			s3=str;
            //构造对象
	        Book _book=new Book(s1,s2,s3);
            //插入list中
            booklist.add(_book);
		}
        js++;
    }
    //关闭
    f1.close();
    f.close();
	//遍历list,搜索是否有相应数据
	for (int i = 0; i < booklist.size(); i++) {
		if(booklist.get(i).getName().equals(s11) 
            && booklist.get(i).getAuthor().equals(s22) 
            && booklist.get(i).getPrice().equals(s33)) { 
			//在list中删除 相应的数据
            booklist.remove(i);
    //打开
	FileWriter ff=null;
	BufferedWriter f11=null;
	ff=new FileWriter("src\\图书信息.txt");
	f11=new BufferedWriter(ff);
	//添加到文本
	for(int i = 0; i < booklist.size(); i++) {
		String s111=booklist.get(i).getName();
		String s222=booklist.get(i).getAuthor();
		String s333=booklist.get(i).getPrice();
		f11.write(s111);
		f11.newLine();
		f11.write(s222);
		f11.newLine();
		f11.write(s333);
		f11.newLine();
    }
	//关闭
	f11.close();
	ff.close();
}

注释:可以只改变需要删除的文本,然后后面的文本补上去。详见:

 

代码

c6e49a945be14cd4acb2f709e1ddb74d.png

接口

package library;

public interface Opretor {
    public abstract void show();
	public abstract void add();
	public abstract void delete();
	public abstract void search();
}

operate拼错了,高中就不会拼这个词。 

Book类

数据成员:书名 name,作者 author,价格  price

package library;

public class Book {
	//书名,作者,价格
	private String name;
	private String author;
	private String price;
	
	Book() {}
	
	Book(String _name,String _author,String _price) {
		this.name=_name;
		this.author=_author;
		this.price=_price;
	}
	
	public void setName(String _name) {
		this.name=_name;
	}
	
	public String getName() {
		return name;
	}
	
	public void setAuthor(String _author) {
		this.author=_author;
	}
	
	public String getAuthor() {
		return author;
	}

	public void setPrice(String _price) {
		this.price=_price;
	}
	
	public String getPrice() {
		return price;
	}
 	
    public void show() {
		System.out.println("Name:                   "+name);
		System.out.println("Author:                 "+author);
		System.out.println("Price:                  "+price);
    }

}

个人感觉set方法没有必要写,但还是写了,显得代码长。

 Reader类

数据成员:读者姓名 name、年龄 age、性别 sex、电话 tel、所在院系 college

package library;

public class Reader {
	//读者姓名、年龄、性别、电话、所在院系
	private String name;
	private String age;
	private String sex;
	private String tel;
	private String college;
	
	public Reader() {}
	
	public Reader(String _name,String _age,String _sex,String _tel,String _college){
		this.name=_name;
		this.age=_age;
		this.sex=_sex;
		this.tel=_tel;
		this.college=_college;
	}
	
	public void setName(String _name) {
		this.name=_name;
	}
	
	public String getName() {
		return name;
	}
	
	public void setAge(String _age) {
		this.age=_age;
	}
	
	public String getAge() {
		return age;
	}

	public void setSex(String _sex) {
		this.sex=_sex;
	}
		
	public String getSex() {
		return sex;
	}

	public void setTel(String _tel) {
		this.tel=_tel;
	}
		
	public String getTel() {
		return tel;
	}
		
	public void setCollege(String _college) {
		this.college=_college;
	}
		
	public String getCollege() {
		return college;
	}
		
	public void show() {
		System.out.println("Name:                   "+name);
		System.out.println("Age:                    "+age);
		System.out.println("Sex:                    "+sex);
		System.out.println("Tel:                    "+tel);
		System.out.println("College:                "+college);
	}

}

User类

继承Reader类,添加了数据成员:账号 accountnumber ,密码 password

package library;

public class User extends Reader{
	String password;
	String accountnumber;
	
	User(){}
	
	User(String _accountnumber,String _password,String _name,String _age,
            String _sex,String _tel,String _college){
		super(_name,_age,_sex,_tel,_college);
		this.password=_password;
		this.accountnumber=_accountnumber;
	}
	
	public void setAccountnumber(String _accountnumber) {
		this.accountnumber=_accountnumber;
	}
	
	public String getAccountnumber() {
		return accountnumber;
	}
	
	public void setPassword(String _password) {
		this.password=_password;
	}
	
	public String getPassword() {
		return password;
	}

}

Informatica类

借阅信息 由Reader和Book 的两个对象和 数据成员 状态 state 组成。

状态 state 用 "借出" "在馆" 表示。

为什么用Reader而不是User???数据成员太多了。

注释:感觉可以在数据一一对应,分别从读者信息,图书信息 两个库中提取 ,将状态存放在Book类中(假设每种图书为一种)或者 放入Reader类中 。

package library;

import java.text.SimpleDateFormat;
import java.util.Date;
import library.Book;

public class Informatica {
	Book book;
	Reader reader;
	String state;
	
	Informatica(){}
	
	Informatica(String _bname,String _author,String _price,String _name,
            String _age,String _sex,String _tel,String _college,String _state){
		this.book=new Book(_bname,_author,_price);
		this.reader=new Reader(_name,_age,_sex,_tel,_college);
		this.state=_state;
	}
	
	public String getState() {
		return state;
	}

}

BookList类

package library;

import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;

public class BookList implements Opretor{
	
	private LinkedList<Book> booklist=new LinkedList<Book>();
	
	BookList(){}
	
	public void show() {
		//应该取出后,构造成类,用类的show方法显示
		//或者 在每个提出的字符串前面 加上 该字符串的信息 如 书名:天龙八部
		File file=new File("src\\图书信息.txt");
		FileReader f=null;		//文件读取对象
		BufferedReader f1=null;	//字符流对象
		//判断文件是否存在,如果不存在则创建一个文件。
		if(file.exists()) {
			//应该判断文件是否为空
			if(file.length()==0)
				System.out.println("馆中暂无信息,请先添加信息。");
			else {
				try {
					f=new FileReader(file);
					f1=new BufferedReader(f);
					String str=null;
					while((str=f1.readLine())!=null) {
						System.out.println(str);
					}
				} catch (Exception e1) {
					// TODO: handle exception
				}finally {
					try {
						f1.close();
						f.close();
					} catch (Exception e2) {
						// TODO: handle exception
					}
				}
			}
		}
		else {
			if(file.length()==0)
				System.out.println("馆中暂无信息,请先添加信息。");
			else {
				try {
					file.createNewFile();
					try {
						f=new FileReader(file);
						f1=new BufferedReader(f);	
						String str=null;
						while((str=f1.readLine())!=null) {
							System.out.println(str);
						}
					} catch (Exception e3) {
						// TODO: handle exception
					}finally {
						try {
							f1.close();
							f.close();
						} catch (Exception e4) {
							// TODO: handle exception
						}
					}
				} catch (Exception e5) {
					// TODO: handle exception
				}
			}	
		}
	}
	
	public void add() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入书名");
		String s1=in.nextLine();
		System.out.println("请输入作者");
		String s2=in.nextLine();
		System.out.println("请输入价格");
		String s3=in.nextLine();
		File file=new File("src\\图书信息.txt");
		FileWriter f=null;
		BufferedWriter f1=null;
		try {
			f=new FileWriter("src\\图书信息.txt",true);//追加
			f1=new BufferedWriter(f);
		//添加到文本
			f1.write(s1);
			f1.newLine();
			f1.write(s2);
			f1.newLine();
			f1.write(s3);
			f1.newLine();
			System.out.println("添加成功。");
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	public void delete() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入书名");
		String s11=in.nextLine();
		System.out.println("请输入作者");
		String s22=in.nextLine();
		System.out.println("请输入价格");
		String s33=in.nextLine();
		File file=new File("src\\图书信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;	
		try {
			f=new FileReader(file);
			f1=new BufferedReader(f);
			String str=null;
			long js=1;//记录第几个字符串
			String s1=null;
			String s2=null;
			String s3=null;
			while((str=f1.readLine())!=null) {
				if(js%3==1)
					s1=str;
				if(js%3==2)
					s2=str;
				if(js%3==0) {
					s3=str;
					Book _book=new Book(s1,s2,s3);
					booklist.add(_book);
				}
				js++;
			}
		} catch (Exception e1) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		boolean isdelete=false;
		for (int i = 0; i < booklist.size(); i++) {
			if(booklist.get(i).getName().equals(s11) 
                    && booklist.get(i).getAuthor().equals(s22) 
                    && booklist.get(i).getPrice().equals(s33)) { 
				booklist.remove(i);
			 	isdelete=true;
			}
		 }
		if(isdelete) {
			System.out.println("删除成功。");
			FileWriter ff=null;
			BufferedWriter f11=null;
			try {
				ff=new FileWriter("src\\图书信息.txt");
				f11=new BufferedWriter(ff);
				for(int i = 0; i < booklist.size(); i++) {
					String s111=booklist.get(i).getName();
					String s222=booklist.get(i).getAuthor();
					String s333=booklist.get(i).getPrice();
					f11.write(s111);
					f11.newLine();
					f11.write(s222);
					f11.newLine();
					f11.write(s333);
					f11.newLine();
				}
			} catch (Exception e) {
			// TODO: handle exception
			}finally {
				try {
					f11.close();
					ff.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		else
			System.out.println("馆中并未找到该书信息。\n请核对后,重新输入。");
	}
	
	public void search() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入书名");
		String s=in.nextLine();
		File file=new File("src\\图书信息.txt");
		FileReader f=null;		//文件读取对象
		BufferedReader f1=null;	//字符流对象
		try {
			f=new FileReader(file);
			f1=new BufferedReader(f);
			String str=null;
			long js=1;//记录第几个字符串
			String s1=null;
			String s2=null;
			String s3=null;
			while((str=f1.readLine())!=null) {
				if(js%3==1)
					s1=str;
				if(js%3==2)
					s2=str;
				if(js%3==0) {
					s3=str;
					Book _book=new Book(s1,s2,s3);
					booklist.add(_book);
				}
				js++;
			}
		} catch (Exception e1) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		boolean issearch=false;
		for (Book i : booklist) {
			if(i.getName().equals(s)) {
				i.show();
				issearch=true;
			}
        }
		if(!issearch) 
			System.out.println("查无此书。\n请核对后重新输入。");
	}
	
	public void clear() {
		booklist.clear();
	}
	
}

ReaderList类

package library;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.Scanner;

public class ReaderList implements Opretor{
	
	private LinkedList<Reader> readerlist=new LinkedList<Reader>();
	
	ReaderList(){}
	
	public void show() {
		File file=new File("src\\读者信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;	
		if(file.exists()) {
			if(file.length()==0)
				System.out.println("馆中暂无信息,请先添加信息。");
			try {
				f=new FileReader(file);
				f1=new BufferedReader(f);
				String str=null;
				while((str=f1.readLine())!=null) {
					System.out.println(str);
				}
			} catch (Exception e1) {
				// TODO: handle exception
			}finally {
				try {
					f1.close();
					f.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}else {
			if(file.length()==0)
				System.out.println("馆中暂无信息,请先添加信息。");
			try {
				file.createNewFile();
				try {
					f=new FileReader(file);
					f1=new BufferedReader(f);	
					String str=null;
					while((str=f1.readLine())!=null) {
						System.out.println(str);
					}
				} catch (Exception e3) {
					// TODO: handle exception
				}finally {
					try {
						f1.close();
						f.close();
					} catch (Exception e4) {
						// TODO: handle exception
					}
				}
			} catch (Exception e5) {
				// TODO: handle exception
				}
			}
		}
	
	public void add() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入姓名");
		String s1=in.nextLine();
		System.out.println("请输入年龄");
		String s2=in.nextLine();
		System.out.println("请输入性别");
		String s3=in.nextLine();
		System.out.println("请输入电话");
		String s4=in.nextLine();
		System.out.println("请输入所在院系");
		String s5=in.nextLine();
		File file=new File("src\\读者信息.txt");
		FileWriter f=null;
		BufferedWriter f1=null;
		try {
			f=new FileWriter("src\\读者信息.txt",true);//追加
			f1=new BufferedWriter(f);
			f1.write(s1);
			f1.newLine();
			f1.write(s2);
			f1.newLine();
			f1.write(s3);
			f1.newLine();
			f1.write(s4);
			f1.newLine();
			f1.write(s5);
			f1.newLine();
			System.out.println("添加成功。");
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	public void delete() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入姓名");
		String s11=in.nextLine();
		System.out.println("请输入年龄");
		String s22=in.nextLine();
		System.out.println("请输入性别");
		String s33=in.nextLine();
		System.out.println("请输入电话");
		String s44=in.nextLine();
		System.out.println("请输入所在院系");
		String s55=in.nextLine();
		File file=new File("src\\读者信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;	
		try {
			f=new FileReader(file);
			f1=new BufferedReader(f);
			String str=null;
			long js=1;
			String s1=null;
			String s2=null;
			String s3=null;
			String s4=null;
			String s5=null;
			while((str=f1.readLine())!=null) {
				if(js%5==1)
					s1=str;
				if(js%5==2)
					s2=str;
				if(js%5==3)
					s3=str;
				if(js%5==4)
					s4=str;
				if(js%5==0) {
					s5=str;
					Reader _reader=new Reader(s1,s2,s3,s4,s5);
					readerlist.add(_reader);
				}
				js++;
			}
		} catch (Exception e1) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		boolean isdelete=false;
		 for (int i = 0; i < readerlist.size(); i++) {
			 if(readerlist.get(i).getName().equals(s11) 
                    && readerlist.get(i).getAge().equals(s22) 
                    && readerlist.get(i).getSex().equals(s33)
					&& readerlist.get(i).getTel().equals(s44) 
                    && readerlist.get(i).getCollege().equals(s55)) {
				//删除后,往前覆盖,readerlist.size()减一	
				 readerlist.remove(i);
				 isdelete=true;
			 }
		 }
		 if(isdelete) {
			 System.out.println("删除成功。");
			FileWriter ff=null;
			BufferedWriter f11=null;
				try {
					ff=new FileWriter("src\\读者信息.txt");
					f11=new BufferedWriter(ff);
					for(int i = 0; i < readerlist.size(); i++) {
						String s111=readerlist.get(i).getName();
						String s222=readerlist.get(i).getAge();
						String s333=readerlist.get(i).getSex();
						String s444=readerlist.get(i).getTel();
						String s555=readerlist.get(i).getCollege();
						f11.write(s111);
						f11.newLine();
						f11.write(s222);
						f11.newLine();
						f11.write(s333);
						f11.newLine();
						f11.write(s444);
						f11.newLine();
						f11.write(s555);
						f11.newLine();
					}
				} catch (Exception e) {
					// TODO: handle exception
				}finally {
					try {
						f11.close();
						ff.close();
					} catch (Exception e2) {
						// TODO: handle exception
					}
				}
		 }		
		else
			System.out.println("馆中并未找到该读者信息。\n请核对后,重新输入。");
	}
	
	public void search() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入读者姓名");
		String s=in.nextLine();
		File file=new File("src\\读者信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;
		try {
			f=new FileReader(file);
			f1=new BufferedReader(f);
			String str=null;
			long js=1;
			String s1=null;
			String s2=null;
			String s3=null;
			String s4=null;
			String s5=null;
			while((str=f1.readLine())!=null) {
				if(js%5==1)
					s1=str;
				if(js%5==2)
					s2=str;
				if(js%5==3)
					s3=str;
				if(js%5==4)
					s4=str;
				if(js%5==0) {
					s5=str;
					Reader _reader=new Reader(s1,s2,s3,s4,s5);
					readerlist.add(_reader);
				}
				js++;
			}
		} catch (Exception e1) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		boolean issearch=false;
		for (Reader i : readerlist) {
			if(i.getName().equals(s)) { 
				i.show();
				issearch=true;
			}
        }
		if(!issearch) 
			System.out.println("查无此读者。\n请核对后重新输入。");
	}
	
	public void clear() {
		readerlist.clear();
	}
	
}

UserList类

package library;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.Scanner;

public class UserList extends ReaderList implements Opretor{
	
	private LinkedList<User> userlist=new LinkedList<User>();
	
	UserList(){}
	
	public void show() {
		File file=new File("src\\用户信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;	
		if(file.exists()) {
			if(file.length()==0)
				System.out.println("馆中暂无信息,请先添加信息。");
			try {
				f=new FileReader(file);
				f1=new BufferedReader(f);
				String str=null;
				while((str=f1.readLine())!=null) {
					System.out.println(str);
				}
			} catch (Exception e1) {
				// TODO: handle exception
			}finally {
				try {
					f1.close();
					f.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}else {
			if(file.length()==0)
				System.out.println("馆中暂无信息,请先添加信息。");
			try {
				file.createNewFile();
				try {
					f=new FileReader(file);
					f1=new BufferedReader(f);	
					String str=null;
					while((str=f1.readLine())!=null) {
						System.out.println(str);
					}
				} catch (Exception e3) {
					// TODO: handle exception
				}finally {
					try {
						f1.close();
						f.close();
					} catch (Exception e4) {
						// TODO: handle exception
					}
				}
			} catch (Exception e5) {
				// TODO: handle exception
				}
			}
		}
	
	public void add() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入用户名");
		String s1=in.nextLine();
		System.out.println("请设置用户密码");
		String s2=in.nextLine();
		System.out.println("请输入用户真实姓名");
		String s3=in.nextLine();
		System.out.println("请输入用户年龄");
		String s4=in.nextLine();
		System.out.println("请输入用户性别");
		String s5=in.nextLine();
		System.out.println("请输入用户电话");
		String s6=in.nextLine();
		System.out.println("请输入用户所在院系");
		String s7=in.nextLine();
		File file=new File("src\\用户信息.txt");
		FileWriter f=null;
		BufferedWriter f1=null;
		try {
			f=new FileWriter("src\\用户信息.txt",true);//追加
			f1=new BufferedWriter(f);
			f1.write(s1);
			f1.newLine();
			f1.write(s2);
			f1.newLine();
			f1.write(s3);
			f1.newLine();
			f1.write(s4);
			f1.newLine();
			f1.write(s5);
			f1.newLine();
			f1.write(s6);
			f1.newLine();
			f1.write(s7);
			f1.newLine();
			System.out.println("添加成功。");
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	public void delete() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入用户名");
		String s11=in.nextLine();
		System.out.println("请设置用户密码");
		String s22=in.nextLine();
		System.out.println("请输入用户真实姓名");
		String s33=in.nextLine();
		System.out.println("请输入用户年龄");
		String s44=in.nextLine();
		System.out.println("请输入用户性别");
		String s55=in.nextLine();
		System.out.println("请输入用户电话");
		String s66=in.nextLine();
		System.out.println("请输入用户所在院系");
		String s77=in.nextLine();
		File file=new File("src\\用户信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;	
		try {
			f=new FileReader(file);
			f1=new BufferedReader(f);
			String str=null;
			long js=1;
			String s1=null;
			String s2=null;
			String s3=null;
			String s4=null;
			String s5=null;
			String s6=null;
			String s7=null;
			while((str=f1.readLine())!=null) {
				if(js%7==1)
					s1=str;
				if(js%7==2)
					s2=str;
				if(js%7==3)
					s3=str;
				if(js%7==4)
					s4=str;
				if(js%7==5)
					s5=str;
				if(js%7==6)
					s6=str;
				if(js%7==0) {
					s7=str;
					User _user=new User(s1,s2,s3,s4,s5,s6,s7);
					userlist.add(_user);
				}
				js++;
			}
		} catch (Exception e1) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		boolean isdelete=false;
		for (int i = 0; i < userlist.size(); i++) {
			if(userlist.get(i).getAccountnumber().equals(s11) 
                && userlist.get(i).getPassword().equals(s22) 
                && userlist.get(i).getName().equals(s33)
				&& userlist.get(i).getAge().equals(s44) 
                && userlist.get(i).getSex().equals(s55) 
				&& userlist.get(i).getTel().equals(s66) 
                && userlist.get(i).getCollege().equals(s77)) {
					userlist.remove(i);
					isdelete=true;
			}
		 }
		if(isdelete) {
			 System.out.println("删除成功。");
			FileWriter ff=null;
			BufferedWriter f11=null;
			try {
				ff=new FileWriter("src\\用户信息.txt");
				f11=new BufferedWriter(ff);
				for(int i = 0; i < userlist.size(); i++) {
					String s111=userlist.get(i).getAccountnumber();
					String s222=userlist.get(i).getPassword();
					String s333=userlist.get(i).getName();
					String s444=userlist.get(i).getAge();
					String s555=userlist.get(i).getSex();
					String s666=userlist.get(i).getTel();
					String s777=userlist.get(i).getCollege();
					f11.write(s111);
					f11.newLine();
					f11.write(s222);
					f11.newLine();
					f11.write(s333);
					f11.newLine();
					f11.write(s444);
					f11.newLine();
					f11.write(s555);
					f11.newLine();
					f11.write(s666);
					f11.newLine();
					f11.write(s777);
					f11.newLine();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}finally {
				try {
					f11.close();
					ff.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		else
			System.out.println("馆中并未找到该用户信息。\n请核对后,重新输入。");
	}
	
	public void search() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入用户名");
		String s=in.nextLine();
		File file=new File("src\\用户信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;	
		try {
			f=new FileReader(file);
			f1=new BufferedReader(f);
			String str=null;
			long js=1;
			String s1=null;
			String s2=null;
			String s3=null;
			String s4=null;
			String s5=null;
			String s6=null;
			String s7=null;	
			while((str=f1.readLine())!=null) {
				if(js%7==1)
					s1=str;
				if(js%7==2)
					s2=str;
				if(js%7==3)
					s3=str;
				if(js%7==4)
					s4=str;
				if(js%7==5)
					s5=str;
				if(js%7==6)
					s6=str;	
				if(js%7==0) {
					s7=str;
					User _user=new User(s1,s2,s3,s4,s5,s6,s7);
					userlist.add(_user);
				}
				js++;
			}
		} catch (Exception e1) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		boolean issearch=false;
		for (User i : userlist) {
			if(i.getAccountnumber().equals(s)) {
				i.show();
				issearch=true;
			}
        }
		if(!issearch) 
			System.out.println("查无此用户。\n请核对后重新输入。");
	}	
	
	public void clear() {
		userlist.clear();
	}
	
}

IFList类

package library;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.Scanner;

public class IFList implements Opretor{
	
	private LinkedList<Informatica> iflist=new LinkedList<>();
	
	IFList() {}
	
	public void show() {
		File file=new File("src\\借阅信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;	
		if(file.exists()) {
			if(file.length()==0)
				System.out.println("馆中暂无信息,请先添加信息。");
			try {
				f=new FileReader(file);
				f1=new BufferedReader(f);
				String str=null;
				while((str=f1.readLine())!=null) {
					System.out.println(str);
				}
			} catch (Exception e1) {
				// TODO: handle exception
			}finally {
				try {
					f1.close();
					f.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}else {
			if(file.length()==0)
				System.out.println("馆中暂无信息,请先添加信息。");
			try {
				file.createNewFile();
				try {
					f=new FileReader(file);
					f1=new BufferedReader(f);	
					String str=null;
					while((str=f1.readLine())!=null) {
						System.out.println(str);
					}
				} catch (Exception e3) {
					// TODO: handle exception
				}finally {
					try {
						f1.close();
						f.close();
					} catch (Exception e4) {
						// TODO: handle exception
					}
				}
			} catch (Exception e5) {
				// TODO: handle exception
				}
			}
		}
	
	public void add() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入书名");
		String s1=in.nextLine();
		System.out.println("请设置作者");
		String s2=in.nextLine();
		System.out.println("请设置价格");
		String s3=in.nextLine();
		System.out.println("请输入用户真实姓名");
		String s4=in.nextLine();
		System.out.println("请输入用户年龄");
		String s5=in.nextLine();
		System.out.println("请输入用户性别");
		String s6=in.nextLine();
		System.out.println("请输入用户电话");
		String s7=in.nextLine();
		System.out.println("请输入用户所在院系");
		String s8=in.nextLine();
		System.out.println("请输入:如果是借出的,请输入:借出;如果是还回的请输入:在馆");
		String s9=in.nextLine();
		//Informatica _i=new Informatica(s1,s2,s3,s4,s5,s6,s7,s8,"在馆");
		//iflist.add(_i);
		File file=new File("src\\借阅信息.txt");
		FileWriter f=null;
		BufferedWriter f1=null;
		try {
			f=new FileWriter("src\\借阅信息.txt",true);//追加
			f1=new BufferedWriter(f);
		//添加到文本
			f1.write(s1);
			f1.newLine();
			f1.write(s2);
			f1.newLine();
			f1.write(s3);
			f1.newLine();
			f1.write(s4);
			f1.newLine();
			f1.write(s5);
			f1.newLine();
			f1.write(s6);
			f1.newLine();
			f1.write(s7);
			f1.newLine();
			f1.write(s8);
			f1.newLine();
			f1.write(s9);
			f1.newLine();
			System.out.println("添加成功。");
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	public void delete() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入书名");
		String s11=in.nextLine();
		System.out.println("请设置作者");
		String s22=in.nextLine();
		System.out.println("请设置价格");
		String s33=in.nextLine();
		System.out.println("请输入用户真实姓名");
		String s44=in.nextLine();
		System.out.println("请输入用户年龄");
		String s55=in.nextLine();
		System.out.println("请输入用户性别");
		String s66=in.nextLine();
		System.out.println("请输入用户电话");
		String s77=in.nextLine();
		System.out.println("请输入用户所在院系");
		String s88=in.nextLine();
		System.out.println("请输入:如果是借出的,请输入:借出;如果是还回的请输入:在馆");
		String s99=in.nextLine();
		File file=new File("src\\借阅信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;	
		try {
			f=new FileReader(file);
			f1=new BufferedReader(f);
			String str=null;
			long js=1;/
			String s1=null;
			String s2=null;
			String s3=null;
			String s4=null;
			String s5=null;
			String s6=null;
			String s7=null;
			String s8=null;
			String s9=null;
			while((str=f1.readLine())!=null) {
				if(js%9==1)
					s1=str;
				if(js%9==2)
					s2=str;
				if(js%9==3)
					s3=str;
				if(js%9==4)
					s4=str;
				if(js%9==5)
					s5=str;
				if(js%9==6)
					s6=str;
				if(js%9==7)
					s7=str;
				if(js%9==8)
					s8=str;
				if(js%9==0) {
					s9=str;
					Informatica _i=new Informatica(s1,s2,s3,s4,s5,s6,s7,s8,s9);
					iflist.add(_i);
				}
				js++;
			}
		} catch (Exception e1) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		boolean isdelete=false;
		for (int i = 0; i < iflist.size(); i++) {
			if(iflist.get(i).book.getName().equals(s11) 
                && iflist.get(i).book.getAuthor().equals(s22) 
                && iflist.get(i).book.getPrice().equals(s33)
				&& iflist.get(i).reader.getName().equals(s44) 
                && iflist.get(i).reader.getAge().equals(s55) 
				&& iflist.get(i).reader.getSex().equals(s66) 
                && iflist.get(i).reader.getTel().equals(s77)
				&& iflist.get(i).reader.getCollege().equals(s88) 
                && iflist.get(i).getState().equals(s99)) {
				 iflist.remove(i);
				 isdelete=true;
			}
		 }
		if(isdelete) {
			 System.out.println("删除成功。");
			FileWriter ff=null;
			BufferedWriter f11=null;
			try {
				ff=new FileWriter("src\\借阅信息.txt");
				f11=new BufferedWriter(ff);
				for(int i = 0; i < iflist.size(); i++) {
					String s111=iflist.get(i).book.getName();
					String s222=iflist.get(i).book.getAuthor();
					String s333=iflist.get(i).book.getPrice();
					String s444=iflist.get(i).reader.getName();
					String s555=iflist.get(i).reader.getAge();
					String s666=iflist.get(i).reader.getSex();
					String s777=iflist.get(i).reader.getTel();
					String s888=iflist.get(i).reader.getCollege();
					String s999=iflist.get(i).getState();
					f11.write(s111);
					f11.newLine();
					f11.write(s222);
					f11.newLine();
					f11.write(s333);
					f11.newLine();
					f11.write(s444);
					f11.newLine();
					f11.write(s555);
					f11.newLine();
					f11.write(s666);
					f11.newLine();
					f11.write(s777);
					f11.newLine();
					f11.write(s888);
					f11.newLine();
					f11.write(s999);
					f11.newLine();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}finally {
				try {
					f11.close();
					ff.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		else
			System.out.println("馆中并未找到该借阅信息。\n请核对后,重新输入。");
	}
	
	public void search() {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入书名");
		String s=in.nextLine();
		File file=new File("src\\借阅信息.txt");
		FileReader f=null;		
		BufferedReader f1=null;	
		try {
			f=new FileReader(file);
			f1=new BufferedReader(f);
			String str=null;
			long js=1;
			String s1=null;
			String s2=null;
			String s3=null;
			String s4=null;
			String s5=null;
			String s6=null;
			String s7=null;
			String s8=null;
			String s9=null;
			while((str=f1.readLine())!=null) {
				if(js%9==1)
					s1=str;
				if(js%9==2)
					s2=str;
				if(js%9==3)
					s3=str;
				if(js%9==4)
					s4=str;
				if(js%9==5)
					s5=str;
				if(js%9==6)
					s6=str;
				if(js%9==7)
					s7=str;
				if(js%9==8)
					s8=str;
				if(js%9==0) {
					s9=str;
					Informatica _i=new Informatica(s1,s2,s3,s4,s5,s6,s7,s8,s9);
					iflist.add(_i);
				}
				js++;
			}
		} catch (Exception e1) {
			// TODO: handle exception
		}finally {
			try {
				f1.close();
				f.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		boolean issearch=false;
		for (Informatica i : iflist) {
			if(i.book.getName().equals(s)) {
				i.book.show();
				i.reader.show();
				System.out.println(i.getState());
				issearch=true;
			}
        }
		if(!issearch) 
			System.out.println("查无此借阅信息。\n请核对后重新输入。");
	}	
	
	public void clear() {
		iflist.clear();
	}

}

main

package library;

import java.util.Scanner;

public class main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		BookList booklist=new BookList();
		ReaderList readerList=new ReaderList();
		UserList userList=new UserList();
		IFList iFList=new IFList();
		
		Scanner in = new Scanner(System.in);
		boolean pd = true;
		while(pd) {
			//在输入书名等数据时 应该加限制
			System.out.println("-------------------------------图书管理系统-------------------------------");
            System.out.println("欢迎使用,请按照下面提示进行操作。\r\n"
            		+ "0:系统退出\r\n"
            		+ "1:图书信息列表显示\r\n"
            		+ "2:图书信息新增\r\n"
            		+ "3:图书信息删除\r\n"
            		+ "4:图书信息查找(按书名查找)\r\n"
            		+ "5:读者信息列表显示\r\n"
            		+ "6:读者信息新增\r\n"
            		+ "7:读者信息删除\r\n"
            		+ "8:读者信息查找(按姓名查找)\r\n"
            		+ "9:借阅信息列表显示\r\n"
            		+ "10:借阅信息新增\r\n"
            		+ "11:借阅信息删除\r\n"
            		+ "12:借阅信息查找(按书名查找)\r\n"
            		+ "13:用户信息列表显示\r\n"
            		+ "14:用户信息新增\r\n"
            		+ "15:用户信息删除\r\n"
            		+ "16:用户信息查找(按用户名查找)\r\n"
            		+ "");
            int i = in.nextInt();
            if (i<0 || i>16) {
                System.out.println("输入范围错误,请重新输入。");
                continue;
            }
            switch(i) {
            case 0: pd=false;System.out.println("您已成功退出程序。感谢使用。");break;
            case 1:booklist.show();		break;
            case 2:booklist.add();      break;
            case 3:booklist.delete();	break;
            case 4:booklist.search();   break;
            case 5:readerList.show();	break;
            case 6:readerList.add(); 	break;
            case 7:readerList.delete();	break;
            case 8:readerList.search();	break;
            case 9:iFList.show();		break;
            case 10:iFList.add();		break;
            case 11:iFList.delete(); 	break;
            case 12:iFList.search();	break;
            case 13:userList.show();	break;
            case 14:userList.add();		break;
            case 15:userList.delete();	break;
            case 16:userList.search();	break;	
            }
            //清空列表
            booklist.clear();
            readerList.clear();
            iFList.clear();
            userList.clear();
		}
	}
}

收获

如何快速的学习一门语言并能用。抄,抄别人的代码,代码,哪里不会搜哪里。

io流的深刻的认识。除了编码,剩下的都是字符串问题。

对Eclipse功能的认识深刻。原来不会用编程工具,真的编不了程序。

以后不拖,尽量不拖,提前做。

调试时出现的错误

大部分都忘了。

main在每次执行完选项后,要将所有的list清空,不然就乱套了。之前search() delete() 所创建的对象都存入了list,下次的操作,如果有 search() delete() 则必定受影响。

如果测试测试数据是直接在文件里写而不是用 add()方法 添加到文本中的,请注意一下两点:

1. String s1=in.nextLine(); 语句中的 in.nextLine() 是会存空格的。

因为这个代码是我(蒟蒻)无脑写的 ,在 search() 和 delete() 方法中 的查询讯息,用的是字符串是否相等。也就是说,如果你在文本文件中直接在测试数据的后面不小心多输入一个空格,那么即使两个比较的字符串的前面相同,因为后面多的那一个字符串,也会变得不同。

2. 文本的编码问题。

还没来的及查资料仔细研究,但实践出来一些避坑的小点,总结如下。

当我们打开.txt文件,6963ec00b2ca4206abbcd99d5eefcd18.png

 单机 查看 ,打开状态栏,我们可以看到文本文件的左下角

8b3d7996b3ad40338a122f5541401f2c.png

 UTF-8

UTF-8_百度百科 (baidu.com)

当我们直接在文本文件中输入时,是按照UTF-8来编码的。

 

 

 

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

返返返

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值