Question: Hello, I am to create a Java application that validates ISBN-10 number entered into a custom Textfield object and then using a custom exception class
Hello, I am to create a Java application that validates ISBN-10 number entered into a custom Textfield object and then using a custom exception class to validate it as a string. I have compiled the classes but everytime i "validate" a number It always comes back as valid.below are the classes i have created please advice me in what I am missing so that my custom exception is called these are
some valid ISBN numbers for testing
031224665X, 0973778709, 0195424409, 0465054676
ISBNVerifierGUI
import java.awt.*; import javax.swing.*;
public class ISBNVerifierGUI extends JPanel { /** * fields */ protected JPanel isbnPanel, // A panel for ISBN label and textfield buttonPanel; // A panel for the buttons // Replace the textfield with your custom control protected ISBNTextField isbn; // A custom control for the ISBN number protected JTextArea status; // Used to display status and error messages protected JButton validateButton, // Validates the entered ISBN # exitButton; // Exits the application //add custom handler ISBNHandler handler = new ISBNHandler(this); /** Constructor */ public ISBNVerifierGUI() { // Set JPanel layout as Border setLayout(new BorderLayout()); // Build the three components for the North, Center and South buildISBNPanel(); JScrollPane sPane = new JScrollPane( status= new JTextArea(20, 20) ); buildButtonPanel(); // Add the components to the JPanel add(isbnPanel, BorderLayout.NORTH); add(sPane, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); }
/** The buildISBNPanel method creates a panel containing the ISBN prompt label and textfield */ private void buildISBNPanel() { // create the panel with a FlowLayout isbnPanel = new JPanel(new FlowLayout());
// Add label and custom control to the panel isbnPanel.add(new JLabel("Enter ISBN-10 #: ")); isbnPanel.add(isbn= new ISBNTextField(20));
}// end buildISBNPanel() method
/** The buildButtonPanel method creates a panel containing buttons. */ private void buildButtonPanel() { //Create the button pannel, buttons & add buttons buttonPanel = new JPanel(); buttonPanel.add( validateButton= new JButton("Validate ISBN") ); buttonPanel.add( exitButton = new JButton("Exit") );
// Add an action listener to the validate and exit buttons. validateButton.addActionListener(handler); exitButton.addActionListener(handler);
}// end buildButtonPanel() method\
}// end ISBNVerifierGUI class
ISBNHandler
public class ISBNHandler implements ActionListener { protected ISBNVerifierGUI gui; public ISBNHandler(ISBNVerifierGUI gui) { this.gui = gui; } public void actionPerformed(ActionEvent e) { if(e.getSource() == gui.validateButton) { try { gui.isbn.getISBN(); } catch(ISBNException error) { System.out.println(error); } gui.status.append("Valid ISBN"); } if(e.getSource() == gui.exitButton) { System.exit(0); } } }
ISBNVerifierClient
import java.awt.*; import javax.swing.*;
public class ISBNVerifierClient extends JFrame { final static private int WIDTH= 450, HEIGHT= 200;//Window width & height private Container c; // A reference to the frame's content pane private ISBNVerifierGUI gui; // A reference to the GUI class
/** Constructor */ public ISBNVerifierClient() { // Get an instance of the content pane c = getContentPane(); // Add the GUI to the content pane c.add(gui= new ISBNVerifierGUI());
// JFrame setup parms: setTitle("ISBN Validator"); setSize(WIDTH,HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); }
public static void main(String[] args) { new ISBNVerifierClient(); }
}// end ISBNVerifierClient class
ISBNTextField
import javax.swing.*; import java.awt.*;
public class ISBNTextField extends JTextField { protected String text; protected String error; protected ISBNVerifierGUI gui; /** * constructor for objects of class ISBNText */ public ISBNTextField(int size) { super(size); } public String getISBN() throws ISBNException { this.text = text; text = getText(); text = validateISBN(text); return text; } private String validateISBN(String text) throws ISBNException { int count = 0; for(int i = 0; i < text.length(); i++) { if(text.charAt(i) != '-') { count++; } } if(count != 10) { checkDigit(text); error = ("isbn " + text + " must be 10 chars long"); throw new ISBNException(error); } if((text.charAt(text.length() -1) == 'x')) { this.checkDigit(text); error = ("isbn " + text + " must be a digit or an X"); throw new ISBNException(error); } if((text.charAt(text.length() -1) != 'x')) { checkDigit(text); } return text; } private boolean checkDigit(String text) throws ISBNException { int n = 0; for(int i = 0; i < text.length() -1; i++) { n += text.indexOf(i + 1) * text.charAt(i); } if(n % 11 == text.indexOf(text.length() -1)) { return true; } else if(n % 11 == 10) { return true; } else { return false; } } }
ISBNException
public class ISBNException extends Exception { public ISBNException(String error) { super(error); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
