大家好,欢迎来到IT知识分享网。
io流用的不是很熟练,还有Book类的应用出了点问题,越改越错,从2个错误改到102个QAQ,孩子想哭,问了好多人也没改成,最后勉强成型,而且上个星期内分泌系统出了点小问题,天天往医院跑,开始敲的太晚了,现在要备战期末考,等期末考结束再改改Book类吧。
主函数和界面
io流
操作
- 添加
- 修改
- 查找
- 排序
- 删除
基本类
- Book
- Borrow
- Reader
主函数和界面
package view;
import functions.Libraryborrow;
import functions.LibraryinformationManagement;
import functions.LibraryreaderManagement;
import java.io.IOException;
import java.text.ParseException;
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException, ParseException {
boolean b = true;
while (b) {
System.out.println("*********************");
System.out.println("1、进入图书管理系统");
System.out.println("2、进入读者管理系统");
System.out.println("3、借阅、归还书籍");
System.out.println("4、退出系统");
System.out.println("*********************");
char c = Tool.tools.readMenuSelection();
switch (c) {
case '1':
LibraryinformationManagement.bookmew();
break;
case '2':
LibraryreaderManagement.readermew();
break;
case '3':
Libraryborrow.sentaku();
break;
case '4':
System.out.println("谢谢使用");
System.exit(0);
break;
}
}
}
}
IT知识分享网
IT知识分享网
package functions;
import EntityClass.Book;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import static functions.Add.AddBook;
import static functions.Find.findbook;
public class LibraryinformationManagement {
public static void bookmew() throws IOException, ClassNotFoundException {
ArrayList<Book> array=new ArrayList<Book>();
while (true) {
System.out.println("1.添加图书");
System.out.println("2.查询图书");
System.out.println("3.图书排序");
System.out.println("4.修改或删除图书");
System.out.println("5.返回上一级");
char c = Tool.tools.readMenuSelection();
switch (c) {
case '1':
AddBook(array);
break;
case '2':
findbook(array);
break;
case '3':
sort.sentakusort();
break;
case '4':
sentaku1();
Change.change1();
break;
case '5':
return;
}
}
}
public static void sentaku1() throws IOException{
System.out.println("您要进行修改还是删除?:"+"\n"+"1、修改\n2、删除");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
Change.sentakuchange1();
break;
case 2:
Delete.sentakudelete1();
break;
}
}
}
package functions;
import EntityClass.Reader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class LibraryreaderManagement {
public static void readermew() throws IOException, ClassNotFoundException {
ArrayList<Reader> array=new ArrayList<Reader>();
while (true) {
System.out.println("1.添加读者信息");
System.out.println("2.查询读者信息");
System.out.println("3.读者信息排序");
System.out.println("4.修改或删除读者信息");
System.out.println("5.返回上一级");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
switch (line) {
case "1":
Add.AddReader(array);
break;
case "2":
Find.findreader(array);
break;
case "3":
sort.sentakusortreader();
break;
case "4":
sentaku1();
break;
case "5":
return;
}
}
}
public static void sentaku1() throws IOException{
System.out.println("您要进行修改还是删除?:"+"\n"+"1、修改\n2、删除");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
Change.change3();
break;
case 2:
Delete.detelebook3();
break;
}
}
}
io流
IT知识分享网package functions;
import EntityClass.Book;
import EntityClass.Borrow;
import EntityClass.Reader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class io {
public static ArrayList inbook() throws IOException {
FileReader fr = new FileReader("myFile\\book.txt");
BufferedReader br = new BufferedReader(fr);
ArrayList<Book> array = new ArrayList<Book>();
String line;
while ((line = br.readLine()) != null) {
String[] strArray = line.split(",");
Book book = new Book();
book.setNum(parseInt(strArray[0]));
book.setName(strArray[1]);
book.setAuthor(strArray[2]);
book.setPrice(strArray[3]);
book.setQuantity(parseInt(strArray[4]));
book.setDate(strArray[5]);
book.setPress(strArray[6]);
array.add(book);
}
br.close();
fr.close();
return array;
}
public static ArrayList inreader() throws IOException {
FileReader fr = new FileReader("myFile\\reader.txt");
BufferedReader br = new BufferedReader(fr);
ArrayList<Reader> array = new ArrayList<Reader>();
String line;
while ((line = br.readLine()) != null) {
String[] strArray = line.split(",");
Reader reader = new Reader();
reader.setNum(Integer.parseInt(strArray[0]));
reader.setName(strArray[1]);
reader.setCollege(strArray[2]);
reader.setClass(strArray[3]);
array.add(reader);
}
br.close();
fr.close();
return array;
}
public static ArrayList inborrow() throws IOException {
FileReader fr = new FileReader("myFile\\borrow.txt");
BufferedReader br = new BufferedReader(fr);
ArrayList<Borrow> array = new ArrayList<Borrow>();
String line;
while ((line = br.readLine()) != null) {
String[] strArray = line.split(",");
Borrow borrow = new Borrow();
borrow.setNum1(parseInt(strArray[0]));
borrow.setName(strArray[1]);
borrow.setNum2(parseInt(strArray[2]));
borrow.setBookname(strArray[3]);
borrow.setBorrowdate(strArray[4]);
borrow.setShouldreturndate(strArray[5]);
borrow.setReturndate(strArray[6]);
array.add(borrow);
}
br.close();
fr.close();
return array;
}
}
操作
1.添加
package functions;
import EntityClass.Book;
import EntityClass.Reader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Add {
//图书信息添加
public static void AddBook(ArrayList<Book>arrayList) throws IOException,ClassNotFoundException{
ArrayList<Book> array1=new ArrayList<Book>();
File file =new File("myFile");
file.mkdirs();
FileWriter fw=new FileWriter("myFile\\book.txt",true);
Scanner sc=new Scanner(System.in);
System.out.println("请输入书名:");
String name=sc.nextLine();
fw.write(name);
System.out.println("请输入书号:");
String n = sc.nextLine();
int num=Integer.parseInt(n);
fw.write(num);
System.out.println("请输入作者:");
String author=sc.nextLine();
fw.write(author);
System.out.println("请输入出版社:");
String press=sc.nextLine();
fw.write(press);
System.out.println("请输入出版时间:");
String date=sc.nextLine();
fw.write(date);
System.out.println("请输入数量:");
int quantity=sc.nextInt();
fw.write(date);
System.out.println("请输入价格:");
sc.nextLine();
String price=sc.nextLine();
fw.write(price);
Book s=new Book();
s.setName(name);
s.setAuthor(author);
s.setDate(date);
s.setNum(num);
s.setPress(press);
s.setPrice(price);
s.setQuantity(quantity);
array1.add(s);
for(int i=0;i<array1.size();i++) {
Book a=new Book();
a=array1.get(i);
fw.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("添加图书成功!");
fw.close();
}
//读者信息添加
public static void AddReader(ArrayList<Reader>arrayList) throws IOException,ClassNotFoundException{
ArrayList<Reader> array1=new ArrayList<Reader>();
File file =new File("myFile");
file.mkdirs();
FileWriter fw=new FileWriter("myFile\\reader.txt",true);
Scanner sc=new Scanner(System.in);
System.out.println("请输入姓名:");
String name=sc.nextLine();
System.out.println("请输入学号:");
String n = sc.nextLine();
int num=Integer.parseInt(n);
System.out.println("请输入学院:");
String college=sc.nextLine();
System.out.println("请输入专业班级:");
String Classs=sc.nextLine();
Reader s=new Reader();
s.setName(name);
s.setNum(num);
s.setCollege(college);
s.setClass(Classs);
array1.add(s);
for(int i=0;i<array1.size();i++) {
Reader a=new Reader();
a=array1.get(i);
fw.write(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getClasss()+"\r\n");
}
System.out.println("添加读者信息成功!");
fw.close();
}
}
2.修改
package functions;
import EntityClass.Book;
import EntityClass.Reader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Change {
public static void sentakuchange1() throws IOException{
System.out.println("您要选择哪种方法进行修改:"+"\n"+"1、使用书名\n2、使用书号");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
change1();
break;
case 2:
change2();
break;
}
}
public static void change1() throws IOException{
//按照书籍名修改书籍
System.out.println("请输入要修改的书名:");
ArrayList<Book> array1=io.inbook();
ArrayList<Book> array2 = null;
boolean bool=false;
int n=0;
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
for(int i=0;i<array1.size();i++)
{
Book a=new Book();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
System.out.println("请输入修改后的书名:");
String name=sc.nextLine();
System.out.println("请输入修改后的书号(仅能输入数字):");
String h = sc.nextLine();
int num=Integer.parseInt(h);
System.out.println("请输入修改后的作者:");
String author=sc.nextLine();
System.out.println("请输入修改后的出版社:");
String press=sc.nextLine();
System.out.println("请输入修改后的出版时间:");
String date=sc.nextLine();
System.out.println("请输入修改后的数量:");
int quantity=sc.nextInt();
System.out.println("请输入修改后的价格:");
String price=sc.nextLine();
Book s=new Book();
s.setName(name);
s.setAuthor(author);
s.setDate(date);
s.setNum(num);
s.setPress(press);
s.setPrice(price);
s.setQuantity(quantity);
array1.set(n,s);
FileWriter fr=new FileWriter("myFile\\book.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
Book a=new Book();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("修改图书信息成功!");
System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
}
public static void change2() throws IOException{
//按照书号修改书籍
System.out.println("请输入要修改的书号(仅能输入数字):");
ArrayList<Book> array1=io.inbook();
ArrayList<Book> array2 = null;
boolean bool=false;
int n=0;
Scanner sc=new Scanner(System.in);
int line=sc.nextInt();
for(int i=0;i<array1.size();i++)
{
Book a=new Book();
a=array1.get(i);
if(a.getNum()==line){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
array2.add(a);
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
System.out.println("请输入修改后的书名:");
String name=sc.nextLine();
System.out.println("请输入修改后的书号(仅能输入数字):");
int num=sc.nextInt();
System.out.println("请输入修改后的作者:");
String author=sc.nextLine();
System.out.println("请输入修改后的出版社:");
String press=sc.nextLine();
System.out.println("请输入修改后的出版时间:");
int quantity=sc.nextInt();
System.out.println("请输入修改后的数量:");
String date=sc.nextLine();
System.out.println("请输入修改后的价格:");
String price=sc.nextLine();
Book s=new Book();
s.setName(name);
s.setAuthor(author);
s.setDate(date);
s.setNum(num);
s.setPress(press);
s.setPrice(price);
s.setQuantity(quantity);
array1.set(n,s);
FileWriter fr=new FileWriter("myFile\\book.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
Book a=new Book();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("修改图书信息成功!");
System.out.println("修改后的书籍信息:\n"+s.getNum() + "," + s.getName() + "," + s.getAuthor() + "," + s.getPrice() + "," + s.getQuantity() + "," + s.getDate() + "," + s.getPress());
br.close();
fr.close();
}
public static void change3() throws IOException{
//按照学号+姓名修改信息
ArrayList<Reader> array1=io.inreader();
ArrayList<Reader> array2 = null;
boolean bool=false;
int n=0;
Scanner sc=new Scanner(System.in);
System.out.println("请输入要修改的学号(仅能输入数字):");
int nu=sc.nextInt();
System.out.println("请输入要修改的姓名:");
String l=sc.nextLine();
String line=sc.nextLine();
for(int i=0;i<array1.size();i++)
{
Reader a=new Reader();
a=array1.get(i);
if(a.getName().equals(line)&&a.getNum()==nu){
System.out.println("您的信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
return;
}
System.out.println("请输入修改后的姓名:");
String name=sc.nextLine();
System.out.println("请输入修改后的学号(仅能输入数字):");
int h = Integer.parseInt(sc.nextLine());
System.out.println("请输入修改后的学院:");
String College=sc.nextLine();
System.out.println("请输入修改后的专业班级:");
String Classs=sc.nextLine();
Reader s=new Reader();
s.setName(name);
s.setNum(h);
s.setCollege(College);
s.setClass(Classs);
array1.set(n,s);
FileWriter fr=new FileWriter("myFile\\reader.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
Reader a=new Reader();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getClasss()+"\r\n");
}
System.out.println("修改读者信息成功!");
System.out.print("修改后的读者信息:\n"+s.getNum() + "," + s.getName() + "," + s.getCollege() + "," + s.getClasss() );
br.close();
fr.close();
}
}
3.查找
package functions;
import EntityClass.Book;
import EntityClass.Borrow;
import EntityClass.Reader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Find {
public static void findbook(ArrayList<Book> arrayList) throws IOException {
System.out.println("1、按书名查询");
System.out.println("2、按作者名查询");
System.out.println("3、按出版社查询");
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
switch (s) {
case 1:
findbook1();
break;
case 2:
findbook2();
break;
case 3:
findbook3();
break;
}
}
public static void findbook1() throws IOException{
System.out.println("请输入书名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<Book> array1=io.inbook();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
Book a=new Book();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
}
}
public static void findbook2() throws IOException{
System.out.println("请输入作者名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<Book> array1=io.inbook();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
Book a=new Book();
a=array1.get(i);
if(a.getAuthor().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
}
}
public static void findbook3() throws IOException{
System.out.println("请输入出版社:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<Book> array1=io.inbook();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
Book a=new Book();
a=array1.get(i);
if(a.getPress().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
}
}
public static void findreader(ArrayList<Reader> arrayList) throws IOException {
System.out.println("1、按学号查询");
System.out.println("2、按姓名查询");
System.out.println("3、按专业班级查询");
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
switch (s) {
case 1:
findreader1();
break;
case 2:
findreader2();
break;
case 3:
findreader3();
break;
}
}
public static void findreader1() throws IOException{
System.out.println("请输入学号:");
Scanner sc=new Scanner(System.in);
String lin=sc.nextLine();
int line=Integer.parseInt(lin);
ArrayList<Reader> array1=io.inreader();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
Reader a=new Reader();
a=array1.get(i);
if(a.getNum()==line){
System.out.println("您要找的学生信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
}
}
public static void findreader2() throws IOException{
System.out.println("请输入姓名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<Reader> array1=io.inreader();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
Reader a=new Reader();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的学生信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
}
}
public static void findreader3() throws IOException{
System.out.println("请输入专业班级:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<Reader> array1=io.inreader();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
Reader a=new Reader();
a=array1.get(i);
if(a.getClasss().equals(line)){
System.out.println("您要找的学生信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
}
}
public static void findborrow(ArrayList<Borrow> arrayList) throws IOException {
System.out.println("1、按学号查询");
System.out.println("2、按书名查询");
System.out.println("3、按书号查询");
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
switch (s) {
case 1:
findborrow1();
break;
case 2:
findborrow2();
break;
case 3:
findborrow2();
break;
}
}
public static void findborrow1() throws IOException{
System.out.println("请输入学号:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<Borrow> array1=io.inborrow();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
Borrow a=new Borrow();
a=array1.get(i);
if(a.getNum1()==Integer.parseInt(line)){
System.out.println("您要找的书籍是:\n"+a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的借阅信息不存在!");
}
}
public static void findborrow2() throws IOException{
System.out.println("请输入书名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<Borrow> array1=io.inborrow();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
Borrow a=new Borrow();
a=array1.get(i);
if(a.getBookname().equals(line)){
System.out.println("您要查询的借阅信息是:\n"+a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的借阅信息不存在!");
}
}
public static void findborrow3() throws IOException{
System.out.println("请输入书号:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<Book> array1=io.inborrow();
boolean bool=false;
for(int i=0;i<array1.size();i++)
{
Borrow a=new Borrow();
a=array1.get(i);
if(a.getNum2()==Integer.parseInt(line)){
System.out.println("您要查询的借阅信息是:\n"+a.getNum1()+","+a.getName()+","+a.getNum2()+","+a.getBookname()+","+a.getBorrowdate()+","+a.getShouldreturndate()+","+a.getReturndate());
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的借阅信息不存在!");
}
}
}
4.排序
package functions;
import EntityClass.Book;
import EntityClass.Reader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class sort {
public static void sentakusort() throws IOException {
System.out.println("您要选择哪种方法进行修改:"+"\n"+"1、使用书号\n2、使用书名");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
sortingBook1(io.inbook());
break;
case 2:
sortingBook2(io.inbook());
break;
}
}
//使用书号排列输出
public static void sortingBook1(ArrayList<Book> array) {
int[] arr = new int[100];
for (int i = 0; i < array.size(); i++) {
Book b = array.get(i);
arr[i] = b.getNum();
}
for (int i1 = 0; i1 < array.size() - 1; i1++) {
for (int j = 0; j < array.size() - 1 - i1; j++) {
Book book1 = array.get(j);
Book book2 = array.get(j+1);
if (book1.getNum()>book2.getNum()) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
Collections.swap(array, j, j + 1);
}
}
}
System.out.println("已完成排序+\n排序后的列表:\n");
for (int i = 0; i < array.size(); i++) {
Book a = array.get(i);
System.out.println(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress());
}
}
//使用书名进行排序
public static void sortingBook2(ArrayList<Book> array) {
int[] arr = new int[100];
for (int i = 0; i < array.size(); i++) {
Book b = array.get(i);
arr[i] = b.getNum();
}
for (int i1 = 0; i1 < array.size() - 1; i1++) {
for (int j = 0; j < array.size() - 1 - i1; j++) {
Book book1 = array.get(j);
Book book2 = array.get(j+1);
int bool= book1.getName().compareTo(book2.getName());
if (bool>0) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
Collections.swap(array, j, j + 1);
}
}
}
System.out.println("已完成排序\n排序后的列表:\n");
for (int i = 0; i < array.size(); i++) {
Book a = array.get(i);
System.out.println(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress());
}
}
public static void sentakusortreader() throws IOException {
System.out.println("您要选择哪种方法进行修改:"+"\n"+"1、使用学号\n2、使用学院");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
sortingreader1(io.inreader());
break;
case 2:
sortingreader2(io.inreader());
break;
}
}
public static void sortingreader1(ArrayList<Reader> array) { //使用学号排列输出
int[] arr = new int[100];
for (int i = 0; i < array.size(); i++) {
Book b = array.get(i);
arr[i] = b.getNum();
}
for (int i1 = 0; i1 < array.size() - 1; i1++) {
for (int j = 0; j < array.size() - 1 - i1; j++) {
Reader book1 = array.get(j);
Reader book2 = array.get(j+1);
if (book1.getNum()>book2.getNum()) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
Collections.swap(array, j, j + 1);
}
}
}
System.out.println("已完成排序\n排序后的列表:\n");
for (int i = 0; i < array.size(); i++) {
Reader a = array.get(i);
System.out.println(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getCollege());
}
}
public static void sortingreader2(ArrayList<Reader> array) {//使用学院进行排序
int[] arr = new int[100];
for (int i = 0; i < array.size(); i++) {
Book b = array.get(i);
arr[i] = b.getNum();
}
for (int i1 = 0; i1 < array.size() - 1; i1++) {
for (int j = 0; j < array.size() - 1 - i1; j++) {
Reader book1 = array.get(j);
Reader book2 = array.get(j+1);
int bool= book1.getName().compareTo(book2.getName());
if (bool>0) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
Collections.swap(array, j, j + 1);
}
}
}
System.out.println("已完成排序\n排序后的列表:\n");
for (int i = 0; i < array.size(); i++) {
Reader a = array.get(i);
System.out.println(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getCollege());
}
}
}
5.删除
package functions;
import EntityClass.Book;
import EntityClass.Reader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Delete {
public static void sentakudelete1() throws IOException{
System.out.println("您要选择哪种方法进行删除?:"+"\n"+"1、使用书名\n2、使用书号");
int s;
Scanner sc = new Scanner(System.in);
while (true)
{
s = sc.nextInt();
if(s!=1&&s!=2)
System.out.println("输入有误,请重新输入");
else break;
}
switch (s){
case 1:
detelebook1();
break;
case 2:
detelebook2();
break;
}
}
public static void detelebook1() throws IOException{
//用书名删除书籍
System.out.println("请输入书名:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
ArrayList<Book> array1=io.inbook();
boolean bool=false;
int n=0;
for(int i=0;i<array1.size();i++)
{
Book a=new Book();
a=array1.get(i);
if(a.getName().equals(line)){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
array1.remove(n);
System.out.println("删除成功!");
FileWriter fr=new FileWriter("myFile\\book.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
Book a=new Book();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("修改图书信息成功!");
br.close();
fr.close();
}
public static void detelebook2() throws IOException{//用书号删除书籍
System.out.println("请输入书号:");
Scanner sc=new Scanner(System.in);
int line=sc.nextInt();
ArrayList<Book> array1=io.inbook();
boolean bool=false;
int n=0;
for(int i=0;i<array1.size();i++)
{
Book a=new Book();
a=array1.get(i);
if(a.getNum()==line){
System.out.println("您要找的书籍是:\n"+a.getNum() + "," + a.getName() + "," + a.getAuthor() + "," + a.getPrice() + "," + a.getQuantity() + "," + a.getDate() + "," + a.getPress());
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的书籍不存在!");
return;
}
ArrayList<Book> array2=new ArrayList<>();
for (int i = 0; i < array1.size() - 1; i++) {
if (i < n) {
array2.set(i, array1.get(i));
} else {
array2.set(i, array1.get(i + 1));
}
}
array1.remove(n);
System.out.println("删除成功!");
FileWriter fr=new FileWriter("myFile\\book.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
Book a=new Book();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getAuthor()+","+a.getPrice()+","+a.getQuantity()+","+a.getDate()+","+a.getPress()+"\r\n");
}
System.out.println("修改图书信息成功!");
br.close();
fr.close();
}
public static void detelebook3() throws IOException{//按照学号+姓名删除信息
ArrayList<Reader> array1=io.inreader();
ArrayList<Reader> array2 = null;
boolean bool=false;
int n=0;
Scanner sc=new Scanner(System.in);
System.out.println("请输入学号:");
int nu=sc.nextInt();
System.out.println("请输入姓名:");
String l=sc.nextLine();
String line=sc.nextLine();
for(int i=0;i<array1.size();i++)
{
Reader a=new Reader();
a=array1.get(i);
if(a.getName().equals(line)&&a.getNum()==nu){
System.out.println("您的信息是:\n"+a.getNum() + "," + a.getName() + "," + a.getCollege() + "," + a.getClasss());
n=i;
bool=true;
}
}
if(bool==false){
System.out.println("您要查找的学生信息不存在!");
return;
}
array1.remove(n);
System.out.println("删除成功!");
FileWriter fr=new FileWriter("myFile\\reader.txt");
BufferedWriter br=new BufferedWriter(fr);
for(int i=0;i<array1.size();i++) {
Reader a=new Reader();
a=array1.get(i);
br.write(a.getNum()+","+a.getName()+","+a.getCollege()+","+a.getClasss()+"\r\n");
}
System.out.println("修改读者信息成功!");
br.close();
fr.close();
}
}
基本类
1.Book
package EntityClass;
public class Book extends Borrow {
private String name;//书名
private String author;//作者
private String price;//价格
private int num;//书号
private String press;//出版社
private String date;//出版日期
private int quantity;//存馆数量
public Book() {
}
public Book(String name, String author, String price, int num, String press, String date, int quantity) {
this.name = name;
this.author = author;
this.price = price;
this.num = num;
this.press = press;
this.date = date;
this.quantity = quantity;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getPrice() {
return price;
}
public int getNum() {
return num;
}
public String getPress() {
return press;
}
public String getDate() {
return date;
}
public int getQuantity() {
return quantity;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(String price) {
this.price = price;
}
public void setNum(int num) {
this.num = num;
}
public void setPress(String press) {
this.press = press;
}
public void setDate(String date) {
this.date = date;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
2.Borrow
package EntityClass;
public class Borrow {
private String name;//姓名
private int num1; //学号
private int num2;//书号
private String bookname; //书名
private String borrowdate;//借阅日期
private String Shouldreturndate;//应还日期
private String returndate;//实际归还日期
public Borrow() {
}
public Borrow(String name, int num1, int num2, String bookname, String borrowdate, String shouldreturndate, String returndate) {
this.name = name;
this.num1 = num1;
this.num2 = num2;
this.bookname = bookname;
this.borrowdate = borrowdate;
Shouldreturndate = shouldreturndate;
this.returndate = returndate;
}
public String getName() {
return name;
}
public int getNum1() {
return num1;
}
public int getNum2() {
return num2;
}
public String getBookname() {
return bookname;
}
public String getBorrowdate() {
return borrowdate;
}
public String getShouldreturndate() {
return Shouldreturndate;
}
public String getReturndate() {
return returndate;
}
public void setName(String name) {
this.name = name;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public void setNum2(int num2) {
this.num2 = num2;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public void setBorrowdate(String borrowdate) {
this.borrowdate = borrowdate;
}
public void setShouldreturndate(String shouldreturndate) {
Shouldreturndate = shouldreturndate;
}
public void setReturndate(String returndate) {
this.returndate = returndate;
}
}
3.Reader
package EntityClass;
public class Reader extends Book {
private String name;//姓名
private int num;//学号
private String college;//学院专业
private String Classs;//班级
public Reader(String name, int num, String college, String aClass) {
this.name = name;
this.num = num;
this.college = college;
Classs = aClass;
}
public Reader() {
}
public String getName() {
return name;
}
public int getNum() {
return num;
}
public String getCollege() {
return college;
}
public String getClasss() {
return Classs;
}
public void setName(String name) {
this.name = name;
}
public void setNum(int num) {
this.num = num;
}
public void setCollege(String college) {
this.college = college;
}
public void setClass(String aClass) {
Classs = aClass;
}
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/10120.html