一个简单的商品管理系统

 


 

 

该商品管理系统开发的目的在于学习。实现的功能都比较简单,只有根据界面的信息进行相应的操作就可以了,目前只做了添加的功能。
声明一个GoodsSystem类(Activity)用来显示界面的信息

 /*
  * 界面上的相关按钮
  */
 Button add;
 EditText id;
 EditText name;
 EditText number; 
 RadioGroup sort;
 RadioButton fuzhuang;
 RadioButton jiadian;
 RadioButton baihuo;
 LibraryDapter goods;//SQLite数据库
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        goods=new LibraryDapter(this);
        add=(Button)findViewById(R.id.button);
        id=(EditText)findViewById(R.id.id);
        name=(EditText)findViewById(R.id.name);
        number=(EditText)findViewById(R.id.number);
        /*对事件进行监听*/
        add.setOnClickListener(this);
        sort=(RadioGroup)findViewById(R.id.sort);
        fuzhuang=(RadioButton)findViewById(R.id.fuzhang);
        jiadian=(RadioButton)findViewById(R.id.frimily);
        baihuo=(RadioButton)findViewById(R.id.baihuo);
    }
 @Override
 public void onClick(View v) {
  String str="";
 /*如果触发事件,则执行if语句里面的相应信息*/
   if(v==add){
    if(fuzhuang.isChecked()){
     str="服装";
    }
    if(jiadian.isChecked()){
     str="家电";
    }
    if(baihuo.isChecked()){
     str="百货";
    }
    try {  
    /*得到商品编号、商品名称、商品类别、商品数量;然后添加都数据库里面,添加成功后返回一个提示信息*/
     goods.goods_ID = id.getText().toString();
     goods.goods_Name = name.getText().toString();
     goods.goods_Short = str;
     goods.goods_Number = Integer.parseInt(number.getText().toString());
     goods.insert();
     Toast.makeText(this, "添加成功!", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
     Toast.makeText(this, "请正确填写添加的信息!", Toast.LENGTH_LONG).show();
    }
   }
  
 }

出了要声明一Activity用来显示界面信息之外,还要创建一个类,用来保存商品信息。这里我是先声明了一个LibraryDapter类,然后在这个类里面再创建一个内部类,该类继承了

SQLiteOpenHelper,SQLiteOpenHelper和SQLiteDatabase是用来处理数据库创建、存储等信息的。首先要定义数据库的名称、表的名称、表的字段名称(商品编号、商品名称、商品类别、商品

数量),具体看下面的代码:
 private static class DBOpenHelper extends SQLiteOpenHelper {

  private static final String DB_Create = "create table " + goods_table
    + " (" + goodsID + " text not null," + goodsName
    + " text not null," + goodsSort + " text not null,"
    + goodsNumber + " integer);";

  public DBOpenHelper(Context context, String name,
    CursorFactory factory, int version) {
   super(context, name, factory, version);
  }

  @Override
  public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
   _db.execSQL("DROP TABLE IF EXISTS " + goods_table);
   onCreate(_db);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   db.execSQL(DB_Create);
  }
 }

这里是LibraryDapter里面的一些方法,主要是进行创建数据库,向数据库添加商品的方法。


 public LibraryDapter(Context _context) {
  context = _context;
  open();
 }
 /*创建数据库*/
 public void open() {
  dbOpenHelper = new DBOpenHelper(context, goods_name, null, goods_versiow);
  try {
   dbsql = dbOpenHelper.getWritableDatabase();
  } catch (SQLiteException e) {
   dbsql = dbOpenHelper.getReadableDatabase();
  }
 }
 /*添加商品*/
 public long insert() {
  ContentValues newValues = new ContentValues();
  newValues.put(goodsID, this.goods_ID);
  newValues.put(goodsName, this.goods_Name);
  newValues.put(goodsSort, this.goods_Short);
  newValues.put(goodsNumber, this.goods_Number);

  return dbsql.insert(goods_table, null, newValues);
 }

 public String toString() {
  String request = "";
  request = "商品编号:" + this.goods_ID + ",";
  request = "商品名称:" + this.goods_Name + ",";
  request = "商品类别:" + this.goods_Short + ",";
  request = "商品数量:" + this.goods_Number + ",";
  return request;
 }

 

