Download presentation
Presentation is loading. Please wait.
Published byClare Dickerson Modified over 6 years ago
1
CompSci 280 S2 2107 Introduction to Software Development
Assignment 3 Help
2
Today’s Agenda Topics: Part 1: Part 2: Part 3:
Updating the books table Insert some more records Part 2: Search by title Display book details Part 3: Black box testing – after the term break A3
3
Part 1 [20 marks] Step 1: Step 2:
Drop table? Update table? Add a status column (“On Loan”, “Available”, “Reserve”…) Step 2: Insert 10 records into the BOOKS table You can use phpMyAdmin to make the above changes. Submission Make a screenshot to show the structure of the table [12marks] Make a screenshot to show the records in the table [4 marks] Make a screenshot or type to show your username, password, url for our markers to run your program [4 marks] A3
4
Part 2: Implementation [50 marks]
Choose your language: Java/Python! Download the corresponding template from Canvas Design your program’s user interface: Text box & button only? Do you want to use table? Or menu, combo box or list box etc? Note: If you implement the program using textboxes and buttons only. The maximum mark you can get is 30/50. SQL statements: Identify the SQL statement to search by a title Identify the SQL statement to display a book’s details A3
5
Marking scheme: 2.1 Documentation inside the Code: Presence of comments and overall programming style (good variable names, method names; Good indentation. Good structure) [5 marks] 2.2 Connection: Users should be able to connect to the MySql Server database and display 10 records A3
6
Marking scheme: Part 1: Searching by title Part 2: Searching by ID
2.3.1 Users should be able to search by entering a title pressing <ENTER> or clicking the search button 2.3.2 A result list is displayed using a simple GUI component [5 marks] () using a table [15 marks] Part 2: Searching by ID 2.4.1 Users should be able to view a book’s details. by entering a Book_ID [5 marks] () by selecting a row in a table control [15 marks] 2.4.2 The book’s details is displayed. A3
7
Java Template Complete the Book class Complete BookTableModel
Complete all constructors Complete BookTableModel Constructor, getValueAt, populateTable, getCurrentBook Complete the A3 main program Complete user, pass, url etc Create GUI components and add them to the content pane Complete the event handlings Title Textfield Search button JTable - selection A3
8
Part 2: Implementation Java - GUI
Work on the GUI component JLabel, JTextField, JTextArea, JScrollPane Layout Managers: FlowLayout, BorderLayout Create the required GUI components Add them either to a JPanel or the content pane of the Jframe For JTable: Create a bookTableModel Add the bookTableModel to a JTable Add the JTable to the scrollpane topPanel.add(titleTextField); pane.add(content, BorderLayout.PAGE_END); bookTableModel = new BookTableModel(JDBC_DRIVER,URL, USER, PASS ); Alternately, you can display the result list in a JTextArea and create a textbox for users to enter a BOOKID to check the book’s details bookTable = new JTable(bookTableModel); bookTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); ... A3
9
Part 2: Implementation Java – Event Handling
Title Search: Add an event handler to the search button and the title text field Get the title (getText()) Populate the bookTableModel to display the result list searchButton.addActionListener(new ActionListener() { // This method is invoked when the user hits ENTER in the field public void actionPerformed(ActionEvent e) { bookTableModel.populateTable(...); bookTableModel.fireTableDataChanged(); } }); A3
10
Part 2: Implementation Java – Event Handling
Book’s details (using a JTable) Add an event handler to the JTable Get the book Display the result (setText) Alternately, you can use a textfield and button to get the book’s details Add an event handler to the details button Get the ID Display the result bookTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent event) { //get the selected book and display the details }); } A3
11
Part 2: Implementation Java – Table Model
Create a new table model: BookTableModel extends AbstractTableModel Attributes: ArrayList<Book>, rows (number of rows), cols (number of cols), String[] columnName, Connection conn Create a constructor Parameters: driver, url, username, password Set up a connection object populateTable(“”) <= select * from BOOKS; Implement the required methods: getValueAt(int row, int col), [getRowCount, getColumnCount, getColumnName] isCellEditable return false Implement the following methods: populateTable(title): populate the table model using the search by title SQL statement getCurrentBook(rowIndex): return a book using the search by id SQL statement done A3
12
Part 2: Implementation Java – Table Model
ID Title Author Data: an ArrayList of Books Methods: populateTable(title) Execute the SQL statement Get data from the result set and create a new book using id, title and author Add the newly created book into the ArrayList getCurrentBook(int row) Get the book_id based on the row index Get data from the result set and create a new book using id, title, author… return the book while (rs.next()) { ... rs.getInt(1)... rs.getString(2)...rs.getString(3) ... } A3
13
Part 2: Implementation Java – Table Model
ID Title Author Methods: getRowCount : returns the number of rows Number of elements in the ArrayList getColumnCount: returns the number of cols (i.e. 3) getValueAt(int row, int col) return the corresponding info Get the book based on the row number Col: 0 – return id by the rowth book Col: 1 – return title … Col: 2 – return author A3
14
Python Template Complete MyTableModel Complete the A3 main program
Constructor, data, populateTable, getCurrentBook Complete the A3 main program Complete user, pass, url etc Create GUI components and add them to the content pane Complete the event handlings Search button table - selection A3
15
Part 2: Implementation Python - GUI
topLayout = QHBoxLayout() topLayout.addWidget(enterLabel)); Work on the GUI component QLineEdit(), QPushButton(), QTextEdit() Layout: QHBoxLayout(), QGridLayout() Create the GUI components Add them to the layout For QTableView: Create a bookTableModel Set the dataModel self.tablemodel = MyTableModel(my_host, my_database, my_user, my_password, self) self.tableview.setModel(self.tablemodel) self.tableview.setSelectionMode(QAbstractItemView.SingleSelection) self.tableview.setSelectionBehavior(QAbstractItemView.SelectRows) A3
16
Part 2: Implementation Python – Event Handling
Set the method Title Search: Add an event handler to the search button and the title text field Get the title (.text()) Populate the bookTableModel to display the result list searchButton.clicked.connect(self.populateTable) def populateTable(self): #get the title from the text field #update the table model ... A3
17
Part 2: Implementation Python – Event Handling
Book’s details (using a QTableView) Add an event handler to the table Get the book/row (index.row()) Display the details Alternately, you can use a text field and button to get the book’s details Add an event handler to the details button Get the ID self.tableview.clicked.connect(self.selectRow) def selectRow(self, index): result = self.tablemodel.populateDetails(index.row()) ... A3
18
Part 2: Implementation Python – Table Model
Create a new table model: class MyTableModel(QAbstractTableModel): header_labels = ["ID", "Title", "Author"] Create a constructor Set up a connection, cursor, self.arraydata = … Implement the required methods: data only, ( headerData, rowCount, columnCount ) <=completed Implement the following methods: populateTable(self, search_title): update the table model using the search by title SQL statement populateDetails(self, row_index): return the book’s details using the search by id SQL statement def __init__(self, my_host, my_database, my_user, my_password, parent=None, *args): QAbstractTableModel.__init__(self, parent, *args) A3
19
Part 2: Implementation Python – Table Model
ID Title Author Data: arraydata: a sequence of sequences Methods: populateTable(self, search_title) Execute the SQL statement get data from the result set and update the self.arraydata populateDetails(self, row_index): Get the book_id based on the row index (arraydata[ ][0]) return the corresponding info self.layoutAboutToBeChanged.emit() self.arraydata = ... self.layoutChanged.emit() A3
20
Part 2: Implementation Java – Table Model
ID Title Author Methods: rowCount: returns the number of rows in arraydata columnCount: returns the number of cols data(self, index, role): return the corresponding information based on the row index and col index headerData(self, section, orientation, role=Qt.DisplayRole): Return the column name if not index.isValid(): return QVariant() elif role != Qt.DisplayRole: return QVariant(self ) if role == Qt.DisplayRole and orientation == Qt.Horizontal: return self.header_labels[section] return QAbstractTableModel.headerData(self, section, orientation, role) A3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.