在线订购系统:J2ME应用的实现与解析
发布时间: 2025-08-17 01:03:20 阅读量: 1 订阅数: 3 

### 在线订购系统:J2ME 应用的实现与解析
在当今数字化的时代,电子商务日益普及,而移动商务(m - commerce)正逐渐崭露头角,随时随地处理业务成为了新的趋势。为了顺应这一潮流,我们将介绍一个基于 J2ME 平台的在线订购系统——OrderManager,它允许用户通过手机进行商品订购。
#### 1. 项目概述
OrderManager 是一个利用 J2ME 平台的 Mobile Information Device Profile (MIDP) 开发的应用程序,结合了 JSP 和第三方 XML 解析器。该应用可分为服务器端和客户端两部分,由于 J2ME 类文件位于手机上,我们主要关注客户端部分。
#### 2. 用户界面
项目的主类名为 OrderManager,它包含了 GUI 组件,用于显示商品列表和库存信息。用户可以查看商品列表,选择要订购的商品,输入订购数量,应用会计算订单的总金额和成本,并通过调用 JSP 脚本更新数据库。
OrderManager 类中有一个内部类 GetData,它负责打开 URL 并实时读取 XML 数据。该类调用一个 JSP 脚本,查询数据库并生成关于商品、可用数量和成本的 XML 数据,然后使用基于 Java 的 XML 解析器解析这些数据。
用户启动应用后,会看到一个显示商品及其可用数量的屏幕。用户可以通过按 Enter 键选择商品,此时会出现一个文本框,用于输入订购数量。用户可以按 Save 按钮保存数量,或按 Back 按钮返回商品列表。用户可以订购任意数量的商品,甚至可以更改之前选择商品的数量。最后,用户可以按 Exit 按钮退出应用。
#### 3. 运行应用
要运行这个应用,需要安装 J2ME Wireless Toolkit。虽然可以不使用该工具包运行应用,但使用它进行编译、预验证和运行会更加简单,因为它有一个易于操作的 GUI,无需手动处理类路径。运行应用的步骤如下:
1. 从开始菜单启动 J2ME Wireless Toolkit 的 KToolbar。
2. 点击 New Project 开始一个新项目。
3. 输入项目名称,例如 OrderManager。
4. 输入主 MIDlet 类的名称,同样为 OrderManager。
5. 点击 Create Project。
6. 将项目的源代码复制到前面的目录中。
7. 返回 KToolbar 并点击 Build 按钮。
8. 项目成功编译和预验证后,点击 Run 按钮。
在执行上述步骤时,需要确保有一个服务器正在运行,并且 JSP 文件应存储在服务器的根目录中。
#### 4. 项目文件
项目包含以下文件:
- **OrderManager.java**:主文件,包含内部类 GetData。
- **sql_xml.jsp**:JSP 脚本,用于从数据库返回数据。
- **sql_order_mon.jsp**:JSP 脚本,根据用户的订单更新数据库。
- **第三方 XML 解析器**。
#### 5. 数据库表结构
服务器上的数据库包含三个表:
| 表名 | 字段 | 说明 |
| ---- | ---- | ---- |
| stock_mast | item_code | 商品代码 |
| | quantity | 商品可用数量 |
| | unit_price | 商品单价 |
| order | order_id | 订单 ID |
| | order_date | 订单日期 |
| | item_code | 订购商品代码 |
| | quantity | 订购数量 |
| | rate | 订单总金额 |
| status | order_id | 订单 ID |
#### 6. 控制流程
应用的控制流程如下:
```mermaid
graph TD;
A[启动应用] --> B[调用 GetData 类];
B --> C[调用 sql_xml.jsp 脚本];
C --> D[读取数据库并返回 XML 数据];
D --> E[解析 XML 数据并存储在向量中];
E --> F[显示商品列表];
F --> G{用户操作};
G -- 选择商品 --> H[输入订购数量];
H -- 保存数量 --> I[更新哈希表];
I --> F;
G -- 按 Total 按钮 --> J[计算总金额并显示];
J -- 按 OK 按钮 --> K[调用 sql_order_mon.jsp 脚本更新数据库];
K --> L[销毁应用];
G -- 按 Exit 按钮 --> L;
```
#### 7. 代码解析
##### 7.1 OrderManager.java
```java
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.kxml.*;
import org.kxml.parser.XmlParser;
import org.kxml.parser.*;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
import java.lang.String;
/**
* This class Displays the User Interface for this Project. It displays
* the list of items available with the stock of every item. Accepts the Quantity
* to be Ordered and Calculates the total Amount of Order based on the Quantity
* Ordered.And updates the Database by calling a JSP passing ITEMCODE, QUNATITY ORDERED
* TOTAL AMOUNT as parameters.
*/
public class OrderManager extends MIDlet implements CommandListener {
/* This vector will contain Item Code */
private Vector vitem = null;
/* This Vector will contain Item Quantity */
private Vector vquantity = null;
/* This Vector will contain Item Price */
private Vector vrate = null;
/* This Class GetData is an internal class and returns Item Code with Quantity and Rate */
private GetData gdata = null;
/* Declaring variables for Display class. */
private Display displaylist = null;
/* Declaration of List which will Display the Items with Quantity */
private List items = null;
/* Declaration of Form */
private Form entryform = null;
private Form displayform = null;
private String itemselect = null;
/* This Command is a backcommmand */
private Command backcommand = null;
/* This command is to view the total */
private Command totalcommand = null;
/* This command is to place an order */
private Command okcommand = null;
/* This command again displays the list of items with the quantity */
private Command goback = null;
/* This command quits the application */
private Command exit = null;
/* This TextFeild is to get the quantity to be ordered */
private TextField enter = null;
/* This Hashtable contains entries of itemcode and the quantity ordered. The key is the itemcode. */
private Hashtable htable = null;
/* This HttpConnection object connects to the web server and updates the database by placing an order */
private HttpConnection connection = null;
/* Constructor Called */
public OrderManager() {
/* This will get the Display Object */
displaylist = Display.getDisplay(this);
/* Declarations */
items = new List("Items", List.IMPLICIT);
items.setCommandListener(this);
htable = new Hashtable();
backcommand = new Command("Back", Command.BACK, 1);
goback = new Command("Back", Command.BACK, 1);
totalcommand = new Command("Total", Command.SCREEN, 1);
okcommand = new Command("OK", Command.SCREEN, 1);
exit = new Command("Exit", Command.EXIT, 1);
}
/* This method is called when the application starts */
public void startApp() {
/* Gdata Instantiated */
gdata = new GetData();
/* GetData parseData method called. This method parses XML */
gdata.parseData();
/* GetData returnItem method returns vector object containing Item list */
vitem = gdata.returnItem();
/* This line trims white spaces from the vector vitem */
vitem.trimToSize();
/* GetData returnStock method returns Vector object containing Stock list(availabel item quantity */
vquantity = gdata.returnStock();
/* Trims empty spaces from Vector */
vquantity.trimToSize();
/* GetData returnPrice method returns Vector object containing Price list */
vrate = gdata.returnPrice();
/* Trims empty spaces from Vector */
vrate.trimToSize();
/* For loop starts
This for loop runs until there are values in Vector vitem(vitem.size() give the size of the vector) */
for (int i = 1; i <= vitem.size(); i++) {
/* In this line List append method is called and the itemcode and the quantity available are added to the list */
items.append((String) vitem.elementAt(i - 1) + " " + (String) vquantity.elementAt(i - 1), null);
}
/* this line will display the list containing the items */
displaylist.setCurrent(items);
/* List is registered for totalcommand and commands other than Select command have to be registered with the List class object by calling its addCommand method */
items.addCommand(totalcommand);
/* List is registred for exit command */
items.addCommand(exit);
/* entryform instantiated. A TextFeild will be added to this form(Form behaves like containers in the JAVA Standard Edition */
entryform = new Form("Entry Form");
/* entryform is registered with backcommand */
entryform.addCommand(backcommand);
/* entry form registered for receiving command */
entryform.setCommandListener(this);
/* displayform is instantaited. This will display the total amount of order with item code */
displayform = new Form("View Total");
/* displayform registered for receiving command */
displayform.setCommandListener(this);
/* displayform is registered with goback command */
displayform.addCommand(goback);
/* displayform is registered with okcommand */
displayform.addCommand(okcommand);
}
/**
* This method is called whenever a user presses a button or a command is given on any
* of the Items. The two parameters are Command object and the displayable object
*/
public void commandAction(Command c, Displayable d) {
int totalbill = 0;
int a = 0;
/* The code in this if condition is executed when an item is selected in the list) */
if (c == List.SELECT_COMMAND) {
/* This line of code gets the String value of the List item selected by calling the getString(). List getSelectedIndex() returns the index of the item selected which is passed as an argument to the getString(). */
String str = items.getString(items.getSelectedIndex());
int i = str.indexOf(" ");
/* This line will get only the item value from str(str contain
```
0
0
相关推荐









