JAVA小记(第一周)

因为在本学期开学前几周,已经根据课本把Java基础的知识看得差不多了,所以直接进行了图书管理系统的书写,本以为图书管理系统只是一个类之间的继承以及构造函数之间的使用,在之前的学习中也没有学io流,在这周中因为学了点Python与数据库的知识,本以为图书管理系统写完就万事大吉了,但是当我交给学长之后,他告诉我要用io流,所以打算下周找时间学习io流的知识,并对自己写的图书管理系统进行改正。

以下是我本周写的不完整的图书管理系统

登录类,分为普通用户登录与图书管理员登录

package 图书管理系统;

import java.util.Scanner;

public class Library {
    Scanner sc = new Scanner(System.in);
    //登录函数:显示登录界面进行选择登录
    public void login() {
        int count = 0;
        Person p = null;
        Person1 p1=null;
        while (count < 2) {
            System.out.println("===============java图书管理系统===============");
            System.out.println("请登录:1.普通用户  2.管理员登录  3.退出系统");

            int n = sc.nextInt();
            //通过输入的值进行登录判断
            if (n == 3) {
                return;
            }
            switch (n) {
                case 1:
                    System.out.println("请输入姓名:");
                    String Uname = sc.next();
                    System.out.println("请输入性别:");
                    String Usex = sc.next();
                    System.out.println("请输入年龄:");
                    int Uage = sc.nextInt();

                    p1 = new User(Uname,Usex,Uage); //构造普通用户对象

                    System.out.println("登录成功!");
                    System.out.println("当前普通用户:"+"姓名:" + p1.getName() + " "+"性别:" + p1.getSex() + " "+"年龄"+ p1.getAge());
                    //普通用户循环操作
                    while (true) {
                        System.out.println("请输入你的操作: 1.借阅书籍 2.归还书籍 3.查询借书信息 4.退出");
                        int i = sc.nextInt();
                        if (i == 4) {
                            System.out.println("您已成功退出!");
                            break;
                        }else {
                            p1.operate();  //调用普通用户的操作方法
                        }
                    }
                    break;
                case 2:
                    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("登录成功!");
                    System.out.println("当前管理员:"+"姓名:" + p.getName() + " "+"性别:" + p.getSex() + " "+"年龄" + p.getAge());
                    while (true) {
                        System.out.println("请输入你的操作: 1.增加书籍 2.查阅书籍 3.显示书籍 4.修改书籍 5.删除书籍信息 6.增加读者 7.查询读者 8.读者排序 9.修改读者 10.删除读者 11.退出");
                        int j = sc.nextInt();
                        if (j == 11) {
                            System.out.println("您已成功退出!");
                            break;
                        }else{
                            p.operate();//调用管理员的操作方法
                        }
                    } break;
            }
        }
    }
}

Book类

package 图书管理系统;

public class Book {
    public String id;//书号
    public String name; //书名
    public String author; //作者
    public double price; //价格

    public String cbs;//出版社

    public String date;//出版日期

    public int sum;//存馆数量

    //通过构造函数给定书的属性
    public Book(){}


    public Book(String id, String name, String author, double price, String cbs, String date, int sum) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
        this.cbs = cbs;
        this.date = date;
        this.sum = sum;
    }
    public Book(int i)
    {
        Person.books=new Book[i];
    }

    public String message() {// 成员方法 显示一条图书信息
        String str = "";
        str = this.id + "\t" + this.name + "\t"+this.author+"\t"+this.cbs+"\t"+this.date+"\t"+this.price+"\t" + this.sum;
        return str;// 返回一条str字符串

    }

    //属性的获取和设置
    public String getId()
    {
        return id;
    }
    public void setId(String id)
    {
        this.id=id;
    }
    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 getcbs()
    {
        return cbs;
    }
    public void setCbs(String cbs)
    {
        this.cbs=cbs;
    }
    public String getDate()
    {
        return date;
    }
    public void setDate(String date)
    {
        this.date=date;
    }
    public void setsum(int sum) {
        this.sum = sum;
    }

    public int getsum() {
        return this.sum;
    }

}

读者类

package 图书管理系统;

public class Readin {
    public int xh;
    public String rname;
    public String xy;
    public String mc;

    public Readin(){}

