Java實現圖書館借閱系統(tǒng)
本文實例為大家分享了Java實現圖書館借閱系統(tǒng)的具體代碼,供大家參考,具體內容如下
Main.java
package com.src1.booksystem;
import com.src1.booksystem.booklist.BookList;
import com.src1.booksystem.users.AdminUser;
import com.src1.booksystem.users.NormalUser;
import com.src1.booksystem.users.User;
import java.util.Scanner;
public class Main {
?? ?public static void main(String[] args) {
?? ??? ?//1.準備書籍
?? ??? ?BookList bookList = new BookList();
?? ??? ?//2登錄
?? ??? ?User user = login();
?? ??? ?while ( true ) {
?? ??? ??? ?int chioce = user.menu();
?? ??? ??? ?//3.根據選擇調用方法
?? ??? ??? ?user.doOperation(chioce, bookList);
?? ??? ?}
?? ?}
?? ?public static User login() {
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?System.out.println("請輸入用戶名:");
?? ??? ?String name = scanner.nextLine();
?? ??? ?if ( "admin".equals(name) ) {
?? ??? ??? ?System.out.println("請輸入管理員密碼:");
?? ??? ??? ?String password = scanner.nextLine();
?? ??? ??? ?if ( "admin".equals(password) ) {
?? ??? ??? ??? ?System.out.println("hello! 尊敬的管理員! 歡迎來到藏經閣");
?? ??? ??? ??? ?return new AdminUser(name);
?? ??? ??? ?} else {
?? ??? ??? ??? ?System.out.println("密碼錯誤,進入用戶端!");
?? ??? ??? ??? ?System.out.println("hello! "+ name+" 歡迎來到藏經閣");
?? ??? ??? ??? ?return new NormalUser(name);
?? ??? ??? ?}
?? ??? ?}else {
?? ??? ??? ?System.out.println("hello! "+ name+" 歡迎來到藏經閣");
?? ??? ??? ?return new NormalUser(name);
?? ??? ?}
?? ?}
}package user
AdminUser.java
package com.src1.booksystem.users;
import com.src1.booksystem.operation.*;
import java.util.Scanner;
public class AdminUser extends User {
?? ?@Override
?? ?public int menu() {
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?System.out.println("1.查找圖書");
?? ??? ?System.out.println("2.新增書籍");
?? ??? ?System.out.println("3.刪除書籍");
?? ??? ?System.out.println("4.書籍列表");
?? ??? ?System.out.println("0.退出系統(tǒng)");
?? ??? ?int chioce = scanner.nextInt();
?? ??? ?return chioce;
?? ?}
?? ?public AdminUser(String name) {
?? ??? ?super(name);
?? ??? ?this.operations = new IOperation[] {
?? ??? ??? ??? ?new ExitOperation(),
?? ??? ??? ??? ?new FindOperation(),
?? ??? ??? ??? ?new AddOperation(),
?? ??? ??? ??? ?new DelOperation(),
?? ??? ??? ??? ?new DisplayOperation()
?? ??? ?};
?? ?}
}NormalUser.java
package com.src1.booksystem.users;
import com.src1.booksystem.operation.*;
import java.util.Scanner;
public class NormalUser extends User {
?? ?@Override
?? ?public int menu() {
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?System.out.println("1.查找圖書");
?? ??? ?System.out.println("2.借閱書籍");
?? ??? ?System.out.println("3.歸還書籍");
?? ??? ?System.out.println("4.書籍列表");
?? ??? ?System.out.println("0.退出系統(tǒng)");
?? ??? ?int chioce = scanner.nextInt();
?? ??? ?return chioce;
?? ?}
?? ?public NormalUser(String name) {
?? ??? ?super(name);
?? ??? ?this.operations = new IOperation[] {
?? ??? ??? ??? ?new ExitOperation(),
?? ??? ??? ??? ?new FindOperation(),
?? ??? ??? ??? ?new BorrowOperation(),
?? ??? ??? ??? ?new ReturnOperation(),
?? ??? ??? ??? ?new DisplayOperation()
?? ??? ?};
?? ?}
}User.java
package com.src1.booksystem.users;
import com.src1.booksystem.booklist.BookList;
import com.src1.booksystem.operation.IOperation;
abstract public class User {
?? ?public String name;
?? ?public IOperation[] operations;
?? ?/**
?? ? * 如果沒有menu 就不能通過user訪問
?? ? */
?? ?public abstract int menu() ;
?? ?public void doOperation(int chioce, BookList bookList){
?? ??? ?//數組當中元素的對象,通過.調用對應operation方法
?? ??? ?operations[chioce].work(bookList);
?? ?}
?? ?public User(String name) {
?? ??? ?this.name = name;
?? ?}
}package book
book.java
package com.src1.booksystem.book;
public class Book {
?? ?private String name;
?? ?private String author;
?? ?private int price;
?? ?private String type;
?? ?private boolean isBorrowed;
?? ?public Book(String name, String author, int price) {
?? ??? ?this.name = name;
?? ??? ?this.author = author;
?? ??? ?this.price = price;
?? ?}
?? ?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 boolean isBorrowed() {
?? ??? ?return isBorrowed;
?? ?}
?? ?public void setBorrowed(boolean borrowed) {
?? ??? ?isBorrowed = borrowed;
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return "[" +
?? ??? ??? ??? ?"書名:" + name +
?? ??? ??? ??? ?", 作者:" + author +
?? ??? ??? ??? ?", 價格:" + price + ","+
?? ??? ??? ??? ?(isBorrowed ? "已借出":"未借出")+"]";
?? ?}
}package booklist
BookList.java
package com.src1.booksystem.booklist;
import com.src1.booksystem.book.Book;
public class BookList {
?? ?private ? Book[] books;
?? ?private int usedSize;
?? ?public BookList() {
?? ??? ?this.books = new Book[10];
?? ??? ?this.books[0] = new Book("西游記","吳承恩",13);
?? ??? ?this.books[1] = new Book("水滸傳","施耐庵",14);
?? ??? ?this.books[2] = new Book("三國演義","羅貫中",15);
?? ??? ?this.usedSize = 3;
?? ?}
?? ?public int getUsedSize() {
?? ??? ?return usedSize;
?? ?}
?? ?public ?void setUsedSize(int usedSize) {
?? ??? ?this.usedSize = usedSize;
?? ?}
?? ?public Book getBook(int pos) {
?? ??? ?return books[pos];
?? ?}
?? ?public void setBooks(int pos,Book book) {
?? ??? ?this.books[pos] = book;
?? ?}
}package operation
AddOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.book.Book;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
public class AddOperation implements ?IOperation{
?? ?@Override
?? ?public void work(BookList bookList) {
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?System.out.println("請輸入書名");
?? ??? ?String name = scanner.nextLine();
?? ??? ?System.out.println("請輸入作者");
?? ??? ?String author = scanner.nextLine();
?? ??? ?System.out.println("請輸入價格");
?? ??? ?int price = scanner.nextInt();
?? ??? ??? ?Book book = new Book(name, author, price);
?? ??? ??? ?int curSize = bookList.getUsedSize();
?? ??? ??? ?bookList.setBooks(curSize, book);
?? ??? ??? ?bookList.setUsedSize(curSize + 1);
?? ??? ??? ?System.out.println("新增成功");
?? ?}
}BorrowOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.book.Book;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
?? ?@Override
?? ?public void work(BookList bookList) {
?? ??? ?System.out.println("借閱書籍");
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?System.out.println("請輸入你要借閱的圖書:");
?? ??? ?String name = scanner.nextLine();
?? ??? ?for (int i = 0; i < bookList.getUsedSize(); i++) {
?? ??? ??? ?Book book = bookList.getBook(i);
?? ??? ??? ?if(book.getName().equals(name)) {
?? ??? ??? ??? ?if(book.isBorrowed()) {
?? ??? ??? ??? ??? ?System.out.println("此書已經借出了!");
?? ??? ??? ??? ??? ?return;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?book.setBorrowed(true);
?? ??? ??? ??? ?System.out.println("借閱成功!");
?? ??? ??? ??? ?return;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?System.out.println("沒系統(tǒng)未收錄這本書!");
?? ?}
}DelOperation
未完善
package com.src1.booksystem.operation;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
import com.src1.booksystem.book.Book;
public class DelOperation implements IOperation{
?? ?@Override
?? ?public void work(BookList bookList) {
?? ??? ?System.out.println("請輸入要刪除的書名:");
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?String delbook = scanner.nextLine();
?? ??? ?int size = bookList.getUsedSize();
?? ??? ?for (int i = 0; i < size; i++) {
?? ??? ??? ?Book book = bookList.getBook(i);
?? ??? ??? ?if(book.getName().equals(delbook)) {
?? ??? ??? ??? ?if(i+1<size) {
?? ??? ??? ??? ??? ?bookList.setBooks(i, bookList.getBook(i + 1));
?? ??? ??? ??? ?}
?? ??? ??? ??? ?bookList.setUsedSize(size-1);
?? ??? ??? ??? ?if(i+2 < size) {
?? ??? ??? ??? ??? ?bookList.setBooks(i+1, bookList.getBook(i + 2));
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?System.out.println("刪除成功!");
?? ?}
}修改
public class DelOperation implements IOperation{
?? ?@Override
?? ?public void work(BookList bookList) {
?? ??? ?System.out.println("請輸入要刪除的書名:");
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?String delbook = scanner.nextLine();
?? ??? ?int size = bookList.getUsedSize();
?? ??? ?int pos = 0;
?? ??? ?int i = 0;
?? ??? ?for ( i = 0; i < size; i++) {
?? ??? ??? ?Book book = bookList.getBook(i);
?? ??? ??? ?if(book.getName().equals(delbook)) {
?? ??? ??? ??? ?pos = i;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if(i == size){
?? ??? ??? ?System.out.println("系統(tǒng)中沒有這本書");
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?for(int j = pos; j < size-1; j++) {
?? ??? ??? ?bookList.setBooks(j, bookList.getBook(j + 1));
?? ??? ?}
?? ??? ?bookList.setUsedSize(size-1);
?? ??? ?System.out.println("刪除成功!");
?? ?}
}DisplayOperation
import com.src1.booksystem.booklist.BookList;
public class DisplayOperation implements IOperation{
?? ?@Override
?? ?public void work(BookList bookList) {
?? ??? ?for (int i = 0; i < bookList.getUsedSize(); i++) {
?? ??? ??? ?System.out.println(bookList.getBook(i));
?? ??? ?}
?? ?}
}ExitOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.booklist.BookList;
public class ExitOperation implements IOperation {
?? ?@Override
?? ?public void work(BookList bookList) {
?? ??? ?System.exit(0);
?? ?}
}FindOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.book.Book;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
?? ?@Override
?? ?public void work(BookList bookList) {
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?System.out.println("請輸入要查找的書名");
?? ??? ?String name = scanner.nextLine();
?? ??? ?for( int i = 0; i < bookList.getUsedSize(); i++) {
?? ??? ??? ?Book book = bookList.getBook(i);
?? ??? ??? ?if(book.getName().equals(name)) {
?? ??? ??? ??? ?System.out.println(book);
?? ??? ??? ??? ?return;
?? ??? ??? ?}
?? ??? ?}
?? ?}
}ReturnOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.book.Book;
import com.src1.booksystem.booklist.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
?? ?@Override
?? ?public void work(BookList bookList) {
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?System.out.println("請輸入你要歸還的書名:");
?? ??? ?String name = scanner.nextLine();
?? ??? ?for (int i = 0; i < bookList.getUsedSize(); i++) {
?? ??? ??? ?Book book = bookList.getBook(i);
?? ??? ??? ?if(book.getName().equals(name)) {
?? ??? ??? ??? ?//說明有這本書
?? ??? ??? ??? ?book.setBorrowed(false);
?? ??? ??? ??? ?System.out.println("歸還成功!");
?? ??? ??? ??? ?return;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?System.out.println("系統(tǒng)未找到本書的相關信息!");
?? ?}
}接口 IOperation
package com.src1.booksystem.operation;
import com.src1.booksystem.booklist.BookList;
public interface IOperation {
?? ? void work(BookList bookList);
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java中報錯org.springframework.jdbc.UncategorizedSQLException的多種
本文主要介紹了Java中報錯org.springframework.jdbc.UncategorizedSQLException的多種解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06
Java Swing GridBagLayout網格袋布局的實現
這篇文章主要介紹了Java Swing GridBagLayout網格袋布局的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12
springboot項目打成jar包后無法獲取static下的靜態(tài)資源文件的問題分析
這篇文章主要介紹了springboot項目打成jar包后無法獲取static下的靜態(tài)資源文件的問題分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08

