赞
踩
目录
【类的说明】:
Library类:写了登陆系统。
Main类:Main中的主函数开启整个项目。
各个Person类:定义的人的属性,并写了属性的set,get方法。
各个operate接口:操作的接口。
Root类:继承了Person类,实现operate接口
reader类:继承了Person02类,实现operate02接口
User类:继承了Person03类,实现operate03接口
Book类:定义了书的一些属性,并且写了属性的get,set方法
Student类:定义了学生的一些属性,并且写了属性的get,set方法
Actor类:定义了借书者的一些属性,并且写了属性的get,set方法
声明:本项目是属于面向对象的编程,用到对象,类,抽象类,接口,继承,实现,数组及数组的操作,for循环,while循环,switch语句,if语句
【实现的功能介绍】:
- import java.util.Scanner;
-
- public class Library {
- Scanner sc = new Scanner(System.in);
- //登录函数:显示登录界面进行选择登录
- public void login() {
- int count = 0;
- Person p = null;
- Person02 p2=null;
- Person03 p3=null;
-
- while (count < 2) {
- System.out.println("===============山农图书管理系统===============");
-
- System.out.println("请登录: 1.管理员登录 2.退出系统");
-
- int n = sc.nextInt();
- //通过输入的值进行登录判断
- if (n == 2) {
- return;
- }
- switch (n) {
-
- case 1:
- System.out.println("请输入管理员姓名:");
- String Rname = sc.next();
- System.out.println("请输入性别:");
- String Rsex = sc.next();
- System.out.println("请输入年龄:");
- int Rage = sc.nextInt();
-
- p = new Root(Rname,Rsex,Rage);//构造管理员对象
- //
- // System.out.println("请输入管理员姓名:");
- // String rname = sc.next();
- // System.out.println("请输入性别:");
- // String rsex = sc.next();
- // System.out.println("请输入年龄:");
- // int rage = sc.nextInt();
- p2 = new reader(Rname,Rsex,Rage);
- p3 = new User(Rname,Rsex,Rage);
-
-
-
- System.out.println("登录成功!");
- System.out.println("当前管理员:"+"姓名:" + p.getName() + " "+"性别:" + p.getSex() + " "+"年龄" + p.getAge());
-
-
- while(true){
- System.out.println("请输入你的操作:1.图书信息 2.读者信息 3.图书借阅");
- int m=sc.nextInt();
-
- if (m == 1) {
- System.out.println("请输入你的操作: 1.整理书籍 2.查阅书籍 3.增加书籍 4.删除书籍 5.显示书籍列表 6.退出");
- int j = sc.nextInt();
- if (j == 6) {
- System.out.println("您已成功退出!");
- break;
- } else {
- p.operate();//调用管理员的操作方法
- }
- }
-
- if (m == 2) {
- System.out.println("请输入你的操作:1.查询读者信息 2.读者信息修改,删除 3.添加读者信息 4.显示学生列表 5.退出" );
- int q=sc.nextInt();
- if(q==5){
- System.out.println("你已经成功退出!");
- break;
- } else{
- p2.operate02();
- }
- }
- if(m==3) {
- System.out.println("请输入你的操作:1.借阅查询 2.图书借阅 3.图书归还 4.图书借阅表 5.退出 ");
- int w=sc.nextInt();
- if(w==5){
- System.out.println("你已经成功退出!");
- break;
- }else{
- p3.operate03();
- }
-
-
- }
-
-
-
- }
-
-
- }
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Library library = new Library(); //构造Library对象
- library.login(); //调用对象的登录方法。
- }
- }
- abstract class Person {
- public String name;
- public String sex;
- public int age;
- /**
- * 默认定义6本书籍。(预留3本新书的buffer,用来添加书籍)
- */
- public static Book[] books = new Book[]{
- new Book(101,"西游记", "吴承恩","凤凰出版社","2003年9月1号",10,88,"名著", true),
- new Book(102,"红楼梦", "曹雪芹","凤凰出版社","2003年9月1号",11, 88, "名著", true),
- new Book(103,"三国演义", "罗贯中","凤凰出版社","2003年9月1号" ,12,88, "名著", true),
- new Book(104,"水浒传", "范仲淹","凤凰出版社","2003年9月1号",13, 88, "名著", true),
- new Book(105,"C++语言基础", "周老师", "电子工业出版社","2003年9月2号",14,35, "计算机", true),
- new Book(106,"Java编程思想", "王老师","电子工业出版社","2003年9月2号",15, 55, "计算机", true),
-
- new Book(),
- new Book(),
- new Book(),
- };
-
- public Person(String name, String sex, int age) {
- this.age = age;
- this.name = name;
- this.sex = sex;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- /**
- * 抽象类中的抽象方法
- */
- public abstract void operate();
-
-
- }
- abstract class Person02 {
- public String name;
- public String sex;
- public int age;
-
- /*
- 定义四名学生,预留三个学生空位用于添加
- */
-
- public static Student[] students=new Student[]{
- new Student(201,"张三","信息学院","计算机类","一班"),
- new Student(202,"李四","信息学院","计算机类","二班"),
- new Student(203,"王五","信息学院","计算机类","三班"),
- new Student(204,"李华","信息学院","计算机类","四班"),
-
- new Student(),
- new Student(),
- new Student(),
-
-
- };
-
- public Person02(String name, String sex, int age) {
- this.age = age;
- this.name = name;
- this.sex = sex;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- /**
- * 抽象类中的抽象方法
- */
- public abstract void operate02();
- }
-
- abstract class Person03 {
- public String name;
- public String sex;
- public int age;
-
- /*
- 定义四名学生,预留三个学生空位用于添加
- */
-
- public static Actor[] actors=new Actor[]{
- new Actor(201,"张三",101,"红楼梦","2020年9月1号","2020年10月1日","2021年9月15号"),
- new Actor(202,"李四",102,"水浒传","2020年9月1号","2020年10月1日","2021年9月15号"),
- new Actor(203,"王五",103,"红楼梦","2020年9月1号","2020年10月1日","2021年9月15号"),
-
-
-
-
- };
-
- public Person03(String name, String sex, int age) {
- this.age = age;
- this.name = name;
- this.sex = sex;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- /**
- * 抽象类中的抽象方法
- */
- public abstract void operate03();
- }
-
- public interface operate { //操作的接口
- void select(Book[] books); //查询,查阅
-
- void delete(Book[] books);//删除,借阅
-
- void add(Book[] books); //增加,还书
-
- void list(Book[] books); //显示书列表
-
-
-
- }
- public interface operate02 {
- void select01(Student[] students); //查询,查阅
-
- void delete01(Student[] students);//删除,借阅
-
- void add01(Student[] students); //增加
-
- void list01(Student[] students); //显示学生列表
- }
-
- public interface operate03 {
- void select02(Actor[] actors); //查询,查阅
-
- void borrow02(Actor[] actors);//借阅
-
- void return02(Actor[] actors); //归还
-
- void list02(Actor[] actors); //列表
- }
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Scanner;
-
- public class reader extends Person02 implements operate02{
- public reader(String name, String sex, int age) {
- //调用人基类的构造函数
- super(name, sex, age);
- }
-
- Scanner sc = new Scanner(System.in);
- /*
-
- 管理员的操作,读者信息
- */
-
- public void operate02() {
- System.out.print("请输入确认操作:");
- int i = sc.nextInt();
- switch (i) {
- case 1:
- //查
- select01(students);
- break;
- case 2:
- //删
- delete01(students);
- break;
- case 3:
- //加
- add01(students);
- break;
- case 4:
- //列表
- list01(students);
- break;
-
- default:
- System.out.println("输入有误!");
- break;
-
- }
- }
-
-
-
- //查询学生信息
-
- public void select01(Student[] students){
-
- System.out.println("请输入学生名进行查询:");
- String name = sc.next();
- if (name != null) {
- boolean flag = false;
- int studentNum = 0;
- for (int i = 0; i < students.length; i++) {
- if (students[i].getName() != null && name.equals(students[i].getName())) {
- flag = true;
- studentNum = i;
- break;
- }
- }
- if (!flag) {
- System.out.println("未查询到此学生!");
- } else {
- System.out.println("学生信息为:" + "\n" + "【学号】---【性名】—— 【学院】——【专业】---【班级】");
- System.out.println(
- "【"+ students[studentNum].getNum()+"】---【" + students[studentNum].getName() + "】—— " +
- "【" + students[studentNum].getName() + "】---【"+students[studentNum].getColl()+"】---【" + students[studentNum].getMajor()+"】---" +
- "【"+students[studentNum].getClb()+"】"
-
- );
- }
- }
- }
-
-
- //读者信息删除修改
- public void delete01(Student[] students){
- System.out.println("请输入要删除的学生姓名:");
- String str = sc.next();
- if (str != null) {
- for (int i = 0; i < students.length; i++) {
- if (students[i].getName().equals(str)) {
- students[i] = null;
- System.out.println("删除成功!");
- break;
- }
- }
- }
-
-
- }
-
- //添加读者信息
- public void add01(Student[] students) {
- System.out.println("请输入学号:");
- int num = sc.nextInt();
- System.out.println("请输入学生名:");
- String name = sc.next();
- System.out.println("请输入学院:");
- String coll = sc.next();
- System.out.println("请输入专业:");
- String major = sc.next();
- System.out.println("请输入班级:");
- String clb = sc.next();
-
-
-
- boolean flag = false;
- for (int i = 0; i < students.length; i++) {
- if (students[i].getName() == null) {
- flag = true;
- students[i].setNum(num);
- students[i].setName(name);
- students[i].setColl(coll);
- students[i].setMajor(major);
- students[i].setClb(clb);
- System.out.println("学号:" +num+" "+"学生名:"+name+" "+"学院:"
- +coll+" "+ "专业:"+major+" "+"班级:"+clb);
- System.out.println("添加成功");
- break;
- }
- }
- if (!flag) {
- System.out.println("【警告】数组预留buffer最多只能添加3本新书,没有位置放置新书了,如需添加多余3本,需要在‘Person’类中添加buffer!" + "\n");
- }
- }
-
- //列出学生列表
- public void list01(Student[] students) {
- System.out.println("图书列表如下:" + "\n" + "【学号】---【学生姓名】—— 【学院】——【专业】---【班级】"); //打印Student数组
- for (int i = 0; i < students.length; i++) {
- if (students[i] != null) {
- if (students[i].num != 0) {
- System.out.println(
- "【"+students[i].getNum()+"】---【" + students[i].getName() + "】—— 【" + students[i].getColl() + "】——" +
- "【"+students[i].getMajor()+"】---【"+students[i].getClb()+"】");
-
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- }
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Scanner;
-
- public class Root extends Person implements operate {
- //继承了人的类,需要重写它的抽象方法operate,实现了operate接口,需要重写接口内的方法
-
- public Root(String name, String sex, int age) {
- //调用人基类的构造函数
- super(name, sex, age);
- }
-
- Scanner sc = new Scanner(System.in);
-
- /**
- * 管理员的操作,图书信息
- */
- @Override
- public void operate() {
- System.out.print("请输入确认操作:");
- int i = sc.nextInt();
- switch (i) {
- case 1:
- //整理
- sort(books);
- break;
- case 2:
- //查
- select(books);
- break;
- case 3:
- //加
- add(books);
- break;
- case 4:
- //删
- delete(books);
- break;
- case 5:
- //列表
- list(books);
- break;
- default:
- System.out.println("输入有误!");
- break;
-
- }
- }
-
- /**
- * compareable整理,重写compare to,如果数组中有空的,则会出现空指针异常,把不空的拷到另一个新的数组,然后实现
- * @param books
- */
- public void sort(Book[] books) {
- System.out.println("图书馆中的书按价格整理如下:" + "\n" + "【书号】---【书名】—— 【作者】——【出版社】---【出版日期】---【存馆数量】---【定价】");
- Arrays.sort(books, new Comparator<Book>() {
- @Override
- public int compare(Book o1, Book o2) {
- return (int) (o1.getPrice() - o2.getPrice());
- }
- });
- //遍历数组打印书的列表
- for (int i = 0; i < books.length; i++) {
- if (books[i].price != 0) {
- System.out.println(
- "【"+books[i].getNum()+"】---【" + books[i].getName() + "】—— 【" + books[i].getAuthor() + "】——" +
- "【"+books[i].getPress()+"】---【"+books[i].getDate()+"】---【"+books[i].getTotal()+"】---【" + books[i].getPrice()
- + "】——【" + books[i].getCategory() + "】——" + books[i].getState());
- }
- }
- }
-
- @Override
- public void select(Book[] books) {
- System.out.println("请输入书名进行查询:");
- String name = sc.next();
- if (name != null) {
- boolean flag = false;
- int bookNum = 0;
- for (int i = 0; i < books.length; i++) {
- if (books[i].getName() != null && name.equals(books[i].getName())) {
- flag = true;
- bookNum = i;
- break;
- }
- }
- if (!flag) {
- System.out.println("未查询到此书籍!");
- } else {
- System.out.println("书籍信息为:" + "\n" + "【书号】---【书名】—— 【作者】——【出版社】---【出版日期】---【存馆数量】---【定价】");
- System.out.println(
- "【"+ books[bookNum].getNum()+"】---【" + books[bookNum].getName() + "】—— " +
- "【" + books[bookNum].getAuthor() + "】---【"+books[bookNum].getPress()+"】---【" + books[bookNum].getDate()+"】---" +
- "【"+books[bookNum].getTotal()+"】" +
- "——【" + books[bookNum]
- .getPrice() + "】——【" + books[bookNum].getCategory() + "】——" + books[bookNum]
- .getState());
- }
- }
- }
-
- @Override
- public void delete(Book[] books) {
- System.out.println("请输入要删除的书名:");
- String str = sc.next();
- if (str != null) {
- for (int i = 0; i < books.length; i++) {
- if (books[i].getName().equals(str)) {
- books[i] = null;
- System.out.println("删除成功!");
- break;
- }
- }
- }
-
- }
-
- @Override
- public void add(Book[] books) {
- System.out.println("请输入书号:");
- int num = sc.nextInt();
- System.out.println("请输入书名:");
- String name = sc.next();
- System.out.println("请输入作者:");
- String author = sc.next();
- System.out.println("请输入出版社:");
- String press = sc.next();
- System.out.println("请输入存馆数量:");
- int total = sc.nextInt();
- System.out.println("请输入价格:");
- double price = sc.nextDouble();
-
-
- boolean flag = false;
- for (int i = 0; i < books.length; i++) {
- if (books[i].getName() == null) {
- flag = true;
-
- books[i].setName(name);
- books[i].setAuthor(author);
- books[i].setPress(press);
- books[i].setTotal(total);
- books[i].setPrice(price);
- books[i].setState(true);
- System.out.println("添加成功");
- break;
- }
- }
- if (!flag) {
- System.out.println("【警告】数组预留buffer最多只能添加3本新书,没有位置放置新书了,如需添加多余3本,需要在‘Person’类中添加buffer!" + "\n");
- }
- }
-
- @Override
- public void list(Book[] books) {
- System.out.println("图书列表如下:" + "\n" + "【书号】---【书名】—— 【作者】——【出版社】---【出版日期】---【存馆数量】---【定价】"); //打印Book数组
- for (int i = 0; i < books.length; i++) {
- if (books[i] != null) {
- if (books[i].price != 0) {
- System.out.println(
- "【"+books[i].getNum()+"】---【" + books[i].getName() + "】—— 【" + books[i].getAuthor() + "】——" +
- "【"+books[i].getPress()+"】---【"+books[i].getDate()+"】---【"+books[i].getTotal()+"】---【" + books[i].getPrice()
- + "】——【" + books[i].getCategory() + "】——" + books[i].getState());
-
- }
- }
- }
- }
- }
- import java.util.Scanner;
-
- public class User extends Person03 implements operate03 { //user类继承了人的类, 实现operate接口
-
- public User(String name, String sex, int age) {
- //调用人基类的构造函数(派生类不继承基类的构造函数)
- super(name, sex, age);
- }
-
- Scanner sc = new Scanner(System.in);
-
- @Override
- public void operate03() { //普通用户的操作
-
- System.out.print("请输入确认操作:");
- int i = sc.nextInt();
- switch (i) {
- case 1:
- //查
- select02(actors);
- break;
- case 2:
- //借
- borrow02(actors);
- break;
- case 3:
- //
- return02(actors);
- break;
- case 4:
- //列表
- list02(actors);
- break;
-
- default:
- System.out.println("输入有误!");
- break;
- }
- }
-
- @Override
- public void select02(Actor[] actors) {
- System.out.println("请输入书名进行查询:");
- String bookme = sc.next();
- if (bookme != null) {
- boolean flag = false;
- int actorNum = 0;
- for (int i = 0; i < actors.length; i++) {
- if (actors[i].getBookme() != null && bookme.equals(actors[i].getBookme())) {
- flag = true;
- actorNum = i;
- break;
- }
- }
- if (!flag) {
- System.out.println("未查询到此书籍!");
- } else {
- System.out.println("书籍信息为:" + "\n" + "【学号】---【姓名】—— 【书号】——【书名】---【借阅日期】---【应还日期】---【归还日期】");
- System.out.println(
- "【" + actors[actorNum].getNum() + "】—— 【" + actors[actorNum].getName() + "】——【" + actors[actorNum]
- .getVolume() + "】——【" +actors[actorNum].getBookme() + "】——" +actors[actorNum]
- .getBodate()+"】——【"+actors[actorNum].getSrdate()+"】——【"+actors[actorNum].getRedate()+" 】"+actors[actorNum].getState() );
- }
- }
- }
-
- @Override
- public void borrow02(Actor[] actors) {
- System.out.println("请输入要借阅的书名:");
- String bookme = sc.next();
- if (bookme != null) {
- for (int i = 0; i < actors.length - 1; i++) {
- if (actors[i].getBookme().equals(bookme)) {
- //借走书时设置状态为false
- if (!actors[i].getState()) {
- System.out.println("【" + bookme + "】" + "已经被借出!");
- } else {
- actors[i].setState(false);
- System.out.println("借阅成功!");
- }
- break;
- }
- }
- }
- }
-
- @Override
- public void return02(Actor[] actors) {
-
- System.out.println("请输入要还的书名:");
- String bookme = sc.next();
-
- if (bookme != null) {
- boolean flag = true;
- int i = 0;
- while (flag) {
- if (actors[i].getBookme().equals(bookme)) {
- if (!actors[i].getState()) {
- actors[i].setState(true);
- System.out.println("还书成功!");
- flag = false;
- } else {
- System.out.println("该书籍已经被还回!");
- break;
- }
- }
- i++;
- }
- }
- }
-
-
- @Override
- public void list02(Actor[] actors) {
- System.out.println("借书列表如下:" + "\n" + "【学号】—— 【姓名】——【书号】——【书名】——【借阅日期】——【应还日期】——【归还日期】——状态"); //打印Book数组
- for (int i = 0; i < actors.length; i++) {
- if (actors[i] != null) {
- if (actors[i].num != 0) {System.out.println(
- "【" + actors[i].getNum() + "】—— 【" + actors[i].getName() + "】——【" + actors[i].getVolume()
- + "】——【" + actors[i].getBookme() + "】——" + actors[i].getBodate()+"】——"+actors[i].getSrdate()+"】——"+actors[i].getRedate() );
-
- }
- }
- }
- }
- }
- public class Actor {
- public int num; //学号
- public String name; //姓名
- public int volume; //书号
- public String bookme; //书名
- public String bodate; //借阅日期
- public String srdate; //应还日期
- public String redate; //归还日期
- private boolean state; //状态 true-未借出 false-已借出
-
-
- public Actor(int num,String name,int volume,String bookme,String bodate,String srdate,String redate){
- this.num=num;
- this.name=name;
- this.volume=volume;
- this.bookme=bookme;
- this.bodate=bodate;
- this.srdate=srdate;
- this.redate=redate;
- this.state = state;
-
-
- }
- public Actor(){this(7);}
- public Actor(int i){Person03.actors=new Actor[i];}
-
- public int getNum(){
- return num;
- }
- public void setNum(int num){
- this.num=num;
- }
-
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name=name;
- }
-
- public int getVolume(){
- return volume;
- }
- public void setVolume(int volume){
- this.volume=volume;
- }
-
- public String getBookme(){
- return bookme;
- }
- public void setBookme(String bookme){
- this.bookme=bookme;
- }
-
- public String getBodate(){return bodate;}
- public void setBodate(String bodate){this.bodate=bodate;}
-
- public String getSrdate(){return srdate;}
- public void setSrdate(String srdate){this.srdate=srdate;}
-
- public String getRedate(){return redate;}
- public void setRedate(String redate){this.redate=redate;}
-
- public boolean getState() {
- return state;
- }
- public void setState(boolean state) {
- this.state = state;
- }
-
-
- }
- public class Book{
- public int num; //书号
- public String name; //书名
- public String author; //作者
- public String press; //出版社
- public String date; //出版日期
- public int total; //存馆数量
- public double price; //价格
- public String category; //分类
- public boolean state; //状态 true-未借出 false-已借出
-
- //通过构造函数给定书的属性
- public Book(int num,String name,String author,String press,String date,int total,double price,String category,boolean state){
- this.num=num;
- this.press=press;
- this.date=date;
- this.total=total;
- this.author = author;
- this.name = name;
- this.price = price;
- this.category = category;
- this.state = state;
- }
-
- public Book() { //默认10本书
- this(6);
- }
- public Book(int i) {
- Person.books = new Book[i];
- }
-
- //属性的获取和设置
- public int getNum(){
- return num;
- }
- public void getNum(int num){
- this.num=num;
- }
-
- public String getPress(){
- return press;
- }
- public void setPress(String press){
- this.press=press;
- }
-
- public String getDate(){
- return date;
- }
- public void setDate(String date){
- this.date=date;
- }
-
- public int getTotal(){
- return total;
- }
- public void setTotal(int total){
- this.total=total;
- }
-
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
- 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 boolean getState() {
- return state;
- }
- public void setState(boolean state) {
- this.state = state;
- }
- }
- public class Student {
- public int num; //学号
- public String name; //姓名
- public String coll; //学院
- public String major; //专业
- public String clb; //班级
-
-
- public Student(int num,String name,String coll,String major,String clb){
- this.num=num;
- this.name=name;
- this.coll=coll;
- this.major=major;
- this.clb=clb;
-
- }
- public Student(){this(5);}
- public Student(int i){Person02.students=new Student[i];}
-
- public int getNum(){
- return num;
- }
- public void setNum(int num){
- this.num=num;
- }
-
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name=name;
- }
-
- public String getColl(){
- return coll;
- }
- public void setColl(String coll){
- this.coll=coll;
- }
-
- public String getMajor(){
- return major;
- }
- public void setMajor(String major){
- this.major=major;
- }
-
- public String getClb(){
- return clb;
- }
- public void setClb(String clb){ this.clb=clb;
- }
-
-
-
-
- }
1.登陆界面
2.图书信息界面
3.读者信息界面
4.图书借阅界面
如有错误敬请评论指正!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。