JavaSE综合练习-图书系统1.0

news2024/10/6 16:19:18

Main

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;

//程序入口函数
public class Main {
    public static User login(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name=scanner.nextLine();

        System.out.println("请输入你的身份,1:管理员  2:普通用户");

        int choice=scanner.nextInt();
        if(choice==1){
            AdminUser adminUser=new AdminUser(name);
            return adminUser;
        }else{
            NormalUser normalUser=new NormalUser(name);
            return normalUser;
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        //user有可能引用管理员用户 有可能引用普通用户
        User user = login();
        while (true) {
            int choice = user.menu();
            //伪代码 io[choice].work(书架);
            user.doIoperations(choice, bookList);
        }
    }
}

book

Book

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isLend;//是否被借出


    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 int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isLend() {
        return isLend;
    }

    public void setLend(boolean lend) {
        isLend = lend;
    }

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
        //this.isLend = isLend;默认值false 未被借出
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                (isLend==true?" 已借出":" 未借出")+
                //", isLend=" + isLend +
                '}';
    }
}

BookList

package book;

//书架
public class BookList {
    private Book[] books = new Book[10];
    private int usedSize;//用来记录当前存放了多少书

    public BookList() {
        books[0] = new Book("三国演义", "罗贯中", 10, "小说");
        books[1] = new Book("西游记", "吴承恩", 12, "小说");
        books[2] = new Book("红楼梦", "曹雪芹", 11, "小说");
        this.usedSize = 3;
    }

    public Book getBook(int pos) {
        return books[pos];
    }

    public void setBook(int pos,Book book){
        books[pos]=book;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public boolean isFull() {
        if (this.usedSize == books.length) {
            return true;
        }
        else return false;
    }
}

operation

AddOperation

package operation;

import book.BookList;
import book.Book;

import java.util.Scanner;

public class AddOperation implements IOPeration {
    public void work(BookList bookList){
        System.out.println("新增图书......");

        if(bookList.isFull()){
            System.out.println("书架满了 不能新增了!");
            return;
        }

        System.out.println("请输入你要新增的图书的书名:");
        Scanner scanner=new Scanner(System.in);
        String bookName=scanner.nextLine();

        System.out.println("请输入你要新增的图书的作者:");
        String author=scanner.nextLine();

        System.out.println("请输入你要新增图书的价格:");
        int price=scanner.nextInt();

        System.out.println("请输入你要新增的图书的类型:");
        String type=scanner.nextLine();

        Book book=new Book(bookName,author,price,type);

        int currentSize=bookList.getUsedSize();

        bookList.setBook(currentSize,book);

        bookList.setUsedSize(currentSize+1);

        System.out.println("新增图书成功!");
    }
    //IOPeration[]
}

BorrowedOperation

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowedOperation implements IOPeration{
    public void work(BookList bookList){
        System.out.println("借阅图书.......");

        System.out.println("请输入你要借阅的图书的书名:");
        Scanner scanner=new Scanner(System.in);
        String bookName=scanner.nextLine();

        int currentSize=bookList.getUsedSize();
        for(int i=0;i<currentSize;i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookName)){
                book.setLend(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("借阅失败!");
    }
}

DelOperation

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    public void work(BookList bookList){
        System.out.println("删除图书......");

        System.out.println("请输入你要删除的图书的书名:");
        Scanner scanner=new Scanner(System.in);
        String bookName=scanner.nextLine();

        int currentSize=bookList.getUsedSize();
        int pos=-1;
        int i;
        for(i=0;i<currentSize;i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(bookName)) {
                //找到这本书了 记录下标
                pos=i;
                break;
            }
        }
        if(i>=currentSize){
            System.out.println("没有你要找的书!");
            return;
        }
        //开始删除
        for (int j = pos; j <currentSize-1 ; j++) {
            Book book=bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setUsedSize(currentSize-1);

        bookList.setBook(currentSize-1,null);

        System.out.println("删除成功!");
    }
}

ExitOperation

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    public void work(BookList bookList){
        System.out.println("删除图书......");

        System.out.println("请输入你要删除的图书的书名:");
        Scanner scanner=new Scanner(System.in);
        String bookName=scanner.nextLine();

        int currentSize=bookList.getUsedSize();
        int pos=-1;
        int i;
        for(i=0;i<currentSize;i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(bookName)) {
                //找到这本书了 记录下标
                pos=i;
                break;
            }
        }
        if(i>=currentSize){
            System.out.println("没有你要找的书!");
            return;
        }
        //开始删除
        for (int j = pos; j <currentSize-1 ; j++) {
            Book book=bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setUsedSize(currentSize-1);

        bookList.setBook(currentSize-1,null);

        System.out.println("删除成功!");
    }
}

ReturnOperation

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书......");

        System.out.println("请输入你要归还的图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(bookName)) {
                book.setLend(false);
                System.out.println("归还成功!");
                return;
            }
        }
        System.out.println("归还失败!");
    }
}

ShowOperation

