Skip to content

Commit

Permalink
负数检测
Browse files Browse the repository at this point in the history
  • Loading branch information
Qianmoxsn committed May 26, 2024
1 parent 55ca537 commit 14c612b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,38 @@ private void onOK() {
// ammount should be a integer
try {
amountInt = Integer.parseInt(amount);
if (amountInt < 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
// show a dialog
JOptionPane.showMessageDialog(this, "Amount should be an Integer", "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(this, "Amount should be an non-negative Integer", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

String rate = rateField.getText();
double rateDouble;
try {
rateDouble = Double.parseDouble(rate);
if (rateDouble < 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
// show a dialog
JOptionPane.showMessageDialog(this, "Amount should be an Double", "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(this, "Rate should be an non-negative Double", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

String month = monthField.getText();
int monthInt;
try {
monthInt = Integer.parseInt(month);
if (monthInt < 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
// show a dialog
JOptionPane.showMessageDialog(this, "Amount should be an Integer", "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(this, "Month should be an non-negative Integer", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ private void onOK() {
String password = new String(pass);
log.debug("Amount: {}, Password: {}", amount, password);

// ammount should be a integer
// ammount should be a integer and non-negtive
try {
amountInt = Integer.parseInt(amount);
if (amountInt < 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
// show a dialog
JOptionPane.showMessageDialog(this, "Amount should be an Integer", "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(this, "Amount should be an non-negative Integer", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ private void onOK() {
// newGoal should be a Integer
try {
doubleGoal = Double.parseDouble(newGoal);
if (doubleGoal < 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
log.error("Invalid goal: {}", newGoal);
JOptionPane.showMessageDialog(this, "Invalid goal: " + newGoal, "Error", JOptionPane.ERROR_MESSAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ private void onOK() {
// Amount should be an integer
try {
amountInt = Integer.parseInt(amount);
if (amountInt <= 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
// Show a dialog
JOptionPane.showMessageDialog(this, "Amount should be an Integer", "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(this, "Amount should be an non-negative Integer", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ private void onOK() {
// id should be a number
try {
id = Long.parseLong(textField1.getText());
if (id <= 0) {
JOptionPane.showMessageDialog(this, "Task No. should be a positive number", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Task No. should be a number", "Error", JOptionPane.ERROR_MESSAGE);
return;
Expand Down

0 comments on commit 14c612b

Please sign in to comment.