JavaSE编写简单的图书管理系统

package 图书管理系统;

import java.util.Scanner;

/**
 * @author 魏榕汘
 * @date 2020/9/25 - 20:06
 * 图书管理系统
 * 1.查看所有的图书
 * 2.增加图书
 * 3.删除图书
 * 4.借出图书
 * 5.归还图书
 * 6.退出
 *
 * 1首先实现登陆的功能
 *2查看功能
 */
public class Demo1 {
    //迷你图书管理系统
    static String[] nameArray={"java教程","python入门","c语言教程","ps大神之路"};//书名
    static String [] authorArray={"张三","李四","王五","赵柳"};//作者
    static int[] stateArray={1,1,2,2};//状态:1.可借 2.不可借
    static int[] borrowCount={67,70,43,15};//累计借出次数

    static Scanner sc=new Scanner(System.in);

    public static void showMenu(){
        System.out.println("欢迎来到图书管理系统!");
        System.out.println("---------主菜单---------");
        System.out.println("1.查看所有的图书");
        System.out.println("2.添加图书");
        System.out.println("3.删除图书");
        System.out.println("4.借出图书");
        System.out.println("5.归还图书");
        System.out.println("退出");
        System.out.println("请选择您要办理的业务:");
    }
    //删除图书
    public static void delBook(){
        System.out.println("请输入书名:");
        String name= sc.next();
        int index=0;
        int msg=1;
        for (int i=0;i<nameArray.length;i++){
            if (nameArray[i].equals(name)){
                index=i;
                msg=2;
            }
        }
        if (msg==2){
            String [] nameArray2=new String[nameArray.length-1];
            String [] authorArray2=new String[nameArray.length-1];
            int[] stateArray2=new int [nameArray.length-1];
            int[] borrowCount2=new int [nameArray.length-1];

            for (int i=0;i<nameArray2.length;i++){
                if (i<index){
                    nameArray2[i]=nameArray[i];
                    authorArray2[i]=authorArray[i];
                    stateArray2[i]=stateArray[i];
                    borrowCount2[i]=borrowCount[i];
                }else if (i>=index){
                    nameArray2[i]=nameArray[i+1];
                    authorArray2[i]=authorArray[i+1];
                    stateArray2[i]=stateArray[i+1];
                    borrowCount2[i]=borrowCount[i+1];
                }
            }
            showBooks(nameArray2,authorArray2,stateArray2,borrowCount2);
            System.out.println(name+"\t"+"书籍已经删除成功");

        }else{
            System.out.println("此书不存在");
        }
    }
    //借出图书
    public static void borrowBook(){
        System.out.println("请输入书名:");
        String name= sc.next();
        String msg="图书不存在";
        for (int i=0;i<nameArray.length;i++){
            if (nameArray[i].equals(name)){
                if (stateArray[i]==1){
                    msg="图书可借";
                    stateArray[i]=2;
                    borrowCount[i]+=1;
                }else{
                    msg="对不起,图书已经借出";
                }
            }
        }
        if (msg.equals("图书可借")){
            System.out.println(name+"借出成功");
            showBooks(nameArray,authorArray,stateArray,borrowCount);


        }else{
            System.out.println(msg);
        }

    }
    //显示所有图书
    public static void showBooks(String[] a,String[] b,int []c,int[] d){
                System.out.println("书名--------作者----状态----借出次数");
            for (int i=0;i<a.length;i++){
                String s="不可借";
                if (c[i]==1){
                    s="可借";
                }
                System.out.println(a[i]+"----"+b[i]+"----"+s+"--------"+d[i]);
            }
    }
    //归还图书
    public static void backBook(){
        System.out.println("请输入书名:");
        String name= sc.next();
        String msg="图书不存在";

        for (int i=0;i<nameArray.length;i++){
            if (nameArray[i].equals(name)){
                if (stateArray[i]==2){
                    msg="图书存在";
                    System.out.println("请输入租借天数:");
                    int days=sc.nextInt();
                    if (days>31&&days<1){
                        System.out.println("输入错误!");
                    }else{
                        stateArray[i]=1;
                        System.out.println(name+"归还成功!");
                        double rent=2.0*days;
                        System.out.println(name+"归还成功!租金:"+rent+"元");
                        showBooks(nameArray,authorArray,stateArray,borrowCount);
                    }
                }
            }
        }


    }
    //增加图书
    public static void addBook(){

        if (nameArray.length>=6){
            System.out.println("书架已经满了");
        }else{
            System.out.println("请输入书名:");
            String name= sc.next();
            System.out.println("请输入作者:");
            String author= sc.next();
            int msg1=1;
            for (String n:nameArray
                 ) {
                if (n.equals(name)){
                    msg1=2;
                }

            }
            if (msg1==1){
            String []nameArray2=new String[nameArray.length+1];
            String []authorArray2=new String[nameArray.length+1];
            int  []stateArray2=new int [nameArray.length+1];
            int  []borrowCount2=new int [nameArray.length+1];
            for (int i=0;i<nameArray.length;i++){
                nameArray2[i]=nameArray[i];
                authorArray2[i]=authorArray[i];
                stateArray2[i]=stateArray[i];
                borrowCount2[i]=borrowCount[i];

            }
                nameArray2[nameArray2.length-1]=name;
                authorArray2[authorArray2.length-1]=author;
                stateArray2[stateArray2.length-1]=1;
                borrowCount2[borrowCount2.length-1]=0;
                System.out.println("添加成功,添加书名为:"+name);
                showBooks(nameArray2,authorArray2,stateArray2,borrowCount2);
            }else{
                System.out.println("此书已经存在!");
            }
        }
    }
    public static String login(){
        String uname="aaa";
        String upwd="111";
        String msg="失败";
        System.out.println("请输入账号:");
        String name=sc.next();
        System.out.println("请输入密码:");
        String pwd=sc.next();
        if (uname.equals(name)&&upwd.equals(pwd)){
            msg="成功";

        }
        return msg;
    }
    public static void main(String[] args) {

        while (true){
            String msg=login();
            if ("失败".equals(msg)){
                System.out.println("登陆失败,请重新登陆!");
                continue;
            }else if("成功".equals(msg)){
                        while (true) {
                            showMenu();
                            int chioce = sc.nextInt();
                            if (chioce == 1) {
                                showBooks(nameArray,authorArray,stateArray,borrowCount);

                            } else if (chioce == 2) {
                                addBook();

                            } else if (chioce == 3) {
                                delBook();

                            } else if (chioce == 4) {
                                borrowBook();
                            } else if (chioce == 5) {
                                backBook();
                            } else if (chioce == 6) {

                            }
                        }
            }
        }

    }
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页