You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
java/CartTableModel.java

77 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package flowershop.view;
import javax.swing.table.AbstractTableModel;
//购物车表格模型
public class CartTableModel extends AbstractTableModel {
// 表格列名columnNames
private String[] columnNames = { "编号", "花卉名", "单支价格", "数量", "应付金额" };
// 表格中数据保存在data二维数组中
private Object[][] data = null;
public CartTableModel(Object[][] data) {
this.data = data;
}
// 返回列数
@Override
public int getColumnCount() {
return columnNames.length;
}
// 返回行数
@Override
public int getRowCount() {
return data.length;
}
// 获得某行某列的数据而数据保存在对象数组data中
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
@Override
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// 数量列可以修改
if (columnIndex == 3) {
return true;
}
return false;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// 只允许修改数量列
if (columnIndex != 3) {
return;
}
try {
// 从表中获得修改之后的商品数量从表而来的数据都String类型
int quantity = new Integer((String) aValue);
// 商品数量不能小于0
if (quantity < 0) {
return;
}
// 更新数量列
data[rowIndex][3] = quantity;
// 计算商品应付金额
double price = (double) data[rowIndex][2];
double totalPrice = price * quantity;
// 更新商品应付金额列
data[rowIndex][4] = new Double(totalPrice);
} catch (Exception e) {
}
}
}