refactor
This commit is contained in:
1 parent
062beda77e
commit
b0055a87bd
25 files changed
+40
-93
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package library;
|
||||
|
||||
public class Administrator {
|
||||
private int iId;
|
||||
private String strName;
|
||||
private Library lib;
|
||||
|
||||
public Administrator(int inId, String inName) {
|
||||
this.iId = inId;
|
||||
this.strName = inName;
|
||||
this.lib = new Library();
|
||||
}
|
||||
|
||||
public boolean register(Student st) {
|
||||
for (Book bk : st.getBg().getLiBag())
|
||||
if (!this.lib.del(bk)) {
|
||||
System.out.println("Book not found in library: " + bk.getStrIsbn());
|
||||
return false;
|
||||
}
|
||||
this.print(st);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void print(Student st) {
|
||||
System.out.println("Administrator ID: " + this.iId);
|
||||
System.out.println("Administrator Name: " + this.strName);
|
||||
System.out.println("Student ID: " + st.getiId());
|
||||
System.out.println("Student Name: " + st.getStrName());
|
||||
for (Book bk : st.getBg().getLiBag())
|
||||
System.out.println(
|
||||
"Book ISBN: " + bk.getStrIsbn() + " Title: " + bk.getStrTitle() + " Count: " + bk.getiCount());
|
||||
}
|
||||
|
||||
public int getiId() {
|
||||
return iId;
|
||||
}
|
||||
|
||||
public void setiId(int iId) {
|
||||
this.iId = iId;
|
||||
}
|
||||
|
||||
public String getStrName() {
|
||||
return strName;
|
||||
}
|
||||
|
||||
public void setStrName(String strName) {
|
||||
this.strName = strName;
|
||||
}
|
||||
|
||||
public Library getLib() {
|
||||
return lib;
|
||||
}
|
||||
|
||||
public void setLib(Library lib) {
|
||||
this.lib = lib;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package library;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Bag {
|
||||
private List<Book> liBag;
|
||||
|
||||
public Bag() {
|
||||
this.liBag = new ArrayList<Book>();
|
||||
}
|
||||
|
||||
public void add(Book bk) {
|
||||
this.liBag.add(bk);
|
||||
}
|
||||
|
||||
public boolean del(String inIsbn) {
|
||||
boolean flag = false;
|
||||
Book tempBk;
|
||||
for (int i = 0; i < this.liBag.size(); i++) {
|
||||
tempBk = this.liBag.get(i);
|
||||
if (tempBk.getStrIsbn().equals(inIsbn)) {
|
||||
this.liBag.remove(i);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public List<Book> getLiBag() {
|
||||
return liBag;
|
||||
}
|
||||
|
||||
public void setLiBag(List<Book> liBag) {
|
||||
this.liBag = liBag;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package library;
|
||||
|
||||
public class Book {
|
||||
private String strIsbn;
|
||||
private String strTitle;
|
||||
private int iCount;
|
||||
|
||||
public Book(String inIsbn, String inTitle, int inCount) {
|
||||
this.strIsbn = inIsbn;
|
||||
this.strTitle = inTitle;
|
||||
this.iCount = inCount;
|
||||
}
|
||||
|
||||
public String getStrIsbn() {
|
||||
return strIsbn;
|
||||
}
|
||||
|
||||
public void setStrIsbn(String strIsbn) {
|
||||
this.strIsbn = strIsbn;
|
||||
}
|
||||
|
||||
public String getStrTitle() {
|
||||
return strTitle;
|
||||
}
|
||||
|
||||
public void setStrTitle(String strTitle) {
|
||||
this.strTitle = strTitle;
|
||||
}
|
||||
|
||||
public int getiCount() {
|
||||
return iCount;
|
||||
}
|
||||
|
||||
public void setiCount(int iCount) {
|
||||
this.iCount = iCount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package library;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Library {
|
||||
private List<Book> liLib;
|
||||
|
||||
public Library() {
|
||||
this.liLib = new ArrayList<Book>();
|
||||
}
|
||||
|
||||
public void add(Book bk) {
|
||||
Book tempBk;
|
||||
for (int i = 0; i < this.liLib.size(); i++) {
|
||||
tempBk = this.liLib.get(i);
|
||||
if (tempBk.getStrIsbn().equals(bk.getStrIsbn())) {
|
||||
tempBk.setiCount(tempBk.getiCount() + bk.getiCount());
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.liLib.add(bk);
|
||||
}
|
||||
|
||||
public boolean del(Book bk) {
|
||||
Book tempBk;
|
||||
for (int i = 0; i < this.liLib.size(); i++) {
|
||||
tempBk = this.liLib.get(i);
|
||||
if (tempBk.getStrIsbn().equals(bk.getStrIsbn())) {
|
||||
if (tempBk.getiCount() > bk.getiCount()) {
|
||||
tempBk.setiCount(tempBk.getiCount() - bk.getiCount());
|
||||
return true;
|
||||
} else if (tempBk.getiCount() == bk.getiCount()) {
|
||||
this.liLib.remove(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package library;
|
||||
|
||||
public class Student {
|
||||
private int iId;
|
||||
private String strName;
|
||||
private Bag bg;
|
||||
|
||||
public Student(int inId, String inName) {
|
||||
this.iId = inId;
|
||||
this.strName = inName;
|
||||
this.bg = new Bag();
|
||||
}
|
||||
|
||||
public void addBook(Book bk) {
|
||||
this.bg.add(bk);
|
||||
}
|
||||
|
||||
public int getiId() {
|
||||
return iId;
|
||||
}
|
||||
|
||||
public void setiId(int iId) {
|
||||
this.iId = iId;
|
||||
}
|
||||
|
||||
public String getStrName() {
|
||||
return strName;
|
||||
}
|
||||
|
||||
public void setStrName(String strName) {
|
||||
this.strName = strName;
|
||||
}
|
||||
|
||||
public Bag getBg() {
|
||||
return bg;
|
||||
}
|
||||
|
||||
public void setBg(Bag bg) {
|
||||
this.bg = bg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package library;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
Administrator adm = new Administrator(1, "John");
|
||||
adm.getLib().add(new Book("123", "Book1", 1));
|
||||
adm.getLib().add(new Book("124", "Book2", 1));
|
||||
adm.getLib().add(new Book("125", "Book3", 1));
|
||||
Student st = new Student(1, "Alice");
|
||||
st.getBg().add(new Book("123", "Book1", 1));
|
||||
st.getBg().add(new Book("124", "Book2", 1));
|
||||
st.getBg().add(new Book("125", "Book3", 1));
|
||||
adm.register(st);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user