Android Studio 制作商品销售记录APP

制作这个APP之前首先要考虑的不是去怎么做,而是去想需求、从需求出发去建数据库、建表。话不多说开始动工吧!!!

预览项目逻辑结构:

本次项目逻辑层有三个包:bean、DBHelper、utils。前端界面部分:3个xml文件。

第一步:建立javabean。还是那句老话,从需求分析:我们这次的对象是商品,商品的属性有:商品id、商品名字、商品数量、商品价格(此处是单价)。既然是商品销售记录,那一定有总价了,数了数一共有5个属性,javabean里对应的也就有五个属性。

这五个属性都给他加上get和set方法,结果下:

package com.example.liaof.goodssale.bean;

/**
 * Created by liaof on 2018/7/24.
 */

public class goods {
    private Integer id;
    private String goodsname;
    private String goodscount;
    private String goodsprice;
    private String price;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getGoodsname() {
        return goodsname;
    }

    public void setGoodsname(String goodsname) {
        this.goodsname = goodsname;
    }

    public String getGoodscount() {
        return goodscount;
    }

    public void setGoodscount(String goodscount) {
        this.goodscount = goodscount;
    }

    public String getGoodsprice() {
        return goodsprice;
    }

    public void setGoodsprice(String goodsprice) {
        this.goodsprice = goodsprice;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public goods(String goodsname, String goodscount, String goodsprice,String price) {
        this.goodsname = goodsname;
        this.goodscount = goodscount;
        this.goodsprice = goodsprice;
        this.price = price;
    }
}

第二步:创建数据库SQLite(一款轻量级数据库,非常好用,不了解的可以网上百度学习一下SQLite的使用),然后创建数据表,表里的字段和我们的javabean里的五个属性相对应,具体代码如下:

package com.example.liaof.goodssale.DBHelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by liaof on 2018/7/24.
 */

public class DBHelper extends SQLiteOpenHelper {
    public DBHelper(Context context){
        super(context,"Goods.db",null,2);

    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table tb_goods(id integer primary key autoincrement,goodsname varchar(50),goodscount varchar(10),goodsprice varchar(10),price varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

第三步:写前端页面,页面很简单,就只有一个标题、三个商品信息输入框(分别是:商品名字、商品数量、商品单价)、外加两个按钮(确认添加、销售情况)代码如下index.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#E6E6FA">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加销售记录"
        android:textSize="28sp"
        android:textStyle="bold"
        android:layout_marginTop="80dp"
        android:layout_gravity="center_horizontal"/>
    <LinearLayout
        android:id="@+id/line1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="商品名字:"
            android:textSize="24sp"/>
        <EditText
            android:id="@+id/edt_goodsname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:background="@android:color/white"
            android:paddingLeft="2dp"
            android:textSize="24sp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/line2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="商品数量(单位/个):"
            android:textSize="24sp"/>
        <EditText
            android:id="@+id/edt_goodscount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:background="@android:color/white"
            android:paddingLeft="2dp"
            android:textSize="24sp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/line3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="商品价格(单位/元):"
            android:textSize="24sp"/>
        <EditText
            android:id="@+id/edt_goodsprice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:background="@android:color/white"
            android:paddingLeft="2dp"
            android:textSize="24sp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/line4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">
        <Button
            android:id="@+id/add_btn"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="确定添加"
            android:textSize="18sp"/>
        <Button
            android:id="@+id/To_goods"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="销售情况"
            android:textSize="18sp"/>
    </LinearLayout>
</LinearLayout>

前端效果如下图:

第四步:获取输入框内容然后对SQLite数据库进行增加数据操作。此处逻辑层要考虑:该商品是否是新入库商品,如果不是,则要查询该商品添加前的总价,并获取过去的单价,此处则不用再输入单价了。代码如下:

package com.example.liaof.goodssale.utils;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.liaof.goodssale.DBHelper.DBHelper;
import com.example.liaof.goodssale.R;

public class index extends AppCompatActivity {
    private EditText edt_goodsname;
    private EditText edt_goodscount;
    private EditText edt_goodsprice;
    private Button btn_add;
    private Button To_goods;
    private String price;
    private DBHelper dbHelper;
    private String goodsname;
    private String goodscount;
    private String goodsprice;
    SQLiteDatabase db;
    SQLiteDatabase redDb;
    String oldcount;
    ContentValues contentValues=new ContentValues();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index);
        init();
        dbHelper=new DBHelper(this);
        db=dbHelper.getWritableDatabase();
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsname = edt_goodsname.getText().toString();
                goodscount = edt_goodscount.getText().toString();
                goodsprice = edt_goodsprice.getText().toString();
                if (wasgoods(goodsname)) {
                    if(goodscount.equals("")||goodsname.equals("")||goodsprice.equals("")){
                        try{
                            Toast.makeText(index.this,"输入不能为空",Toast.LENGTH_SHORT).show();
                            price = String.valueOf((Float.parseFloat(goodsprice)) * (Integer.parseInt(goodscount)));
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                        return;
                    }
                    price = String.valueOf((Float.parseFloat(goodsprice)) * (Integer.parseInt(goodscount)));
                    System.out.println("总价1:" + price);
                    Toast.makeText(index.this, "添加成功!", Toast.LENGTH_SHORT).show();
                    contentValues.put("goodsname", goodsname);
                    contentValues.put("goodscount", goodscount);
                    contentValues.put("goodsprice", goodsprice);
                    contentValues.put("price", price);
                    db.insert("tb_goods", null, contentValues);

                }else{
                    if(goodscount.equals("")||goodsname.equals("")){
                        try{
                            Toast.makeText(index.this,"输入不能为空",Toast.LENGTH_SHORT).show();
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                        return;
                    }
                    String sql="select * from tb_goods where goodsname=?";
                    Cursor cursor = db.rawQuery(sql, new String[] {goodsname});
                    while(cursor.moveToNext()){
                        oldcount=cursor.getString(cursor.getColumnIndex("goodscount"));
                        goodsprice=cursor.getString(cursor.getColumnIndex("goodsprice"));
                        edt_goodsprice.setText(goodsprice);
                        goodscount=String.valueOf((Integer.parseInt(goodscount))+(Integer.parseInt(oldcount)));
                        price = String.valueOf((Float.parseFloat(goodsprice)) * (Integer.parseInt(goodscount)));
                        System.out.println("总价:" + price);
                        Toast.makeText(index.this, "添加成功!", Toast.LENGTH_SHORT).show();
                        contentValues.put("goodscount",goodscount);
                        contentValues.put("price",price);
                        db.update("tb_goods",contentValues,"goodsname=?",new String[]{goodsname});

                    }

                }

            }
        });
        To_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(index.this,showgoods.class);
                startActivity(intent);
                finish();
            }
        });

    }
    public void init(){
        edt_goodsname=(EditText)findViewById(R.id.edt_goodsname);
        edt_goodscount=(EditText)findViewById(R.id.edt_goodscount);
        edt_goodsprice=(EditText)findViewById(R.id.edt_goodsprice);
        btn_add=(Button)findViewById(R.id.add_btn);
        To_goods=(Button)findViewById(R.id.To_goods);
    }
    public boolean wasgoods(String name){

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql="select * from tb_goods where goodsname=?";
        Cursor cursor = db.rawQuery(sql, new String[] {name});
        System.out.println(cursor.getCount());
        if(cursor.getCount()!=0){
            System.out.println("调用了1");
            cursor.close();
            return false;
        }
        System.out.print("调用了2");
        return true;
    }
    public boolean onKeyDown(int keyCode, KeyEvent event)

    {

        if (keyCode == android.view.KeyEvent.KEYCODE_BACK ){
           finish();
        }
        return  false;
    }
}

