商城管理系统
題 目 商城管理系統設計與實現
系 別信息與控制工程系
專 業計算機科學與技術
班 級1701
學 號173030112
姓 名李亞楠
1. 基于Android的商城管理系統設計與實現
1.1系統需求分析
1.1.1用例分析
用例圖如1.1所示
圖1.1 商城用例圖
1.1.2數據邏輯模型
商城管理系統ER圖如圖1.2所示
n
n n
1 1
n
圖1.2 商城管理系統E-R圖
1.2系統總體設計
圖1.3商城管理功能圖
在互聯網迅猛發展的時代,網上購物已經成為許多人日常生活的一部分,足不出戶,便可得天下萬物。而在移動平臺技術日益成熟的如今,在移動平臺上實現網上購物功能更是未來的趨勢,時代的潮流。
為了在Android平臺上實現移動商城系統,本文以基于Linux的開放源代碼操作系統Android為研究對象,進行實際的開發工作。主要任務包括三個方面:第一,在Android系統上開發一款功能強大,可維護性好,用戶友好的手機客戶端軟件;第二,開發一款能有效支持移動平臺應用的,穩定,高效的服務端后臺系統;第三,尋求一種能夠有效的解決移動平臺與服務器之間數據交互,同時降低系統消耗的數據傳輸途徑。
論文從項目的背景出發,介紹了基于Android的移動商城系統的開發過程和研究價值,探討了如何將各項先進技術融入項目的開發過程中。本文以本系統的部分后臺開發,前臺客戶端相關功能的實現,以及前后臺之間數據傳輸的開發過程為例,詳細的描述了移動商城系統的設計和實現的過程,同時從系統開發的需求分析,詳細設計到最終的測試等多個方面對系統進行了全方位的描述,并探討了下一步的改進計劃。
1.3系統數據庫設計
根據前面E-R圖設計出實現系統功能所需要的數據庫表的結構,并以表格的形式列出每一個數據庫表的表結構,即字段名、類型、長度、小數位數、是否主鍵、索引、是否允許為空、說明等。并介紹表與表之間的邏輯關系。如表3.1所示:
表3.1 user
|
字段名 |
數據類型 |
長度 |
主鍵 |
索引 |
外鍵 |
可空 |
|
rowid |
text |
11 |
是 |
否 |
否 |
|
|
userid |
int |
50 |
否 |
否 |
否 |
|
|
password |
text |
50 |
否 |
否 |
否 |
表3.2 goods
|
字段名 |
數據類型 |
長度 |
主鍵 |
索引 |
外鍵 |
可空 |
|
rowid |
text |
11 |
是 |
否 |
否 |
|
|
shopname |
text |
20 |
否 |
否 |
否 |
|
|
shopintro |
text |
50 |
否 |
否 |
否 |
|
|
shopprice |
int |
10 |
否 |
否 |
否 |
|
|
shopnum |
int |
Unknow |
否 |
否 |
否 |
|
|
shopdeller |
text |
30 |
否 |
否 |
否 |
表3.3buyer_goods(部門表)
|
字段名 |
數據類型 |
長度 |
主鍵 |
索引 |
外鍵 |
可空 |
|
rowid |
int |
11 |
是 |
否 |
否 |
|
|
id |
int |
20 |
否 |
否 |
否 |
|
|
buyerid |
int |
50 |
否 |
否 |
否 |
|
|
buyershopnum |
text |
50 |
否 |
否 |
否 |
|
|
shopname |
text |
50 |
否 |
否 |
否 |
1.4系統詳細設計
1.4.1商家模塊
表3.4商家模塊
|
編號 |
輸入 |
處理 |
輸出 |
|
1 |
用戶名和密碼 |
根據輸入用戶名從數據庫中找一個同名的用戶并且進行密碼比對 |
成功則跳轉到添加商品,失敗就轉到登錄頁面 |
|
2 |
用戶名和密碼 |
向數據庫中插入一個新的商家用戶 |
成功則提示失敗則轉到添加頁面 |
1.4.2顧客模塊
表3.10部門管理模塊
|
編號 |
輸入 |
處理 |
輸出 |
|
1 |
用戶名和密碼 |
向數據庫中插入一個新的顧客用戶 |
成功則提示失敗則轉到添加頁面 |
|
2 |
充值 |
輸入充值金額 |
成功則提示充值成功 |
|
3 |
購買 |
點擊要購買商品 |
成功點擊已購商品顯示購買的商品則購買成功 |
1.4.3登錄設計
界面設計的時候用了toast提示,然后用intent切換頁面。登錄注冊頁面如下圖3.4 所示
圖3.4登錄注冊頁面圖
1.5系統實現
1.5.1注冊模塊
注冊包括商家注冊和顧客注冊。
1.5.1.1顧客注冊
顧客注冊包括輸入賬號,輸入密碼,輸入充值金額之后點顧客,最后點擊注冊。
1.5.1.2商家注冊
商家注冊包括輸入賬號,輸入密碼,輸入充值金額之后點商家,最后點擊注冊。
顧客和商家注冊代碼如下:
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
try{
// 打開數據庫,如果數據庫不存在則創建數據庫
mSQLiteDatabase= this.openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
} catch(Exception ex) {
Toast.makeText(this, "打開或創建數據庫異常:"+ ex.getMessage(), 2000).show();
}
edt_userid= (EditText)this.findViewById(R.id.edt_userid);
edt_userpwd= (EditText)this.findViewById(R.id.edt_userpwd);
edt_usermoney= (EditText)this.findViewById(R.id.edt_usermoney);
Button bt3= (Button) findViewById(R.id.button3);
bt3.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
// TODOAuto-generated method stub
ShopregActivity.this.finish();
}
});
Button button_register= (Button) this.findViewById(R.id.button_register);
button_register.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
// TODOAuto-generated method stub
String userid= edt_userid.getText().toString();
String userpwd= edt_userpwd.getText().toString();
intusermoney=Integer.parseInt(edt_usermoney.getText().toString());
RadioButton radio_buyer=(RadioButton) findViewById(R.id.radio_buyer);
String type= "seller";
if(radio_buyer.isChecked()){
type= "buyer";
}
try{
mSQLiteDatabase.execSQL("insert into user values('"+userid+"','"+userpwd+"','"+usermoney+"','"+type+"')");
Toast.makeText(ShopregActivity.this, "注冊成功!", 2000).show();
}catch(Exception e){
Toast.makeText(ShopregActivity.this, "新增用戶失敗:"+ e.getMessage(), 2000).show();
}
}
});
}
1.5.2商家登錄模塊
添加商品的名稱價格數量,之后點擊發布商品。
商家發布商品代碼: @Override
publicvoidonClick(View v) {
// TODOAuto-generated method stub
String shopname= edit_shopname.getText().toString();
String shopintro= edit_shopintro.getText().toString();
intshopprice= Integer.parseInt(edit_shopprice.getText()
.toString());
intshopnum= Integer.parseInt(edit_shopnum.getText()
.toString());
String sql= "insert into goods values('"+ shopname+ "','"
+ shopintro+ "',"+shopprice+ ","+ shopnum+ ",'"+ userId+ "')";
try{
// 打開數據庫,如果數據庫不存在則創建數據庫
mSQLiteDatabase= openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopAddsActivity.this, "添加商品成功!",
1000).show();
Intent i= newIntent(ShopAddsActivity.this,
null);//SellerCheckShopActivity.class
Bundle b= newBundle();
b.putString("user", userId);
i.putExtra("data", b);
startActivity(i);
} catch(Exception e) {
Toast.makeText(ShopAddsActivity.this,
"添加商品異常!" + e.getMessage(), 1000).show();
}
}
});
}
1.5.3用戶登錄模塊
主要可以查看商家發布的商品名稱,自己的余額,如果余額不足可以輸入充值金額然后點擊充值按鈕進行充值。
購買商品代碼實現:publicvoidonClick(View v) {
// TODOAuto-generated method stub
String shopname= tv.getText().toString();
buy_shopnum=Integer.parseInt(edt_buy_shopnum.getText().toString());
intprice=Integer.parseInt(shopprice);
inttotal_price=buy_shopnum*price;
try{
String a="select usermoney from user where userid='"+userId+"' ";
Cursor cursor= mSQLiteDatabase.rawQuery(a, null);
while(cursor.moveToNext()) {
buyermoney= cursor.getInt(cursor.getColumnIndex("usermoney"));
}
if(buyermoney>=total_price){
String sql= "update goods set shopnum=shopnum-"+buy_shopnum+" where shopname='"+shopname+"' ";
mSQLiteDatabase.execSQL(sql);
sql="update user set usermoney=usermoney-"+total_price+" where userid='"+userId+"'";
mSQLiteDatabase.execSQL(sql);
sql= "insert into buyer_goods(buyerid,shopname,buyshopnum) values('"+ userId+ "','"
+ shopname+ "','"+buy_shopnum+"')";
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopbSelectActivity.this, "購買成功!",
1000).show();
Intent i= newIntent(ShopbSelectActivity.this,
ShopbSelectActivity.class);
Bundle b= newBundle();
b.putString("user", userId);
i.putExtra("data", b);
startActivity(i);
}
else{Toast.makeText(ShopbSelectActivity.this, "余額不足,請充值!",
3000).show();}
} catch(Exception e) {
Toast.makeText(ShopbSelectActivity.this,
"添加商品異常!"+ e.getMessage(), 1000).show();
}
}
});
充值代碼實現:
btn_myshop.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
Intent i= newIntent(ShopbSelectActivity.this,
ShopbuyerActivity.class);
Bundle b= newBundle(); //發送數據
b.putString("user", userId);
i.putExtra("data", b);
startActivity(i);
}
});
edt_addmoney=(EditText)this.findViewById(R.id.edittext_addmoney);
btn_addmoney= (Button)this.findViewById(R.id.button_addmoney);
btn_addmoney.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
String addmoney=edt_addmoney.getText().toString();
String sql= "update user set usermoney=usermoney+"+addmoney+" where userid='"+userId+"' ";
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopbSelectActivity.this, "成功充值"+addmoney+"元!",
1000).show();
Intent i= newIntent(ShopbSelectActivity.this,
ShopbSelectActivity.class);
Bundle b= newBundle();
b.putString("user", userId);
i.putExtra("data", b);
startActivity(i);
}
});
1.5.3.1查看已購商品
點擊查看已購商品,頁面跳轉到已購商品的頁面,頁面顯示你當前的余額和你所購買的商品名稱,商品單價,購買商品數量和你購買時商家的名稱。點擊返回按鈕可以返回到前一個頁面。
查看已購商品代碼實現:
privatevoidselectData() {
String sql= "select g.shopname,g.shopprice,bd.buyshopnum,g.shopseller from buyer_goods as bd,goods as g where bd.buyerid='"
+ userId+ "' and bd.shopname=g.shopname";
Cursor cursor= mSQLiteDatabase.rawQuery(sql, null);
if(cursor!= null) {
// 定義存儲查詢結果的List,每一行數據類型為Map<String, Object>,其中String代表列名,Object為值
List<Map<String, Object>> list= newArrayList<Map<String, Object>>();
Map<String, Object> map;
// 遍歷游標對象,每一行獲取的數據存放到Map<String, Object>類型的變量map中
while(cursor.moveToNext()) {
map= newHashMap<String, Object>();
map.put("shopname", cursor.getString(cursor.getColumnIndex("shopname")));
map.put("shopprice", cursor.getInt(cursor.getColumnIndex("shopprice")));
map.put("buyshopnum", cursor.getInt(cursor.getColumnIndex("buyshopnum")));
map.put("shopseller", cursor.getString(cursor.getColumnIndex("shopseller")));
list.add(map);
}
// 創建Adapter,并于ListView綁定
SimpleAdapter adapter= newSimpleAdapter(this, list, R.layout.buyer_myshop,
newString[] { "shopname", "shopprice", "buyshopnum", "shopseller"},
newint[] { R.id.tv_buyer_myshopname, R.id.tv_buyer_myshopprice, R.id.tv_buyer_myshopnum,
R.id.tv_buyer_mysellername});
lv1.setAdapter(adapter);
}
}
privatevoidselectBuyermoney() {
String sql= "select usermoney from user where userid='"+ userId+ "'";
// 獲得查詢結果對應的游標對象
Cursor cursor= mSQLiteDatabase.rawQuery(sql, null);
if(cursor!= null) {
// 定義存儲查詢結果的List,每一行數據類型為Map<String, Object>,其中String代表列名,Object為值
List<Map<String, Object>> list= newArrayList<Map<String, Object>>();
Map<String, Object> map;
while(cursor.moveToNext()) {
map= newHashMap<String, Object>();
map.put("usermoney", cursor.getInt(cursor.getColumnIndex("usermoney")));
list.add(map);
}
// 創建Adapter,并于ListView綁定
SimpleAdapter adapter= newSimpleAdapter(this, list, R.layout.buyer_money2, newString[] { "usermoney"},
newint[] { R.id.tv_buyer_money2});
lv2.setAdapter(adapter);
}
}
1.6系統測試
本次實習所用的開發環境為
開發技術:android
數據庫:sqlLite
開發工具:eclipse
1.6.1項目測試
1、測試登錄功能
2、測試注冊功能
3、測試添加商品功能
4、測試用戶購買充值功能
5、測試查已購商品功能
1.6.2測試結果
以上截圖測試了本系統的大部分功能,并且成功實現了每個功能。
2. 課程設計總結
本次課程設計,讓我受益匪淺,這次的課程設計把自己學的java集合利用上去了,非常開心。還有就是雖然上課聽老師講課,但是自己學完做做作業之后就沒再看過,現在很多知識都忘記了,通過這次的課程設計我回憶了一遍android的基礎東西,而且把java知識復習了,通過這次的android課程設計讓我更加了解了這門學科,讓自己更有興趣的去學習這門學科,讓我感覺非常開心。
然后就是通過這次的課程設計我感覺到了寫代碼需要非常嚴謹,在這次的課程設計中我深有體會,尤其是調用layout中的 控件,在我感覺如果寫的不嚴謹在你調用這個控件時就非常的困難,所以我們在課程設計需要把每個控件的ID設置嚴謹一點,這樣我們才能輕松地調用這個控件,還有就是不能有一點錯誤,不然整個程序就會不能順利地運行,還有一點就是需要做到不懂就問的習慣,讓老師同學幫助一下這樣的話,讓你在寫代碼將會更加的流暢,還有一點就是不論遇到什么困難和難題都需要我們的堅持,在堅持之后我們一定能夠成功完成。總之,在這次的課程設計當中我學習到了很多知識,讓我也鞏固了一下書本上的知識,此次的課程設計在我學習生涯上起到了至關重要的一步。
附錄:
商城購物系統的實現
使用技術:
UI設計:建立了5個界面
數據庫使用:運用sqlLite數據庫建立了3個表分別是用戶信息表、商品表和用戶購買商品表。
Activity組件的應用:運用了Activity組件實現了登錄界面的跳轉功能和
查看已購商品的跳轉界面。
ListView的使用:使用了2個listview實現了商家添加商品的顯示和查看用戶已購商品的顯示。
UML類圖:
代碼:
清單文件:
/ShopCenter/src/com/example/shopcenter/MainActivity.java
/ShopCenter/src/com/example/shopcenter/ShopAddsActivity.java
/ShopCenter/src/com/example/shopcenter/ShopbSelectActivity.java
/ShopCenter/src/com/example/shopcenter/ShopbuyerActivity.java
/ShopCenter/src/com/example/shopcenter/ShopregActivity.java
/ShopCenter/res/layout/activity_buyer_check_shop.xml
/ShopCenter/res/layout/activity_buyer_select_shop.xml
/ShopCenter/res/layout/activity_main.xml
/ShopCenter/res/layout/activity_register.xml
/ShopCenter/res/layout/activity_seller_add_shop.xml
/ShopCenter/res/layout/buyer_money.xml
/ShopCenter/res/layout/buyer_money2.xml
/ShopCenter/res/layout/buyer_myshop.xml
/ShopCenter/res/layout/buyer_shop.xml
/ShopCenter/res/layout/shop_buyer.xml
布局文件:
截圖:
/ShopCenter/res/layout/activity_buyer_check_shop.xml
/ShopCenter/res/layout/activity_buyer_select_shop.xml
/ShopCenter/res/layout/activity_main.xml
/ShopCenter/res/layout/activity_register.xml
/ShopCenter/res/layout/activity_seller_add_shop.xml
/ShopCenter/res/layout/buyer_money.xml
/ShopCenter/res/layout/buyer_money2.xml
/ShopCenter/res/layout/buyer_myshop.xml
/ShopCenter/res/layout/buyer_shop.xml
/ShopCenter/res/layout/shop_buyer.xml
Java原代碼:
主界面
packagecom.example.shopcenter;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;
publicclassMainActivity extendsActivity {
//定義用于訪問SQLite的對象
privateSQLiteDatabase mSQLiteDatabase = null;
//數據庫名稱
privatestaticfinalString DATABASE_NAME = "test.db";
privatestaticfinalString CREATE_TABLE_USER="create table if not exists user(userid text primary key,userpwd text,usermoney integer ,usertype text)";
privatestaticfinalString CREATE_TABLE_GOODS="create table if not exists goods(shopname text primary key,shopintro text,shopprice integer,shopnum integer,shopseller text)";
privatestaticfinalString CREATE_TABLE_BUYER_GOODS="create table if not exists buyer_goods(id integer primary key autoincrement,buyerid text,shopname text,buyshopnum integer)";
privateEditText edt_login_userid,edt_login_userpwd;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
// 打開數據庫,如果數據庫不存在則創建數據庫
mSQLiteDatabase = this.openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
} catch(Exception ex) {
Toast.makeText(this, "打開或創建數據庫異常:"+ ex.getMessage(), 2000).show();
}
try{
mSQLiteDatabase.execSQL(CREATE_TABLE_USER);
mSQLiteDatabase.execSQL(CREATE_TABLE_GOODS);
mSQLiteDatabase.execSQL(CREATE_TABLE_BUYER_GOODS);
} catch(Exception ex) {
Toast.makeText(this, "創建用戶表異常:"+ ex.getMessage(), 2000).show();
}
Button goto_register = (Button) this.findViewById(R.id.button_goto_register);
goto_register.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
// TODOAuto-generated method stub
Intent i = newIntent(MainActivity.this,ShopregActivity.class);
startActivity(i);
}
});
edt_login_userid = (EditText)this.findViewById(R.id.edt_login_userid);
edt_login_userpwd = (EditText)this.findViewById(R.id.edt_login_userpwd);
Button user_login = (Button) this.findViewById(R.id.button_login);
user_login.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
// TODOAuto-generated method stub
String login_userid = edt_login_userid.getText().toString();
String login_userpwd = edt_login_userpwd.getText().toString();
try{
String sql = "select * from user where userid='"+login_userid+"' and userpwd='"+login_userpwd+"'";
Cursor cursor = mSQLiteDatabase.rawQuery(sql,null);
if(cursor.moveToNext()){
String type = cursor.getString(cursor.getColumnIndex("usertype"));
Bundle b = newBundle();
b.putString("user", login_userid);
if(type.equals("seller")){
Intent i = newIntent(MainActivity.this,ShopAddsActivity.class);
i.putExtra("data", b);
startActivity(i);
}else{
Intent i = newIntent(MainActivity.this,ShopbSelectActivity.class);
i.putExtra("data", b);
startActivity(i);
}
}else{
Toast.makeText(MainActivity.this, "賬號或密碼錯誤!", 2000).show();
}
}catch(Exception e){
Toast.makeText(MainActivity.this, e.getMessage(), 2000).show();
}
}
});
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}
用戶注冊類:
package com.example.shopcenter;
import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class ShopregActivity extends Activity {
//定義用于訪問SQLite的對象
private SQLiteDatabase mSQLiteDatabase = null;
//數據庫名稱
private static final String DATABASE_NAME = "test.db";
private EditText edt_userid,edt_userpwd,edt_usermoney;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
try {
// 打開數據庫,如果數據庫不存在則創建數據庫
mSQLiteDatabase = this.openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
} catch (Exception ex) {
Toast.makeText(this, "打開或創建數據庫異常:" + ex.getMessage(), 2000).show();
}
edt_userid = (EditText)this.findViewById(R.id.edt_userid);
edt_userpwd = (EditText)this.findViewById(R.id.edt_userpwd);
edt_usermoney= (EditText)this.findViewById(R.id.edt_usermoney);
Button bt3 = (Button) findViewById(R.id.button3);
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ShopregActivity.this.finish();
}
});
Button button_register = (Button) this.findViewById(R.id.button_register);
button_register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String userid = edt_userid.getText().toString();
String userpwd = edt_userpwd.getText().toString();
int usermoney=Integer.parseInt(edt_usermoney.getText().toString());
RadioButton radio_buyer =(RadioButton) findViewById(R.id.radio_buyer);
String type = "seller";
if(radio_buyer.isChecked()){
type = "buyer";
}
try{
mSQLiteDatabase.execSQL("insert into user values('"+userid+"','"+userpwd+"','"+usermoney+"','"+type+"')");
Toast.makeText(ShopregActivity.this, "注冊成功!", 2000).show();
}catch(Exception e){
Toast.makeText(ShopregActivity.this, "新增用戶失敗:" + e.getMessage(), 2000).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.register, menu);
return true;
}
}
商品發布類:
packagecom.example.shopcenter;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;
publicclassShopAddsActivity extendsActivity {
privateSQLiteDatabase mSQLiteDatabase= null;
privatestaticfinalString DATABASE_NAME= "test.db";
privateEditText edit_shopnum, edit_shopname, edit_shopprice,edit_shopintro;
privateString userId;
Button button_addshop, button_reset,button_seller_myshop;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seller_add_shop);
Intent i = this.getIntent();
Bundle b = i.getBundleExtra("data");
userId= b.getString("user");
edit_shopname= (EditText) findViewById(R.id.edt_shopname);
edit_shopintro= (EditText) findViewById(R.id.edt_shopintro);
edit_shopprice= (EditText) findViewById(R.id.edt_shopprice);
edit_shopnum= (EditText) findViewById(R.id.edit_shopnum);
Button bt5 = (Button) findViewById(R.id.button5);
bt5.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
// TODOAuto-generated method stub
ShopAddsActivity.this.finish();
}
});
button_addshop= (Button) findViewById(R.id.button_addshop);
button_addshop.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
// TODOAuto-generated method stub
String shopname = edit_shopname.getText().toString();
String shopintro = edit_shopintro.getText().toString();
intshopprice = Integer.parseInt(edit_shopprice.getText()
.toString());
intshopnum = Integer.parseInt(edit_shopnum.getText()
.toString());
String sql = "insert into goods values('"+ shopname + "','"
+ shopintro + "',"+shopprice + ","+ shopnum + ",'"+ userId+ "')";
try{
// 打開數據庫,如果數據庫不存在則創建數據庫
mSQLiteDatabase= openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopAddsActivity.this, "添加商品成功!",
1000).show();
Intent i = newIntent(ShopAddsActivity.this,
null);//SellerCheckShopActivity.class
Bundle b = newBundle();
b.putString("user", userId);
i.putExtra("data", b);
startActivity(i);
} catch(Exception e) {
// Toast.makeText(ShopAddsActivity.this,
// "添加商品異常!" + e.getMessage(), 1000).show();
}
}
});
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.seller_add_shop, menu);
returntrue;
}
}
已購商品類:
package com.example.shopcenter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ShopbuyerActivity extends Activity {
private SQLiteDatabase mSQLiteDatabase = null;
// 數據庫名稱
private static final String DATABASE_NAME = "test.db";
private String userId, shopname;
private TextView tv;
private ListView lv1, lv2;
int buyermoney;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_check_shop);
Intent i = this.getIntent(); // 接收數據
Bundle b = i.getBundleExtra("data");
userId = b.getString("user");
lv2 = (ListView) this.findViewById(R.id.lv_buyer_money);
lv1 = (ListView) this.findViewById(R.id.lv_buyer_myshop);
mSQLiteDatabase = openOrCreateDatabase(DATABASE_NAME, Activity.MODE_PRIVATE, null);
selectBuyermoney();
selectData();
Button bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ShopbuyerActivity.this.finish();
}
});
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// 根據參數arg2獲取當前位置的數據(行),并根據key獲取對應的value
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) lv1.getItemAtPosition(arg2);
tv.setText(map.get("shopname").toString());
shopname = map.get("shopname").toString();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.buyer_check_shop, menu);
return true;
}
private void selectData() {
String sql = "select g.shopname,g.shopprice,bd.buyshopnum,g.shopseller from buyer_goods as bd,goods as g where bd.buyerid='"
+ userId + "' and bd.shopname=g.shopname";
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
// 定義存儲查詢結果的List,每一行數據類型為Map<String, Object>,其中String代表列名,Object為值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
// 遍歷游標對象,每一行獲取的數據存放到Map<String, Object>類型的變量map中
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put("shopname", cursor.getString(cursor.getColumnIndex("shopname")));
map.put("shopprice", cursor.getInt(cursor.getColumnIndex("shopprice")));
map.put("buyshopnum", cursor.getInt(cursor.getColumnIndex("buyshopnum")));
map.put("shopseller", cursor.getString(cursor.getColumnIndex("shopseller")));
list.add(map);
}
// 創建Adapter,并于ListView綁定
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.buyer_myshop,
new String[] { "shopname", "shopprice", "buyshopnum", "shopseller" },
new int[] { R.id.tv_buyer_myshopname, R.id.tv_buyer_myshopprice, R.id.tv_buyer_myshopnum,
R.id.tv_buyer_mysellername });
lv1.setAdapter(adapter);
}
}
private void selectBuyermoney() {
String sql = "select usermoney from user where userid='" + userId + "'";
// 獲得查詢結果對應的游標對象
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
// 定義存儲查詢結果的List,每一行數據類型為Map<String, Object>,其中String代表列名,Object為值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put("usermoney", cursor.getInt(cursor.getColumnIndex("usermoney")));
list.add(map);
}
// 創建Adapter,并于ListView綁定
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.buyer_money2, new String[] { "usermoney" },
new int[] { R.id.tv_buyer_money2 });
lv2.setAdapter(adapter);
}
}
}
商品購買類:
package com.example.shopcenter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
@SuppressLint("ShowToast")
public class ShopbSelectActivity extends Activity {
private SQLiteDatabase mSQLiteDatabase = null;
//數據庫名稱
private static final String DATABASE_NAME = "test.db";
private TextView tv;
private ListView lv1,lv2;
private Button btn_selectshop,btn_myshop,btn_addmoney;
private String userId;
private String shopprice;
EditText edt_addmoney,edt_buy_shopnum;
int buy_shopnum;
int buyermoney;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_select_shop);
Intent i = this.getIntent();
Bundle b = i.getBundleExtra("data");
userId = b.getString("user");
tv = (TextView)this.findViewById(R.id.tv_buyer_selectshopname);
lv2 = (ListView) this.findViewById(R.id.lv_buyer_money);
lv1 = (ListView) this.findViewById(R.id.lv_buyer_shop);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) lv1
.getItemAtPosition(arg2);
tv.setText(map.get("shopname").toString());
shopprice=map.get("shopprice").toString();
}
});
try {
mSQLiteDatabase = this.openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
} catch (Exception ex) {
Toast.makeText(this, "打開或創建數據庫異常:" + ex.getMessage(), 2000).show();
}
selectData();
selectBuyermoney();
edt_buy_shopnum=(EditText)this.findViewById(R.id.edittext_buy_shopnum);
btn_selectshop = (Button)this.findViewById(R.id.button_buyer_selectshop);
btn_selectshop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String shopname = tv.getText().toString();
buy_shopnum=Integer.parseInt(edt_buy_shopnum.getText().toString());
int price=Integer.parseInt(shopprice);
int total_price=buy_shopnum*price;
try {
String a="select usermoney from user where userid='"+userId+"' " ;
Cursor cursor = mSQLiteDatabase.rawQuery(a, null);
while (cursor.moveToNext()) {
buyermoney= cursor.getInt(cursor.getColumnIndex("usermoney"));
}
if(buyermoney>=total_price){
String sql = "update goods set shopnum=shopnum-"+buy_shopnum+" where shopname='"+shopname+"' " ;
mSQLiteDatabase.execSQL(sql);
sql="update user set usermoney=usermoney-"+total_price+" where userid='"+userId+"'";
mSQLiteDatabase.execSQL(sql);
sql = "insert into buyer_goods(buyerid,shopname,buyshopnum) values('" + userId + "','"
+ shopname + "','"+buy_shopnum+"')";
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopbSelectActivity.this, "購買成功!",
1000).show();
Intent i = new Intent(ShopbSelectActivity.this,
ShopbSelectActivity.class);
Bundle b = new Bundle();
b.putString("user", userId);
i.putExtra("data", b);
startActivity(i);
}
else {Toast.makeText(ShopbSelectActivity.this, "余額不足,請充值!",
3000).show();}
} catch (Exception e) {
Toast.makeText(ShopbSelectActivity.this,
"添加商品異常!" + e.getMessage(), 1000).show();
}
}
});
btn_myshop= (Button)this.findViewById(R.id.button_buyer_myshop);
btn_myshop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ShopbSelectActivity.this,
ShopbuyerActivity.class);
Bundle b = new Bundle(); //發送數據
b.putString("user", userId);
i.putExtra("data", b);
startActivity(i);
}
});
edt_addmoney=(EditText)this.findViewById(R.id.edittext_addmoney);
btn_addmoney= (Button)this.findViewById(R.id.button_addmoney);
btn_addmoney.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String addmoney=edt_addmoney.getText().toString();
String sql = "update user set usermoney=usermoney+"+addmoney+" where userid='"+userId+"' " ;
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopbSelectActivity.this, "成功充值"+addmoney+"元!",
1000).show();
Intent i = new Intent(ShopbSelectActivity.this,
ShopbSelectActivity.class);
Bundle b = new Bundle();
b.putString("user", userId);
i.putExtra("data", b);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.buyer_select_shop, menu);
return true;
}
private void selectData() {
String sql = "select * from goods ";
//獲得查詢結果對應的游標對象
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
//定義存儲查詢結果的List,每一行數據類型為Map<String, Object>,其中String代表列名,Object為值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
//遍歷游標對象,每一行獲取的數據存放到Map<String, Object>類型的變量map中
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put("shopname", cursor.getString(cursor.getColumnIndex("shopname")));
map.put("shopintro", cursor.getString(cursor.getColumnIndex("shopintro")));
map.put("shopprice", cursor.getInt(cursor.getColumnIndex("shopprice")));
map.put("shopnum", cursor.getString(cursor.getColumnIndex("shopnum")));
map.put("shopseller", cursor.getString(cursor.getColumnIndex("shopseller")));
list.add(map);
}
//創建Adapter,并于ListView綁定
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.buyer_shop, new String[] { "shopname","shopintro", "shopprice",
"shopnum","shopseller"}, new int[] { R.id.tv_buyer_myshopname,R.id.tv_buyer_myshopintro, R.id.tv_buyer_myshopprice,
R.id.tv_buyer_shopnum ,R.id.tv_buyer_sellername});
lv1.setAdapter(adapter);
}
}
private void selectBuyermoney(){
String sql = "select usermoney from user where userid='"+userId+"'";
//獲得查詢結果對應的游標對象
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
//定義存儲查詢結果的List,每一行數據類型為Map<String, Object>,其中String代表列名,Object為值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put("usermoney", cursor.getInt(cursor.getColumnIndex("usermoney")));
list.add(map);
}
//創建Adapter,并于ListView綁定
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.buyer_money, new String[] { "usermoney"}, new int[] { R.id.tv_buyer_money});
lv2.setAdapter(adapter);
}
}
}
總結
- 上一篇: 学习的目的是什么?
- 下一篇: 外卖ERP管理系统(二)