Android期末设计——外卖点餐系统(设计介绍)

一、课程设计目的

     1.android开发的综合训练

     2.提高软件开发的综合能力

二、开发环境

         Android Studio

三、课程设计内容

网上订餐项目是一个类似外卖的项目,其中包含订餐的店铺、各店铺的菜单、购物车以及订单与付款等模块。在店铺列表中可以看到店铺的名称、月销售、起送价格与配送费用、配送时间以及福利等信息,点击店铺列表中的任意一个店铺,进入到店铺详情界面,该界面主要显示店铺中的菜单,同时可以将想要吃的菜添加到购物车中,选完菜之后可以点击该界面中的“去结算”按钮,进入到订单界面,在该界面核对已点的菜单信息,并通过“去支付”按钮进行付款。

参考截图(注:采用单机版开发)

Android设计获取:https://download.csdn.net/download/qq_61562251/87861222?spm=1001.2014.3001.5501

 

四、课程设计过程

各功能模块设计,关键代码,运行截图

用户登入验证

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点击监听滑动监听与数值传递、适配器
//item界面设置

MyAdapter myAdapter = new MyAdapter();

merchantList.setAdapter(myAdapter);



//listView点击监听,滑动监听

merchantList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
    ///List

    @Override

    //点击监听

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

    }



    // 状态改变时调用

    public void onScrollStateChanged(AbsListView absListView, int scrollState) {

        switch (scrollState) {

            case AbsListView.OnScrollListener.SCROLL_STATE_FLING:

                Log.i("logi", "进入惯性滑动状态");

                break;

            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:

                Log.i("logi", "进入正在滑动状态");

                break;

            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:

                Log.i("logi", "滑动停止状态");

                break;

        }

    }

});


//item界面设置

class MyAdapter extends BaseAdapter {



    //获取 item 条目总数

    @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 数据对象

    @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 布局文件------将布局文件转换为一个控件

        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;

    }

}
 
 
数据库获取
//全部商家获取

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;

}
 
 



//实现商店点击事件跳转

Bundle b = getIntent().getExtras();//获取上一个界面传递的数值

if( b != null) {

    name=b.getString("name");//获取具体数值

    merchantName.setText(name.toCharArray(), 0, name.length());

    position = b.getInt("position");

    Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();

    //对上一个界面传递的position值进行switch方法执行,获取商家头像

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

    }

}
 
 


//数据库指定内容输出

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内部键数据输入

viewHolder.src.setBackgroundResource(srcS.get(i));

viewHolder.good.setText(" " + titles.get(i));

viewHolder.price.setText(" " + priceS.get(i));

viewHolder.detail.setText(" " + goodDetailS.get(i));
 
 


//商品添加点击监听

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]++;

    }

});



//结算按钮点击事件

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

    }

});

用户注册并触发另外两个数据库的存储

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)){
    
    //执行用户添加后,同时对商家和商品进行添加

                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状态到SaveTable,默认模式

                    //默认模式:代表文件是私有数据,只能被应用本身访问,写入的内容会覆盖源文件的内容

                    SharedPreferences sharedP=getSharedPreferences("SaveTable",MODE_PRIVATE);

                    SharedPreferences.Editor editor=sharedP.edit();//API超连接,类似于map

                    int num=sharedP.getInt("number", 0);//获取储存的值

                    num++;

                    editor.putInt("number", num);//上传数值

                    editor.commit();//数据提交

                    finish();//结束数据上传

                }

            }else{

                Toast.makeText(getApplicationContext(), "添加失败", Toast.LENGTH_SHORT).show();

            }

//            Toast.makeText(getApplicationContext(), "添加失败", Toast.LENGTH_SHORT).show();

        }



//item界面设置

MyAdapter myAdapter = new MyAdapter();

menu.setAdapter(myAdapter);



//传递数据获取,商品金额计算

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界面设置

MyAdapter myAdapter = new MyAdapter();

menu.setAdapter(myAdapter);



//传递数据获取,商品金额计算

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

}

  • 课程设计总结

本订餐系统一共调用了3个数据库,设计了9个界面。

其中考虑到商家和商品数据库的添加必须提前加入,并且添加事件不能直接放在主类中多次执行。设计触发执行的位置在注册页面,当用户注册添加到数据库时,随之商品和商家数据库一起添加。

在用户界面和商家界面上,使用数据库的读取方法,将数据库的所有数据进行输出为List<String>,然后存入对应的listview的item中。其中,使用for循环获取List<String>的长度。

在每一个界面间的数据关系,使用Bundle方法进行数值传递。例如用户界面点击跳转到对应的商家界面,利用Bundle传递商家名字,利用商家名字对商品数据库进行查找对应商品。

商家登入界面,对所输入的名字进行数据库查询,若查询不到,则进行数据添加。商家管理的三个界面是实现商品数据库的增删改查。

猜你喜欢

转载自blog.csdn.net/qq_61562251/article/details/131032783