赞
踩
所需要的知识:类,对象,抽象类,接口,封装,继承,多态,顺序表
源码链接:
创建 关于书的 包
有书名,作者,价格,类型,以及是否借出等信息,这里用private进行封装,可有效保护代码隐私。
- class Books{
- private String name;//书名
- private String author;//作者
- private String price;//价格
- private String type;//类型
- private boolean isBorrowed;//是否借出
- }
写出构造方法
注意:对于isBorrowed的Boolean类型,无需写进构造方法之中。
- public class Book {
- private String name;//书名
- private String author;//作者
- private String price;//价格
- private String type;//类型
- private boolean isBorrowed;//是否借出
-
- public Book(String name, String author, String price, String type) {
- this.name = name;
- this.author = author;
- this.price = price;
- this.type = type;
- }
- }
为了访问被private封装的属性,必须提供getter与setter方法。
并且再提供一个toString方法
- public class Book {
- private String name;//书名
- private String author;//作者
- private String price;//价格
- private String type;//类型
- private boolean isBorrowed;//是否借出
-
- public Book(String name, String author, String price, String type) {
- this.name = name;
- this.author = author;
- this.price = price;
- this.type = type;
- }
-
- //
- 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 String getPrice() {
- return price;
- }
-
- public void setPrice(String price) {
- this.price = price;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public boolean isBorrowed() {
- return isBorrowed;
- }
-
- public void setBorrowed(boolean borrowed) {
- isBorrowed = borrowed;
- }
-
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price='" + price + '\'' +
- ", type='" + type + '\'' +
- ", isBorrowed=" + isBorrowed +
- '}';
- }
- }
- package books;
-
- public class BookList {
- private Book[] books = new Book[10];
- private int usedSize;
-
- public BookList(){
- books[0] = new Book("三国演义","罗贯中",30,"小说");
- books[1] = new Book("水浒传","施耐庵",40,"小说");
- books[2] = new Book("红楼梦","曹雪芹",50,"小说");
- books[3] = new Book("西游记","吴承恩",60,"小说");
-
- this.usedSize = 4;
- }
-
- public int getUsedSize() {
- return usedSize;
- }
-
- public void setUsedSize(int usedSize) {
- this.usedSize = usedSize;
- }
- }
获取pos位置的书,以及在pos位置设置为一本书(添加一本书)
- //获取pos位置的书
- public Book getPos(int pos){
- return this.books[pos];
- }
- //设置pos下标为一本书
- public void setBook(int pos,Book book){
- this.books[pos] = book;
- }
对于使用者来说,既有管理员,也有普通用户。管理员与普通用户的目的是不一样的,但是他们有一些相同的特点。
他们相同特点,比如都有名字
- package user;
-
- import java.security.PrivateKey;
-
-
- public class User {
- protected String name;
- public User(String name){
- this.name = name;
- }
- }
由于在主程序中需要对于User类中填写menu(菜单)方法,且并不实现menu,所以将User改为抽象类
protected IOperation[] iOperations;
填写如下代码,创建数组
protected IOperation[] iOperations;
然后创建doWork方法,来实现操作
public void doWork(int choice, BookList bookList){ iOperations[choice].work(bookList); }
创建AdminUser类,继承User类
- package user;
-
-
- public class AdminUser extends User{
- public AdminUser(String name){
- super(name);
- }
- }
管理员菜单:
- package user;
-
- import java.util.Scanner;
-
- public class AdminUser extends User{
- public AdminUser(String name){
- super(name);
- }
-
- public int menu(){
- System.out.println("==========管理员菜单==========");
- System.out.println("hello "+this.name+"欢迎来到这里!:>");
- System.out.println("1.查找图书:>");
- System.out.println("2.新增图书:>");
- System.out.println("3.删除图书:>");
- System.out.println("4.展示图书:>");
- System.out.println("0.退出系统:>");
- System.out.println("============================");
-
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- return choice;
- }
- }
创建数组,为后续完成1-4的各项操作
- 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 DisplayOperation()
- };
- }
-
-
- public int menu(){
- System.out.println("==========管理员菜单==========");
- System.out.println("hello "+this.name+"欢迎来到这里!:>");
- System.out.println("1.查找图书:>");
- System.out.println("2.新增图书:>");
- System.out.println("3.删除图书:>");
- System.out.println("4.展示图书:>");
- System.out.println("0.退出系统:>");
- System.out.println("============================");
-
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- return choice;
- }
-
-
- }
创建NormalUser类,继承User类
- package user;
-
-
- public class NormalUser extends User{
- public NormalUser(String name){
- super(name);
- }
- }
普通用户菜单:
- package user;
-
- import java.util.Scanner;
-
- public class NormalUser extends User{
- public NormalUser(String name){
- super(name);
- }
-
- public int menu(){
- System.out.println("==========普通用户菜单==========");
- System.out.println("hello "+this.name+"欢迎来到这里!:>");
- System.out.println("1.查找图书:>");
- System.out.println("2.借阅图书:>");
- System.out.println("3.归还图书:>");
- System.out.println("0.退出系统:>");
- System.out.println("==============================");
-
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- return choice;
- }
- }
-
创建数组,为后续完成1-3的各项操作
- 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 BorrowOperation(),
- new ReturnOperation()
-
- };
- }
-
-
- public int menu(){
- System.out.println("==========普通用户菜单==========");
- System.out.println("hello "+this.name+"欢迎来到这里!:>");
- System.out.println("1.查找图书:>");
- System.out.println("2.借阅图书:>");
- System.out.println("3.归还图书:>");
- System.out.println("0.退出系统:>");
- System.out.println("==============================");
-
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- return choice;
- }
- }
并且写一些类,去一一实现
在实现每一个类之前,可以写一个接口,来使后面的代码便于书写
- package operation;
-
- import books.BookList;
-
- public interface IOperation {
- void work(BookList bookList);
- }
源码
- package operation;
-
- import books.Book;
- import books.BookList;
-
- public class AddOperation implements IOperation {
- public void work(BookList bookList){
- System.out.println("新增图书:>");
- System.out.println("请输入图书的名字:>");
- String name = scanner.nextLine();
-
- System.out.println("请输入图书的作者:>");
- String author = scanner.nextLine();
-
- System.out.println("请输入图书的类型:>");
- String type = scanner.nextLine();
-
- System.out.println("请输入图书的价格:>");
- double price = scanner.nextDouble();
-
- Book book = new Book(name,author,price,type);
- int size = bookList.getUsedSize();
- bookList.setBook(size,book);
-
- bookList.setUsedSize(size+1);
- System.out.println("图书新增成功!:>");
-
- }
- }
源码
- package operation;
-
- import books.Book;
- import books.BookList;
-
- public class BorrowOperation implements IOperation{
- public void work(BookList bookList){
- System.out.println("借阅图书:>");
-
- System.out.println("你想借阅什么书? :>");
- String name = scanner.nextLine();
-
- int size = bookList.getUsedSize();
- for(int i = 0; i < size; i++){
- Book book = bookList.getPos(i);
- if(name.equals(book.getName())){
- book.setBorrowed(true);
- System.out.println("借阅成功! :>");
- System.out.println(book);
- return;
- }
- }
- System.out.println("没有你要借阅的这本书:>");
-
- }
- }
源码
- package operation;
-
- import books.Book;
- import books.BookList;
-
- public class DelOperation implements IOperation{
- public void work(BookList bookList){
- System.out.println("删除图书:>");
-
- System.out.println("请输入你要删除的书名:>");
- String name = scanner.nextLine();
- int currentSize = bookList.getUsedSize();
- int i;
- int index = 0;
- for (i = 0; i < currentSize; i++) {
- Book book = bookList.getPos(i);
- if(book.getName().equals(name)){
- index = i;
- break;
- }
- }
- if(i >= currentSize){
- System.out.println("没有你要删除的书");
- return;
- }
- for (int j = index; j < currentSize-1; j++) {
- Book book = bookList.getPos(j+1);
- bookList.setBook(j,book);
- }
- bookList.setBook(currentSize,null);
- bookList.setUsedSize(currentSize-1);
- System.out.println("删除成功!:>");
-
- }
- }
源码
- package operation;
-
- import books.Book;
- import books.BookList;
-
- public class DisplayOperation implements IOperation{
- public void work(BookList bookList){
- System.out.println("展示图书:>");
-
- int size = bookList.getUsedSize();
- for(int i = 0;i < size;i++){
- Book book = bookList.getPos(i);
- System.out.println(book);
- }
- }
- }
源码
- package operation;
-
- import books.BookList;
-
- public class ExitOperation implements IOperation{
- public void work(BookList bookList){
- System.out.println("退出系统:>");
- System.exit(0);
- }
- }
- package operation;
-
- import books.Book;
- import books.BookList;
-
- public class FindOperation implements IOperation{
- public void work(BookList bookList){
- System.out.println("查找图书:>");
- String name = scanner.nextLine();
- int size = bookList.getUsedSize();
- for(int i = 0; i < size; i++){
- Book book = bookList.getPos(i);
- if(name.equals(book.getName())){
- System.out.println("找到了这本书,信息如下:>");
- System.out.println(book);
- return;
- }
- }
- System.out.println("没有找到这本书:>");
- }
- }
- package operation;
-
- import books.Book;
- import books.BookList;
-
- public class ReturnOperation implements IOperation{
- public void work(BookList bookList){
- System.out.println("归还图书:>");
-
- System.out.println("你想归还什么书? :>");
- String name = scanner.nextLine();
-
- int size = bookList.getUsedSize();
- for(int i = 0; i < size; i++){
- Book book = bookList.getPos(i);
- if(name.equals(book.getName())){
- book.setBorrowed(false);
- System.out.println("归还成功! :>");
- System.out.println(book);
- return;
- }
- }
- System.out.println("没有你要归还的这本书:>");
- }
- }
创建登录方法,并且在主函数中用user接收
- import user.AdminUser;
- import user.NormalUser;
- import user.User;
-
- import java.util.Scanner;
- public class Main {
- public static User login(){
- System.out.println("请输入你的姓名:>");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- System.out.println("请输入你的身份:>");
- System.out.println("1.管理员 0.普通用户");
- int choice = scanner.nextInt();
- if(choice == 1){
- return new AdminUser(name);
- }else{
- return new NormalUser(name);
- }
- }
-
- public static void main(String[] args) {
- User user = login();//向上转型
- int choice = user.menu();//动态绑定
- }
- }
此时,对于user。无法直接获取菜单。应在父类(User)中,写出菜单方法
然后在,主程序中使用user调用menu。
最终的主函数代码:
- import books.Book;
- import books.BookList;
- import user.AdminUser;
- import user.NormalUser;
- import user.User;
-
- import java.util.Scanner;
-
- public class Main {
- public static User login(){
- System.out.println("请输入你的姓名:>");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- System.out.println("请输入你的身份:>");
- System.out.println("1.管理员 0.普通用户");
- int choice = scanner.nextInt();
- if(choice == 1){
- return new AdminUser(name);
- }else{
- return new NormalUser(name);
- }
- }
-
- public static void main(String[] args) {
- BookList bookList = new BookList();
- User user = login();//向上转型
- while (true) {
- int choice = user.menu();//动态绑定
- user.doWork(choice, bookList);
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。