前兩篇介紹了主頁面和Student,這一篇來講Book類和工作人員。
Book是圖書管理系統的核心,查書,借書,還書,增加書都與Book類息息相關。Book類的設計很簡單:包含信息:名稱、作者、頁數、價錢、出版日期、數量、在架數量。
Book類的代碼:

1 package com.example.administrator.library1; 2 3 import org.litepal.annotation.Column; 4 import org.litepal.crud.LitePalSupport; 5 6 public class Book extends LitePalSupport{ 7 int id; 8 private String name; 9 private String writer,page,price,time; 10 private int amount,in_shelf; 11 public Book(String name,String writer,String page,String price,String time,int amount) 12 { 13 this.name=name; 14 this.writer=writer; 15 this.page=page; 16 this.price=price; 17 this.time=time; 18 this.amount=amount; 19 in_shelf=amount; 20 } 21 public void setAmount(int amount) { 22 this.amount = amount; 23 } 24 public int getAmount() { 25 return amount; 26 } 27 public void setIn_shelf(int in_shelf) { 28 this.in_shelf = in_shelf; 29 } 30 public int getIn_shelf() { 31 return in_shelf; 32 } 33 public void setId(int id) { 34 this.id = id; 35 } 36 public int getId() { 37 return id; 38 } 39 public void setName(String name) { 40 this.name = name; 41 } 42 public void setPrice(String price) { 43 this.price = price; 44 } 45 public void setPage(String page) { 46 this.page = page; 47 } 48 public void setTime(String time) { 49 this.time = time; 50 } 51 public void setWriter(String writer) { 52 this.writer = writer; 53 } 54 public String getName() { 55 return name; 56 } 57 public String getPage() { 58 return page; 59 } 60 public String getPrice() { 61 return price; 62 } 63 public String getTime() { 64 return time; 65 } 66 public String getWriter() { 67 return writer; 68 } 69 70 }
Staff的類也很簡單,包含職員的一些個人信息。

1 package com.example.administrator.library1; 2 3 import org.litepal.LitePal; 4 import org.litepal.crud.LitePalSupport; 5 6 public class Staff extends LitePalSupport { 7 private int id; 8 private String account,password,name,staff_number,mail_box; 9 int age; 10 public Staff(String account,String password,String name,String staff_number,String mail_box,int age) 11 { 12 this.account=account; 13 this.age=age; 14 this.staff_number=staff_number; 15 this.mail_box=mail_box; 16 this.password=password; 17 this.name=name; 18 } 19 20 public int getId() { 21 return id; 22 } 23 public String getName() { 24 return name; 25 } 26 public String getPassword() { 27 return password; 28 } 29 public int getAge() { 30 return age; 31 } 32 public String getStaff_number() { 33 return staff_number; 34 } 35 public String getMail_box() { 36 return mail_box; 37 } 38 public String getAccount() { 39 return account; 40 } 41 public void setName(String name) { 42 this.name = name; 43 } 44 public void setId(int id) { 45 this.id = id; 46 } 47 public void setPassword(String password) { 48 this.password = password; 49 } 50 public void setAge(int age) { 51 this.age = age; 52 } 53 public void setAccount(String account) { 54 this.account = account; 55 } 56 public void setMail_box(String mail_box) { 57 this.mail_box = mail_box; 58 } 59 public void setStaff_number(String staff_number) { 60 this.staff_number = staff_number; 61 } 62 }
職員的登錄界面,大致與Student的登錄界面相同這里只將代碼貼出:

