一、課程設(shè)計目的
???? 1.android開發(fā)的綜合訓練
???? 2.提高軟件開發(fā)的綜合能力
二、開發(fā)環(huán)境
???????? Android Studio
三、課程設(shè)計內(nèi)容
網(wǎng)上訂餐項目是一個類似外賣的項目,其中包含訂餐的店鋪、各店鋪的菜單、購物車以及訂單與付款等模塊。在店鋪列表中可以看到店鋪的名稱、月銷售、起送價格與配送費用、配送時間以及福利等信息,點擊店鋪列表中的任意一個店鋪,進入到店鋪詳情界面,該界面主要顯示店鋪中的菜單,同時可以將想要吃的菜添加到購物車中,選完菜之后可以點擊該界面中的“去結(jié)算”按鈕,進入到訂單界面,在該界面核對已點的菜單信息,并通過“去支付”按鈕進行付款。
參考截圖(注:采用單機版開發(fā))
Android設(shè)計獲?。篽ttps://download.csdn.net/download/qq_61562251/87861222?spm=1001.2014.3001.5501
?
四、課程設(shè)計過程
各功能模塊設(shè)計,關(guān)鍵代碼,運行截圖
用戶登入驗證
public void user_into(View view) {
??? name=user_name.getText().toString();
??? password=user_password.getText().toString();
??? userHelper = new UserHelper(getApplicationContext(), UserHelper.name, null, 1);
??? if(TextUtils.isEmpty(user_name.getText())||TextUtils.isEmpty(user_password.getText())){
??????? tips.setText("輸入不能為空!");
??? }else{
??????? if(userHelper.login(name,password)){
??????????? Toast.makeText(getApplicationContext(), "登入成功", Toast.LENGTH_SHORT).show();
??????????? Intent intent = new Intent(MainActivity.this, UserInterface.class);
??????????? startActivity(intent);
??????? }else{ tips.setText("賬號或密碼不正確!"); }
??? }
}
public boolean login(String username, String password) {
??? SQLiteDatabase db = this.getWritableDatabase();
??? String sql = "Select * from "+userTable+" where user=? and password=?";
??? Cursor cursor = db.rawQuery(sql, new String[]{username, password});
??? if (cursor.moveToFirst()) {
??????? cursor.close();
??????? return true;
??? }
??? return false;
}
Listview點擊監(jiān)聽滑動監(jiān)聽與數(shù)值傳遞、適配器
//item界面設(shè)置
MyAdapter myAdapter = new MyAdapter();
merchantList.setAdapter(myAdapter);
//listView點擊監(jiān)聽,滑動監(jiān)聽
merchantList.setOnItemClickListener(new AdapterView.OnItemClickListener() {///List
??? @Override
??? //點擊監(jiān)聽
??? public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
??????? String name = (String) merchantList.getAdapter().getItem(position);
??????? Bundle bundle1 = new Bundle();
??????? bundle1.putInt("position",position);
??????? bundle1.putString("name",name);
??????? Intent intent = new Intent(UserInterface.this, MerchantInterface.class);
??????? intent.putExtras(bundle1);
??????? startActivity(intent);
??? }
??? // 狀態(tài)改變時調(diào)用
??? public void onScrollStateChanged(AbsListView absListView, int scrollState) {
??????? switch (scrollState) {
??????????? case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
??????????????? Log.i("logi", "進入慣性滑動狀態(tài)");
??????????????? break;
??? ????????case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
??????????????? Log.i("logi", "進入正在滑動狀態(tài)");
??????????????? break;
??????????? case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
??????????????? Log.i("logi", "滑動停止狀態(tài)");
?????????????? ?break;
??????? }
??? }
});
//item界面設(shè)置
class MyAdapter extends BaseAdapter {
??? //獲取 item 條目總數(shù)
??? @Override
??? public int getCount() {
??????? merchantHelper = new MerchantHelper(getApplicationContext(), MerchantHelper.name, null, 1);
??????? List<String> titles=merchantHelper.readAll();
??????? int i;
??????? for(i=0;i<titles.size();i++){
??????????? System.out.println(i);
??????? }
??????? return i;
??? }
??? //返回 item 數(shù)據(jù)對象
??? @Override
??? public Object getItem(int i) {
??????? merchantHelper = new MerchantHelper(getApplicationContext(), MerchantHelper.name, null, 1);
??????? List<String> titles=merchantHelper.readAll();
??????? return titles.get(i);
??? }
??? //返回 item 的 id
??? @Override
??? public long getItemId(int i) {
??????? return i;
??? }
??? //得到 item 視圖
??? @Override
??? public View getView(int i, View view, ViewGroup viewGroup) {
??????? merchantHelper = new MerchantHelper(getApplicationContext(), MerchantHelper.name, null, 1);
??????? List<String> titles=merchantHelper.readAll();
???? ???//加載 list_item.xml 布局文件------將布局文件轉(zhuǎn)換為一個控件
??????? View view1 = View.inflate(UserInterface.this,R.layout.item,null);
??????? TextView title = view1.findViewById(R.id.title);
??????? TextView price = view1.findViewById(R.id.price);
??????? ImageView iv = view1.findViewById(R.id.iv);
??????? title.setText(titles.get(i));
??????? price.setText(prices[i]);
??????? iv.setBackgroundResource(icons[i]);
??????? return view1;
??? }
}
數(shù)據(jù)庫獲取
//全部商家獲取
public List<String> readAll () {
??? List<String> allCommodities = new ArrayList<String>();
??? SQLiteDatabase db = this.getWritableDatabase();
??? Cursor cursor = db.rawQuery("select * from "+merchantTable,null);
??? if(cursor.moveToFirst()) {
??????? do {
??????????? String title =cursor.getString(0);
??????????? allCommodities.add(title);
??????? }while (cursor.moveToNext());
??? }
??? cursor.close();
??? return allCommodities;
}
//實現(xiàn)商店點擊事件跳轉(zhuǎn)
Bundle b = getIntent().getExtras();//獲取上一個界面?zhèn)鬟f的數(shù)值
if( b != null) {
??? name=b.getString("name");//獲取具體數(shù)值
??? merchantName.setText(name.toCharArray(), 0, name.length());
??? position = b.getInt("position");
??? Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
??? //對上一個界面?zhèn)鬟f的position值進行switch方法執(zhí)行,獲取商家頭像
??? switch (position){
??????? case 0:
??????????? headPortrait.setBackgroundResource(R.drawable.img_12);
??????????? break;
??????? case 1:
??????????? headPortrait.setBackgroundResource(R.drawable.img_13);
??????????? break;
??????? case 2:
??????????? headPortrait.setBackgroundResource(R.drawable.img_14);
??????????? break;
??????? case 3:
??????????? headPortrait.setBackgroundResource(R.drawable.img_15);
??????????? break;
??????? case 4:
??????????? headPortrait.setBackgroundResource(R.drawable.img_16);
??????????? break;
??????? case 5:
??????????? headPortrait.setBackgroundResource(R.drawable.img_17);
??????????? break;
??????? default:
??????????? throw new IllegalStateException("Unexpected value: " + position);
??? }
}
//數(shù)據(jù)庫指定內(nèi)容輸出
List<String> titles = goodsHelper.readAllStr(merchant, 0);
List<String> goodDetailS = goodsHelper.readAllStr(merchant, 2);
List<String> merchantS = goodsHelper.readAllStr(merchant, 1);
List<Integer> srcS = goodsHelper.readAllInt(merchant, 3);
List<Integer> priceS = goodsHelper.readAllInt(merchant, 4);
//item內(nèi)部鍵數(shù)據(jù)輸入
viewHolder.src.setBackgroundResource(srcS.get(i));
viewHolder.good.setText(" " + titles.get(i));
viewHolder.price.setText(" " + priceS.get(i));
viewHolder.detail.setText(" " + goodDetailS.get(i));
//商品添加點擊監(jiān)聽
viewHolder.add.setOnClickListener(new View.OnClickListener() {
??? @Override
??? public void onClick(View v) {
??????? Toast.makeText(getApplicationContext(), "添加商品:"+viewHolder.good.getText().toString(), Toast.LENGTH_SHORT).show();
??????? priceSum[0] =priceSum[0]+priceS.get(i);
??????? sumS[0] ="已添加:"+j[0]+"件? ¥";
??????? sum.setText(sumS[0]);
??????? money.setText(String.valueOf(priceSum[0]));
??????? j[0]++;
??? }
});
//結(jié)算按鈕點擊事件
settlement.setOnClickListener(new View.OnClickListener() {
??? @Override
??? public void onClick(View v) {
??????? Bundle bundle1 = new Bundle();
??????? bundle1.putInt("sumPrice",Integer.valueOf(money.getText().toString()));
??????? bundle1.putString("goodName",viewHolder.good.getText().toString());
??????? bundle1.putString("merchantName",merchantS.get(i));
??????? Intent intent = new Intent(MerchantInterface.this, Settlement.class);
??????? intent.putExtras(bundle1);
??????? startActivity(intent);
??? }
});
用戶注冊并觸發(fā)另外兩個數(shù)據(jù)庫的存儲
if (TextUtils.isEmpty(user_name.getText())){
??????????? Toast.makeText(getApplicationContext(), "用戶名不能為空", Toast.LENGTH_SHORT).show();
??????? }else if (TextUtils.isEmpty(user_password.getText())){
??????????? Toast.makeText(getApplicationContext(), "密碼不能為空", Toast.LENGTH_SHORT).show();
??????? }else if (TextUtils.isEmpty(user_repassword.getText())){
??????????? tips.setText("兩次密碼輸入不正確!");
????? ??????user_repassword.setText("");
??????? }else if(user_password.getText().toString().equals(user_repassword.getText().toString())==false){
??????????? tips.setText("兩次密碼輸入不正確!");
??????????? user_repassword.setText("");
??????? } else if(user_password.getText().toString().equals(user_repassword.getText().toString())) {
??????????? if(userHelper.addUser(name,password)){//執(zhí)行用戶添加后,同時對商家和商品進行添加
??????????????? if (merchantHelper.addUser("黃氏鹵味","188559922100")&&
??????????????????? merchantHelper.addUser("千本壽司","135164884200")&&
??????????????????? merchantHelper.addUser("悠然餐廳","199428835511")&&
????? ??????????????merchantHelper.addUser("每日早餐","177880011223")&&
??????????????????? merchantHelper.addUser("88快餐","133569010236")&&
……(省略相似代碼)
?????? ?????????????goodsHelper.addGood("韓式炸雞","華萊士","約500克",R.drawable.img_53, 20))
??????????????? {
??????????? ????????Toast.makeText(getApplicationContext(), "添加成功", Toast.LENGTH_SHORT).show();
??????????????????? //保存activity狀態(tài)到SaveTable,默認模式
???????????? ???????//默認模式:代表文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問,寫入的內(nèi)容會覆蓋源文件的內(nèi)容
??????????????????? SharedPreferences sharedP=getSharedPreferences("SaveTable",MODE_PRIVATE);
??????????????????? SharedPreferences.Editor editor=sharedP.edit();//API超連接,類似于map
??????????????????? int num=sharedP.getInt("number", 0);//獲取儲存的值
??????????????????? num++;
??????????????????? editor.putInt("number", num);//上傳數(shù)值
??????????????????? editor.commit();//數(shù)據(jù)提交
??????????????????? finish();//結(jié)束數(shù)據(jù)上傳
??????????????? }
??????????? }else{
??????????????? Toast.makeText(getApplicationContext(), "添加失敗", Toast.LENGTH_SHORT).show();
??????????? }
//??????????? Toast.makeText(getApplicationContext(), "添加失敗", Toast.LENGTH_SHORT).show();
??????? }
//item界面設(shè)置
MyAdapter myAdapter = new MyAdapter();
menu.setAdapter(myAdapter);
//傳遞數(shù)據(jù)獲取,商品金額計算
b = getIntent().getExtras();
if( b != null) {
??? name=b.getString("merchantName");
??? merchant.setText(name.toCharArray(), 0, name.length());
??? price = b.getInt("sumPrice");
??? Toast.makeText(getApplicationContext(), String.valueOf(price), Toast.LENGTH_SHORT).show();
??? price=price+5;
??? total.setText("¥ "+String.valueOf(price));
}
//item界面設(shè)置
MyAdapter myAdapter = new MyAdapter();
menu.setAdapter(myAdapter);
//傳遞數(shù)據(jù)獲取,商品金額計算
b = getIntent().getExtras();
if( b != null) {
??? name=b.getString("merchantName");
??? merchant.setText(name.toCharArray(), 0, name.length());
??? price = b.getInt("sumPrice");
??? Toast.makeText(getApplicationContext(), String.valueOf(price), Toast.LENGTH_SHORT).show();
??? price=price+5;
??? total.setText("¥ "+String.valueOf(price));
}
//地址
public void address(View view) {
??? showExitDialog01();
}
// 可輸入文本的提示框
private void showExitDialog01(){
??? final EditText edt = new EditText(this);
??? edt.setMinLines(3);
??? new AlertDialog.Builder(this)
??????????? .setTitle("請輸入")
??????????? .setIcon(android.R.drawable.ic_dialog_info)
??????????? .setView(edt)
??????????? .setPositiveButton("確定", new DialogInterface.OnClickListener() {
??????????????? public void onClick(DialogInterface arg0, int arg1) {
??????????????????? addressB.setText(edt.getText().toString());
??????????????? }
??????????? })
?????????? ?.setNegativeButton("取消", null)
??????????? .show();
}
//時間
public void time(View view) {
??? showExitDialog02();
}
// 單選提示框
private void showExitDialog02(){
??? new AlertDialog.Builder(this)
??????????? .setTitle("請選擇")
??????????? .setIcon(android.R.drawable.ic_dialog_info)
??????????? .setSingleChoiceItems(new String[]{"11:00~12:00","12:00~13:00","17:00~18:00","18:00~19:00","19:00~20:00","20:00~21:00"}, -1, new DialogInterface.OnClickListener(){
??????????????? public void onClick(DialogInterface arg0, int arg1) {
??????????????????? switch (arg1) {
??????????????????????? case 0:timeB.setText("11:00~12:00");break;
??????????????????????? case 1:timeB.setText("12:00~13:00");break;
??????????????????????? case 2:timeB.setText("17:00~18:00");break;
?????? ?????????????????case 3:timeB.setText("18:00~19:00");break;
??????????????????????? case 4:timeB.setText("19:00~20:00");break;
??????????????????????? case 5:timeB.setText("20:00~21:00");break;
??????????????????? }
??????????????????? arg0.dismiss();
??? ????????????}
??????????? })
??????????? .setNegativeButton("取消", null)
??????????? .show();
}
//付款
public void settlement(View view) {
??? onBackPressed();
}
public void onBackPressed(){
??? AlertDialog dialog;
??? ImageView img = new ImageView(this);
? ??img.setImageResource(R.drawable.img_55);
??? AlertDialog.Builder builder=new AlertDialog.Builder(this)
??????????? .setTitle("請掃碼付款")
???? ???????.setView(img);
??? dialog=builder.create();
??? dialog.show();
}
- 課程設(shè)計總結(jié)
本訂餐系統(tǒng)一共調(diào)用了3個數(shù)據(jù)庫,設(shè)計了9個界面。
其中考慮到商家和商品數(shù)據(jù)庫的添加必須提前加入,并且添加事件不能直接放在主類中多次執(zhí)行。設(shè)計觸發(fā)執(zhí)行的位置在注冊頁面,當用戶注冊添加到數(shù)據(jù)庫時,隨之商品和商家數(shù)據(jù)庫一起添加。
在用戶界面和商家界面上,使用數(shù)據(jù)庫的讀取方法,將數(shù)據(jù)庫的所有數(shù)據(jù)進行輸出為List<String>,然后存入對應(yīng)的listview的item中。其中,使用for循環(huán)獲取List<String>的長度。
在每一個界面間的數(shù)據(jù)關(guān)系,使用Bundle方法進行數(shù)值傳遞。例如用戶界面點擊跳轉(zhuǎn)到對應(yīng)的商家界面,利用Bundle傳遞商家名字,利用商家名字對商品數(shù)據(jù)庫進行查找對應(yīng)商品。
商家登入界面,對所輸入的名字進行數(shù)據(jù)庫查詢,若查詢不到,則進行數(shù)據(jù)添加。商家管理的三個界面是實現(xiàn)商品數(shù)據(jù)庫的增刪改查。
文章來源:http://www.zghlxwxcb.cn/news/detail-771735.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-771735.html
到了這里,關(guān)于Android期末設(shè)計——外賣點餐系統(tǒng)(設(shè)計介紹)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!