package operation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOPeration {
    public void work(BookList bookList) {
        System.out.println("显示图书.......");
        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

FindOperation

package operation;
import book.Book;
import book.BookList;

import java.util.Scanner;


public class FindOperation implements IOPeration {
    public void work(BookList bookList){
        System.out.println("查找图书......");
        System.out.println("请输入你要查找的图书名:");
        Scanner scanner=new Scanner(System.in);
        String bookName= scanner.nextLine();

        int currentSize= bookList.getUsedSize();

        for(int i=0;i<currentSize;i++){
            Book book=bookList.getBook(i);
            if(book.getName().equals(bookName)){
                System.out.println("找到了这本书:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要找的书......");
    }
}

IOPration

package operation;
import book.Book;
import book.BookList;

import java.util.Scanner;


package operation;

import book.BookList;

public interface IOPeration {
     void work(BookList bookList);
}

user

AdminUser

package user;

import operation.*;

import java.util.Scanner;

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.ioPerations=new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation(),
        };
    }

    public int menu(){
        System.out.println("******管理员菜单******");
        System.out.println("1.查找图书");
        System.out.println("2.新增图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("0.退出系统");
        System.out.println("**********************");
        System.out.println("请输入你的操作:");
        Scanner scanner=new Scanner(System.in);
        int choice= scanner.nextInt();
        return choice;
    }
}

NormalUser

package user;

import operation.*;

import java.util.Scanner;

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.ioPerations=new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowedOperation(),
                new ReturnOperation(),
        };
    }
    public int menu(){
        System.out.println("******普通用户菜单******");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("**********************");
        System.out.println("请输入你的操作:");
        Scanner scanner=new Scanner(System.in);
        int choice= scanner.nextInt();
        return choice;
    }
}

User

package user;
import book.BookList;
import operation.IOPeration;

import operation.IOPeration;
public abstract class User {
    protected String name;
    protected IOPeration[] ioPerations;
    public User(String name){
        this.name=name;
    }
    public abstract int menu();

