在我之前的几篇博客中对于Java语法,思想等等进行了总结,但是还是差点那味,就是没有将这些知识串起来那么今天的这篇博客我就将Java中:类 对象 封装 继承 多态 抽象类 接口等等知识共同使用完成一个Java的小项目一起来看
我们先来看看这个小项目都有什么功能
可以看到通过输入姓名和身份可以有不同的操作选择,增删查改呀之类的看起来挺简单的我们就来实现以下,我们来看一下所用到的类
知道了功能和类我们就来实现这个项目
在这个包中我们定义了两个类:Book和BookList分别来说以下
在Book类中我定义了书的名字,作者,类型,价格等成员变量,并且使用private修饰提供getset方法来体现封装,提供了构造方法,重写了toString方法方便进行打印,代码很简单看看就可以
package 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, 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 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==false?" 未借出":" 已借出")+
'}';
}
}
在BookList中定义了书这个类的数组和有几本数的记录,构造方法中初始化了四本书,提供了关于数组操作和记录书的方法
package book;
public class BookList {
private Book[] books=new Book[10];
private int useSize=0;
public BookList() {
books[0] = new Book("三国演义", "罗贯中", 34, "小说");
books[1]=new Book("西游记","吴承恩",25,"小说");
books[2]=new Book("红楼梦","曹雪芹",32,"小说");
books[3]=new Book("水浒传","施耐庵",45,"小说");
this.useSize=4;
}
public int getUseSize() {
return useSize;
}
public void setUseSize(int useSize) {
this.useSize = useSize;
}
/*
得到pos下标的书
* */
public Book getBook(int pos) {
return books[pos];
}
/*设置一本书在pos位置
* */
public void setBook(int pos,Book book) {
books[pos]=book;
}
}
这里要说一下了为什么不把增删查改方法放到这里,是因为我想把方法单独提取出来易读便于后续的修改
在这个包中定义了三个类父类User,子类AdminUser和子类NormalUser,主要是菜单的展示和实例化对应选择的对象
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
public User(String name){
this.name=name;
}
protected IOperation[] iOperations;
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
iOperations[choice].work(bookList);
}
}
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
iOperations=new IOperation[]{//初始化
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
};
}
public int menu() {
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 sca=new Scanner(System.in);
int choice=sca.nextInt();
return choice;
}
}
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
iOperations=new IOperation[]{//初始化
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
public int menu() {
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 sca=new Scanner(System.in);
int choice=sca.nextInt();
return choice;
}
}
我们在父类中定义了接口数组,在子类中通过初始化用户对象不同使得数组内对象不同从而管理员和学生操作选择不同
在这个包中我实现了增删查改等的类通过接口调用不同类中方法
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("新增图书");
System.out.println("图书名字");
Scanner sca=new Scanner(System.in);
String name=sca.nextLine();
System.out.println("图书作者");
String author=sca.nextLine();
System.out.println("图书类型");
String type=sca.nextLine();
System.out.println("图书价格");
int price=sca.nextInt();
Book book=new Book(name,author,price,type);
int currentSize= bookList.getUseSize();
bookList.setBook(currentSize,book);
bookList.setUseSize(currentSize+1);
System.out.println("新增成功");
}
}
BorrowOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("借阅图书");
System.out.println("请输入要借阅的图书");
Scanner sca=new Scanner(System.in);
String bookName=sca.nextLine();
int currentSize= bookList.getUseSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(bookName)) {
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("没有要借阅的书");
}
}
DelOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("删除图书");
System.out.println("请输入要删除图书的名字");
Scanner sca=new Scanner(System.in);
String name=sca.nextLine();
int currentSize= bookList.getUseSize();
int index=0;
int i = 0;
for (; i
DisPlayOperation
package operation;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示图书");
int currentSize= bookList.getUseSize();
for (int i=0;i
ExitOperation
package operation;
import book.Book;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出程序");
int currentSize= bookList.getUseSize();
for (int i=0;i
FindOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("查找图书");
System.out.println("请输入要查找的图书");
Scanner sca=new Scanner(System.in);
String bookName=sca.nextLine();
int currentSize= bookList.getUseSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(bookName)) {
System.out.println("找到了信息如下");
System.out.println(book);
return;
}
}
System.out.println("没有要找的书");
}
}
ReturnOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("归还图书");
System.out.println("请输入要归还的图书");
Scanner sca=new Scanner(System.in);
String bookName=sca.nextLine();
int currentSize= bookList.getUseSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(bookName)) {
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("没有要归还的书");
}
}
IOperation
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
主函数实现了login()方法能够根据方法返回值确定用户身份,在循环中只要不选择退出循环就会一直执行
import book.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 sca=new Scanner(System.in);
String name=sca.nextLine();
System.out.println("请输入身份:1.管理员 0.学生");
int choice=sca.nextInt();
if(choice==1) {
return new AdminUser(name);
} else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList books=new BookList();
User user=login();
while(true) {
int choice=user.menu();
user.doOperation(choice,books);
}
}
}
个人感觉这个程序最难的点在于这里
我画图分析一下
以上就是实现这个小项目的所有代码了,觉得不理解的可以把代码拷贝下来自己跑一下,对于新手练习我觉得是非常好的项目,还请点赞评论!!!会持续更新Java知识的 蟹蟹