基于Android平台的图书管理系统的制作(1)

时间:2023-03-09 15:30:32
基于Android平台的图书管理系统的制作(1)
在学习了郭神的第一行代码前半段之后,想通过一次实践来完成对已学知识的巩固。于是码下了这个图书管理系统客户端。
IDE Android studio,语言 JAVA、XML;
在刚开始设计的时候对于这个程序的设定是:
1.支持学生、教师、工作人员三种不同身份的客户。
2.学生、老师各自注册账号,并用账号登录。
3.学生、老师能够查询、借阅、归还图书,逾期没有归还图书的客户将被列入黑名单。
4.含有图书馆的介绍与新闻通知板块。
5.管理人员能够添加书籍。
6.含有新书上架和借阅排行榜两个list。
7.所有信息从服务器*问得到,支持不同客户端共享相同数据
在经过了一段时间的磨蹭之后,终于将图书管理系统基本完成:
但和最开始的设定有了一些偏差:由于教师和学生的不同之处仅仅是借阅书籍的最大数量不同,因此将教师这一身份舍去。在写客户端向服务器(Windows下建了一个微型服务器)通信的时候遇到了很大的困难,长时间的码代码、编译、通过、服务器无反应之后,暂时放弃了连接服务器这一功能,将所有用户与书籍的数据储存在了Android系统自带的数据库中。这样使得过程难度降低了很多,但是无法实现不同客户端访问的数据统一化,也就是从联网退化至了单机。
但是学习仍在路上,以后有时间当将这些不足得以改善。
 
下面来讲解图书管理系统的制作过程。
主页面:

新手上路,界面很low》》》》》》
 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:gravity="center"
8 android:orientation="vertical"
9 tools:context=".MainActivity">
10
11 <ImageView
12 android:id="@+id/image_view_id"
13 android:layout_width="match_parent"
14 android:layout_height="0dp"
15 android:layout_weight="6"
16 android:src="@drawable/img_1" />
17
18 <TextView
19 android:layout_width="match_parent"
20 android:layout_height="0dp"
21 android:gravity="center"
22 android:textSize="30sp"
23 android:layout_weight="2"
24 android:text="欢迎使用图书馆安卓客户端" />
25
26 <LinearLayout
27 android:layout_width="match_parent"
28 android:layout_height="0dp"
29 android:layout_weight="4"
30 android:orientation="horizontal">
31
32 <Button
33 android:id="@+id/introduce_id"
34 android:layout_width="0dp"
35 android:layout_height="match_parent"
36 android:layout_weight="1"
37 android:textSize="21sp"
38 android:text="图书馆介绍" />
39
40 <Button
41 android:id="@+id/news_id"
42 android:layout_width="0dp"
43 android:layout_height="match_parent"
44 android:layout_weight="1"
45 android:textSize="21sp"
46 android:text="图书馆新闻通知" />
47 </LinearLayout>
48
49 <LinearLayout
50 android:layout_width="match_parent"
51 android:layout_height="0dp"
52 android:layout_weight="4"
53 android:orientation="horizontal">
54
55 <Button
56 android:id="@+id/newBook_id"
57 android:layout_width="0dp"
58 android:layout_height="match_parent"
59 android:layout_weight="1"
60 android:textSize="21sp"
61 android:text="新书上架" />
62
63 <Button
64 android:id="@+id/ranking_id"
65 android:layout_width="0dp"
66 android:layout_height="match_parent"
67 android:layout_weight="1"
68 android:textSize="21sp"
69 android:text="借阅排行榜" />
70 </LinearLayout>
71
72 <LinearLayout
73 android:layout_width="match_parent"
74 android:layout_height="0dp"
75 android:layout_weight="4">
76
77 <Button
78 android:id="@+id/registerStudent_id"
79 android:layout_width="0dp"
80 android:layout_height="match_parent"
81 android:layout_weight="1"
82 android:textSize="21sp"
83 android:text="学生注册" />
84 <Button
85 android:id="@+id/blacklist_id"
86 android:layout_width="0dp"
87 android:layout_height="match_parent"
88 android:layout_weight="1"
89 android:textSize="21sp"
90 android:text="黑名单" />
91 </LinearLayout>
92 </LinearLayout>

activity_main

下面是主程序的代码:

  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.Menu;
