Android Studio 制作商品销售记录APP

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

预览项目逻辑结构:

Android Studio 制作商品销售记录APP_第1张图片

本次项目逻辑层有三个包: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:



    
    
        
        
    
    
        
        
    
    
        
        
    
    
        

前端效果如下图:

Android Studio 制作商品销售记录APP_第2张图片

第四步:获取输入框内容然后对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 布局,每一条消息的样式:



    
    
    
    


2、showgoods页面:




    
        
        
        
        
    

    

    
    



逻辑层代码:

要用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 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、添加商品:

Android Studio 制作商品销售记录APP_第3张图片

 

2、查看商品:

Android Studio 制作商品销售记录APP_第4张图片

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

3、再添加vipSp不写单价

Android Studio 制作商品销售记录APP_第5张图片

结果:

Android Studio 制作商品销售记录APP_第6张图片

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

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

 

你可能感兴趣的:(android,studio)