Java 图书馆计划-分配及;借书

Java 图书馆计划-分配及;借书,java,eclipse,string,Java,Eclipse,String,我应该用java创建一个图书馆程序,允许您创建用户并最多签出3本书。我真的是java初学者,所以我很抱歉我的代码到处都是,可能没有意义 下面是我尝试的library类(我还有一个单独的用户、图书和图书接口类) 我主要关注的是: 我有两个ArrayList,一个用于输入用户列表,另一个用于输入书籍列表。但是,我如何才能将某些已签出的图书分配给某个用户,并确保他们借阅的图书不超过3本 我在main方法中放了很多代码,但是我最终在静态和非静态方面遇到了很多问题 我如何才能为每本书创建“状态”?例如,

我应该用java创建一个图书馆程序,允许您创建用户并最多签出3本书。我真的是java初学者,所以我很抱歉我的代码到处都是,可能没有意义

下面是我尝试的library类(我还有一个单独的用户、图书和图书接口类) 我主要关注的是:

  • 我有两个ArrayList,一个用于输入用户列表,另一个用于输入书籍列表。但是,我如何才能将某些已签出的图书分配给某个用户,并确保他们借阅的图书不超过3本

  • 我在main方法中放了很多代码,但是我最终在静态和非静态方面遇到了很多问题

  • 我如何才能为每本书创建“状态”?例如,如果“远大前程”已签出,如何为其指定“借来的”并确保没有其他人可以借来

该程序运行至今,但缺乏深度,因为我不知道如何在某个指定的用户下签出/签入图书

再次为我的代码中的所有不一致感到抱歉,我真的非常感谢您的帮助