7 import android.view.MenuItem;
8 import android.view.View;
9 import android.widget.Button;
10 import android.widget.Toast;
11
12 import org.litepal.LitePal;
13 /*------
14 郭志---18年暑假
15 */
16
17 public class MainActivity extends AppCompatActivity {
18
19 private Button button_introduce,button_information,button_ranking,button_newBooks,button_register_student,button_blacklist;
20 @Override
21 public boolean onCreateOptionsMenu(Menu menu) {
22 getMenuInflater().inflate(R.menu.main,menu);
23 return true;
24 }
25
26 @Override
27 public boolean onOptionsItemSelected(MenuItem item) {
28 switch (item.getItemId())
29 {
30 case R.id.student_id:
31 Intent intent_student=new Intent(MainActivity.this,Login_student.class);
32 startActivity(intent_student);
33 break;
34 /*case R.id.teacher_id:
35 Intent intent_teacher=new Intent(MainActivity.this,Login_teacher.class);
36 startActivity(intent_teacher);
37 break;*/
38 case R.id.staff_id:
39 Intent intent_staff=new Intent(MainActivity.this,Login_staff.class);
40 startActivity(intent_staff);
41 break;
42 }
43 return true;
44 }
45
46 @Override
47 protected void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
49 setContentView(R.layout.activity_main);
50 button_introduce=findViewById(R.id.introduce_id);
51 button_register_student=findViewById(R.id.registerStudent_id);
52 button_blacklist=findViewById(R.id.blacklist_id);
53 button_information=findViewById(R.id.news_id);
54 button_ranking=findViewById(R.id.ranking_id);
55 button_newBooks=findViewById(R.id.newBook_id);
56
57 button_introduce.setOnClickListener(new View.OnClickListener() {
58 @Override
59 public void onClick(View view) {
60 Intent intent=new Intent(MainActivity.this,Introduction.class);
61 startActivity(intent);
62 finish();
63 }
64 });
65 button_register_student.setOnClickListener(new View.OnClickListener() {
66 @Override
67 public void onClick(View view) {
68 Intent intent=new Intent(MainActivity.this,Register_student.class);
69 startActivity(intent);
70 finish();
71 }
72 });
73 button_blacklist.setOnClickListener(new View.OnClickListener() {
74 @Override
75 public void onClick(View view) {
76 Intent intent=new Intent(MainActivity.this,Blacklist.class);
77 startActivity(intent);
78 finish();
79 }
80 });
81 button_information.setOnClickListener(new View.OnClickListener() {
82 @Override
83 public void onClick(View view) {
84 Intent intent=new Intent(MainActivity.this,News.class);
85 startActivity(intent);
86 finish();
87 }
88 });
89 button_ranking.setOnClickListener(new View.OnClickListener() {
90 @Override
91 public void onClick(View view) {
92 Intent intent=new Intent(MainActivity.this,Ranking.class);
93 startActivity(intent);
94 finish();
95 }
96 });
97 button_newBooks.setOnClickListener(new View.OnClickListener() {
98 @Override
99 public void onClick(View view) {
100 Intent intent=new Intent(MainActivity.this,New_Books.class);
101 startActivity(intent);
102 finish();
103 }
104 });
105
106 }
107 }

MainActivity.java

当然我们的程序都得先在注册表中进行注册,一些权限的开启也在注册表中。
 1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.administrator.library1">
4
5 <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
6 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
7 <uses-permission android:name="android.permission.INTERNET" />
8
9 <application
10 android:name="org.litepal.LitePalApplication"
11 android:allowBackup="true"
12 android:icon="@mipmap/ic_launcher"
13 android:label="图书馆"
14 android:roundIcon="@mipmap/ic_launcher_round"
15 android:supportsRtl="true"
16 android:theme="@style/AppTheme">
17 <activity android:name=".MainActivity">
18 <intent-filter>
19 <action android:name="android.intent.action.MAIN" />
20
21 <category android:name="android.intent.category.LAUNCHER" />
22 </intent-filter>
23 </activity>
24 <activity android:name=".Login_student" />
25 <activity android:name=".Login_teacher" />
26 <activity android:name=".Login_staff" />
27 <activity android:name=".Register_student" />
28 <activity android:name=".Register_teacher" />
29 <activity android:name=".Introduction" />
30 <activity android:name=".Student_homepage" />
31 <activity android:name=".Teacher_homepage" />
32 <activity android:name=".Staff_homepage" />
33 <activity android:name=".News" />
34 <activity android:name=".staff_input" />
35 <activity android:name=".Ranking" />
36 <activity android:name=".New_Books" />
37 <activity android:name=".Blacklist"></activity>
38 </application>
39
40 </manifest>

manifest

整个项目中用到了Github上面的许多开源项目,如Litepal,OKhttp,这些都在build grade中引用过,代码贴出:

 1 apply plugin: 'com.android.application'
2
3 android {
4 compileSdkVersion 26
5 defaultConfig {
6 applicationId "com.example.administrator.library1"
7 minSdkVersion 15
8 targetSdkVersion 26
9 versionCode 1
10 versionName "1.0"
11 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 }
13 buildTypes {
14 release {
15 minifyEnabled false
16 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 }
18 }
19 }
20
21 dependencies {
22 implementation fileTree(dir: 'libs', include: ['*.jar'])
23 implementation 'com.android.support:appcompat-v7:26.1.0'
24 implementation 'com.android.support.constraint:constraint-layout:1.1.2'
25 testImplementation 'junit:junit:4.12'
26 implementation 'com.squareup.okhttp3:okhttp:3.11.0'
27 implementation 'com.android.support:recyclerview-v7:26.1.0'
28 implementation 'org.litepal.android:core:2.0.0'
29 androidTestImplementation 'com.android.support.test:runner:1.0.2'
30 androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
31 }

build gradle

整个管理系统无论是哪个用户,哪一个功能都离不开数据,这些数据都存储在本地的数据库里。关于数据库的设计是这样的:

 1 <?xml version="1.0" encoding="utf-8"?>
2 <litepal>
3 <dbname value="library"></dbname>
4 <version value="10"></version>
5 <list>
6 <mapping class="com.example.administrator.library1.Student"></mapping>
7 <mapping class="com.example.administrator.library1.Teacher"></mapping>
8 <mapping class="com.example.administrator.library1.Book"></mapping>
9 <mapping class="com.example.administrator.library1.Staff"></mapping>
10 <mapping class="com.example.administrator.library1.Simple"></mapping>
11 </list>
12 </litepal>

library库由五个表构成:学生、教师(停用)、书籍、职员、简单信息(用于存储记住密码之后的账号、密码信息)组成。

下一篇,我将来讲述学生的注册、登录、借还书功能的实现。

(先附上整个项目的源码地址:https://github.com/Guozhi-explore/androidlibrary)