gpt4 book ai didi

java - 使用 Item 类的 ArrayList 的销售点应用程序。如何从列表中返回孤立/特定的值?

转载 作者:行者123 更新时间:2023-12-02 11:32:24 27 4
gpt4 key购买 nike

这里是 Java 菜鸟。我正在尝试构建一个简单的销售点结帐应用程序。我在 mysql 中有一个简单的项目数据库,如下图所示。

enter image description here

我有一个如下构建的 Item 类,其中每列都有 getter 和 setter

public class Item {
private String itemName;
private double unitPrice;
private boolean weightReqd;
private boolean quanReqd;
private boolean recalled;
private boolean ageRest;
private boolean eCpn;
private double eCpnAmt;

public Item(){}

public Item(String itemName, double unitPrice, boolean weightReqd, boolean quanReqd, boolean recalled,
boolean ageRest, boolean eCpn, double eCpnAmt){
this.itemName = itemName;
this.unitPrice = unitPrice;
this.weightReqd = weightReqd;
this.quanReqd = quanReqd;
this.recalled = recalled;
this.ageRest = ageRest;
this.eCpn = eCpn;
this.eCpnAmt = eCpnAmt;
}

public String getItemName() {
return itemName;
}

public void setItemName(String itemName) {
this.itemName = itemName;
}

public double getUnitPrice() {
return unitPrice;
}

public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}

public boolean isWeightReqd() {
return weightReqd;
}

public void setWeightReqd(boolean weightReqd) {
this.weightReqd = weightReqd;
}

public boolean isQuanReqd() {
return quanReqd;
}

public void setQuanReqd(boolean quanReqd) {
this.quanReqd = quanReqd;
}

public boolean isRecalled() {
return recalled;
}

public void setRecalled(boolean recalled) {
this.recalled = recalled;
}

public boolean isAgeRest() {
return ageRest;
}

public void setAgeRest(boolean ageRest) {
this.ageRest = ageRest;
}

public boolean iseCpn() {
return eCpn;
}

public void seteCpn(boolean eCpn) {
this.eCpn = eCpn;
}

public double geteCpnAmt() {
return eCpnAmt;
}

public void seteCpnAmt(double eCpnAmt) {
this.eCpnAmt = eCpnAmt;
}

}

然后我使用 Item 类中的 setter 将记录添加到项目列表(列表)中。

public class CreateItemList {

private static Connection conn = null;
public List<Item> itemList = new ArrayList<Item>();

public void createDbConnection() throws ClassNotFoundException, SQLException {
String myUrl = "jdbc:mysql://localhost:3306/itemdb?autoReconnect=true&useSSL=false";
conn = DriverManager.getConnection(myUrl, "******", "*********");
}

public void addItemsToList() throws SQLException, ClassNotFoundException {
String query = "SELECT * FROM item_source";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);

try
{
while (rs.next())
{
Item item = new Item();
item.setItemName(rs.getString("Item Name"));
item.setUnitPrice(rs.getDouble("Unit Price"));
item.setWeightReqd(rs.getBoolean("Weight Reqd"));
item.setQuanReqd(rs.getBoolean("Quan Reqd"));
item.setRecalled(rs.getBoolean("Recall"));
item.setAgeRest(rs.getBoolean("Age Rest"));
item.seteCpn(rs.getBoolean("Ecpn"));
item.seteCpnAmt(rs.getDouble("Ecpn Amt"));

itemList.add(item);
}
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e);
}
st.close();
}

}

现在我想做的下一件事是创建异常处理程序方法。因此,如果用户输入所需重量标记为“true”的项目,我希望 is WeightReqd 方法返回 true。但我正在为此苦苦挣扎。我不确定如何返回扫描项目的 boolean 值,特别是“重量要求”字段。

public class ExceptionsHandler {

public boolean isWeightReqd(List<Item> itemList){
//HOW DO I RETURN THE BOOLEAN VALUE FOR THE WEIGHT REQD FIELD FROM THE LIST??
return false;
}

非常感谢您的帮助!如果有更好的方法的话,还可以指导一下这个程序的总体结构!

最佳答案

您需要为扫描的特定项目调用 ExceptionHandler 类。您不需要在 isWeightReqd() 方法中传递整个列表。只传递您的元素。将您的方法签名更改为:public boolean isWeightReqd(Item itemList) 并且应该得到它。

关于java - 使用 Item 类的 ArrayList 的销售点应用程序。如何从列表中返回孤立/特定的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49199038/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com