Push all
This commit is contained in:
parent
05f4c98fbe
commit
1df8ec14a8
1
.gitignore
vendored
1
.gitignore
vendored
@ -152,3 +152,4 @@ $RECYCLE.BIN/
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
data/
|
||||
|
@ -1,8 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$/../../src/main">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../src/main/java" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="temurin-18" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Gradle: com.google.code.gson:gson:2.10.1" level="project" />
|
||||
<orderEntry type="library" name="Gradle: com.miglayout:miglayout:3.7.4" level="project" />
|
||||
</component>
|
||||
</module>
|
@ -10,6 +10,8 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.google.code.gson:gson:2.10.1'
|
||||
implementation 'com.miglayout:miglayout:3.7.4'
|
||||
}
|
||||
|
||||
jar {
|
||||
|
14
src/main/java/tech/nevets/Debug.java
Normal file
14
src/main/java/tech/nevets/Debug.java
Normal file
@ -0,0 +1,14 @@
|
||||
package tech.nevets;
|
||||
|
||||
import tech.nevets.util.DataUtils;
|
||||
import tech.nevets.util.Employee;
|
||||
|
||||
public class Debug {
|
||||
static Employee e = new Employee(100, "Steven", "Tracey");
|
||||
static Employee[] arr = new Employee[]{e};
|
||||
|
||||
public static void main(String[] args) {
|
||||
new DataUtils<>(Employee.class).saveToDataArray(arr, "EmployeeData", Employee[].class);
|
||||
|
||||
}
|
||||
}
|
21
src/main/java/tech/nevets/STDieArguments.java
Normal file
21
src/main/java/tech/nevets/STDieArguments.java
Normal file
@ -0,0 +1,21 @@
|
||||
package tech.nevets;
|
||||
|
||||
import tech.nevets.util.Die;
|
||||
|
||||
public class STDieArguments {
|
||||
public static void main(String[] args) {
|
||||
final int SIX_SIDES = 6;
|
||||
final int TWENTY_SIDES = 20;
|
||||
|
||||
Die sixDie = new Die(SIX_SIDES);
|
||||
Die twentyDie = new Die(TWENTY_SIDES);
|
||||
|
||||
rollDie(sixDie);
|
||||
rollDie(twentyDie);
|
||||
}
|
||||
|
||||
public static void rollDie(Die d) {
|
||||
System.out.println("Rolling a " + d.getSides() + " sided die.");
|
||||
System.out.println("The die's value: " + d.roll());
|
||||
}
|
||||
}
|
193
src/main/java/tech/nevets/STEmployeeHours.java
Normal file
193
src/main/java/tech/nevets/STEmployeeHours.java
Normal file
@ -0,0 +1,193 @@
|
||||
package tech.nevets;
|
||||
|
||||
import net.miginfocom.swing.MigLayout;
|
||||
import tech.nevets.util.CastUtils;
|
||||
import tech.nevets.util.DataUtils;
|
||||
import tech.nevets.util.Employee;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class STEmployeeHours extends JFrame {
|
||||
|
||||
private JPanel contentPane;
|
||||
private static final ArrayList<Employee> employeeList = new ArrayList<>();
|
||||
private static final DataUtils<Employee> EMPLOYEE_DATA_UTILS = new DataUtils<>(Employee.class);
|
||||
private static final CastUtils<Employee> EMPLOYEE_CAST_UTILS = new CastUtils<>(Employee.class);
|
||||
|
||||
JLabel idLabel;
|
||||
JTextField idField;
|
||||
JButton submitEmpId;
|
||||
JLabel empFirstName;
|
||||
JLabel empLastName;
|
||||
JLabel empHours;
|
||||
|
||||
JLabel addHoursLabel;
|
||||
JTextField addHoursField;
|
||||
JButton addHoursButton;
|
||||
|
||||
public STEmployeeHours() {
|
||||
contentPane = new JPanel();
|
||||
employeeList.clear();
|
||||
employeeList.addAll(List.of(EMPLOYEE_CAST_UTILS.castArray(EMPLOYEE_DATA_UTILS.getArrayFromData("EmployeeData"))));
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
EMPLOYEE_DATA_UTILS.saveToDataArray(EMPLOYEE_CAST_UTILS.castArray(employeeList.toArray()), "EmployeeData", Employee[].class);
|
||||
}
|
||||
});
|
||||
setContentPane(contentPane);
|
||||
contentPane.setBorder(new EmptyBorder(1, 1, 1, 1));
|
||||
MigLayout layout = new MigLayout(
|
||||
"",
|
||||
"[][fill][]",
|
||||
"[][][][][][]"
|
||||
);
|
||||
contentPane.setLayout(layout);
|
||||
|
||||
idLabel = new JLabel("Employee Id:");
|
||||
contentPane.add(idLabel, "cell 0 0");
|
||||
idField = new JTextField();
|
||||
contentPane.add(idField, "cell 1 0,w 150");
|
||||
|
||||
AtomicInteger empIdCache = new AtomicInteger();
|
||||
submitEmpId = new JButton("Submit");
|
||||
submitEmpId.addActionListener(al -> {
|
||||
empIdCache.set(Integer.parseInt(idField.getText()));
|
||||
boolean isFound = false;
|
||||
for (Employee e : employeeList) {
|
||||
if (idField.getText().equals(String.valueOf(e.getEmployeeId()))) {
|
||||
isFound = true;
|
||||
idLabel.setText("Employee Id:");
|
||||
updateUI(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!isFound) {
|
||||
idLabel.setText("User Not Found");
|
||||
}
|
||||
});
|
||||
contentPane.add(submitEmpId, "cell 2 0");
|
||||
|
||||
empFirstName = new JLabel("First Name: ");
|
||||
contentPane.add(empFirstName, "cell 1 1");
|
||||
empLastName = new JLabel("Last Name: ");
|
||||
contentPane.add(empLastName, "cell 1 2");
|
||||
empHours = new JLabel("Total Hours: ");
|
||||
contentPane.add(empHours, "cell 1 3");
|
||||
|
||||
addHoursLabel = new JLabel("Hours To Add:");
|
||||
contentPane.add(addHoursLabel, "cell 0 4");
|
||||
addHoursField = new JTextField();
|
||||
contentPane.add(addHoursField, "cell 1 4");
|
||||
addHoursButton = new JButton("Add");
|
||||
addHoursButton.addActionListener(al -> {
|
||||
updateEmpHours(empIdCache.get(), Integer.parseInt(addHoursField.getText()));
|
||||
updateUI(empIdCache.get());
|
||||
addHoursField.setText("");
|
||||
});
|
||||
contentPane.add(addHoursButton, "cell 2 4,grow");
|
||||
|
||||
JButton newUserBtn = new JButton("New User");
|
||||
newUserBtn.addActionListener(al -> {
|
||||
AddUserFrame newUserFrame = new AddUserFrame();
|
||||
newUserFrame.setLocationRelativeTo(null);
|
||||
newUserFrame.pack();
|
||||
newUserFrame.setVisible(true);
|
||||
});
|
||||
contentPane.add(newUserBtn, "cell 1 5,grow");
|
||||
}
|
||||
|
||||
private void updateUI(Employee e) {
|
||||
empFirstName.setText("First Name: " + e.getEmployeeFirstName());
|
||||
empLastName.setText("Last Name: " + e.getEmployeeLastName());
|
||||
empHours.setText("Total Hours: " + e.getEmployeeHours());
|
||||
}
|
||||
|
||||
private void updateUI(int empId) {
|
||||
for (Employee e : employeeList) {
|
||||
if (e.getEmployeeId() == empId) {
|
||||
empFirstName.setText("First Name: " + e.getEmployeeFirstName());
|
||||
empLastName.setText("Last Name: " + e.getEmployeeLastName());
|
||||
empHours.setText("Total Hours: " + e.getEmployeeHours());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateEmpHours(int id, int hoursToAdd) {
|
||||
for (Employee e : employeeList) {
|
||||
if (e.getEmployeeId() == id) {
|
||||
e.addHours(hoursToAdd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
STEmployeeHours frame = new STEmployeeHours();
|
||||
frame.setBounds(0, 0, 860, 480);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
|
||||
}
|
||||
|
||||
private class AddUserFrame extends JFrame {
|
||||
public AddUserFrame() {
|
||||
setResizable(false);
|
||||
JPanel auContentPane = new JPanel();
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
setContentPane(auContentPane);
|
||||
auContentPane.setBorder(new EmptyBorder(1, 1, 1, 1));
|
||||
MigLayout layout = new MigLayout(
|
||||
"",
|
||||
"[][fill][]",
|
||||
"[][][][][][]"
|
||||
);
|
||||
auContentPane.setLayout(layout);
|
||||
|
||||
JLabel errorLabel = new JLabel("");
|
||||
auContentPane.add(errorLabel, "cell 0 5");
|
||||
|
||||
JLabel idLabel = new JLabel("User Id:");
|
||||
auContentPane.add(idLabel, "cell 0 1");
|
||||
JTextField idField = new JTextField();
|
||||
auContentPane.add(idField, "cell 1 1");
|
||||
JLabel firstNameLabel = new JLabel("First Name:");
|
||||
auContentPane.add(firstNameLabel, "cell 0 2");
|
||||
JTextField firstNameField = new JTextField();
|
||||
auContentPane.add(firstNameField, "cell 1 2");
|
||||
JLabel lastNameLabel = new JLabel("Last Name:");
|
||||
auContentPane.add(lastNameLabel, "cell 0 3");
|
||||
JTextField lastNameField = new JTextField();
|
||||
auContentPane.add(lastNameField, "cell 1 3");
|
||||
JButton submitButton = new JButton("Create");
|
||||
submitButton.addActionListener(al -> {
|
||||
boolean idExists = false;
|
||||
for (Employee e : employeeList) {
|
||||
if (e.getEmployeeId() == Integer.parseInt(idField.getText())) {
|
||||
errorLabel.setText("User with that id already exists");
|
||||
idExists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!idExists) {
|
||||
Employee e = new Employee(Integer.parseInt(idField.getText()), firstNameField.getText(), lastNameField.getText());
|
||||
employeeList.add(e);
|
||||
this.dispose();
|
||||
}
|
||||
});
|
||||
auContentPane.add(submitButton, "cell 1 4");
|
||||
JButton cancelButton = new JButton("Cancel");
|
||||
cancelButton.addActionListener(al -> {
|
||||
this.dispose();
|
||||
});
|
||||
auContentPane.add(cancelButton, "cell 2 4");
|
||||
}
|
||||
}
|
||||
}
|
15
src/main/java/tech/nevets/STMileageD.java
Normal file
15
src/main/java/tech/nevets/STMileageD.java
Normal file
@ -0,0 +1,15 @@
|
||||
package tech.nevets;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class STMileageD {
|
||||
public static void main(String[] args) {
|
||||
JOptionPane.showMessageDialog(null, "This program will calculate mileage");
|
||||
int miles = Integer.parseInt(JOptionPane.showInputDialog("Enter the miles driven"));
|
||||
int gallons = Integer.parseInt(JOptionPane.showInputDialog("Enter the gallons used"));
|
||||
float mpg = (float) miles / (float) gallons;
|
||||
JOptionPane.showMessageDialog(null, mpg + " miles per gallon");
|
||||
String mpgFormatted = String.format("%.2f miles per gallon", mpg);
|
||||
JOptionPane.showMessageDialog(null, mpgFormatted);
|
||||
}
|
||||
}
|
18
src/main/java/tech/nevets/STMileageS.java
Normal file
18
src/main/java/tech/nevets/STMileageS.java
Normal file
@ -0,0 +1,18 @@
|
||||
package tech.nevets;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class STMileageS {
|
||||
private static Scanner sc = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("This program will calculate mileage");
|
||||
System.out.print("Enter the miles driven: ");
|
||||
int miles = sc.nextInt();
|
||||
System.out.print("Enter the gallons used: ");
|
||||
int gallons = sc.nextInt();
|
||||
float mpg = (float) miles / (float) gallons;
|
||||
System.out.println(mpg + " miles per gallon");
|
||||
System.out.printf("%.2f miles per gallon", mpg);
|
||||
}
|
||||
}
|
26
src/main/java/tech/nevets/STValueReturn.java
Normal file
26
src/main/java/tech/nevets/STValueReturn.java
Normal file
@ -0,0 +1,26 @@
|
||||
package tech.nevets;
|
||||
|
||||
public class STValueReturn {
|
||||
public static void main(String[] args) {
|
||||
int total;
|
||||
int value1 = 20;
|
||||
int value2 = 40;
|
||||
|
||||
total = sum(value1, value2);
|
||||
|
||||
System.out.println("The sum of " + value1 + " and " + value2 + " is " + total);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of the 2 passed parameters
|
||||
* @param num1 the first number to add
|
||||
* @param num2 the second number to add
|
||||
* @return the sum of the 2 passed numbers
|
||||
*/
|
||||
public static int sum(int num1, int num2) {
|
||||
int result;
|
||||
result = num1 + num2;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
7
src/main/java/tech/nevets/c5t9/Main.java
Normal file
7
src/main/java/tech/nevets/c5t9/Main.java
Normal file
@ -0,0 +1,7 @@
|
||||
package tech.nevets.c5t9;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
}
|
||||
}
|
24
src/main/java/tech/nevets/util/CastUtils.java
Normal file
24
src/main/java/tech/nevets/util/CastUtils.java
Normal file
@ -0,0 +1,24 @@
|
||||
package tech.nevets.util;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class CastUtils<T> {
|
||||
private Class<T> clazz;
|
||||
|
||||
public CastUtils(Class<T> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public T[] castArray(Object[] array) {
|
||||
T[] newArray = (T[]) Array.newInstance(clazz, array.length);
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
newArray[i] = (T) array[i];
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
public T castObject(Object obj) {
|
||||
return (T) obj;
|
||||
}
|
||||
}
|
78
src/main/java/tech/nevets/util/DataUtils.java
Normal file
78
src/main/java/tech/nevets/util/DataUtils.java
Normal file
@ -0,0 +1,78 @@
|
||||
package tech.nevets.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class DataUtils<T> {
|
||||
private Class<T> clazz;
|
||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
public DataUtils(Class<T> clazz) {
|
||||
this.clazz = clazz;
|
||||
File dataDir = new File("./data/");
|
||||
if (!dataDir.exists()) {
|
||||
dataDir.mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
public void saveToDataArray(T[] array, String fileName, Class<T[]> clazzLocal) {
|
||||
String json = gson.toJson(array, clazzLocal);
|
||||
writeJsonToFile(json, fileName);
|
||||
}
|
||||
|
||||
public void saveToDataObject(T data, String fileName) {
|
||||
String json = gson.toJson(data, clazz);
|
||||
writeJsonToFile(json, fileName);
|
||||
}
|
||||
|
||||
private void writeJsonToFile(String json, String fileName) {
|
||||
try {
|
||||
FileWriter fw = new FileWriter("./data/" + fileName + ".json");
|
||||
fw.write(json);
|
||||
fw.flush();
|
||||
fw.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public T[] getArrayFromData(String fileName) {
|
||||
File file = new File("./data/" + fileName + ".json");
|
||||
JsonArray ja;
|
||||
try {
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||
ja = gson.fromJson(br, JsonArray.class);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
if (ja == null) {
|
||||
ja = new JsonArray();
|
||||
}
|
||||
|
||||
Object[] dataArray = new Object[ja.size()];
|
||||
|
||||
for (int i = 0; i < ja.size(); i++) {
|
||||
dataArray[i] = gson.fromJson(ja.get(i).getAsJsonObject(), clazz);
|
||||
}
|
||||
|
||||
return new CastUtils<>(clazz).castArray(dataArray);
|
||||
}
|
||||
|
||||
public T getObjectFromData(String fileName) {
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader("./data/" + fileName + ".json"));
|
||||
return gson.fromJson(br, clazz);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
22
src/main/java/tech/nevets/util/Die.java
Normal file
22
src/main/java/tech/nevets/util/Die.java
Normal file
@ -0,0 +1,22 @@
|
||||
package tech.nevets.util;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class Die {
|
||||
private final int sides;
|
||||
private final Random r;
|
||||
|
||||
public Die(int sides) {
|
||||
this.sides = sides;
|
||||
r = ThreadLocalRandom.current();
|
||||
}
|
||||
|
||||
public int roll() {
|
||||
return (r.nextInt(sides - 1) + 1);
|
||||
}
|
||||
|
||||
public int getSides() {
|
||||
return sides;
|
||||
}
|
||||
}
|
40
src/main/java/tech/nevets/util/Employee.java
Normal file
40
src/main/java/tech/nevets/util/Employee.java
Normal file
@ -0,0 +1,40 @@
|
||||
package tech.nevets.util;
|
||||
|
||||
public class Employee {
|
||||
private final int employeeId;
|
||||
private final String employeeFirstName;
|
||||
private final String employeeLastName;
|
||||
private int employeeHours;
|
||||
|
||||
public Employee(int employeeId, String employeeFirstName, String employeeLastName) {
|
||||
this.employeeId = employeeId;
|
||||
this.employeeFirstName = employeeFirstName;
|
||||
this.employeeLastName = employeeLastName;
|
||||
employeeHours = 0;
|
||||
}
|
||||
|
||||
public int getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public String getEmployeeFirstName() {
|
||||
return employeeFirstName;
|
||||
}
|
||||
|
||||
public String getEmployeeLastName() {
|
||||
return employeeLastName;
|
||||
}
|
||||
|
||||
public int getEmployeeHours() {
|
||||
return employeeHours;
|
||||
}
|
||||
|
||||
public void addHours(int hours) {
|
||||
employeeHours += hours;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{ \"employeeId\":" + employeeId + ", \"employeeFirstName\":\"" + employeeFirstName + "\", \"employeeLastName\":\"" + employeeLastName + "\", \"employeeHours\":" + employeeHours + " }";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user