
要使用Java编写超市购物程序,您需要使用Java数据库编程技术来连接到您的数据库并帆早执行相关的数据库 *** 作。在Java中,您含轿滑可以使用JDBC(Java数据库连接)API来连接到数据库并执行SQL语句。例如,您可以使用JDBC API来执行以下 *** 作:
创建数据库连接
执行增删改查(CRUD) *** 作
提交或回滚事务
执行存储过程和函数
查询数据库元数据
此外,您还可以使用Java的面向对象编程技术来封装数据库 *** 作,以便更方便地在您的程序中使用。例如,您可以创建一个类来表示超市商品,并定义一些方法来执行商品的增删改查 *** 作。这样,您就可以在程序中通过调用这些方法谈腊来简单地完成对数据库的 *** 作,而不需要编写复杂的SQL语句。
实体类代码:using System
using System.Collections.Generic
using System.Text
namespace ProsceniumSystem
{
/// <summary>
/// 图书Bean
/// </summary>
public class BookBean
{
private int _bookNum
private int _bookID
private string _bookName
private double _bookPrice
private double _total
public BookBean()
{
}
#region
/// <summary>
/// 获取或设置图书名称
/// </summary>
public string BookName
{
set
{
this._bookName = value
}
get
{
return this._bookName
}
}
/// <summary>
/// 获取或设置图书数量
/// <搜悉/summary>
public int BookNum
{
set
{
if (value >0)
{
this._bookNum = value
}
else
{
this._bookNum = 0
}
}
get
{
return this._bookNum
}
}
/// <summary>
//世败乎/ 获取或设置图书ID
/// </summary>
public int BookID
{
set
{
if (value >0)
{
this._bookID = value
}
else
{
this._bookID = 0
}
}
get
{
return this._bookID
}
}
/// <summary>
/// 获得或设置枯散图书价格
/// </summary>
public double BookPrice
{
set
{
if (value >0)
{
this._bookPrice = value
}
else
{
this._bookPrice = -1
}
}
get
{
return this._bookPrice
}
}
/// <summary>
/// 获得图书总价
/// </summary>
public double Total
{
get
{
this._total = this._bookPrice * this._bookNum
return this._total
}
}
#endregion
}
}
购物车实现代码:
using System
using System.Collections
using System.Text
using System.Data
namespace ProsceniumSystem
{
/// <summary>
/// 购物车类
/// </summary>
public class ShoppingCar
{
private double _total = 0
private int _count
private BookBean _books
private Hashtable ht
private ArrayList al
/// <summary>
/// 初始化指定大小的购物车
/// </summary>
/// <param name="count">维数</param>
public ShoppingCar(int count)
{
this._count = count
al = new ArrayList(_count)
ht = new Hashtable(_count)
}
/// <summary>
/// 默认构造方法
/// </summary>
public ShoppingCar()
{
al = new ArrayList()
ht = new Hashtable()
}
/// <summary>
/// 添加书籍
/// </summary>
/// <param name="books">图书Bean</param>
public void AddBook(BookBean books)
{
this._books = books
if (ht.ContainsKey(this._books.BookID))
{
_books = (BookBean)ht[_books.BookID]
_books.BookNum = _books.BookNum + books.BookNum
ht[_books.BookID] = _books
}
else
{
this.ht.Add(this._books.BookID, _books)
}
}
/// <summary>
/// 删除图书
/// </summary>
/// <param name="bookid">图书ID</param>
public void RemoveBook(int bookid)
{
ht.Remove(bookid)
}
/// <summary>
/// 删除图书
/// </summary>
/// <param name="bookid">图书ID</param>
/// <param name="number">删除数量</param>
public void RemoveBook(int bookid,int number)
{
foreach (DictionaryEntry de in ht)
{
if (de.Key.ToString().Equals(bookid.ToString()))
{
_books = (BookBean)ht[bookid]
_books.BookNum = _books.BookNum - number
ht[bookid] = _books
}
}
}
/// <summary>
/// 清空购物车
/// </summary>
public void ClearShoppingCar()
{
if (ht.Count >0)
{
ht.Clear()
}
}
/// <summary>
/// 查看购物车商品集合
/// </summary>
/// <returns></returns>
public ArrayList GetAllCarMerchandise()
{
foreach (DictionaryEntry de in ht)
{
al.Add(de.Value)
}
return al
}
/// <summary>
/// 计算总价
/// </summary>
/// <returns></returns>
public double GetTotal()
{
foreach (DictionaryEntry de in ht)
{
this._books = (BookBean)de.Value
this._total += _books.Total
}
return this._total
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)