生活随笔
收集整理的這篇文章主要介紹了
【安卓大作业】Android Studio图书管理系统 听雨轩
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【安卓大作業(yè)】Android Studio圖書管理系統(tǒng) 聽雨軒
一、界面展示
二、功能介紹
登陸注冊,借閱圖書、收藏圖書、修改個(gè)人信息、錢包充值、退出登錄
三、關(guān)鍵代碼展示
(1)database
public class DbHelper extends SQLiteOpenHelper {private static final String SQL_BOOK
= "create table if not exists "+Constants
.TABLE_NAME_BOOK
+"(" +"number INTEGER NOT NULL primary key autoincrement," +"type varchar(50) NOT NULL," +"price float NOT NULL," +"public_com varchar(50) DEFAULT '聽雨喧出版社'," +"count int(11) NOT NULL," +"name varchar(50) NOT NULL," +"cover varchar(50) DEFAULT NULL," +"introduce varchar(128) DEFAULT NULL," +"look_number int(11) default 0," +"star_number int(11) default 0," +"brow_number int(11) default 0" +")";private static final String SQL_USER
= "create table if not exists "+Constants
.TABLE_NAME_USER
+"(" +"id INTEGER NOT NULL primary key autoincrement," +"type int(11) DEFAULT 1," +"sex varchar(5) DEFAULT NULL," +"major varchar(100) DEFAULT NULL," +"classnum int(11) DEFAULT NULL," +"name varchar(50) NOT NULL unique," +"cover varchar(128) DEFAULT NULL," +"age int(11) DEFAULT 0," +"phone int(11) DEFAULT 0," +"jointime datetime DEFAULT NULL," +"email int(11) DEFAULT 0," +"password varchar(50) not null"+")";private static final String SQL_OUT_BOOK
= "create table if not exists "+Constants
.TABLE_NAME_OUT_BOOK
+"(" +"id INTEGER primary key autoincrement," +"outtime varchar(100) not null," +"userid int(11) not null," +"bookname varchar(51) not null," +"duration int(11) default 30," +"state int(11) default 0" +")";private static final String SQL_FAVIOR_BOOK
= "create table if not exists "+Constants
.TABLE_NAME_FAVOR
+"(" +"id INTEGER primary key autoincrement," +"time varchar(100) not null," +"userid int(11) not null," +"bookname varchar(51) not null," +"state int(11) default 1" +")";private static final String SQL_OPERATION
= "create table if not exists "+Constants
.TABLE_NAME_RECORD_OPERATION
+"(" +"id INTEGER primary key autoincrement," +"operation varchar(128) not null," +"username varchar(51) not null," +"time varchar(100) not null" +")";public DbHelper(Context context
){super(context
,"tingyuxuan.db",null
,1);}public DbHelper(Context context
, String name
, SQLiteDatabase
.CursorFactory factory
, int version
) {super(context
, name
, factory
, version
);}@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase
) {sqLiteDatabase
.execSQL(SQL_BOOK
);sqLiteDatabase
.execSQL(SQL_USER
);sqLiteDatabase
.execSQL(SQL_OUT_BOOK
);sqLiteDatabase
.execSQL(SQL_OPERATION
);sqLiteDatabase
.execSQL(SQL_FAVIOR_BOOK
);System
.out
.println("數(shù)據(jù)庫創(chuàng)建成功");}@Overridepublic void onUpgrade(SQLiteDatabase sqLiteDatabase
, int i
, int i1
) {}public void addUser(String name
,String pwd
,String email
,int type
){SQLiteDatabase db
= this.getWritableDatabase();ContentValues values
= new ContentValues();values
.put("name",name
);values
.put("password",pwd
);values
.put("email",email
);values
.put("type",type
);db
.insert("user",null
,values
);values
.clear();db
.close();}public int getUserLevl(String name
){SQLiteDatabase db
= this.getWritableDatabase();Cursor cursor
= db
.rawQuery("select type from user where name='" + name
+ "'", null
);cursor
.moveToLast();int type
=0;try{type
= cursor
.getInt(cursor
.getColumnIndex("type"));}catch (SQLException e
){e
.printStackTrace();}finally {db
.close();return type
;}}public String
getUserName(int id
){SQLiteDatabase db
= this.getWritableDatabase();Cursor cursor
= db
.rawQuery("select name from user where id='" + id
+ "'", null
);cursor
.moveToLast();String name
="";try{name
= cursor
.getString(cursor
.getColumnIndex("name"));}catch (SQLException e
){e
.printStackTrace();}finally {db
.close();return name
;}}public int getUserId(String name
){SQLiteDatabase db
= this.getWritableDatabase();Cursor cursor
= db
.rawQuery("select id from user where name='" + name
+ "'", null
);cursor
.moveToLast();int type
=0;try{type
= cursor
.getInt(cursor
.getColumnIndex("id"));}catch (SQLException e
){e
.printStackTrace();}finally {db
.close();return type
;}}public String
getBookName(int id
){SQLiteDatabase db
= this.getWritableDatabase();Cursor cursor
= db
.rawQuery("select name from book where id=" + id
, null
);cursor
.moveToLast();String nmae
="";try{nmae
= cursor
.getString(cursor
.getColumnIndex("name"));}catch (SQLException e
){e
.printStackTrace();}finally {db
.close();return nmae
;}}public List
<BookBean> getAllBooks(){List
<BookBean> books
= new ArrayList<>();SQLiteDatabase db
= this.getWritableDatabase();Cursor cursor
= db
.query("book", null
, null
, null
, null
, null
, "number desc", null
);while (cursor
.moveToNext()) {BookBean book
= new BookBean();String name
= cursor
.getString(cursor
.getColumnIndex("name"));String introduce
= cursor
.getString(cursor
.getColumnIndex("introduce"));String pub
= cursor
.getString(cursor
.getColumnIndex("public_com"));String type
= cursor
.getString(cursor
.getColumnIndex("type"));int count
= cursor
.getInt(cursor
.getColumnIndex("count"));int look_number
= cursor
.getInt(cursor
.getColumnIndex("look_number"));int star_number
= cursor
.getInt(cursor
.getColumnIndex("star_number"));int price
= cursor
.getInt(cursor
.getColumnIndex("price"));int id
= cursor
.getInt(cursor
.getColumnIndex("number"));book
.setNumber(id
);book
.setName(name
);book
.setPublic_com(pub
);book
.setLook_number(look_number
);book
.setStar_number(star_number
);book
.setIntroduce(introduce
);book
.setCount(count
);book
.setPrice(price
);book
.setType(type
);books
.add(book
);}cursor
.close();db
.close();return books
;}public void addBook(BookBean book
){SQLiteDatabase db
= this.getReadableDatabase();ContentValues values
= new ContentValues();values
.put("name",book
.getName());values
.put("price",book
.getPrice());values
.put("count",book
.getCount());values
.put("introduce","這是有關(guān):"+book
.getName()+"的介紹");values
.put("type",book
.getType());long book1
= db
.insert("book", null
, values
);System
.out
.println("-=-----book1---添加一本-"+book1
);values
.clear();db
.close();}public void addBooks(){SQLiteDatabase db
= this.getReadableDatabase();ContentValues values
= new ContentValues();Random random
= new Random();String bookName
= Constants
.getBookName(random
.nextInt(100));values
.put("name", bookName
);values
.put("price",random
.nextInt(90)/2);values
.put("count",random
.nextInt(40));values
.put("introduce","這是有關(guān):"+bookName
+"的介紹");values
.put("type","文學(xué)");db
.insert("book",null
,values
);values
.clear();db
.close();}public void addDefaultData(){SQLiteDatabase db
= this.getWritableDatabase();ContentValues values
= new ContentValues();for(int i
=1;i
<11;i
++){Random random
= new Random();int index
= random
.nextInt(100);String bookName
= Constants
.getBookName(index
);values
.put("type","文學(xué)類");values
.put("price",i
+index
);values
.put("star_number",0);values
.put("public_com","聽雨軒出版社");values
.put("count",20+i
);values
.put("name",bookName
+"傳");values
.put("introduce","介紹:"+bookName
);try {long book
= db
.insert("book", null
, values
);}catch (SQLException e
){e
.printStackTrace();}finally {values
.clear();}}db
.close();}public void addToMyBorreow(String username
,String bookName
) {SimpleDateFormat simpleDateFormat
= new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date
= new Date(System
.currentTimeMillis());String time
= simpleDateFormat
.format(date
);int userId
= getUserId(username
);SQLiteDatabase db
= this.getReadableDatabase();ContentValues values
= new ContentValues();values
.put("userid",userId
);values
.put("bookname",bookName
);values
.put("outtime",time
);try{db
.insert("outbook",null
,values
);}catch (SQLException e
){e
.printStackTrace();}finally {values
.clear();db
.close();}}public void getUserBorrows(String name
){String sql
= "select j.outtime,b.name,j.duration from outbook j,book b,user u where u.name='"+name
+"' and u.id=j.userid and b.number=j.bookid ";SQLiteDatabase db
= this.getWritableDatabase();Cursor cursor
= db
.rawQuery(sql
, null
);
while(cursor
.moveToNext()){String outtime
= cursor
.getString(cursor
.getColumnIndex("outtime"));String name2
= cursor
.getString(cursor
.getColumnIndex("name"));int duration
= cursor
.getInt(cursor
.getColumnIndex("duration"));}}public void selectAllBorrow(String name
){List
<BorrowBean> books
= new ArrayList<>();int userId
= getUserId(name
);SQLiteDatabase db
= this.getReadableDatabase();Cursor cursor
= db
.query("outbook", null
, null
, null
, null
, null
, null
);while (cursor
.moveToNext()){int id
= cursor
.getInt(cursor
.getColumnIndex("userid"));if(userId
== id
){BorrowBean bean
= new BorrowBean();String outtime
= cursor
.getString(cursor
.getColumnIndex("outtime"));String name2
= cursor
.getString(cursor
.getColumnIndex("name"));bean
.setTime(outtime
);bean
.setName(name2
);System
.out
.println("------------------"+outtime
+"---"+name2
);}}}public List
<UserBean> getAllUsers(){List
<UserBean> users
= new ArrayList<>();SQLiteDatabase db
= this.getWritableDatabase();
Cursor cursor
= db
.rawQuery("select * from user order by id desc", null
);while(cursor
.moveToNext()){int id
= cursor
.getInt(cursor
.getColumnIndex("id"));int type
= cursor
.getInt(cursor
.getColumnIndex("type"));String name
= cursor
.getString(cursor
.getColumnIndex("name"));String sex
= cursor
.getString(cursor
.getColumnIndex("sex"));String major
= cursor
.getString(cursor
.getColumnIndex("major"));String email
= cursor
.getString(cursor
.getColumnIndex("email"));String pwd
= cursor
.getString(cursor
.getColumnIndex("password"));UserBean bean
= new UserBean();bean
.setType(type
);bean
.setPassword(pwd
);bean
.setId(id
);bean
.setMajor(major
);bean
.setSex(sex
);bean
.setEmail(email
);bean
.setName(name
);users
.add(bean
);}cursor
.close();db
.close();return users
;}
}
總結(jié)
以上是生活随笔為你收集整理的【安卓大作业】Android Studio图书管理系统 听雨轩的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。