    public void doIoperations(int choice,BookList bookList){
        this.ioPerations[choice].work(bookList);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1526588.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

HTML表格(HTML 表格的使用,收藏这一篇就够了)

你好&#xff0c;我是云桃桃。 一个希望帮助更多朋友快速入门 WEB 前端的程序媛。 今天聊聊 table。HTML <table> 元素用于创建表格&#xff0c;它是一种将数据按行和列组织排列的结构&#xff0c;用于在网页中呈现复杂的数据集。HTML 表格具有以下 2 种主要用途&#x…

java方法的引用传递和值传递

1、方法的值参数传递 下面代码&#xff0c;它会在控制台输出什么&#xff1f; public class ArrayTest {public static void main(String[] args) {int number 100;System.out.println(number);change(number);System.out.println(number);}public static void change(int n…

Qt学习--继承(并以分文件实现)

基类 & 派生类 一个类可以派生自多个类&#xff0c;这意味着&#xff0c;它可以从多个基类继承数据和函数。定义一个派生类&#xff0c;我们使用一个类派生列表来指定基类。类派生列表以一个或多个基类命名。 总结&#xff1a;简单来说&#xff0c;父类有的&#xff0c;子…

【Paper Reading】6.RLHF-V 提出用RLHF的1.4k的数据微调显著降低MLLM的虚幻问题

分类 内容 论文题目 RLHF-V: Towards Trustworthy MLLMs via Behavior Alignment from Fine-grained Correctional Human Feedback 作者 作者团队&#xff1a;由来自清华大学和新加坡国立大学的研究者组成&#xff0c;包括Tianyu Yu, Yuan Yao, Haoye Zhang, Taiwen He, Y…

[SaaS] 淘宝设计AI

“淘宝设计AI” 让国际大牌造世界双11超级品牌 超级发布https://mp.weixin.qq.com/s/xFVDARQHxlweKAYG91DtYw下面是一个完整的品牌营销海报设计流程&#xff0c;AIGC起到了巨大作用&#xff0c;但是仍然很难去一步解决这个问题&#xff0c;还是逐步修改的一个过程。 Midjouner…

java 面向对象--equals方法

Object 类的使用 类 java.lang.Object是类层次结构的根类&#xff0c;即所有其它类的父类。每个类都使用 Object 作为超类。 Object类型的变量与除Object以外的任意引用数据类型的对象都存在多态引用 method(Object obj){…} //可以接收任何类作为其参数 Person o new Person…

【NTN 卫星通信】 TN和多NTN配合的应用场景

1 场景描述 此场景描述了农村环境&#xff0c;其中MNO (运营商TerrA)仅在城市附近提供本地地面覆盖&#xff0c;而MNO (SatA)提供广泛的NTN覆盖。SatA使用GSO轨道和NGSO轨道上的卫星。SatA与TerrA有漫游协议&#xff0c;允许:   所有TerrA用户的连接&#xff0c;当这些用户不…

超分之SwinIR

SwinIR: Image restoration using Swin TransformerSwinIR: 使用Swin Transformer 进行图像恢复Liang J, Cao J, Sun G, et al.Proceedings of the IEEE/CVF international conference on computer vision. 2021: 1833-1844. 摘要 首先&#xff0c;介绍了Image restoration的含…

Ingress 基于URL路由多个服务

文章目录 前言一、基于请求地址转发不同应用的pod1.创建一个nginx的pod和一个apache的pod及其各自的service2.创建ingress实现一个地址两个path分别访问nginx和apache3.验证根据域名web2.study.com的两个路径/foo和/bar来访问到不同的pod4.分别在nginx和apache的pod里创建网站目…

win32汇编弹出对话框

之前书上有一个win32 asm 的odbc例子&#xff0c;它有一个窗体&#xff0c;可以执行sql&#xff1b;下面看一下弹出一个录入数据的对话框&#xff1b; 之前它在.code段包含2个单独的asm文件&#xff0c;增加第三个&#xff0c;增加的这个里面是弹出对话框的窗口过程&#xff0…

Python--类中作用域

1、在面向对象编程中&#xff0c;主要的变量就是成员变量&#xff08;属性&#xff09;和局部变量 class Cat:# 属性name Noneage None# n1, n2, result为局部变量def cal(self, n1, n2):result n1 n2print(f"result{result}") 2、作用域的分类&#xff1a;属性…

Vue3-03_组件基础_上

单页面应用程序 什么是单页面应用程序 单页面应用程序&#xff08;英文名&#xff1a;Single Page Application&#xff09;简称 SPA&#xff0c;顾 名思义&#xff0c;指的是一个 Web 网站中只有唯一的一个 HTML 页面&#xff0c;所有的 功能与交互都在这唯一的一个页面内完…

09|代理(上):ReAct框架,推理与行动的协同

应用思维链推理并不能解决大模型的固有问题&#xff1a;无法主动更新自己的知识&#xff0c;导致出现事实幻觉。也就是说&#xff0c;因为缺乏和外部世界的接触&#xff0c;大模型只拥有训练时见过的知识&#xff0c;以及提示信息中作为上下文提供的附加知识。如果你问的问题超…

Fitten Code对JetBrains支持再升级,新增7大功能

十科技基于计图框架&#xff0c;推出基于代码大模型的 AI 代码助手 ——Fitten Code&#xff0c;今天&#xff0c;Fitten Code再升级&#xff0c;新增7大功能。特此转载。 「一键开启编程新时代&#xff0c;Fitten Code 对 JetBrains 支持再升级&#xff01;」 Fitten Code代码…

新火种AI|英伟达GTC大会在即,它能否撑住场面,为AI缔造下一个高度?

作者&#xff1a;小岩 编辑&#xff1a;彩云 英伟达不完全属于AI行业&#xff0c;但神奇的是&#xff0c;整个AI领域都有着英伟达的传说。因为几乎所有的AI巨头都需要英伟达的芯片来提供算力支持。 也正因此&#xff0c;纵使AI赛道人来人往&#xff0c;此起彼伏&#xff0c;…

zabbix企业微信接入结合海螺问问编写的shell脚本

前言 博客懒得写详细了&#xff0c;视频剪的累死了&#xff0c;看视频就好了 白帽小丑的个人空间-白帽小丑个人主页-哔哩哔哩视频 shell脚本 #!/bin/bash #set -x CorpID"" #我的企业下面的CorpID Secret"" #创建的应用那…

阿里云服务器计算型、通用型、内存型各实例计算、存储等性能介绍

在阿里云目前的活动中&#xff0c;属于计算型实例规格的云服务器有计算型c7、计算型c7a、计算型c8a、计算型c8y这几个实例规格&#xff0c;属于通用型实例规格的云服务器有通用型g7、通用型g7a、通用型g8a、通用型g8y&#xff0c;属于内存型实例规格的云服务器有内存型r7、内存…

Linux信号机制(二)

目录 一、信号的阻塞 二、信号集操作函数 三、sigprocmask函数 四、pause函数 五、sigsuspend函数 一、信号的阻塞 有时候不希望在接到信号时就立即停止当前执行&#xff0c;去处理信号&#xff0c;同时也不希望忽略该信号&#xff0c;而是延时一段时间去调用信号处理函数。…

【闲聊】-后端框架发展史

框架&#xff0c;是为了解决系统复杂性&#xff0c;提升开发效率而产生的工具&#xff0c;主要服务于研发人员。 当然&#xff0c;框架还有更深层的作用&#xff0c;框架的沉淀是一种高级的抽象&#xff0c;会将人类的业务逐步抽象为统一标准又灵活可变的结构&#xff0c;为各行…

Java-CAS 原理与 JUC 原子类

由于 JVM 的 synchronized 重量级锁涉及到操作系统&#xff08;如 Linux&#xff09; 内核态下的互斥锁&#xff08;Mutex&#xff09;的使用&#xff0c; 其线程阻塞和唤醒都涉及到进程在用户态和到内核态频繁切换&#xff0c; 导致重量级锁开销大、性能低。 而 JVM 的 synchr…