*internal screaming*

This commit is contained in:
2023-10-21 00:37:53 -05:00
parent 68440bd402
commit 01f7640bdd
62 changed files with 5125 additions and 84 deletions

View File

@@ -0,0 +1,256 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp5.files_calebfontenot;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;
/**
*
* @author caleb
*/
public class AddressBookFX extends Application {
static File addressBook = new File("AddressBookFX.dat");
static int addressArrayPointer = 0;
static ArrayList<AddressBookEntry> addressArray = new ArrayList<>();
static TextField nameTextField = new TextField();
static TextField streetTextField = new TextField();
static TextField cityTextField = new TextField();
static TextField stateTextField = new TextField();
static TextField zipTextField = new TextField();
static Label addressBookCounter = new Label("Items in address book:");
static Label indexLabel = new Label();
@Override
public void start(Stage stage) throws Exception
{
getData();
BorderPane primaryBorderPane = new BorderPane();
GridPane textFieldGridPane = new GridPane();
GridPane addressFieldPane = new GridPane();
GridPane buttonFieldPane = new GridPane();
// Text fields / labels
textFieldGridPane.add(new Label("Name"), 0, 0);
textFieldGridPane.add(new Label("Street"), 0, 1);
textFieldGridPane.add(new Label("City"), 0, 2);
addressFieldPane.add(new Label("State"), 2, 0);
addressFieldPane.add(new Label("ZIP"), 4, 0);
textFieldGridPane.add(nameTextField, 1, 0);
textFieldGridPane.add(streetTextField, 1, 1);
textFieldGridPane.add(addressFieldPane, 1, 2);
addressFieldPane.add(cityTextField, 1, 0);
addressFieldPane.add(stateTextField, 3, 0);
addressFieldPane.add(zipTextField, 5, 0);
textFieldGridPane.getColumnConstraints().add(new ColumnConstraints(50));
primaryBorderPane.setTop(textFieldGridPane);
// Buttons
Button addButton = new Button("Add");
Button firstButton = new Button("First");
Button nextButton = new Button("Next");
Button previousButton = new Button("Previous");
Button lastButton = new Button("Last");
Button updateButton = new Button("Update");
buttonFieldPane.add(indexLabel, 0, 0);
buttonFieldPane.add(addButton, 1, 0);
buttonFieldPane.add(firstButton, 2, 0);
buttonFieldPane.add(nextButton, 3, 0);
buttonFieldPane.add(previousButton, 4, 0);
buttonFieldPane.add(lastButton, 5, 0);
buttonFieldPane.add(updateButton, 6, 0);
buttonFieldPane.add(addressBookCounter, 7, 0);
primaryBorderPane.setBottom(buttonFieldPane);
buttonFieldPane.setAlignment(Pos.CENTER);
buttonFieldPane.setHgap(10);
addButton.setOnAction(e -> {
addressArray.add(new AddressBookEntry());
updateData();
});
firstButton.setOnAction(e -> {
addressArrayPointer = 0;
getEntry(addressArrayPointer);
});
nextButton.setOnAction(e -> {
if (addressArrayPointer >= addressArray.size() - 1) {
addressArrayPointer = 0;
} else {
addressArrayPointer++;
}
getEntry(addressArrayPointer);
});
previousButton.setOnAction(e -> {
if (addressArrayPointer > 0) {
addressArrayPointer--;
} else {
addressArrayPointer = addressArray.size() - 1;
}
getEntry(addressArrayPointer);
});
lastButton.setOnAction(e -> {
addressArrayPointer = addressArray.size() - 1;
getEntry(addressArrayPointer);
});
updateButton.setOnAction(e -> {
AddressBookEntry entry = addressArray.get(addressArrayPointer);
entry.setName(nameTextField.getText());
entry.setCity(nameTextField.getText());
entry.setStreet(streetTextField.getText());
entry.setCity(cityTextField.getText());
entry.setState(stateTextField.getText());
entry.setZip(zipTextField.getText());
updateData();
});
// init fields with data from first entry
getEntry(0);
Scene scene = new Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
public static AddressBookEntry getEntry(int index)
{
AddressBookEntry entry = addressArray.get(index);
indexLabel.setText("Current index: " + index);
addressBookCounter.setText("Items in address book: " + addressArray.size());
nameTextField.setText(entry.getName());
streetTextField.setText(entry.getStreet());
cityTextField.setText(entry.getCity());
stateTextField.setText(entry.getState());
zipTextField.setText(entry.getZip());
return entry;
}
public static void updateData()
{
try (ObjectOutputStream fileStream = new ObjectOutputStream(new FileOutputStream(addressBook))) {
fileStream.writeObject(addressArray);
} catch (IOException ex) {
System.out.println(ex);
}
}
public static void getData()
{
if (!addressBook.exists()) {
addressArray.add(new AddressBookEntry());
updateData();
} else {
try (ObjectInputStream fileStream = new ObjectInputStream(new FileInputStream(addressBook))) {
addressArray = (ArrayList<AddressBookEntry>) fileStream.readObject();
} catch (IOException ex) {
System.out.println(ex);
} catch (ClassNotFoundException ex) {
System.out.println(ex);
}
}
}
public static void main(String[] args)
{
launch();
}
}
class AddressBookEntry implements Serializable {
private String name;
private String street;
private String city;
private String state;
private String zip;
public AddressBookEntry()
{
this.name = "Enter a name here.";
this.street = "Enter a street here.";
this.city = "Enter a city here.";
this.state = "Enter a state here.";
this.zip = "Enter a zip here.";
}
public AddressBookEntry(String name, String street, String city, String state, String zip)
{
this.name = name;
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
public String getZip()
{
return zip;
}
public void setZip(String zip)
{
this.zip = zip;
}
public String getState()
{
return state;
}
public void setState(String state)
{
this.state = state;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
public String getStreet()
{
return street;
}
public void setStreet(String street)
{
this.street = street;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}

View File

@@ -0,0 +1,30 @@
package com.calebfontenot.mp5.files_calebfontenot;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
var javaVersion = SystemInfo.javaVersion();
var javafxVersion = SystemInfo.javafxVersion();
var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
var scene = new Scene(new StackPane(label), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}

View File

@@ -0,0 +1,98 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp5.files_calebfontenot;
import java.io.DataInput;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
*
* @author caleb
*/
public class CombineFilesFX extends Application {
static List<File> files = null;
final FileChooser fileChooser = new FileChooser();
@Override
public void start(final Stage stage) throws Exception
{
fileChooser.setTitle("Open File to Split...");
BorderPane primaryBorderPane = new BorderPane();
GridPane textFieldGridPane = new GridPane();
Label infoLabel = new Label("Point me at the files that were output by SplitFilesFX.java.");
Label chooseFile = new Label("Choose files to combine: ");
Button openFilePicker = new Button("Choose...");
Button run = new Button("Start");
textFieldGridPane.add(chooseFile, 0, 0);
textFieldGridPane.add(openFilePicker, 1, 0);
primaryBorderPane.setAlignment(run, Pos.CENTER);
primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane);
primaryBorderPane.setBottom(run);
openFilePicker.setOnAction(e -> {
files = fileChooser.showOpenMultipleDialog(openFilePicker.getScene().getWindow());
});
run.setOnAction(e -> {
if (files == null) {
e.consume();
}
combineFiles(files);
});
Scene scene = new Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
public static void combineFiles(List<File> filesToCombine)
{
String outputPath = filesToCombine.get(0).getParent();
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + "reconstructed_" +filesToCombine.get(0).getName().substring( 0, (filesToCombine.get(0).getName().length() - 2)))) {
System.out.println("Writing to " + outputPath + "/" + "reconstructed_" +filesToCombine.get(0).getName().substring( 0, ( filesToCombine.get(0).getName().length() - 2)));
for (File file : filesToCombine) {
try (FileInputStream dataIn = new FileInputStream(file)) {
System.out.println("Opening the source file!");
byte[] buffer = new byte[4096]; // You can adjust the buffer size as needed
int bytesRead;
while ((bytesRead = dataIn.read(buffer)) != -1) {
dataOut.write(buffer, 0, bytesRead);
}
}
}
} catch (IOException ex) {
System.out.println(ex);
}
}
public static void main(String[] args)
{
launch();
}
}
/*
*/

View File

@@ -0,0 +1,52 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp5.files_calebfontenot;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
/**
*
* @author caleb
*/
public class Exercise17_01 {
public static void main(String[] args)
{
try (RandomAccessFile fileIO = new RandomAccessFile("Exercise17_01.txt", "rw")) {
int random = 0;
System.out.println("Writing data to file...");
for (int i = 1; i < 100 + 1; ++i) {
random = (int) (Math.random() * 9) + 1;
System.out.print(random + " ");
if (i != 0 && i % 10 == 0) {
System.out.println();
}
fileIO.writeInt(random);
}
System.out.println("Wrote to the file successfully!");
System.out.println("File contents:");
fileIO.seek(0);
int readIterator = 1;
while (true) {
try {
System.out.print(fileIO.readInt() + " ");
if (readIterator != 0 && readIterator % 10 == 0) {
System.out.println();
}
++readIterator;
} catch (EOFException e) {
break;
}
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp5.files_calebfontenot;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* @author caleb
*/
public class Exercise17_03 {
static int fileSize = Math.abs((int) (Math.random() * 1024));
public static void writeData()
{
try (FileOutputStream fileWrite = new FileOutputStream("Exercise17_03.dat")) {
// Write a unspecified number of integers into the file.
// Must be positive!
int randInt = 0;
for (int i = 0; i < fileSize; ++i) {
randInt = (int) (Math.random() * 10);
fileWrite.write(randInt);
}
} catch (IOException ex) {
System.out.println(ex);
}
System.out.println("Wrote data to the file!");
}
public static int[] readData()
{
int[] fileData = new int[fileSize];
try (FileInputStream fileRead = new FileInputStream("Exercise17_03.dat")) {
// Read the data back
int dataIterator = 0;
int dataStream = 0;
while (fileRead.available() > 0) {
dataStream = fileRead.read();
fileData[dataIterator++] = dataStream;
System.out.print(dataStream + " ");
if ((dataIterator + 1) % 10 == 0) {
System.out.println();
}
}
} catch (IOException ex) {
System.out.println(ex);
}
return fileData;
}
public static void main(String[] args)
{
System.out.println("Ints to write: " + fileSize);
writeData();
int[] fileData = readData();
// Sum the digits
int sum = 0;
for (int i: fileData) {
sum += i;
}
System.out.println("\nThe sum of the integers in the file is: " + sum);
}
}

View File

@@ -0,0 +1,79 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp5.files_calebfontenot;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
*
* @author caleb
*/
public class Exercise17_05 {
public static void main(String[] args) {
File dataFile = new File("Exercise17_05.dat");
if(!dataFile.exists()) {
try (ObjectOutputStream fileStream = new ObjectOutputStream(new FileOutputStream(dataFile))) {
fileStream.writeObject(new DataContainer());
} catch (IOException ex) {
System.out.println(ex);
}
}
DataContainer data = null;
try (ObjectInputStream fileStream = new ObjectInputStream(new FileInputStream(dataFile))) {
data = (DataContainer) fileStream.readObject();
} catch (IOException ex) {
System.out.println(ex);
} catch (ClassNotFoundException ex) {
System.out.println(ex);
}
// Now print out the data!
System.out.println("We got the data from the file!");
System.out.println(data.toString());
}
}
class DataContainer implements Serializable {
int[] intArray = {1, 2, 3, 4};
Date currentDate = new Date();
double doubleMoment = 5.5;
public int[] getIntArray()
{
return intArray;
}
public Date getCurrentDate()
{
return currentDate;
}
public double getDoubleMoment()
{
return doubleMoment;
}
@Override
public String toString()
{
String intString = "[";
for (int i = 0; i < intArray.length - 1; ++i) {
intString += intArray[i];
if (i == (intArray.length - 2)) {
intString += "]";
} else {
intString += ", ";
}
}
return "DataContainer{" + "intArray=" + intString + ", currentDate=" + currentDate + ", doubleMoment=" + doubleMoment + '}';
}
}

View File

@@ -0,0 +1,97 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp5.files_calebfontenot;
import java.io.DataInput;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
*
* @author caleb
*/
public class SplitFilesFX extends Application {
static File file = null;
final FileChooser fileChooser = new FileChooser();
@Override
public void start(final Stage stage) throws Exception
{
fileChooser.setTitle("Open File to Split...");
BorderPane primaryBorderPane = new BorderPane();
GridPane textFieldGridPane = new GridPane();
Label infoLabel = new Label("If you split a file named tmp into 3 smaller files,\n the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. ");
Label chooseFile = new Label("Choose a file to split: ");
Button openFilePicker = new Button("Choose...");
Label splitCountLabel = new Label("Enter the amount of files to split into: ");
TextField splitCount = new TextField();
Button run = new Button("Start");
textFieldGridPane.add(chooseFile, 0, 0);
textFieldGridPane.add(openFilePicker, 1, 0);
textFieldGridPane.add(splitCountLabel, 0, 1);
textFieldGridPane.add(splitCount, 1, 1);
primaryBorderPane.setAlignment(run, Pos.CENTER);
primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane);
primaryBorderPane.setBottom(run);
openFilePicker.setOnAction(e -> {
file = fileChooser.showOpenDialog(openFilePicker.getScene().getWindow());
});
run.setOnAction(e -> {
if (file == null) {
e.consume();
}
splitFile(file, Integer.parseInt(splitCount.getText()));
});
Scene scene = new Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
public static void splitFile(File fileToSplit, int splitCount) {
int outputFileSize = (int) fileToSplit.length() / splitCount;
String outputPath = fileToSplit.getParent();
// Open the original file
try (FileInputStream dataIn = new FileInputStream(fileToSplit)) {
System.out.println("Opening the source file!");
for (int i = 0; i < splitCount; ++i) {
//dataIn.mark(outputFileSize * (i + 1));
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + fileToSplit.getName() + "." + (i + 1))) {
dataOut.write(dataIn.readNBytes(outputFileSize), 0, outputFileSize);
System.out.println("Writing to " + outputPath + "/" + fileToSplit.getName() + "." + (i + 1));
}
}
} catch (IOException ex) {
System.out.println(ex);
}
}
public static void main(String[] args)
{
launch();
}
}
/*
*/

View File

@@ -0,0 +1,13 @@
package com.calebfontenot.mp5.files_calebfontenot;
public class SystemInfo {
public static String javaVersion() {
return System.getProperty("java.version");
}
public static String javafxVersion() {
return System.getProperty("javafx.version");
}
}

View File

@@ -0,0 +1,4 @@
module com.calebfontenot.mp5.files_calebfontenot{
requires javafx.controls;
exports com.calebfontenot.mp5.files_calebfontenot;
}