Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Swing application that looks and behaves like the example located at th

ID: 3561367 • Letter: C

Question

Create a Swing application that looks and behaves like the example located at the top of this assignment. Start with your code from week one and implement the use of the Oracle database in place of the array used in week one.

The functionality you are looking for is the following:

Modify your CLASSPATH environment variable to include both the Oracle thin drivers .jar file and the current working directory.

Load the Oracle Thin Drivers Create a connection to the Oracle database. Execute a query of the database to retrieve a populated ResultSet.

Then with the populated ResultSet: The Previous button will iterate through the ResultSet moving to the previous element each time the button is clicked and will then update the GUI with the newly selected data. If the Previous button is selected while the ResultSet is positioned at the first element, your program should then move the last element and update the display with the newly selected data. The Next button will iterate through the ResultSet moving to the next element each time the button is clicked and will then update the GUI with the newly selected data. If the Next button is selected while the ResultSet is positioned at the last element, your program should then move the first element and update the display with the newly selected data. When the Reset button is selected you should move to the first element in the ResultSet and update the display.

Week 1 Code that needs to be edited to pull from database.

//main program to given data

import javax.swing.*;

import java.awt.*;

class Weekone extends JFrame

{

private JButton buttonPrev = new JButton("Prev");

private JButton buttonReset = new JButton("Reset");

private JButton buttonNext = new JButton("Next");

private JLabel labelHeader = new JLabel("Database Browser",JLabel.CENTER);

private JLabel labelName = new JLabel("Name");

private JLabel labelAddress = new JLabel("Address");

private JLabel labelCity = new JLabel("City");

private JLabel labelState = new JLabel("State");

private JLabel labelZip = new JLabel("Zip");

private JTextField textFieldName = new JTextField();

private JTextField textFieldAddress = new JTextField();

private JTextField textFieldCity = new JTextField();

private JTextField textFieldState = new JTextField();

private JTextField textFieldZip = new JTextField();

DataClass[] DataClassArray = {

new DataClass("James", "William", "101 Here", "Ok", "123234"),

new DataClass("Martin", "Edan", "102 There", "Androed", "123235"),

new DataClass("Ebenzer", "Rachel", "103 No Where", "OK", "123236") };

int arrayPointer = 0;

public Weekone(String title)

{

super(title);

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

JPanel cp = (JPanel) getContentPane( ) ; labelHeader.setFont(new Font("CourierNew", Font.Italic, 12));

labelHeader.setBounds(40, 10, 300, 50);

buttonPrev.setBounds(30, 250, 80, 25);

buttonReset.setBounds(150, 250, 80, 25);

buttonNext.setBounds(270, 250, 80, 25);

labelName.setBounds(10, 80, 80, 25);

labelAddress.setBounds(10, 110, 80, 25);

labelCity.setBounds(10, 140, 80, 25);

labelState.setBounds(10, 170, 80, 25);

labelZip.setBounds(10, 200, 80, 25);

textFieldName.setBounds(120, 80, 250, 25);

textFieldAddress.setBounds(120, 110, 250, 25);

textFieldCity.setBounds(120, 140, 250, 25);

textFieldState.setBounds(120, 170, 250, 25);

textFieldZip.setBounds(120, 200, 250, 25);

cp.setLayout(null);

cp.add(labelHeader);

cp.add(buttonPrev);

cp.add(buttonReset);

cp.add(buttonNext);

cp.add(labelName);

cp.add(textFieldName);

cp.add(labelAddress);

cp.add(textFieldAddress);

cp.add(labelCity);

cp.add(textFieldCity);

cp.add(labelState);

cp.add(textFieldState);

cp.add(labelZip);

cp.add(textFieldZip);

addWindowListener(new java.awt.event.WindowAdapter( )

{

public void windowClosing(java.awt.event.WindowEvent evt)

{

shutDown( );

}

});

buttonPrev.addActionListener(new java.awt.event.ActionListener( )

{ public void actionPerformed(java.awt.event.ActionEvent evt)

{ if (--arrayPointer < 0)

{

arrayPointer = DataClassArray.length - 1;

}

setFields(arrayPointer);

}

}); buttonNext.addActionListener(new java.awt.event.ActionListener()

{

public void actionPerformed(java.awt.event.ActionEvent evt)

{

if (++arrayPointer == DataClassArray.length)

arrayPointer = 0;

setFields(arrayPointer);

}

});

buttonReset.addActionListener(new java.awt.event.ActionListener()

{

public void actionPerformed(java.awt.event.ActionEvent evt)

{

arrayPointer = 0;

setFields(arrayPointer); }

});

setFields(arrayPointer);

}

private void setFields(int position)

{

textFieldName.setText(DataClassArray[position].getName( ));

textFieldAddress.setText(DataClassArray[position].getAddess( ));

textFieldCity.setText(DataClassArray[position].getCity( ));

textFieldState.setText(DataClassArray[position].getState( ));

textFieldZip.setText(DataClassArray[position].getZip( ));

}

private void shutDown( )

{

int returnVal = JOptionPane.showConfirmDialog(this,"Are you you want to EXIT?");

if (returnVal == JOptionPane.YES_OPTION)

{ System.exit(0);

}

} public static void main(String args[ ])

{ Weekone= new Weekone("Database Browser");

a2.setSize(400, 350);

a2.setVisible(true);

}

}

class DataClass