第四步:点击销售情况时跳转到商品销售详情显示页面,采用了listview去渲染,因此在showgoods类里要写一个适配器方法。先看前端代码:

1、item 布局,每一条消息的样式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">
    <TextView
        android:id="@+id/tv_goodsname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="名字"
        android:gravity="center"
        android:textSize="18sp"
    />
    <TextView
        android:id="@+id/tv_goodscount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="1"
        android:textSize="18sp"
        />
    <TextView
        android:id="@+id/tv_goodsprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="120"
        android:gravity="center"
        android:textSize="18sp"
        />
    <TextView
        android:id="@+id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="120"
        android:gravity="center"
        android:textSize="18sp"
        />

</LinearLayout>

2、showgoods页面:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.liaof.goodssale.utils.showgoods">
<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="销售情况"
    android:textSize="28sp"
    android:textStyle="bold"
    android:layout_marginTop="5dp"
    android:gravity="center"
    />
    <LinearLayout
        android:layout_below="@id/tv_title"
        android:id="@+id/line1"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/goodsname"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:textColor="@android:color/black"
            android:layout_height="wrap_content"
            android:text="商品名称"/>
        <TextView
            android:id="@+id/goodscount"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:textColor="@android:color/black"
            android:layout_height="wrap_content"
            android:text="商品数量(单位/个)"/>
        <TextView
            android:id="@+id/goodsprice"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@android:color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="单价(单位/元)"/>
        <TextView
            android:id="@+id/sprice"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@android:color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="销售额(单位/元)"/>
    </LinearLayout>

    <ListView
        android:layout_below="@id/line1"
        android:id="@+id/list_goods"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_alignParentBottom="true"
    android:background="#FFFF00"
    android:padding="5dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="总计:"
        android:layout_weight="1"
        android:textColor="@android:color/black"
        android:textSize="22sp"
        />
    <TextView
        android:id="@+id/tv_all_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_weight="1"
        android:gravity="right"
        android:textSize="22sp"
        />
