书类实现:
public class Book {
private String name
;
private String author
;
private double price
;
private String tpey
;
private boolean isBrrow
;
public Book(String name
, String author
, double price
, String tpey
) {
this.name
= name
;
this.author
= author
;
this.price
= price
;
this.tpey
= tpey
;
}
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 double getPrice() {
return price
;
}
public void setPrice(double price
) {
this.price
= price
;
}
public String
getTpey() {
return tpey
;
}
public void setTpey(String tpey
) {
this.tpey
= tpey
;
}
public boolean isBrrow() {
return isBrrow
;
}
public void setBrrow(boolean brrow
) {
isBrrow
= brrow
;
}
@Override
public String
toString() {
return "book{" +
"name='" + name
+ '\'' +
", author='" + author
+ '\'' +
", price=" + price
+
", tpey='" + tpey
+ '\'' +
"," +
(isBrrow
? "已被借出":"未被借出")+
'}';
}
}
图书列表:
public class BookList {
private Book
[] books
= new Book[100];
private int Usesize
= 0;
public BookList() {
books
[0] = new Book("java编程思想","程筱栋",12.5,"学习");
books
[1] = new Book("剑指offer","高琼",13.5,"练习");
Usesize
= 2;
}
public Book
getBooks(int pos
) {
return books
[pos
];
}
public void setBooks(int pos
,Book books
) {
this.books
[pos
] = books
;
}
public int getUsesize() {
return Usesize
;
}
public void setUsesize(int count
) {
this.Usesize
= this.Usesize
+count
;
}
}
接口实现
public interface Iopeartion {
Scanner sc
= new Scanner(System
.in
);
void work(BookList bookList
);
}
添加书籍类
public class AddOpeartion implements Iopeartion{
@Override
public void work(BookList bookList
) {
System
.out
.println("请输入书名");
String name
= sc
.next();
System
.out
.println("请输入作者");
String anthor
= sc
.next();
System
.out
.println("请输入价格");
double price
= sc
.nextDouble();
System
.out
.println("请输入类型");
String tpey
= sc
.next();
int size
= bookList
.getUsesize();
Book book
= new Book(name
,anthor
,price
,tpey
);
bookList
.setBooks(size
,book
);
bookList
.setUsesize(1);
System
.out
.println("添加成功");
}
}
删除书籍
public class DleOpeartion implements Iopeartion{
@Override
public void work(BookList bookList
) {
System
.out
.println("请输入要删除的书名");
String name
= sc
.next();
int i
= 0;
for (; i
< bookList
.getUsesize(); i
++) {
if(bookList
.getBooks(i
).getName().equals(name
)){
break;
}
}
if(i
>= bookList
.getUsesize()){
System
.out
.println("此书不存在");
return;
}else{
for (int j
= i
; j
< bookList
.getUsesize()-1; j
++) {
bookList
.setBooks(j
,bookList
.getBooks(j
+1));
}
}
bookList
.setUsesize(-1);
System
.out
.println("删除成功!");
}
}
展示所有书籍
public class Displayopeartion implements Iopeartion {
@Override
public void work(BookList bookList
) {
for (int i
= 0; i
< bookList
.getUsesize(); i
++) {
System
.out
.println(bookList
.getBooks(i
));
}
}
}
查看书籍
public class FindOpeartion implements Iopeartion {
@Override
public void work(BookList bookList
) {
System
.out
.println("请输入你要查阅的书籍");
String name
= sc
.next();
int i
= 0;
for (; i
< bookList
.getUsesize(); i
++) {
if(bookList
.getBooks(i
).getName().equals(name
)){
break;
}
}
if(i
>= bookList
.getUsesize()){
System
.out
.println("此书不存在");
return;
}else{
System
.out
.println(bookList
.getBooks(i
));
}
}
}
借阅书籍
public class BrrrowOpeartion implements Iopeartion{
@Override
public void work(BookList bookList
) {
System
.out
.println("请输入要借阅的书");
String name
= sc
.next();
int i
=0;
for (; i
< bookList
.getUsesize(); i
++) {
if(bookList
.getBooks(i
).getName().equals(name
)){
break;
}
}
if(i
>= bookList
.getUsesize()){
System
.out
.println("此书不存在");
return;
}
else if(bookList
.getBooks(i
).isBrrow()){
System
.out
.println("此书已被借阅");
return;
}else {
bookList
.getBooks(i
).setBrrow(true);
System
.out
.println("借阅成功");
}
}
}
归还书籍
public class ReturnOpeation implements Iopeartion{
@Override
public void work(BookList bookList
) {
System
.out
.println("请输入你要归还的书籍名: ");
String name
= sc
.next();
int i
= 0;
for (; i
< bookList
.getUsesize(); i
++) {
if(bookList
.getBooks(i
).getName().equals(name
)) {
break;
}
}
if (i
>= bookList
.getUsesize()) {
System
.out
.println("此书不存在!");
return;
} else if (bookList
.getBooks(i
).isBrrow() == false) {
System
.out
.println("此书未被借阅!");
return;
} else {
bookList
.getBooks(i
).setBrrow(false);
System
.out
.println("归还成功!");
}
}
}
退出系统
public class ExitOpeartion implements Iopeartion{
@Override
public void work(BookList bookList
) {
System
.out
.println("已退出系统!");
System
.exit(0);
}
}
定义User抽象类
public abstract class User {
private String name
;
Scanner sc
= new Scanner(System
.in
);
protected Iopeartion
[] iopeartions
;
public abstract int mean();
public void doIopartion(int choic
, BookList bookList
){
iopeartions
[choic
].work(bookList
);
}
}
管理员类
public class Boss extends User{
public Boss(){
this.iopeartions
= new Iopeartion[]{
new ExitOpeartion(), new AddOpeartion(), new DleOpeartion(), new Displayopeartion()
};
}
@Override
public int mean() {
System
.out
.println("===管理员界面===");
System
.out
.println("1.添加书籍");
System
.out
.println("2.删除书籍");
System
.out
.println("3.查看所有书籍");
System
.out
.println("0.退出系统");
int choic
= sc
.nextInt();
return choic
;
}
}
普通用户
public class Nmirluser extends User{
public Nmirluser(){
this.iopeartions
= new Iopeartion[]{
new ExitOpeartion(), new FindOpeartion(), new BrrrowOpeartion(), new ReturnOpeation()
};
}
@Override
public int mean() {
System
.out
.println("===用户界面===");
System
.out
.println("1.查阅书籍");
System
.out
.println("2.借阅书籍");
System
.out
.println("3.归还书籍");
System
.out
.println("0.退出系统");
int choic
= sc
.nextInt();
return choic
;
}
}
mai函数设计
public class Maingoto {
public static void main(String
[] args
) {
User user
= login();
BookList bookList
= new BookList();
while(true){
int choic
= user
.mean();
user
.doIopartion(choic
,bookList
);
}
}
public static User
login(){
Scanner sc
= new Scanner(System
.in
);
System
.out
.println("请输入姓名: ");
String name
= sc
.next();
System
.out
.println("请选择身份");
int choic
= sc
.nextByte();
if (choic
== 0){
return new Nmirluser();
}else {
return new Boss();
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-499769.html