导入java.awt.List;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Scanner;
导入java.util.Collections;
公共班级图书馆
{
静态ArrayList UserList=新ArrayList();
静态ArrayList BookList=newArrayList();
公共静态字符串状态;
公共静态字符串借用器;
公共静态字符串日期;
公共静态字符串返回日期;
公共字符串status1=“可用”;
公共字符串status2=“借用”;
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
int-choice=0;
System.out.println(“**********************欢迎来到公共图书馆!”;
System.out.println(“请从以下选项中选择:”);
System.out.println(“*************************************************************************************************************************”);
while(选项!=9)
{
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:签出簿”);
System.out.println(“8:登记簿”);
System.out.println(“9:SearchBook”);
System.out.println(“10:搜索用户”);
System.out.println(“9:Exit”);
choice=input.nextInt();
开关(选择)
{
案例1://添加新用户
系统输出打印(“输入用户的名字:”);
String firstName=input.next();//从输入中读取名称
系统输出打印(“输入用户姓氏:”);
字符串lastName=input.next();
UserList.add(新用户(firstName,lastName));//将名称添加到列表中
System.out.println(“----您已成功添加新用户!”;
打破
案例2://添加新书
系统输出打印(“输入书名:”);
字符串title1=input.next();
扫描仪输入1=新扫描仪(System.in);
系统输出打印(“输入图书作者:”;
字符串author1=input.next();
Book book1=新书(标题1);
图书目录。增加(标题1);
FullBookList.add(fullBook);
System.out.println(“----您已成功添加新书!”;
status=“可用”;
借款日期=“无”;
returnDate=“无”;
借款人=“无”;
打破
案例3://编辑用户名称
System.out.println(“输入原始用户名称:”);
String originalName=input.next();
System.out.println(“输入编辑的用户名称:”);
String editedName=input.next();
//Collections.replaceAll(UserList、originalName、editedName);
if(UserList.contains(originalName))
{
}
案例4://编辑书籍
案例5://显示所有用户
System.out.println(用户列表);
打破
案例6://显示所有书籍
系统输出打印项次(书目);
打破
案例7://看看书
顾客。结账本();
打破
案例8://签入一本书
用户。签入本();
打破
}
}
}

}
注意:这可能需要对主循环进行一些重构。

好吧,在我看来,我们有三个类在这里发挥作用:一些顾客,可以签出和签入书籍,一些书籍,有“可用”和“签出”的状态,还有一个图书馆,其中包含书籍。因此,我们需要3节课:

我将从这本书开始,使用伪代码来解释要实现的概念

class Book
{
    //private fields
    private final String title;
    private final String author;
    private Status available = true;
    //note--i would prefer using an Enum called status for this, 
    //but a boolean true/false value works adequately

    //Constructor
    public Book(string title, string author) {}

    //accessors for title, author, available
    //setter for available--used for Library only--there are better ways to ensure
    //Patrons can't set the status of the book, but for now this is the simplest way
}
如您所见,书籍具有不需要更改的不可变字段,以及一个跟踪其状态的字段。一个更好的实现可能会使库成为跟踪簿状态,因为这样做更有逻辑意义,代码也更好,但这是一个简单的实现

其次是图书馆,它需要很多书:

class Library
{
    private final ArrayList<Book> books;

    //Constructor
    public Library ()
    {
        books = loadBooks();
    }

    //some methods
    private ArrayList<Book> loadBooks () {}
    //however you want to create all your books (file input, whatever)

    public bool isBookAvailable (Book b)
    {
        if b isn't in library: return false
        else return (b in books).isAvailable()
    }

    public Book checkoutBook (Book b)
    { get book (checking availability, possibly returning a null Book), set status to unavailable, return it }

    public Book checkinBook (Book b)
    { check that this the book belongs to library, set status to available }
}
类库
{
私人最终ArrayList书籍;
//建造师
公共图书馆()
{
图书=负荷书();
}
//一些方法
专用ArrayList loadBooks(){}
//但是,您希望创建所有书籍(文件输入,无论什么)
公共图书馆isBookAvailable(b册)
{
如果b不在库中:返回false
else返回(b在帐簿中)。isAvailable()
}
公共借书簿(b册)
{获取书本(检查可用性,可能返回空书本),将状态设置为不可用,返回它}
公共图书登记簿(b册)
{检查这本书是否属于图书馆,将状态设置为可用}
}
正如我之前所说,这并不完美。我可以花相当多的时间不断地讨论如何改进设计,但为了简单起见,我不会

现在,顾客们。一个问题是,用户应该只有一个图书馆可以访问吗?或者他们访问多个图书馆?我想他们会拜访不止一个,是吗
class Patron
{
    private final String name;
    private final Book[] books = new Book[3];//you can see I'm limiting them to 3 books
    private int index = 0;

    //Constructor
    public Patron (String name) {}

    //methods
    public void checkoutBook (Book b, Library l)
    {//could be tricky
        check books status in l (l.isBookAvailable(b))
        if available: 
            if space (index < 2) Book newBook = l.checkoutBook(b); books[index++] = newBook;
            else: no space
        else: not available
    }

    public void checkinBook (int bookIndex, Library l)
    {
         if bookIndex < 3:
             if books[index] != null:
                 l.checkinBook (books[index]);
                 books[index--] = null;
             else: no book
         else: not valid index
    }
}
Note: At the time it can store only 50 books for simlicity in program
package library;

import java.util.Scanner;



public class book {

public int sNo;
public String bookName;
public String authorName;
public int bookQty;
public int bookQtyCopy;

Scanner input = new Scanner(System.in);

public book(){

    System.out.println("Enter Serial No of Book:");
    this.sNo = input.nextInt();
    input.nextLine();
    System.out.println("Enter Book Name:");
    this.bookName = input.nextLine();
    System.out.println("Enter Author Name:");
    this.authorName = input.nextLine();
    System.out.println("Enter Quantity of Books:");
    this.bookQty = input.nextInt();
    bookQtyCopy = this.bookQty;

}

}
package library;

import java.util.Scanner;

public class books {

book theBooks[] = new book[50];     // Array that stores 'book' Objects.
public static int count;    // Counter for No of book objects Added in Array.

Scanner input = new Scanner(System.in);




public int compareBookObjects(book b1, book b2){

    if (b1.bookName.equalsIgnoreCase(b2.bookName)){

        System.out.println("Book of this Name Already Exists.");
        return 0;

    }
    if (b1.sNo==b2.sNo){

        System.out.println("Book of this Serial No Already Exists.");
        return 0;
    }
    return 1;
}

public void addBook(book b){

    for (int i=0; i<count; i++){

        if (this.compareBookObjects(b, this.theBooks[i]) == 0)
            return;

    }

    if (count<50){

        theBooks[count] = b;
        count++;

    }
    else{

        System.out.println("No Space to Add More Books.");

    }

}

public void searchBySno(){

    System.out.println("\t\t\t\tSEARCH BY SERIAL NUMBER\n");

    int sNo;
    System.out.println("Enter Serial No of Book:");
    sNo = input.nextInt();

    int flag = 0;
    System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
    for (int i=0; i<count; i++){

        if (sNo == theBooks[i].sNo){

            System.out.println(theBooks[i].sNo + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + 
                theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty);
            flag++;
            return;

        }

    }
    if (flag == 0)
        System.out.println("No Book for Serial No " + sNo + " Found.");

}

public void searchByAuthorName(){

    System.out.println("\t\t\t\tSEARCH BY AUTHOR'S NAME");
    input.nextLine();
    System.out.println("Enter Author Name:");
    String authorName = input.nextLine();
    int flag = 0;
    System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
    for (int i=0; i<count; i++){

        if (authorName.equalsIgnoreCase(theBooks[i].authorName)){

            System.out.println(theBooks[i].sNo + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + 
                theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty);
            flag++;
        }

    }
    if (flag == 0)
        System.out.println("No Books of " + authorName + " Found.");

}


public void showAllBooks(){

    System.out.println("\t\t\t\tSHOWING ALL BOOKS\n");
    System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
    for (int i=0; i<count; i++){

        System.out.println(theBooks[i].sNo + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + 
                theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty);


    }

}

public void upgradeBookQty(){

    System.out.println("\t\t\t\tUPGRADE QUANTITY OF A BOOK\n");
    System.out.println("Enter Serial No of Book");
    int sNo = input.nextInt();
    for (int i=0; i<count; i++){

        if (sNo == theBooks[i].sNo){

            System.out.println("Enter No of Books to be Added:");
            int addingQty = input.nextInt();
            theBooks[i].bookQty += addingQty;
            theBooks[i].bookQtyCopy += addingQty;
            return;

        }

    }

}


public void dispMenu(){

    System.out.println("----------------------------------------------------------------------------------------------------------");
    System.out.println("Enter 0 to Exit Application.");
    System.out.println("Enter 1 to Add new Book.");
    System.out.println("Enter 2 to Upgrade Quantity of a Book.");
    System.out.println("Enter 3 to Search a Book.");
    System.out.println("Enter 4 to Show All Books.");
    System.out.println("Enter 5 to Register Student.");
    System.out.println("Enter 6 to Show All Registered Students.");
    System.out.println("Enter 7 to Check Out Book. ");
    System.out.println("Enter 8 to Check In Book");
    System.out.println("------------------------------------------------------------- 
   ---------------------------------------------");

}

public int isAvailable(int sNo){

    //returns the index number if available



    for (int i=0; i<count; i++){

        if (sNo == theBooks[i].sNo){
            if(theBooks[i].bookQtyCopy > 0){

                System.out.println("Book is Available.");
                return i;

            }
            System.out.println("Book is Unavailable");
            return -1;

        }

    }

    System.out.println("No Book of Serial Number " + " Available in Library.");
    return -1;


}

public book checkOutBook(){

    System.out.println("Enter Serial No of Book to be Checked Out.");
    int sNo = input.nextInt();

    int bookIndex =isAvailable(sNo);

    if (bookIndex!=-1){

        //int bookIndex = isAvailable(sNo);
        theBooks[bookIndex].bookQtyCopy--;

        return theBooks[bookIndex];
    }

    return null;

}

public void checkInBook(book b){

    for (int i=0; i<count; i++){

        if (b.equals(theBooks[i]) ){

            theBooks[i].bookQtyCopy++;
            return;

        }

    }

}







 }
package library;

import java.util.Scanner;    
public class student {

String studentName;
String regNum;

book borrowedBooks[] = new book[3];
public int booksCount = 0;

Scanner input = new Scanner(System.in);

public student(){

    System.out.println("Enter Student Name:");
    this.studentName = input.nextLine();

    System.out.println("Enter Reg Number:");
    this.regNum = input.nextLine();

}
}
package library;

import java.util.Scanner;

public class students {

Scanner input = new Scanner(System.in);

student theStudents[] = new student[50];

//books book;


public static int count = 0;

public void addStudent(student s){

    for (int i=0; i<count; i++){

        if(s.regNum.equalsIgnoreCase(theStudents[i].regNum)){

            System.out.println("Student of Reg Num " + s.regNum + " is Already Registered.");
            return;
        }

    }

    if (count<=50){

        theStudents[count] = s;
        count++;

    }

}
public void showAllStudents(){

    System.out.println("Student Name\t\tReg Number");
    for (int i=0; i<count; i++){

        System.out.println(theStudents[i].studentName + "\t\t" + theStudents[i].regNum);

    }


}

public int isStudent(){
    //return index number of student if available

     //System.out.println("Enter Student Name:");
    //String studentName = input.nextLine();

    System.out.println("Enter Reg Number:");
    String regNum = input.nextLine();

    for (int i=0; i<count; i++){

        if (theStudents[i].regNum.equalsIgnoreCase(regNum)){

            return i;

        }

    }
    System.out.println("Student is not Registered.");
    System.out.println("Get Registered First.");


    return -1;

}
public void checkOutBook(books book){
    int studentIndex =this.isStudent();

    if (studentIndex!=-1){
        System.out.println("checking out");

        book.showAllBooks();//jjjjjjjjjjjj
        book b = book.checkOutBook();
        System.out.println("checking out");
        if (b!= null){

            if (theStudents[studentIndex].booksCount<=3){
                System.out.println("adding book");
                theStudents[studentIndex].borrowedBooks[theStudents[studentIndex].booksCount] = b;
                theStudents[studentIndex].booksCount++;
                return;

            }
            else {

                System.out.println("Student Can not Borrow more than 3 Books.");
                return;

            }
        }
        System.out.println("Book is not Available.");

    }

}

public void checkInBook(books book){

    int studentIndex = this.isStudent();
    if (studentIndex != -1){
        System.out.println("S.No\t\t\tBook Name\t\t\tAuthor Name");
        student s = theStudents[studentIndex];
        for (int i=0; i<s.booksCount; i++){

            System.out.println(s.borrowedBooks[i].sNo+ "\t\t\t" + s.borrowedBooks[i].bookName + "\t\t\t"+
                    s.borrowedBooks[i].authorName);

        }
        System.out.println("Enter Serial Number of Book to be Checked In:");
        int sNo = input.nextInt();
        for (int i=0; i<s.booksCount; i++){

            if (sNo == s.borrowedBooks[i].sNo){

                book.checkInBook(s.borrowedBooks[i]);
                s.borrowedBooks[i]=null;
                return;

            }


        }
        System.out.println("Book of Serial No "+sNo+"not Found");

    }



}


}
package library;

import java.util.Scanner;

public class Library {


public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("********************Welcome to the Student Library!********************");
    System.out.println("              Please Select From The Following Options:               ");
System.out.println("**********************************************************************");
    books ob = new books();
    students obStudent = new students();
    int choice;
    int searchChoice;

    do{

        ob.dispMenu();
        choice = input.nextInt();

        switch(choice){

            case 1:
                book b = new book();
                ob.addBook(b);
                break;

            case 2:
                ob.upgradeBookQty();
                break;

            case 3:
                System.out.println("Enter 1 to Search with Serial No.");
                System.out.println("Enter 2 to Search with Author Name(Full Name).");
                searchChoice = input.nextInt();

                switch(searchChoice){

                    case 1:
                        ob.searchBySno();
                        break;
                    case 2:
                        ob.searchByAuthorName();

                }
                break;

            case 4:
                ob.showAllBooks();
                break;
            case 5:
                student s = new student();
                obStudent.addStudent(s);
                break;
            case 6:
                obStudent.showAllStudents();
                break;
            case 7:
                obStudent.checkOutBook(ob);
                break;
            case 8:
                obStudent.checkInBook(ob);
                break;
            default:
                System.out.println("CHOICE SHOULD BE BETWEEN 0 TO 8.");

        }

    }
    while (choice!=0);
































}

}