{ // To save space I declared all String objects on a single line

String name, address, city, state, zipCode;

DataClass(String name, String address, String city, String state,

String zipCode)

{

this.name = name;

this.address = address;

this.city = city;

this.state = state;

this.zipCode = zipCode;

}

String getName( )

{

return this.name;

}

String getAddress( )

{

return this.address;

}

String getCity( )

{

return this.city;

} String getState( )

{

return this.state;

} String getZip()

{

return this.zipCode;

}

}

Explanation / Answer

import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import org.junit.Before; import org.junit.Test; public class AddressBookTest { private Contact c1, c2, c3, c4, c5, c6, c7; private AddressBookPersist addressBookPersist; @Before public void setUp() { addressBookPersist = new AddressBookPersist(); createNewContacts(); } public static void main(String[] args) { AddressBookTest test = new AddressBookTest(); test.setUp(); test.sortFriendsByTheirNames(); test.uniqueFriendsFromTwoAddressBooks(); test.uniqueFriendsFromThreeAddressBooks(); } @Test public void uniqueFriendsFromTwoAddressBooks() { System.out.println("========== Unique Friends from Two Address Books =========="); addTwoAddressBooks(); List addressBooks = addressBookPersist.getAddressBooks(); printInput(addressBooks); // Get unique contacts from two AddressBooks Set uniqueContacts = AddressBook.getUniqueContacts(addressBooks); printOutput(addressBooks, uniqueContacts); // The unique contacts from these two address books should be c1 and c4 Set expected = new HashSet(Arrays.asList(c1, c4)); assertTrue(uniqueContacts.equals(expected)); } @Test public void uniqueFriendsFromThreeAddressBooks() { System.out.println("========== Unique Friends from Three Address Books =========="); addThreeAddressBooks(); List addressBooks = addressBookPersist.getAddressBooks(); printInput(addressBooks); // Get unique contacts from three AddressBooks Set uniqueContacts = AddressBook.getUniqueContacts(addressBooks); printOutput(addressBooks, uniqueContacts); // The unique contacts from three address books should be c1, c4 and c5, c6, c7 Set expected = new HashSet(Arrays.asList(c4, c5, c6, c7)); assertTrue(uniqueContacts.equals(expected)); } @Test public void sortFriendsByTheirNames() { System.out.println("========== Display the list of friends sorted by their name =========="); AddressBook addressBook = new AddressBook("ab1"); addressBook.addContact(c5); addressBook.addContact(c1); addressBook.addContact(c4); addressBook.addContact(c2); addressBook.addContact(c3); System.out.println("==Input=="); System.out.println("Address Book: " + addressBook.getName()); System.out.println("Friends:"); for (Contact contact : addressBook.getContacts()) { System.out.println(contact); } System.out.println(); Collections .sort(addressBook.getContacts(), new ContactNameComparator()); System.out.println("==Output=="); System.out.println("Address Book: " + addressBook.getName()); System.out.println("Friends:"); for (Contact contact : addressBook.getContacts()) { System.out.println(contact); } System.out.println(" "); // Sorted list assertTrue("Bob".equals(addressBook.getContacts().get(0).getName())); assertTrue("Jane".equals(addressBook.getContacts().get(1).getName())); assertTrue("John".equals(addressBook.getContacts().get(2).getName())); assertTrue("Mary".equals(addressBook.getContacts().get(3).getName())); assertTrue("Ruby".equals(addressBook.getContacts().get(4).getName())); } private void createNewContacts() { c1 = new Contact("Bob", "02 9218 5479"); c2 = new Contact("Mary", "04 9218 5479"); c3 = new Contact("Jane", "02 9 605 3147"); c4 = new Contact("John", "02 605 3147"); c5 = new Contact("Ruby", "03 9 605 3147"); c6 = new Contact("Paul", "03 9 605 3147"); c7 = new Contact("Zee", "03 9 605 3147"); } private void addTwoAddressBooks() { addressBookPersist.removeAllAddressBooks(); AddressBook ab1 = new AddressBook("ab1"); AddressBook ab2 = new AddressBook("ab2"); // AddContacts to the addressBooks ab1.addContact(c1); ab1.addContact(c2); ab1.addContact(c3); ab2.addContact(c2); ab2.addContact(c4); ab2.addContact(c3); addressBookPersist.addAddressBook(ab1); addressBookPersist.addAddressBook(ab2); } private void addThreeAddressBooks() { addressBookPersist.removeAllAddressBooks(); AddressBook ab1 = new AddressBook("ab1"); AddressBook ab2 = new AddressBook("ab2"); AddressBook ab3 = new AddressBook("ab3"); // AddContacts to the addressBooks ab1.addContact(c1); ab1.addContact(c2); ab1.addContact(c3); ab2.addContact(c2); ab2.addContact(c4); ab2.addContact(c3); ab3.addContact(c1); ab3.addContact(c5); ab3.addContact(c6); ab3.addContact(c7); addressBookPersist.addAddressBook(ab1); addressBookPersist.addAddressBook(ab2); addressBookPersist.addAddressBook(ab3); } private void printInput(List addressBooks){ System.out.println("==Input=="); for (AddressBook addressBook : addressBooks) { System.out.println("Address Book: " + addressBook.getName()); System.out.println("Friends:"); for (Contact c : addressBook.getContacts()) { System.out.println(c.getName()); } System.out.println(""); } } private void printOutput(List addressBooks,Set uniqueContacts){ System.out.println("==Output=="); System.out.print("Address Books: "); String names = ""; for (AddressBook addressBook : addressBooks) { names += addressBook.getName() + ", "; } if (names.length() > 0) { System.out.println(names.substring(0, names.lastIndexOf(","))); } for (Contact c : uniqueContacts) { System.out.println(c.getName()); } System.out.println(" "); } }