    public Readin(int xh, String rname, String xy, String mc) {
        this.xh = xh;
        this.rname = rname;
        this.xy = xy;
        this.mc = mc;
    }
    public Readin(int i)
    {
        Person.reads=new Readin[i];
    }
    public String rmessage() {// 成员方法 显示一条图书信息
        String str = "";
        str = this.xh + "\t" + this.rname + "\t" + this.xy + "\t" + this.mc;
        return str;
    }
    public int  getXh()
    {
        return xh;
    }
    public void setXh(int xh)
    {
        this.xh=xh;
    }
    public String getRname()
    {
        return rname;
    }
    public void setRname(String rname)
    {
        this.rname=rname;
    }
    public String getXy()
    {
        return xy;
    }
    public void setXy(String xy)
    {
        this.xy=xy;
    }
    public String getMc()
    {
        return mc;
    }
    public void setMc(String mc)
    {
        this.mc=mc;
    }
}

图书阅览表

package 图书管理系统;

public class booklook {
    public String xh;
    public String rname;
    public String id;
    public String name;
    public String jdate;
    public String gdate;
    public booklook(){};

    public booklook(String xh, String rname, String id, String name, String jdate,  String gdate) {
        this.xh = xh;
        this.rname = rname;
        this.id = id;
        this.name = name;
        this.jdate = jdate;
        this.gdate = gdate;
    }

    public String bookmessage() {// 成员方法 显示一条图书信息
        String str = "";
        str = this.xh + "\t" + this.rname + "\t" + this.id + "\t" + this.name + "\t" + this.jdate + "\t"  + this.gdate;
        return str;
    }

    public String getXh() {
        return xh;
    }

