287 lines
20 KiB
HTML
287 lines
20 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>AddressBookFX.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.ST2 {color: #ffc66d}
|
|
.ST5 {color: #9876aa}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST1 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST4 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/AddressBookFX.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.io.File;
|
|
<span class="literal">import</span> java.io.FileInputStream;
|
|
<span class="literal">import</span> java.io.FileOutputStream;
|
|
<span class="literal">import</span> java.io.IOException;
|
|
<span class="literal">import</span> java.io.ObjectInputStream;
|
|
<span class="literal">import</span> java.io.ObjectOutputStream;
|
|
<span class="literal">import</span> java.io.Serializable;
|
|
<span class="literal">import</span> java.util.ArrayList;
|
|
<span class="literal">import</span> javafx.application.Application;
|
|
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">geometry</span><span class="comment">.</span><span class="comment">HPos</span><span class="comment">;</span>
|
|
<span class="literal">import</span> javafx.geometry.Pos;
|
|
<span class="literal">import</span> javafx.scene.Scene;
|
|
<span class="literal">import</span> javafx.scene.control.Button;
|
|
<span class="literal">import</span> javafx.scene.control.Label;
|
|
<span class="literal">import</span> javafx.scene.control.TextField;
|
|
<span class="literal">import</span> javafx.scene.layout.ColumnConstraints;
|
|
<span class="literal">import</span> javafx.scene.layout.GridPane;
|
|
<span class="literal">import</span> javafx.scene.layout.BorderPane;
|
|
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">scene</span><span class="comment">.</span><span class="comment">layout</span><span class="comment">.</span><span class="comment">RowConstraints</span><span class="comment">;</span>
|
|
<span class="literal">import</span> javafx.stage.Stage;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> AddressBookFX <span class="literal">extends</span> Application {
|
|
|
|
<span class="literal">static</span> File <span class="ST1">addressBook</span> = <span class="literal">new</span> File(<span class="string">"</span><span class="string">AddressBookFX.dat</span><span class="string">"</span>);
|
|
<span class="literal">static</span> <span class="literal">int</span> <span class="ST1">addressArrayPointer</span> = <span class="number">0</span>;
|
|
<span class="literal">static</span> ArrayList<AddressBookEntry> <span class="ST1">addressArray</span> = <span class="literal">new</span> ArrayList<>();
|
|
<span class="literal">static</span> TextField <span class="ST1">nameTextField</span> = <span class="literal">new</span> TextField();
|
|
<span class="literal">static</span> TextField <span class="ST1">streetTextField</span> = <span class="literal">new</span> TextField();
|
|
<span class="literal">static</span> TextField <span class="ST1">cityTextField</span> = <span class="literal">new</span> TextField();
|
|
<span class="literal">static</span> TextField <span class="ST1">stateTextField</span> = <span class="literal">new</span> TextField();
|
|
<span class="literal">static</span> TextField <span class="ST1">zipTextField</span> = <span class="literal">new</span> TextField();
|
|
<span class="literal">static</span> Label <span class="ST1">addressBookCounter</span> = <span class="literal">new</span> Label(<span class="string">"</span><span class="string">Items in address book:</span><span class="string">"</span>);
|
|
<span class="literal">static</span> Label <span class="ST1">indexLabel</span> = <span class="literal">new</span> Label();
|
|
|
|
@Override
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">start</span>(Stage stage) <span class="literal">throws</span> Exception
|
|
{
|
|
<span class="ST3">getData</span>();
|
|
BorderPane primaryBorderPane = <span class="literal">new</span> BorderPane();
|
|
GridPane textFieldGridPane = <span class="literal">new</span> GridPane();
|
|
GridPane addressFieldPane = <span class="literal">new</span> GridPane();
|
|
GridPane buttonFieldPane = <span class="literal">new</span> GridPane();
|
|
|
|
<span class="comment">// Text fields / labels</span>
|
|
textFieldGridPane.add(<span class="literal">new</span> Label(<span class="string">"</span><span class="string">Name</span><span class="string">"</span>), <span class="number">0</span>, <span class="number">0</span>);
|
|
textFieldGridPane.add(<span class="literal">new</span> Label(<span class="string">"</span><span class="string">Street</span><span class="string">"</span>), <span class="number">0</span>, <span class="number">1</span>);
|
|
textFieldGridPane.add(<span class="literal">new</span> Label(<span class="string">"</span><span class="string">City</span><span class="string">"</span>), <span class="number">0</span>, <span class="number">2</span>);
|
|
addressFieldPane.add(<span class="literal">new</span> Label(<span class="string">"</span><span class="string">State</span><span class="string">"</span>), <span class="number">2</span>, <span class="number">0</span>);
|
|
addressFieldPane.add(<span class="literal">new</span> Label(<span class="string">"</span><span class="string">ZIP</span><span class="string">"</span>), <span class="number">4</span>, <span class="number">0</span>);
|
|
textFieldGridPane.add(<span class="ST1">n</span><span class="ST1">ameTextField</span>, <span class="number">1</span>, <span class="number">0</span>);
|
|
textFieldGridPane.add(<span class="ST1">s</span><span class="ST1">treetTextField</span>, <span class="number">1</span>, <span class="number">1</span>);
|
|
textFieldGridPane.add(addressFieldPane, <span class="number">1</span>, <span class="number">2</span>);
|
|
addressFieldPane.add(<span class="ST1">c</span><span class="ST1">ityTextField</span>, <span class="number">1</span>, <span class="number">0</span>);
|
|
addressFieldPane.add(<span class="ST1">s</span><span class="ST1">tateTextField</span>, <span class="number">3</span>, <span class="number">0</span>);
|
|
addressFieldPane.add(<span class="ST1">z</span><span class="ST1">ipTextField</span>, <span class="number">5</span>, <span class="number">0</span>);
|
|
textFieldGridPane.getColumnConstraints().add(<span class="literal">new</span> ColumnConstraints(<span class="number">5</span><span class="number">0</span>));
|
|
primaryBorderPane.setTop(textFieldGridPane);
|
|
|
|
<span class="comment">// Buttons</span>
|
|
Button addButton = <span class="literal">new</span> Button(<span class="string">"</span><span class="string">Add</span><span class="string">"</span>);
|
|
Button firstButton = <span class="literal">new</span> Button(<span class="string">"</span><span class="string">First</span><span class="string">"</span>);
|
|
Button nextButton = <span class="literal">new</span> Button(<span class="string">"</span><span class="string">Next</span><span class="string">"</span>);
|
|
Button previousButton = <span class="literal">new</span> Button(<span class="string">"</span><span class="string">Previous</span><span class="string">"</span>);
|
|
Button lastButton = <span class="literal">new</span> Button(<span class="string">"</span><span class="string">Last</span><span class="string">"</span>);
|
|
Button updateButton = <span class="literal">new</span> Button(<span class="string">"</span><span class="string">Update</span><span class="string">"</span>);
|
|
buttonFieldPane.add(<span class="ST1">i</span><span class="ST1">ndexLabel</span>, <span class="number">0</span>, <span class="number">0</span>);
|
|
buttonFieldPane.add(addButton, <span class="number">1</span>, <span class="number">0</span>);
|
|
buttonFieldPane.add(firstButton, <span class="number">2</span>, <span class="number">0</span>);
|
|
buttonFieldPane.add(nextButton, <span class="number">3</span>, <span class="number">0</span>);
|
|
buttonFieldPane.add(previousButton, <span class="number">4</span>, <span class="number">0</span>);
|
|
buttonFieldPane.add(lastButton, <span class="number">5</span>, <span class="number">0</span>);
|
|
buttonFieldPane.add(updateButton, <span class="number">6</span>, <span class="number">0</span>);
|
|
buttonFieldPane.add(<span class="ST1">a</span><span class="ST1">ddressBookCounter</span>, <span class="number">7</span>, <span class="number">0</span>);
|
|
|
|
primaryBorderPane.setBottom(buttonFieldPane);
|
|
buttonFieldPane.setAlignment(Pos.<span class="ST1">CENTER</span>);
|
|
buttonFieldPane.setHgap(<span class="number">1</span><span class="number">0</span>);
|
|
|
|
addButton.setOnAction(e -> {
|
|
<span class="ST1">addressArray</span>.add(<span class="literal">new</span> AddressBookEntry());
|
|
<span class="ST3">updateData</span>();
|
|
});
|
|
firstButton.setOnAction(e -> {
|
|
<span class="ST1">addressArrayPointer</span> = <span class="number">0</span>;
|
|
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
|
|
});
|
|
nextButton.setOnAction(e -> {
|
|
<span class="literal">if</span> (<span class="ST1">addressArrayPointer</span> >= <span class="ST1">addressArray</span>.size() - <span class="number">1</span>) {
|
|
<span class="ST1">addressArrayPointer</span> = <span class="number">0</span>;
|
|
} <span class="literal">else</span> {
|
|
<span class="ST1">addressArrayPointer</span>++;
|
|
}
|
|
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
|
|
});
|
|
previousButton.setOnAction(e -> {
|
|
<span class="literal">if</span> (<span class="ST1">addressArrayPointer</span> > <span class="number">0</span>) {
|
|
<span class="ST1">addressArrayPointer</span>--;
|
|
} <span class="literal">else</span> {
|
|
<span class="ST1">addressArrayPointer</span> = <span class="ST1">addressArray</span>.size() - <span class="number">1</span>;
|
|
}
|
|
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
|
|
});
|
|
lastButton.setOnAction(e -> {
|
|
<span class="ST1">addressArrayPointer</span> = <span class="ST1">addressArray</span>.size() - <span class="number">1</span>;
|
|
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
|
|
});
|
|
updateButton.setOnAction(e -> {
|
|
AddressBookEntry entry = <span class="ST1">addressArray</span>.get(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
|
|
entry.setName(<span class="ST1">n</span><span class="ST1">ameTextField</span>.getText());
|
|
entry.setCity(<span class="ST1">n</span><span class="ST1">ameTextField</span>.getText());
|
|
entry.setStreet(<span class="ST1">s</span><span class="ST1">treetTextField</span>.getText());
|
|
entry.setCity(<span class="ST1">c</span><span class="ST1">ityTextField</span>.getText());
|
|
entry.setState(<span class="ST1">s</span><span class="ST1">tateTextField</span>.getText());
|
|
entry.setZip(<span class="ST1">z</span><span class="ST1">ipTextField</span>.getText());
|
|
<span class="ST3">updateData</span>();
|
|
});
|
|
|
|
<span class="comment">// init fields with data from first entry</span>
|
|
<span class="ST3">getEntry</span>(<span class="number">0</span>);
|
|
|
|
Scene scene = <span class="literal">new</span> Scene(primaryBorderPane);
|
|
stage.setScene(scene);
|
|
stage.show();
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> AddressBookEntry <span class="ST4">getEntry</span>(<span class="literal">int</span> index)
|
|
{
|
|
AddressBookEntry entry = <span class="ST1">addressArray</span>.get(index);
|
|
<span class="ST1">indexLabel</span>.setText(<span class="string">"</span><span class="string">Current index: </span><span class="string">"</span> + index);
|
|
<span class="ST1">addressBookCounter</span>.setText(<span class="string">"</span><span class="string">Items in address book: </span><span class="string">"</span> + <span class="ST1">addressArray</span>.size());
|
|
<span class="ST1">nameTextField</span>.setText(entry.getName());
|
|
<span class="ST1">streetTextField</span>.setText(entry.getStreet());
|
|
<span class="ST1">cityTextField</span>.setText(entry.getCity());
|
|
<span class="ST1">stateTextField</span>.setText(entry.getState());
|
|
<span class="ST1">zipTextField</span>.setText(entry.getZip());
|
|
<span class="literal">return</span> entry;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">updateData</span>()
|
|
{
|
|
<span class="literal">try</span> (ObjectOutputStream fileStream = <span class="literal">new</span> ObjectOutputStream(<span class="literal">new</span> FileOutputStream(<span class="ST1">a</span><span class="ST1">ddressBook</span>))) {
|
|
fileStream.writeObject(<span class="ST1">a</span><span class="ST1">ddressArray</span>);
|
|
} <span class="literal">catch</span> (IOException ex) {
|
|
System.<span class="ST1">out</span>.println(ex);
|
|
}
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">getData</span>()
|
|
{
|
|
<span class="literal">if</span> (!<span class="ST1">addressBook</span>.exists()) {
|
|
<span class="ST1">addressArray</span>.add(<span class="literal">new</span> AddressBookEntry());
|
|
<span class="ST3">updateData</span>();
|
|
} <span class="literal">else</span> {
|
|
<span class="literal">try</span> (ObjectInputStream fileStream = <span class="literal">new</span> ObjectInputStream(<span class="literal">new</span> FileInputStream(<span class="ST1">a</span><span class="ST1">ddressBook</span>))) {
|
|
<span class="ST1">addressArray</span> = (ArrayList<AddressBookEntry>) fileStream.readObject();
|
|
} <span class="literal">catch</span> (IOException ex) {
|
|
System.<span class="ST1">out</span>.println(ex);
|
|
} <span class="literal">catch</span> (ClassNotFoundException ex) {
|
|
System.<span class="ST1">out</span>.println(ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">main</span>(String[] args)
|
|
{
|
|
<span class="ST3">launch</span>();
|
|
}
|
|
}
|
|
|
|
<span class="literal">class</span> AddressBookEntry <span class="literal">implements</span> Serializable {
|
|
|
|
<span class="literal">private</span> String <span class="ST5">name</span>;
|
|
<span class="literal">private</span> String <span class="ST5">street</span>;
|
|
<span class="literal">private</span> String <span class="ST5">city</span>;
|
|
<span class="literal">private</span> String <span class="ST5">state</span>;
|
|
<span class="literal">private</span> String <span class="ST5">zip</span>;
|
|
|
|
<span class="literal">public</span> AddressBookEntry()
|
|
{
|
|
<span class="literal">this</span>.<span class="ST5">name</span> = <span class="string">"</span><span class="string">Enter a name here.</span><span class="string">"</span>;
|
|
<span class="literal">this</span>.<span class="ST5">street</span> = <span class="string">"</span><span class="string">Enter a street here.</span><span class="string">"</span>;
|
|
<span class="literal">this</span>.<span class="ST5">city</span> = <span class="string">"</span><span class="string">Enter a city here.</span><span class="string">"</span>;
|
|
<span class="literal">this</span>.<span class="ST5">state</span> = <span class="string">"</span><span class="string">Enter a state here.</span><span class="string">"</span>;
|
|
<span class="literal">this</span>.<span class="ST5">zip</span> = <span class="string">"</span><span class="string">Enter a zip here.</span><span class="string">"</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> AddressBookEntry(String name, String street, String city, String state, String zip)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST5">name</span> = name;
|
|
<span class="literal">this</span>.<span class="ST5">street</span> = street;
|
|
<span class="literal">this</span>.<span class="ST5">city</span> = city;
|
|
<span class="literal">this</span>.<span class="ST5">state</span> = state;
|
|
<span class="literal">this</span>.<span class="ST5">zip</span> = zip;
|
|
}
|
|
|
|
<span class="literal">public</span> String <span class="ST2">getZip</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST5">zip</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setZip</span>(String zip)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST5">zip</span> = zip;
|
|
}
|
|
|
|
<span class="literal">public</span> String <span class="ST2">getState</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST5">state</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setState</span>(String state)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST5">state</span> = state;
|
|
}
|
|
|
|
<span class="literal">public</span> String <span class="ST2">getCity</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST5">city</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setCity</span>(String city)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST5">city</span> = city;
|
|
}
|
|
|
|
<span class="literal">public</span> String <span class="ST2">getStreet</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST5">street</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setStreet</span>(String street)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST5">street</span> = street;
|
|
}
|
|
|
|
<span class="literal">public</span> String <span class="ST2">getName</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST5">name</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setName</span>(String name)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST5">name</span> = name;
|
|
}
|
|
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|