首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图书馆程序--图书的分配和借阅

图书馆程序--图书的分配和借阅
EN

Stack Overflow用户
提问于 2015-12-04 10:56:46
回答 1查看 30.4K关注 0票数 3

我应该用java创建一个库程序,允许您创建赞助者,最多可以查看3本书。我真的是java的初学者,所以我很抱歉,我的代码到处都是,可能没有意义。

下面是我尝试的图书馆类(我还有一个单独的赞助人、图书和图书接口类)我的主要关注点是:

  • 我有两个ArrayLists,一个用于输入用户列表,另一个用于输入图书列表。然而,我如何才能将某些已结账的书籍分配给某个用户&确保它们的借入不超过3本?
  • 我在主方法中输入了大量的代码,但是最后在静态和非静态的东西上出现了很多问题。
  • 我如何才能为每一本书创建状态?例如,如果“远大的期望”被检查出来,如何将“借来的”分配给它,并确保没有其他人可以借到它?

这个程序进行到目前为止,但缺乏深度,因为我不知道如何在某一特定赞助人的情况下查阅书籍。

再次为我的代码中的不一致之处感到抱歉,我真的很感谢您的帮助!

代码语言:javascript
复制
import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner; 
import java.util.Collections; 
public class Library 
{


	static ArrayList <Patron> UserList = new ArrayList<Patron>();
	static ArrayList <String> BookList = new ArrayList <String> (); 
	
	public static String status;
	public static String borrower; 
	public static String borrowDate; 
	public static String returnDate; 
	public String status1 = "Available";
	public String status2 = "Borrowed";
	
	
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		int choice = 0;
		System.out.println("********************Welcome to the Public Library!********************");
		System.out.println("              Please Select From The Following Options:               ");
		System.out.println("**********************************************************************");
		
		while(choice != 9)
		{
			System.out.println("1: Add new patron");
			System.out.println("2: Add new book");
			System.out.println("3: Edit patron");
			System.out.println("4: Edit book");
			System.out.println("5: Display all patrons");
			System.out.println("6: Display all books");
			System.out.println("7: Check out book");
			System.out.println("8: Check in book");
			System.out.println("9: Search book");
			System.out.println("10: Search Patron");
			System.out.println("9: Exit");
			choice = input.nextInt();

			
		switch(choice)
		{
		case 1: //Add new patron
			System.out.print("Enter patron first name: ");
			String firstName = input.next(); //read name from input
			System.out.print("Enter patron last name: ");
			String lastName = input.next(); 

			UserList.add(new Patron(firstName, lastName)); //add name to list
			System.out.println("-----You have successfully added a new patron!-----");
			break; 
							
		case 2: //Add new book
			System.out.print("Enter book title: ");
			String title1 = input.next();
				
			Scanner input1 = new Scanner(System.in);
			System.out.print("Enter book author: ");
			String author1 = input.next(); 

			Book book1 = new Book(title1);				
			BookList.add(title1);
			FullBookList.add(fullBook);
			System.out.println("-----You have successfully added a new book!-----");
			
			status = "available";
			borrowDate = "none";
			returnDate = "none";
			borrower = "none";
			
			break; 
			
		case 3: //Edit patron name
			System.out.println("Enter original patron name: ");
			String originalName = input.next(); 
			System.out.println("Enter edited patron name: ");
			String editedName = input.next(); 
			//Collections.replaceAll(UserList, originalName, editedName);
			if(UserList.contains(originalName))
			{
				
			}
				
		case 4: //edit book
			
			
		case 5: //display all patrons	
				System.out.println(UserList); 
				break; 
				
		case 6: //display all books 
				System.out.println(BookList); 
				break; 
				
		case 7: //check out a book
				Patron.CheckOutBook(); 
				break; 
		case 8: //check in a book
				Patron.CheckInBook(); 
				break; 
				
			
			}
		}
	}
}

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.Scanner;

public class Patron 
{
	Scanner input = new Scanner(System.in);
	private String first; 
	private String last; 
	int bookCount = 0;	//amount books user has in pocket
	int books = 0;
	
	
	//constructor to "add new patron" by entering their name. 
	public Patron(String f, String l)
	{
		first = f; 
		last = l; 
	}
	
	public String toString()
	{
		return first + " " + last; 
	}
	
	public String getName() 
	{
		return first +  " " + last; 
	}

	public static void CheckOutBook()
	{
		System.out.println("Enter book title to be check out: ");
		Scanner input = new Scanner(System.in);
		String bookCheckOut = input.next(); 
		if(Library.BookList.contains(bookCheckOut))
		{
			Library.BookList.remove(bookCheckOut);
			System.out.println("-----" + bookCheckOut + " has been checked out!-----");
			System.out.println ("-------" + bookCheckOut + " is due in 7 days!-------");
							
		}
		else 
			System.out.println(bookCheckOut + " is not in the library. Please enter "
			+ "a different book to be checked out");
		
	}
	
	public static void CheckInBook()
	{
		System.out.println("Enter book title to be checked in: ");
		Scanner input = new Scanner(System.in);
		String bookCheckIn = input.next(); 
		if(Library.BookList.contains(bookCheckIn))
		{
			Library.BookList.add(bookCheckIn);
			System.out.println("-----" + bookCheckIn + " has been checked in!-----");	
							
		}
		else 
			System.out.println(bookCheckIn + " is not in the library. Please enter "
			+ "a different book to be checked out");
	}
	
	public boolean canBorrow()
	{
		if(bookCount <= 3)
		{
			return true; 
		} 
		else 
		{
			return false; 
		}
	}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-05 17:25:04

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

好的,在我看来,我们这里有三门课:一些赞助者,可以结账,还有一些书,有“可用”和“退房”的状态,还有一个图书馆,里面有书。所以,我们需要3门课:

我将从Book开始,并使用伪代码来解释要实现的概念。

代码语言:javascript
复制
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
}

正如您所看到的,Books有不需要更改的不可变字段,还有一个跟踪它状态的字段。更好的实现可能会使库跟踪图书状态,因为这样做更有逻辑意义和更好的代码,但这是一个简单的实现。

接下来是图书馆,它需要大量的书籍:

代码语言:javascript
复制
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 }
}

就像我之前说的,这并不完美。我可以花相当长的时间来讨论如何改进设计,但为了简单起见,不会。

现在,顾客们。一个问题是,顾客是否应该只有一家图书馆可以参观?或者他们会访问多个图书馆?我假设他们不止一次访问,因为有时图书馆没有你想要的所有书。

代码语言:javascript
复制
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
    }
}

当然,其他实用工具,如显示书籍(库、赞助人)和toString方法可能是有用的。但是现在主要的方法是创建一些赞助者,一个图书馆,并给顾客一个通过菜单登记和登记书籍的机会。您已经完成了繁重的工作;您现在可以对输入和输出进行工作。

有什么问题吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34086737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档