In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Java sequence table how to achieve the book management system", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java sequence table how to achieve the library management system" bar!
I. brief introduction
The purpose of implementing this project is to consolidate and understand the previous knowledge points: classes, abstract classes, encapsulation, inheritance, polymorphism, interfaces, etc.
II. Core needs
Management side
consults books
adds books
deletes a book
prints a list of books
exits the system
Client end
query Books
borrows books
returns books
prints a list of books
exits the system
Third, the design of the class
1. Create a book class
The book class contains information such as the name, price, type, author and whether the book is lent, and generates constructors, Getter () and Setter () methods, and toString methods (note that member variables should be modified with the private keyword as much as possible)
Public class Book {private String name; private double price; private String type; private String author; private boolean isBorrowed; public Book (String name, double price, String type, String author) {this.name = name; this.price = price; this.type = type; this.author = author;} public String getName () {return name } public void setName (String name) {this.name = name;} public double getPrice () {return price;} public void setPrice (double price) {this.price = price;} public String getType () {return type;} public void setType (String type) {this.type = type;} public String getAuthor () {return author } public void setAuthor (String author) {this.author = author;} public boolean isBorrowed () {return isBorrowed;} public void setBorrowed (boolean borrowed) {isBorrowed = borrowed } @ Override public String toString () {return "Book {" + "name='" + name +'\'+ ", price=" + price + ", type='" + type +'\'+ ", author='" + author +'\'+ ", status:" + (isBorrowed)? "lent": "not lent") +'}';}}
two。 Create a book list class
The book list class is used to store books. We can initialize several books in the list to facilitate subsequent testing.
Public class BookList {private Book [] books = new Book [10]; private int usedSize; public BookList () {books [0] = new Book (Romance of the three Kingdoms, 19, novel, Luo Guanzhong); books [1] = new Book (Outlaws of the Marsh, 29, novel, Shi Nai an); books [2] = new Book (Journey to the West, 39, novel, Wu Chengen) 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 setBook (int pos,Book book) {books [pos] = book;}}
3. Create a user class
Create a user class and define it as an abstract class, and then create a normal user class and an administrator class that inherit from the user class:
Create a user class and define it as an abstract class:
Public abstract class User {protected String name; protected IOperation [] iOperations; public abstract int menu (); public void doWork (int choice, BookList bookList) {iOperations.work (bookList);} public User (String name) {this.name = name;}}
Create an administrator user class:
Public class AdminUser extends User {public AdminUser (String name) {super (name); this.iOperations = new IOperation [] {new ExitOperation (), new FindOperation (), new AddOperation (), new DisplayOperation (), new DelOperation ()} } @ Override public int menu () {System.out.println ("= administrator menu ="); System.out.println ("Hello, administrator" + this.name+ ":"); System.out.println ("Welcome to the library!"); System.out.println ("1. Find books "); System.out.println (" 2. Add books "); System.out.println (" 3. " Show books "); System.out.println (" 4. " Delete Book "); System.out.println (" 0. Exit the system "); System.out.println (" = "); Scanner scanner = new Scanner (System.in); int choice = scanner.nextInt (); return choice;}}
Create a normal user class:
Public class NormalUser extends User {public NormalUser (String name) {super (name); this.iOperations = new IOperation [] {new ExitOperation (), new DisplayOperation (), new FindOperation (), new BorrowOperation (), new ReturnOperation (),} } @ Override public int menu () {System.out.println ("= regular user menu ="); System.out.println ("Hello, user" + this.name+ ":"); System.out.println ("Welcome to the Library!"); System.out.println ("1. Show books "); System.out.println (" 2. " Find books "); System.out.println (" 3. Borrow books); System.out.println ("4. Return the book "); System.out.println (" 0. Exit the system "); System.out.println (" = "); Scanner scanner = new Scanner (System.in); int choice = scanner.nextInt (); return choice;}}
4. Create an operation-related class
First, create an interface to implement polymorphism:
Public interface IOperation {void work (BookList bookList);}
Create and add a book class:
Public class AddOperation implements IOperation {public void work (BookList bookList) {Scanner scanner = new Scanner (System.in); System.out.println ("Please enter Book name:"); String name = scanner.nextLine (); System.out.println ("Please enter Price:"); double price = scanner.nextDouble (); System.out.println ("Please enter Type:") String type = scanner.next (); System.out.println ("Please enter author:"); String author = scanner.next (); Book book = new Book (name,price,type,author); int usedSize = bookList.getUsedSize (); bookList.setBook (usedSize,book); bookList.setUsedSize (+ + usedSize); System.out.println ("add Book successful!");}}
Create a find book class:
Public class FindOperation implements IOperation {public void work (BookList bookList) {System.out.println ("Please enter the title of the book:"); Scanner scanner = new Scanner (System.in); String name = scanner.next (); for (int item0)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.