1 package com.example.administrator.library1; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.CheckBox; 9 import android.widget.EditText; 10 import android.widget.Toast; 11 12 import org.litepal.LitePal; 13 14 import java.util.List; 15 16 public class Login_staff extends AppCompatActivity { 17 18 private EditText staff_editAccount,staff_editPassword; 19 private CheckBox staff_checkBox; 20 private Button staff_button_commit; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_login_staff); 25 staff_editAccount=findViewById(R.id.accountStaff_id); 26 staff_editPassword=findViewById(R.id.passwordStaff_id); 27 staff_checkBox=findViewById(R.id.rememberPassStaff_id); 28 staff_button_commit=findViewById(R.id.commitStaff_id); 29 Simple simple=LitePal.find(Simple.class,3); 30 staff_editAccount.setText(simple.getAccount()); 31 staff_editPassword.setText(simple.getPassword()); 32 33 staff_button_commit.setOnClickListener(new View.OnClickListener() { 34 @Override 35 public void onClick(View view) { //在這里之前已經出錯 36 List<Staff> staffs_account = LitePal.findAll(Staff.class); 37 boolean is_account_available = false; 38 String st_account = staff_editAccount.getText().toString(); 39 String st_password = staff_editPassword.getText().toString(); 40 for (int i = 0; i < staffs_account.size(); ++i) { 41 if (st_account.equals(staffs_account.get(i).getAccount())) { //get(i)這里的查詢是從0開始的,即是get(0)查詢的是id為1的員工 42 is_account_available = true; 43 if (st_password.equals(staffs_account.get(i).getPassword())) { 44 if(staff_checkBox.isChecked()) 45 { 46 Simple simple= LitePal.find(Simple.class,3); 47 simple.setAccount(staff_editAccount.getText().toString()); 48 simple.setPassword(staff_editPassword.getText().toString()); 49 simple.save(); 50 } 51 else 52 { 53 Simple simple1=LitePal.find(Simple.class,3); 54 simple1.setAccount(""); 55 simple1.setPassword(""); 56 simple1.save(); 57 } 58 Intent intent = new Intent(Login_staff.this, Staff_homepage.class); 59 intent.putExtra("extra_data", i+1); 60 startActivity(intent); 61 finish(); 62 } else { 63 Toast.makeText(Login_staff.this, "密碼錯誤", Toast.LENGTH_SHORT).show(); 64 break; //記住密碼還沒有用上 65 } 66 } 67 } 68 if (is_account_available == false) { 69 Toast.makeText(Login_staff.this, "賬號不存在", Toast.LENGTH_SHORT).show(); 70 } 71 } 72 }); 73 } 74 }

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:orientation="vertical" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".Login_student"> 9 10 <ImageView 11 android:layout_width="match_parent" 12 android:layout_height="211dp" 13 android:src="@drawable/img_login" /> 14 <LinearLayout 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:layout_marginTop="20dp" 18 android:layout_marginLeft="20dp" 19 android:layout_marginRight="20dp" 20 android:orientation="horizontal"> 21 <TextView 22 android:layout_width="0dp" 23 android:layout_height="match_parent" 24 android:layout_weight="1" 25 android:textSize="21sp" 26 android:text="賬號: "/> 27 <EditText 28 android:id="@+id/accountStaff_id" 29 android:layout_width="0dp" 30 android:layout_height="match_parent" 31 android:layout_weight="5" 32 android:hint="type here"/> 33 </LinearLayout> 34 <LinearLayout 35 android:orientation="horizontal" 36 android:layout_width="match_parent" 37 android:layout_marginLeft="20dp" 38 android:layout_marginRight="20dp" 39 android:layout_height="wrap_content"> 40 <TextView 41 android:layout_width="0dp" 42 android:layout_height="match_parent" 43 android:layout_weight="1" 44 android:textSize="21sp" 45 android:text="密碼 :"/> 46 <EditText 47 android:id="@+id/passwordStaff_id" 48 android:layout_width="0dp" 49 android:layout_height="match_parent" 50 android:layout_weight="5" 51 android:hint="type here"/> 52 </LinearLayout> 53 <LinearLayout 54 android:layout_marginLeft="20dp" 55 android:layout_marginRight="20dp" 56 android:layout_width="match_parent" 57 android:layout_height="wrap_content"> 58 <CheckBox 59 android:id="@+id/rememberPassStaff_id" 60 android:layout_width="wrap_content" 61 android:layout_height="wrap_content" 62 android:checked="true"/> 63 <TextView 64 android:layout_width="wrap_content" 65 android:layout_height="wrap_content" 66 android:textSize="18sp" 67 android:text="記住密碼"/> 68 </LinearLayout> 69 <Button 70 android:id="@+id/commitStaff_id" 71 android:layout_width="150dp" 72 android:layout_height="wrap_content" 73 android:layout_gravity="center" 74 android:textSize="20sp" 75 android:text="登錄"/> 76 </LinearLayout>
下面講述staff職員的個人主頁,中間有兩個Button,分別是添加書籍和返回。
點擊添加書籍會進入另一個界面填寫新入庫的書籍信息,提交后將返回職員的主頁。
由於進入填寫信息的界面再次返回職員主頁后,此時想要返回MainActivity需要點擊三下手機自帶的返回鍵,於是在職員主頁中添加了一個返回按鈕。
下面使用了listview,展示當前圖書館所有書籍的名稱、作者與各本書的數量。
代碼貼出來如下:

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".Staff_homepage"> 9 <ImageView 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:src="@drawable/img_login"/> 13 <TextView 14 android:id="@+id/sta_home_infor_id" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:layout_gravity="center" 18 android:gravity="center" 19 android:textSize="20sp"/> 20 <Button 21 android:id="@+id/sta_input_book_id" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:text="增加書籍"/> 25 <Button 26 android:id="@+id/sta_return_id" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:text="返回主菜單"/> 30 <ListView 31 android:id="@+id/staff_home_list_id" 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content"> 34 </ListView> 35 </LinearLayout>

1 package com.example.administrator.library1; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.ArrayAdapter; 8 import android.widget.Button; 9 import android.widget.ListView; 10 import android.widget.TextView; 11 12 import org.litepal.LitePal; 13 14 import java.util.ArrayList; 15 import java.util.List; 16 17 public class Staff_homepage extends AppCompatActivity { 18 private int staff_id; 19 Staff staff; 20 private TextView textView_name; 21 private Button button_add,button_return; 22 private List<Book> totalbooks=new ArrayList<>(); 23 private List<String> booknames=new ArrayList<>(); 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_staff_homepage); 28 textView_name = (TextView) findViewById(R.id.sta_home_infor_id); 29 button_add = (Button) findViewById(R.id.sta_input_book_id); 30 button_return = (Button) findViewById(R.id.sta_return_id); 31 totalbooks = LitePal.findAll(Book.class); 32 33 final Intent intent = getIntent(); 34 staff_id = intent.getIntExtra("extra_data", 1); 35 staff = LitePal.find(Staff.class, staff_id); 36 textView_name.setText("員工編號: " + staff.getId() + " " + "姓名: " + staff.getName()); 37 38 button_add.setOnClickListener(new View.OnClickListener() { 39 @Override 40 public void onClick(View view) { 41 Intent intent1 = new Intent(Staff_homepage.this, staff_input.class); 42 startActivity(intent1); 43 } 44 }); 45 46 button_return.setOnClickListener(new View.OnClickListener() { 47 @Override 48 public void onClick(View view) { 49 Intent intent = new Intent(Staff_homepage.this, MainActivity.class); 50 startActivity(intent); 51 } 52 }); 53 54 for (int i = 0; i < totalbooks.size(); ++i) { 55 booknames.add("名稱:" + totalbooks.get(i).getName() + " " + "作者:" + totalbooks.get(i).getWriter() + " " + "數量:" + totalbooks.get(i).getAmount()); 56 } 57 ArrayAdapter<String> adapter = new ArrayAdapter<String>(Staff_homepage.this, android.R.layout.simple_list_item_1, booknames); 58 ListView listView = (ListView) findViewById(R.id.staff_home_list_id); 59 listView.setAdapter(adapter); 60 } 61 }
運行是界面如圖:
關於職員的功能介紹完畢,下一篇來講述圖書館app的新書上架、借閱排行、黑名單、圖書館介紹、圖書館新聞模塊。