布局这里用的是线性布局,但在实际开发中,一个好的布局是比较重要的都比较简单。所以废话我也不多说,直接看我是怎么样布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
 <TextView 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="商品编号:"
     android:textSize="18dip"/>
 <EditText android:id="@+id/id"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>
  </LinearLayout>
 <LinearLayout android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
  <TextView android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="商品名称:"
      android:textSize="18dip"/>
  <EditText android:id="@+id/name"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>
 </LinearLayout>
 <LinearLayout android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
  <TextView android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="商品类别:"
      android:textSize="18dip"/>
  <RadioGroup  android:orientation="horizontal"
   android:id="@+id/sort"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <RadioButton android:id="@+id/fuzhang"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="服装"
    android:textSize="18dip"/>
   <RadioButton android:id="@+id/frimily"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="家电"
    android:textSize="18dip"/>
   <RadioButton android:id="@+id/baihuo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="百货"
    android:textSize="18dip"/>
   </RadioGroup>
 </LinearLayout>
 <LinearLayout android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
  <TextView android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="商品数量:"
      android:textSize="18dip"/>
  <EditText android:id="@+id/number"
    android:phoneNumber="true"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>
 </LinearLayout>
 <Button android:id="@+id/button"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="添加商品"/>
</LinearLayout>

 

以上是我做的一个简单的android手机商品管理系统,目前值做了添加的功能,当然这里面有很多代码都还可以在优化,比如用来管理商品信息的数据库类可以整合到一个类,把内部类去掉。至于要看大家是怎么使用了。

 

 

 

 

 

 

一个简易商品管理系统通常包含商品信息的添加、删除、修改和查询等基本功能。下面是一个基于C语言的简易商品管理系统简单示例,包含商品结构体定义和简单的功能实现。 首先定义一个商品结构体,用于存储商品信息: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 商品信息结构体 typedef struct { int id; // 商品ID char name[50]; // 商品名称 float price; // 商品价格 int quantity; // 商品库存量 } Product; // 商品列表,用于存储多个商品信息 #define MAX_PRODUCTS 100 Product products[MAX_PRODUCTS]; int productCount = 0; // 当前商品数量 // 函数声明 void addProduct(int id, const char* name, float price, int quantity); void deleteProduct(int id); void updateProduct(int id, const char* name, float price, int quantity); Product* getProduct(int id); void listProducts(); ``` 以下是商品管理系统简单功能实现: ```c // 添加商品 void addProduct(int id, const char* name, float price, int quantity) { if (productCount < MAX_PRODUCTS) { products[productCount].id = id; strcpy(products[productCount].name, name); products[productCount].price = price; products[productCount].quantity = quantity; productCount++; } else { printf("商品数量已达上限,无法添加新商品。\n"); } } // 删除商品 void deleteProduct(int id) { int i; for (i = 0; i < productCount; i++) { if (products[i].id == id) { for (int j = i; j < productCount - 1; j++) { products[j] = products[j + 1]; } productCount--; return; } } printf("未找到ID为%d的商品。\n", id); } // 更新商品信息 void updateProduct(int id, const char* name, float price, int quantity) { Product* p = getProduct(id); if (p != NULL) { strcpy(p->name, name); p->price = price; p->quantity = quantity; } else { printf("未找到ID为%d的商品。\n", id); } } // 获取商品信息 Product* getProduct(int id) { for (int i = 0; i < productCount; i++) { if (products[i].id == id) { return &products[i]; } } return NULL; } // 列出所有商品信息 void listProducts() { for (int i = 0; i < productCount; i++) { printf("ID: %d, 名称: %s, 价格: %.2f, 数量: %d\n", products[i].id, products[i].name, products[i].price, products[i].quantity); } } ``` 在这个示例,我们定义了商品信息结构体`Product`,一个能够存储最多100个商品的数组`products`,以及一个记录当前商品数量的变量`productCount`。我们提供了添加、删除、更新和列出商品信息的基本函数。`getProduct`函数用于通过商品ID获取商品指针,如果不存在则返回`NULL`。 请注意,上述代码是一个框架性质的示例,它没有包含完整的用户界面和数据持久化部分,实际使用时需要根据具体需求进行完善和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值