</LinearLayout>

</RelativeLayout>

逻辑层代码:

要用sql语句将所有的商品总价加起来作为总价显示出来,具体代码如下

package com.example.liaof.goodssale.utils;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.liaof.goodssale.DBHelper.DBHelper;
import com.example.liaof.goodssale.R;
import com.example.liaof.goodssale.bean.goods;

import java.util.ArrayList;

public class showgoods extends AppCompatActivity {
    private ListView listView;
    private DBHelper dbHelper;
    private ArrayList<goods> goodsList;
    private TextView tv_all_price;
    private double all_money;
    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showgoods);
        listView=(ListView)findViewById(R.id.list_goods);
        tv_all_price=(TextView)findViewById(R.id.tv_all_price);
        dbHelper=new DBHelper(this);
        db=dbHelper.getReadableDatabase();
        goodsList=new ArrayList<>();
        String sql="select * from tb_goods";
        Cursor cursor=db.rawQuery(sql,null);
        while (cursor.moveToNext()){
            String goodsname=cursor.getString(cursor.getColumnIndex("goodsname"));
            String goodscount=cursor.getString(cursor.getColumnIndex("goodscount"));
            String goodsprice=cursor.getString(cursor.getColumnIndex("goodsprice"));
            String price=cursor.getString(cursor.getColumnIndex("price"));
            all_money=Double.parseDouble(price)+all_money;
            goods good=new goods(goodsname,goodscount,goodsprice,price);
            goodsList.add(good);
        }
        tv_all_price.setText(String.valueOf(all_money)+"元");
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return goodsList.size();
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int i, View convertView, ViewGroup parent) {
                View view;
                if (convertView==null){
                    view = View.inflate(getBaseContext(),R.layout.list_goods_item,null);
                }else{
                    view = convertView;
                }
                goods good=goodsList.get(i);
                TextView goodsname=(TextView)view.findViewById(R.id.tv_goodsname);
                TextView goodscount=(TextView)view.findViewById(R.id.tv_goodscount);
                TextView goodsprice=(TextView)view.findViewById(R.id.tv_goodsprice);
                TextView price=(TextView)view.findViewById(R.id.tv_price);
                goodsname.setText(good.getGoodsname());
                goodscount.setText(good.getGoodscount());
                goodsprice.setText(good.getGoodsprice());
                price.setText(good.getPrice());
                return view;
            }
        });
    }
    public boolean onKeyDown(int keyCode, KeyEvent event)

    {

        if (keyCode == android.view.KeyEvent.KEYCODE_BACK ){
            Intent intent=new Intent(showgoods.this,index.class);
            startActivity(intent);
            finish();
        }
        return  false;
        }

}

看看效果吧:

1、添加商品:

 

2、查看商品:

看到上图的结果正是我们需要的结果,这就说明已满足需求

3、再添加vipSp不写单价

结果:

vipSp也变化了,这个程序也算是做好了!!!

源码地址在我的下载那里,找‘商品销售情况’

 

  • 7
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码是一生的追求

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值