我正在编写一个可用于输入交通票的小程序(教育目的)主页上有三个简单选项新票,它接收所有信息以创建新的票对象并添加到列表中。截至目前,该部分工作正常。
我遇到的问题是我的支付票选项,用户输入票号和他们希望在票上支付的金额,点击支付票按钮后,程序应编辑精细实例变量。付款金额。
我尝试过编写几种方法并调用它们,使用各种不同的for循环来比较和执行此函数。
这里有一些我认为相关的代码。
家庭班级(主要班级)
//created the empty list
List<Ticket> links = new ArrayList<Ticket>();
//Populates the ArrayList uppon button click, this portion is working fine as of now
Button btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//method to add the information to a list or array
String last = captureLastName.getText();
String first = captureFirstName.getText();
String date = captureDate.getText();
String location = captureLocation.getText();
String violation = (String) captureViolation.getSelectedItem();
double fine = process.assignFine(violation);
// String type
// String speedOver
String officerBN = captureOfficerBadgeNumber.getText();
String ticketN = captureTicketNumber.getText();
Ticket newTik = new Ticket(last, first, date, location,
violation, officerBN, ticketN, fine);
links.add(newTik);
}
});
//Capturing information the user put in upon button click
JButton btnPay = new JButton("Pay Ticket");
btnPay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//assigning the ticket number to variable
String ticN = captureTickNum.getText();
//assigning payment amount to variable
String pmt = capturePaymentAmount.getText();
//making payment amount a double
double payAmt = Double.parseDouble(pmt);
for (int x = 0; x < links.size(); x++) {
if (links.get(x).getTicketNumber() == ticN) {
double newFine = (links.get(x).getFine()) - payAmt;
links.get(x).setFine(newFine);
}
}
}
});