    public String getRname() {
        return rname;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getJdate() {
        return jdate;
    }


    public String getGdate() {
        return gdate;
    }

    public void setXh(String xh) {
        this.xh = xh;
    }

    public void setRname(String rname) {
        this.rname = rname;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setJdate(String jdate) {
        this.jdate = jdate;
    }

    public void setGdate(String gdate) {
        this.gdate = gdate;
    }
}

普通用户登录后

package 图书管理系统;


abstract class Person1 {
    public String name;
    public String sex;
    public int age;

    public static Book[] books = new Book[]{
            new Book("A", "西游记", "吴承恩", 10, "新华", "1990", 10),
            new Book("B", "红楼梦", "曹雪芹", 20, "新华", "2000", 10),
            new Book("C", "三国演义", "罗贯中", 15, "新华", "2000", 10),
            new Book(),
            new Book(),
            new Book()
    };
    public static Readin[] reads = new Readin[]{
            new Readin(10001, "小明", "信院", "2103"),
            new Readin(10002, "小张", "机电", "2101"),
            new Readin(10003, "小李", "信院", "2010"),
            new Readin(),
            new Readin(),
            new Readin(),
    };

    public static booklook[] looks = new booklook[]{
            new booklook(),
            new booklook(),
            new booklook(),
            new booklook(),
            new booklook(),
            new booklook()
    };

    public Person1(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();

    public abstract void select1(Book[] books,booklook[] looks,Readin[] reads);

    public abstract void delete1(Book[] books,booklook[] looks,Readin[] reads);

    public abstract void add1(Book[] books,booklook[] looks,Readin[] reads);

}


package 图书管理系统;

public interface Operate1 {
    void select1(Book[] books,booklook[] looks,Readin[] reads);
    void delete1(Book[] books,booklook[] looks,Readin[] reads);
    void add1(Book[] books,booklook[] looks,Readin[] reads);
}


package 图书管理系统;


import java.util.Scanner;

public class User extends Person1 implements Operate1 {   //user类继承了人的类,  实现operate接口

    public User(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:
                //借
                delete1(books, looks, reads);
                break;
            case 2:
                //还
                add1(books, looks, reads);
                break;
            case 3:
                //查
                select1(books,looks,reads);
                break;
            default:
                System.out.println("输入有误!");
                break;
        }
    }

    @Override
    public void select1(Book[] books, booklook[] looks, Readin[] reads) {
        System.out.println("请输入学号进行查询:");
        String xh = sc.next();
        if (name != null) {
            int flag = 0;
            for (int i = 0; i < looks.length; i++) {
                if (looks[i].getXh().equals(xh)) {
                    System.out.println(looks[i].bookmessage());
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) System.out.println("暂时没有此人!");
        }
    }


    @Override
    public void delete1(Book[] books, booklook[] looks, Readin[] reads) {
        System.out.println("请输入你的学号");
        String xh = sc.next();
        System.out.println("请输入要借阅的书名:");
        String name = sc.next();
        if (name != null) {
            for (int i = 0; i < books.length; i++) {
                if (books[i].getName().equals(name)) {
                    if (books[i].sum == 0) {
                        System.out.println("该图书已被借完");
                    }
                    else {
                        for (int j = 0; j < books.length; j++) {
                            if (looks[j].getXh() == null) {
                                looks[j].setXh(xh);
                                looks[j].setRname(reads[i].getRname());
                                looks[j].setId(books[i].getId());
                                looks[j].setName(books[i].getName());
                                System.out.println("请输入借阅日期");
                                String jdate = sc.next();
                                looks[j].setJdate(jdate);
                                books[i].sum--;
                                System.out.println("借阅成功!");
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void add1(Book[] books, booklook[] looks, Readin[] reads) {
        System.out.println("请输入你的学号");
        String xh = sc.next();

        System.out.println("请输入要还的书名:");
        String name = sc.next();

        if (name != null) {
            boolean flag = true;
            int i = 0;
            while (flag) {
                if (books[i].getName().equals(name)) {
                    for (int j = 0; j < books.length; j++) {
                        if (looks[j].getGdate() == null ) {
                            System.out.println("请输入你的归还日期");
                            String gdate = sc.next();
                            looks[j].setGdate(gdate);

                            books[i].sum++;
                            System.out.println("还书成功!");
                            flag = false;
                            break;
                        }
                    }
                }

                i++;
            }
        }
    }
}

管理员登录后

package 图书管理系统;

abstract class Person {
    public String name;
    public String sex;
    public int age;

    public static Book[] books = new Book[]{
            new Book("A", "西游记", "吴承恩", 10, "新华", "1990", 10),
            new Book("B", "红楼梦", "曹雪芹", 20, "新华", "2000", 10),
            new Book("C", "三国演义", "罗贯中", 15, "新华", "2000", 10),
            new Book(),
            new Book(),
            new Book()
    };
    public static Readin[] reads = new Readin[]{
            new Readin(10001, "小明", "信院", "2103"),
            new Readin(10002, "小张", "机电", "2101"),
            new Readin(10003, "小李", "信院", "2010"),
            new Readin(),
            new Readin(),
            new Readin(),
    };

    public static booklook[] looks = new booklook[]{
            new booklook(),
            new booklook(),
            new booklook(),
            new booklook(),
            new booklook(),
            new booklook()
    };

    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();

    public abstract void select(Book[] books);

    public abstract void delete(Book[] books);

    public abstract void add(Book[] books);

}

package 图书管理系统;

public interface Operate {  //操作的接口
    void select(Book[] books); //查询,查阅
    void delete(Book[] books);//删除,借阅
    void add(Book[] books); //增加,还书
    void list(Book[] books); //显示书列表


}



package 图书管理系统;

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:
                //整理
                add(books);
                break;
            case 2:
                //查
                select(books);
                break;
            case 3:
                //加
                list(books);
                break;
            case 4:
                //删
                xg(books);
                break;
            case 5:
                //列表
                delete(books);
                break;
            case 6:
                radd(reads);
                break;
            case 7:
                rselect(reads);
                break;
            case 8:
                rsort(reads);
                break;
            case 9:
                rxg(reads);
                break;
            case 10:
                rdelete(reads);
                break;
            default:
                System.out.println("输入有误!");
                break;

        }
    }




    @Override
    public void select(Book[] books) {
        System.out.println("请输入书名进行查询:");
        String name = sc.next();
        if(name != null) {
            int flag = 0;		//是否找到的标记位
            for (int i = 0; i < books.length; i++) {
                if (books[i].getName().equals(name)) {
                    System.out.println("有此书籍!");

                    System.out.println(books[i].message());
                    flag = 1;
                    break;
                }
            }
            if(flag == 0) System.out.println("暂时没有此书!");
        }
    }



    @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("请输入书籍编号:");
        String id = sc.next();// 输入书籍编号
        System.out.println("请输入书名:");
        String name = sc.next();
        System.out.println("请输入作者:");
        String author = sc.next();
        System.out.println("请输入价格:");
        double price = sc.nextDouble();
        System.out.println("请输入书籍出版社");
        String cbs = sc.next();
        System.out.println("请输入书籍出版日期");
        String date = sc.next();
        System.out.println("请输入书籍数量:");
        int sum = sc.nextInt();

        boolean flag = false;
        for (int i = 0; i < books.length; i++) {
            if (books[i].getName() == null) {
                flag = true;
                books[i].setId(id);
                books[i].setName(name);
                books[i].setAuthor(author);
                books[i].setPrice(price);
                books[i].setDate(date);
                books[i].setCbs(cbs);
                books[i].setsum(sum);
                System.out.println("添加成功");
                break;
            }
        }
        if (!flag) {
            System.out.println("【警告】数组只能添加3本新书,没有位置放置新书了,如需添加多余3本,需要在‘Person’类中添加buffer!" + "\n");
        }

    }

    @Override
    public void list(Book[] books) {
        //打印Book数组
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                if (books[i].price != 0) {
                    System.out.println(
                            books[i].message());

                }
            }
        }
    }

    public void xg(Book[] books) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入你要修改的图书编号");
        String num = input.next();// 输入编号
        for (int i = 0; i < books.length; i++) {
            if (num.equals(books[i].getId())) {// 如果编号存在 编号匹配

                System.out.println("请输入新的图书编号:");
                String newnum = input.next();// 输入新的图书编号
                books[i].setId(newnum);// 设置新的图书编号

                System.out.println("请输入新的图书名字");
                String newname = input.next();// 输入新的图书名字
                books[i].setName(newname);// 设置新的图书名字

                System.out.println("请输入新的图书作者");
                String newauthor = input.next();
                books[i].setAuthor(newauthor);

                System.out.println("请输入新的图书价格");
                Double newprice = input.nextDouble();
                books[i].setPrice(newprice);

                System.out.println("请输入新的图书出版社");
                String newcbs = input.next();
                books[i].setCbs(newcbs);

                System.out.println("请输入新的图书出版日期");
                String newdate = input.next();
                books[i].setDate(newdate);

                System.out.println("请输入新的图书数量:");
                int newsum = input.nextInt();// 输入新的图书数量
                books[i].setsum(newsum);
                System.out.println("修改图书信息成功");

            }
        }

    }
    public void radd(Readin[] reads){
        System.out.println("请输入读者学号:");
        int xh = sc.nextInt();
        System.out.println("请输入读者姓名");
        String rname=sc.next();
        System.out.println("请输入读者学院");
        String xy=sc.next();
        System.out.println("请输入读者专业班级");
        String mc=sc.next();

        boolean flagg = false;
        for (int i = 0; i < reads.length; i++) {
            if (reads[i].getRname()== null) {
                flagg = true;
                reads[i].setXh(xh);
                reads[i].setRname(rname);
                reads[i].setXy(xy);
                reads[i].setMc(mc);
                System.out.println("添加成功");
                break;
            }
        }
        if (!flagg) {
            System.out.println("【警告】数组预留buffer最多只能添加3位读者,需要在‘Person’类中添加buffer!" + "\n");
        }
    }
    public void rselect(Readin[] reads){
        System.out.println("请输入读者姓名进行查询:");
        String name = sc.next();
        if(name != null) {
            int flag = 0;		//是否找到的标记位
            for (int i = 0; i < reads.length; i++) {
                if (reads[i].getRname().equals(name)) {
                    System.out.println("有此读者!");

                    System.out.println(reads[i].rmessage());
                    flag = 1;
                    break;
                }
            }
            if(flag == 0) System.out.println("暂时没有此读者!");
        }
    }
    public void rsort(Readin[] reads){


        Arrays.sort(reads, new Comparator() {
            @Override
            public int compare(Readin o1, Readin o2) {
                return (int) (o1.getXh() - o2.getXh());
            }
        });
        //遍历数组打印书的列表
        for (int i = 0; i < reads.length; i++) {
            if (reads[i].getXh() != 0) {
                System.out.println(
                        reads[i].rmessage());
            }
        }
    }
    public void rdelete(Readin[] reads) {
        System.out.println("请输入要删除的读者名与学号:");
        String str = sc.next();
        int str1=sc.nextInt();
        if (str != null) {
            for (int i = 0; i < reads.length; i++) {
                if (reads[i].getXh()==str1)
                {
                    reads[i] = null;
                    System.out.println("删除成功!");
                    break;
                }
            }
        }

    }
    public void rxg(Readin[] reads)
    {
        System.out.println("请输入要修改的读者名与学号:");
        String str = sc.next();
        int str1=sc.nextInt();
        for (int i = 0; i < reads.length; i++) {
            if (reads[i].xh==str1) {// 如果编号存在 编号匹配

                System.out.println("请输入新的读者学号:");
                int xh = sc.nextInt();
                reads[i].setXh(xh);

                System.out.println("请输入新的读者名字");
                String rname = sc.next();
                reads[i].setRname(rname);

                System.out.println("请输入新的读者学院");
                String xy = sc.next();
                reads[i].setXy(xy);

                System.out.println("请输入新的读者专业班级");
                String mc = sc.next();
                reads[i].setMc(mc);

                System.out.println("修改读者信息成功");

            }
        }
    }
}

最后,定义一个Main函数进行运行

package 图书管理系统;

public class Main {
    public static void main(String[] args) {
        System.out.println("2021213866赵志敏");
        Library library = new Library(); //构造Library对象
        library.login(); //调用对象的登录方法。
    }
}

你可能感兴趣的:(java,开发语言)