课后拓展作业

发布时间:2024年01月05日

操作类

import java.util.ArrayList;
import java.util.Scanner;

public class booktem{
    Scanner sc = new Scanner(System.in);
    ArrayList<Acconte> accontes = new ArrayList<>();
    Acconte login;


    public void select() {
        while (true) {
            System.out.println("===请选择===");
            System.out.println("1.登录账号");
            System.out.println("2.注册账号");
            System.out.println("3.退出");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    logon();
                 break;
                case 2:
                    register();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("输入有误");
            }
        }
    }

    public void register() {
        Acconte acc = new Acconte();
        System.out.println("请输入用户名");
        String name = sc.next();
        acc.setAccount(name);

        System.out.println("请输入密码");
        String password = sc.next();
        acc.setPassword(password);
        System.out.println("注册成功!");
        accontes.add(acc);

    }


    private void QuerybasedID() {
        System.out.println("请输入书籍编号");
        int id = sc.nextInt();
        for (int i = 0; i < accontes.size(); i++) {
            Acconte add = accontes.get(i);
            if (add.getId() == id) {

                System.out.println("编号:" + add.getId() + "\t书名:" + add.getName() + "\t作者" + add.getTitle() +
                        "\t出版时间:" + add.getYear() + "年" + add.getMoon() + "月" + add.getSun() + "日");

            }else {
                System.out.println("无此书");
            }
        }


    }

    public void Queryallinformation() {
        if (accontes.size() == 0) {
            System.out.println("当前无数据");
        }

        for (int i = 0; i < accontes.size(); i++) {
            Acconte acconte = accontes.get(i);
            System.out.println("编号:" + acconte.getId() + "\t\t\t书名:" + acconte.getName() + "\t\t\t作者:" + acconte.getTitle() +
                    "\t\t\t出版时间:" + acconte.getYear() + "年" + acconte.getMoon() + "月" + acconte.getSun() + "日");
        }
    }

    //新增
    public void newbook() {
        Acconte acc = new Acconte();
        System.out.println("请输入编号");
        int id = sc.nextInt();
        acc.setId(id);
        System.out.println("请输入书名");
        String name = sc.next();
        acc.setName(name);
        System.out.println("请输入作者");
        String title = sc.next();
        acc.setTitle(title);
        System.out.println("请输入出版年份");
        int year = sc.nextInt();
        acc.setYear(year);
        System.out.println("请输入出版年份");
        int yue = sc.nextInt();
        acc.setMoon(yue);
        System.out.println("请输入出版年份");
        int sum = sc.nextInt();
        acc.setSun(sum);
        accontes.add(acc);
    }

    public void logon() {

        if (accontes.size() == 0) {
            System.out.println("当前系统无账号");
            return;
        }

        System.out.println("请输入用户名");
        String usename = sc.next();
        Acconte add = query(usename);
        if (add == null) {
            System.out.println("无当前用户名");
        } else {
            System.out.println("请输入密码");
            String password = sc.next();
            if (add.getPassword().equals(password)) {
                System.out.println("登录成功");
                login = add;
                showUserCommand();
                return;

            }
        }

    }
    public void getnamequery(){
        System.out.println("请输入书名");
        String name = sc.next();
        for (int i = 0; i < accontes.size(); i++) {
            Acconte add = accontes.get(i);
            if (add.getName().contains(name)) {

                System.out.println("编号:" + add.getId() + "\t书名:" + add.getName() + "\t作者" + add.getTitle() +
                        "\t出版时间:" + add.getYear() + "年" + add.getMoon() + "月" + add.getSun() + "日");

            }else {
                System.out.println("无此书");
            }
        }
    }
    public void getnamedelte(){
        System.out.println("请输入书名");
        String name = sc.next();
        for (int i = 0; i < accontes.size(); i++) {
            Acconte add = accontes.get(i);
            if (add.getName().equals(name)) {
                accontes.remove(i);


            }else {
                System.out.println("无此书");
            }
        }
    }
    public void getidmodify(){
        System.out.println("请输入书籍编号");
        int id = sc.nextInt();
        for (int i = 0; i < accontes.size(); i++) {
            Acconte acc = accontes.get(i);
            if (acc.getId()==id) {
                System.out.println("请输入修改的编号:");
                int id2 = sc.nextInt();
                acc.setId(id2);
                System.out.println("请输入修改后书名");
                String name = sc.next();
                acc.setName(name);
                System.out.println("请输入修改后作者");
                String title = sc.next();
                acc.setTitle(title);
                System.out.println("请输入修改后出版年份");
                int year = sc.nextInt();
                acc.setYear(year);
                System.out.println("请输入修改后出版年份");
                int yue = sc.nextInt();
                acc.setMoon(yue);
                System.out.println("请输入修改后出版年份");
                int sum = sc.nextInt();
                acc.setSun(sum);

                System.out.println("编号:" + acc.getId() + "\t书名:" + acc.getName() + "\t作者" + acc.getTitle() +
                        "\t出版时间:" + acc.getYear() + "年" + acc.getMoon() + "月" + acc.getSun() + "日");

            }
        }
    }

    private void showUserCommand() {
        while (true) {
            System.out.println(login.getAccount() + "您可以选择如下功能进行账户处理");
            System.out.println("1.根据图书名称查询图书");
            System.out.println("2.根据图书编号查询图书");
            System.out.println("3.根据图书编号修改图书");
            System.out.println("4.根据图书名称删除图书");
            System.out.println("5.查看所有图书");
            System.out.println("6.新增图书");
            System.out.println("7.退出登录");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    getnamequery();
                    break;
                case 2:
                    QuerybasedID();
                    break;
                case 3:
                    getidmodify();
                    break;
                case 4:
                    getnamedelte();
                    break;
                case 5:
                    Queryallinformation();


                case 6:
                    newbook();

                case 7:
                    System.out.println(login.getAccount() + "退出成功");
                    return;
                default:
                    System.out.println("输入指令有误请重新输入");
            }
        }
    }

    public Acconte query(String id) {
        for (int i = 0; i < accontes.size(); i++) {
            Acconte add = accontes.get(i);
            if (add.getAccount().equals(id)) {
                return add;
            }
        }
        return null;
    }
}

储存类

public class Acconte {
    String name ="西游记";
    int id=123;
    String title="小吴";//作者
    int year=2023;//年
    int moon=2;//月
    int sun=32;//日

    String Account;
    String password;

    public Acconte() {
    }

    public Acconte(String name, int id, String title, int year, int moon, int sun, String account, String password) {
        this.name = name;
        this.id = id;
        this.title = title;
        this.year = year;
        this.moon = moon;
        this.sun = sun;
        Account = account;
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMoon() {
        return moon;
    }

    public void setMoon(int moon) {
        this.moon = moon;
    }

    public int getSun() {
        return sun;
    }

    public void setSun(int sun) {
        this.sun = sun;
    }

    public String getAccount() {
        return Account;
    }

    public void setAccount(String account) {
        Account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        booktem bookSystem= new booktem();
        bookSystem.select();
    }
}

文章来源:https://blog.csdn.net/diyahao/article/details/135388332
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。