商城管理系統

 

 

 

 

 

 

 

 

 

 

 

    商城管理系統設計與實現   java

    信息與控制工程系         android

    計算機科學與技術         sql

    1701                     數據庫

    173030112                服務器

    李亞楠                   app

 

1. 基於Android的商城管理系統設計與實現

1.1 系統需求分析

1.1.1 用例分析

用例圖如1.1所示eclipse

 

圖1.1 商城用例圖數據庫設計

 

 

 

 

 

 

 

 

 

1.1.2 數據邏輯模型

商城管理系統ER圖如圖1.2所示ide

 

 

 

 

                       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.3  buyer_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 商家註冊

商家註冊包括輸入帳號,輸入密碼,輸入充值金額以後點商家,最後點擊註冊。

 

顧客和商家註冊代碼以下:

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();

}

}

});

}

 

1.5.2 商家登陸模塊

添加商品的名稱價格數量,以後點擊發布商品。

 

 

商家發佈商品代碼: @Override

public void onClick(View v) {

// TODO Auto-generated method stub

String shopname = edit_shopname.getText().toString();

String shopintro = edit_shopintro.getText().toString();

int shopprice = Integer.parseInt(edit_shopprice.getText()

.toString());

int shopnum = 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 = new Intent(ShopAddsActivity.this,

null);//SellerCheckShopActivity.class

 Bundle b = new Bundle();

 b.putString("user", userId);

i.putExtra("data", b);

startActivity(i);

} catch (Exception e) {

Toast.makeText(ShopAddsActivity.this,

"添加商品異常!" + e.getMessage(), 1000).show();

}

}

});

 

}

1.5.3 用戶登陸模塊

主要能夠查看商家發佈的商品名稱,本身的餘額,若是餘額不足能夠輸入充值金額而後點擊充值按鈕進行充值。

 

購買商品代碼實現: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.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);

}

});

1.5.3.1 查看已購商品

點擊查看已購商品,頁面跳轉到已購商品的頁面,頁面顯示你當前的餘額和你所購買的商品名稱,商品單價,購買商品數量和你購買時商家的名稱。點擊返回按鈕能夠返回到前一個頁面。

 

查看已購商品代碼實現:

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);

 

}

 

}

1.6 系統測試

本次實習所用的開發環境爲

開發技術:android

數據庫:sqlLite

開發工具:eclipse

1.6.1 項目測試 

1、測試登陸功能

2、測試註冊功能

3、測試添加商品功能

四、測試用戶購買充值功能

5、測試查已購商品功能

1.6.2 測試結果

 

 

 

 

 

 

 

以上截圖測試了本系統的大部分功能,而且成功實現了每一個功能。

 

2. 課程設計總結

本次課程設計,讓我受益不淺,此次的課程設計把本身學的java集合利用上去了,很是開心。還有就是雖然上課聽老師講課,可是本身學完作作做業以後就沒再看過,如今不少知識都忘記了,經過此次的課程設計我回憶了一遍android的基礎東西,並且把java知識複習了,經過此次的android課程設計讓我更加了解了這門學科,讓本身更有興趣的去學習這門學科,讓我感受很是開心。

而後就是經過此次的課程設計我感受到了寫代碼須要很是嚴謹,在此次的課程設計中我深有體會,尤爲是調用layout中的 控件,在我感受若是寫的不嚴謹在你調用這個控件時就很是的困難,因此咱們在課程設計須要把每一個控件的ID設置嚴謹一點,這樣咱們才能輕鬆地調用這個控件,還有就是不能有一點錯誤,否則整個程序就會不能順利地運行,還有一點就是須要作到不懂就問的習慣,讓老師同窗幫助一下這樣的話,讓你在寫代碼將會更加的流暢,還有一點就是不論遇到什麼困難和難題都須要咱們的堅持,在堅持以後咱們必定可以成功完成。總之,在此次的課程設計當中我學習到了不少知識,讓我也鞏固了一下書本上的知識,這次的課程設計在我學習生涯上起到了相當重要的一步。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

附錄:

         商城購物系統的實現

使用技術:

UI設計:創建了5個界面

數據庫使用:運用sqlLite數據庫創建了3個表分別是用戶信息表、商品表和用戶購買商品表。

Activity組件的應用運用了Activity組件實現了登陸界面的跳轉功能和

查看已購商品的跳轉界面。

ListView的使用使用了2listview實現了商家添加商品的顯示和查看用戶已購商品的顯示。     

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原代碼:

主界面

package com.example.shopcenter;

 

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.Button;

import android.widget.EditText;

import android.widget.Toast;

 

public class MainActivity extends Activity {

//定義用於訪問SQLite的對象

private SQLiteDatabase mSQLiteDatabase = null;

//數據庫名稱

private static final String DATABASE_NAME = "test.db";

private static final String CREATE_TABLE_USER="create table if not exists user(userid text primary key,userpwd text,usermoney  integer ,usertype text)";

private static final String CREATE_TABLE_GOODS="create table if not exists goods(shopname text primary key,shopintro text,shopprice integer,shopnum integer,shopseller text)";

private static final String CREATE_TABLE_BUYER_GOODS="create table if not exists buyer_goods(id integer primary key autoincrement,buyerid text,shopname text,buyshopnum integer)";

private EditText edt_login_userid,edt_login_userpwd;

 

@Override

protected void onCreate(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(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent i = new Intent(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(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-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 = new Bundle();

 

  b.putString("user", login_userid);

  

  if(type.equals("seller")){

Intent i = new Intent(MainActivity.this,ShopAddsActivity.class);

i.putExtra("data", b);

startActivity(i);

   }else{

   Intent i = new Intent(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

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

    

}

用戶註冊類:

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;

}

 

}

 

 

商品發佈類:

package com.example.shopcenter;

 

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

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.Toast;

 

 

public class ShopAddsActivity extends Activity {

private SQLiteDatabase mSQLiteDatabase = null;

private static final String DATABASE_NAME = "test.db";

private EditText edit_shopnum, edit_shopname, edit_shopprice,edit_shopintro;

private String userId;

Button button_addshop, button_reset,button_seller_myshop;

 

@Override

protected void onCreate(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(new OnClickListener() {

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

ShopAddsActivity.this.finish();

}

});

button_addshop = (Button) findViewById(R.id.button_addshop);

button_addshop.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

String shopname = edit_shopname.getText().toString();

String shopintro = edit_shopintro.getText().toString();

int shopprice = Integer.parseInt(edit_shopprice.getText()

.toString());

int shopnum = 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 = new Intent(ShopAddsActivity.this,

null);//SellerCheckShopActivity.class

 Bundle b = new Bundle();

 b.putString("user", userId);

i.putExtra("data", b);

startActivity(i);

} catch (Exception e) {

// Toast.makeText(ShopAddsActivity.this,

// "添加商品異常!" + e.getMessage(), 1000).show();

}

}

});

 

}

 

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.seller_add_shop, menu);

return true;

}

 

}

已購商品類:

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);

    

 

 

}

 

}

 

 

 

 

}

相關文章
相關標籤/搜索