目录
面向对象编程,首先分析需求,抽象出来对象。对具体的对象在进行细分,比如对象之间的分类,用不同的包去维护不同类别的对象,具体类别的对象还可以进行共性的抽取,后期开发也好进行一些操作。
🎈图书管理系统,有管理员和普通用户,书籍的信息,对书籍的查看,添加,修改等等一些操作。
🎈首先把普通用户和管理员,和书抽象出来对象。前两者和书可以分为两个类别。普通用户和管理员都是对系统的使用者,可以进行共性的抽取。对于书来说,只是一个单纯的对象,而对于图书管理系统来说需要对大量书籍进行管理,因此可以用这个对象设置一个书架,也就是数组,我们只需要对这个数组进行操作。对于书籍的一些操作,可以认为是一些标准的行为,可以实现一个接口,让每一个操作去实现这些接口。
核心分析
🪖最后需要我们把,各种对象,各种操作整合起来。根据用户选择来实例化管理员对象或者普通用户对象,用它们的父类引用类型接收(向上提升)。根据用户选择进行相应操作,那么就需要调用各种操作类里面的方法,管理员或者普通用户实例化对象时,将各种操作的对象存储起来,我们只需要选择不同对象去调用不同操作方法即可。
AdminUser类
- 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 ShowOperation(),
- };
- }
- public int menu() {
- System.out.println("欢迎" + 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 scan = new Scanner(System.in);
- int choose = scan.nextInt();
- return choose;
- }
-
- }
NormalUser类
- 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("欢迎" + this.name + "来到图书小练习");
- System.out.println("1. 查找图书");
- System.out.println("2. 借阅图书");
- System.out.println("3. 归还图书");
- System.out.println("0. 退出系统");
- System.out.println("请选择你的操作:");
- Scanner scan = new Scanner(System.in);
- int choose = scan.nextInt();
- return choose;
- }
- }
User抽象类
- package user;
-
- import book.BookList;
- import operation.IOperation;
-
- public abstract class User {
- protected String name;
-
- IOperation[] iOperations;
- public User(String name) {
- this.name = name;
- }
- public abstract int menu();
- public void doOpeation(int choose, BookList bookList) {
- iOperations[choose].work(bookList);
- }
- }
Main类(主类)
- import book.BookList;
- import user.AdminUser;
- import user.NormalUser;
- import user.User;
-
- import java.util.Scanner;
-
- public class Main {
- private static User login() {
-
- System.out.println("请输入你的姓名:");
- Scanner scan = new Scanner(System.in);
- String name = scan.nextLine();
- System.out.println("请输入你的身份 1. 管理员 0. 普通用户");
- int choose = scan.nextInt();
- if(choose == 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 choose = user.menu();
- user.doOpeation(choose,bookList);
- }
-
- }
- }
Book类
- package book;
-
- public class Book {
- private String name;
- private String author;
- private int price;
- private String type;
- private boolean isBorrow;
-
- public Book(String name, String author, int 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 int getPrice() {
- return price;
- }
-
- public void setPrice(int price) {
- this.price = price;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public Boolean getBorrow() {
- return isBorrow;
- }
-
- public void setBorrow(Boolean borrow) {
- isBorrow = borrow;
- }
-
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price=" + price +
- ", type='" + type + '\'' +
- ", isBorrow=" + (isBorrow == true ? " 已借出" :" 未借出")+
- '}';
- }
- }
BookList类
- package book;
-
- public class BookList {
- //引用数组
- private Book[] books = new Book[10];
- private int usedSize;
-
- public BookList() {
- books[0] = new Book("三国演义","罗贯中", 89,"小说");
- books[1] = new Book("红楼梦","曹雪芹",78,"小说");
- books[2] = new Book("西游记","吴承恩",99,"小说");
- this.usedSize = 3;
- }
-
- public int getUsedSize() {
- return usedSize;
- }
-
- public void setUsedSize(int usedSize) {
- this.usedSize = usedSize;
- }
-
- public Book getPos(int i){
- return books[i];
- }
- public void setBook(Book book, int pos) {
- books[pos] = book;
- }
-
- }
AddOperation类
- package operation;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class AddOperation implements IOperation{
- public void work(BookList bookList) {
- System.out.println("请输入你要增加的书名:");
- Scanner scan = new Scanner(System.in);
- String bookName = scan.nextLine();
- System.out.println("请输入作者:");
- String author = scan.nextLine();
-
- System.out.println("请输入类型");
- String type = scan.nextLine();
- //防止nextInt读取回车
- System.out.println("请输入价格:");
- int price = scan.nextInt();
-
- Book book = new Book(bookName,author,price,type);
- bookList.setBook(book,bookList.getUsedSize());
-
- bookList.setUsedSize(bookList.getUsedSize() + 1);
- }
- }
BorrowOperation类
- package operation;
-
- import book.BookList;
-
- import java.util.Scanner;
-
- public class BorrowOperation implements IOperation{
- public void work(BookList bookList) {
- System.out.println("借阅图书");
- System.out.println("请输入你要借阅的图书");
- Scanner scan = new Scanner(System.in);
- String name = scan.nextLine();
- for (int i = 0; i < bookList.getUsedSize(); i++) {
- if(name.equals(bookList.getPos(i).getName())) {
- if(bookList.getPos(i).getBorrow()) {
- System.out.println("抱歉你要借阅的图书以借出");
- return;
- } else {
- bookList.getPos(i).setBorrow(true);
- System.out.println("借阅成功");
- return;
- }
- }
- }
- System.out.println("抱歉,没有你要借阅的图书");
- }
- }
DelOperation类
- package operation;
-
- import book.BookList;
-
- import java.util.Scanner;
-
- public class DelOperation implements IOperation{
- public void work(BookList bookList) {
- System.out.println("删除图书");
- System.out.println("请输入你要删除图书的名字:");
- Scanner scan = new Scanner(System.in);
- String name = scan.nextLine();
- for(int i = 0; i < bookList.getUsedSize(); i++) {
- if(name.equals(bookList.getPos(i).getName())){
- for(int j = i; j < bookList.getUsedSize() - 1; j++) {
- bookList.setBook(bookList.getPos(j + 1), j);
- }
- //防止内存泄漏,删除后数组最后一位数据还引用着对象
- bookList.setBook(null,bookList.getUsedSize() - 1);
- bookList.setUsedSize(bookList.getUsedSize() - 1);
- System.out.println("删除成功");
- return;
- }
- }
- System.out.println("抱歉,没有你要删除的图书");
-
- }
-
- }
ExitOperation类
- package operation;
-
- import book.BookList;
-
- public class ExitOperation implements IOperation{
- public void work(BookList bookList) {
- System.out.println("退出系统");
- System.exit(0);
- }
-
- }
FindOperation类
- package operation;
-
- import book.BookList;
-
- import java.util.Scanner;
-
- public class FindOperation implements IOperation{
- public void work(BookList bookList) {
- System.out.println("查找图书");
- System.out.println("请输入你要查找的书名");
- Scanner scan = new Scanner(System.in);
- String bookName = scan.nextLine();
- for(int i = 0; i < bookList.getUsedSize(); i++){
- if(bookName.equals(bookList.getPos(i).getName())){
- System.out.println("你要查找图书的信息");
- System.out.println(bookList.getPos(i));
- return;
- }
- }
- System.out.println("抱歉,没有你要查找的图书");
- }
- }
ReturnOperation类
- package operation;
-
- import book.BookList;
-
- import java.util.Scanner;
-
- public class ReturnOperation implements IOperation{
- public void work(BookList bookList) {
- System.out.println("归还图书");
- System.out.println("请输入你要归还的书名:");
- Scanner scan = new Scanner(System.in);
- String name = scan.nextLine();
- for(int i = 0; i < bookList.getUsedSize(); i++) {
- if(name.equals(bookList.getPos(i).getName())) {
- if(bookList.getPos(i).getBorrow()) {
- bookList.getPos(i).setBorrow(false);
- System.out.println("归还成功");
- return;
- }else {
- System.out.println("你的书已经归还,无需再次归还");
- return;
- }
-
- }
- }
- System.out.println("没有你要归还的图书");
- }
- }
ShowOperation类
- package operation;
-
- import book.BookList;
-
- public class ShowOperation implements IOperation{
- public void work(BookList bookList) {
- for(int i = 0; i < bookList.getUsedSize(); i++){
- System.out.println(bookList.getPos(i));
- }
- }
- }
IOperation接口
- package operation;
-
- import book.BookList;
-
- public interface IOperation {
- void work(BookList bookList);
- }
🐵面向对象编程,需求分析,框架搭建,然后才是核心代码的编写,需要大量去思考,锻炼自己的这种思维。