📄 inventory.java
字号:
import java.sql.*;
import javax.swing.JOptionPane;
public class Inventory extends SortedList {
//存货清单构造函数
public Inventory() {
super(100, new ItemComparer());
String str1,str2;
double p;
int u;
String sqlstr;
sqlstr="select * from 库存";
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:库存");
Statement stmt=con.createStatement();
ResultSet result=stmt.executeQuery(sqlstr);
while(result.next())
{
str1=result.getString(1);
str2=result.getString(2);
p=result.getDouble(3);
u=result.getInt(4);
add(new InventoryItem(str1,str2,p,u));
}
}
catch(ClassNotFoundException e)
{System.err.println(e.getMessage());
}
catch(SQLException e)
{System.err.println(e.getMessage());
}
}
//根据用户输入的id,返回相应的存货条目,不存在的话,返回null
public InventoryItem getPart(String _id) {
int index = indexOf(new Item(_id));
if (index >= 0)
return (InventoryItem) get(index);
else
return null;
}
//在存货清单中,根据用户输入的id和用户输入的数量,增加相应货物的数量
public void addUnits(String _id, int _number) {
InventoryItem item = getPart(_id);
if (item != null)
{ int n=_number;
String sqlstr="update 库存 set units=units+'"+n+"' where ID="+"'"+_id+"'";
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:库存");
Statement stmt=con.createStatement();
// ResultSet result=stmt.executeQuery(sqlstr);
}
catch(ClassNotFoundException e)
{System.err.println(e.getMessage());
}
catch(SQLException e)
{System.err.println(e.getMessage());
}
item.add(_number);}
else
JOptionPane.showMessageDialog(null, "没有找到和输入ID对应的项!", "警告",
JOptionPane.ERROR_MESSAGE);
}
//在存货清单中,根据用户输入的id和用户输入的数量,减少相应货物的数量
public void removeUnits(String _id, int _number) {
InventoryItem item = getPart(_id);
if ((item != null) && (item.units >= _number) && (_number >= 0))
{
int n=_number;
String sqlstr;
sqlstr="update 库存 set units=units-'"+n+"' where ID="+"'"+_id+"'";
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:库存");
Statement stmt=con.createStatement();
ResultSet result=stmt.executeQuery(sqlstr);
}
catch(ClassNotFoundException e)
{System.err.println(e.getMessage());
}
catch(SQLException e)
{System.err.println(e.getMessage());
}
item.remove(_number);
}
else
JOptionPane.showMessageDialog(null, "没有找到和输入ID对应的项或空间不足!", "警告",
JOptionPane.ERROR_MESSAGE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -