diff --git a/.gitignore b/.gitignore index feb4ffd..89bd4a3 100644 --- a/.gitignore +++ b/.gitignore @@ -152,3 +152,4 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk +data/ diff --git a/.idea/modules/CPS121SkillsAssessment.main.iml b/.idea/modules/CPS121SkillsAssessment.main.iml index 549cdd9..80c8852 100644 --- a/.idea/modules/CPS121SkillsAssessment.main.iml +++ b/.idea/modules/CPS121SkillsAssessment.main.iml @@ -1,8 +1,13 @@ - + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1ca9c0d..f6ca7ac 100644 --- a/build.gradle +++ b/build.gradle @@ -10,6 +10,8 @@ repositories { } dependencies { + implementation 'com.google.code.gson:gson:2.10.1' + implementation 'com.miglayout:miglayout:3.7.4' } jar { diff --git a/src/main/java/tech/nevets/Debug.java b/src/main/java/tech/nevets/Debug.java new file mode 100644 index 0000000..a46af6b --- /dev/null +++ b/src/main/java/tech/nevets/Debug.java @@ -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); + + } +} diff --git a/src/main/java/tech/nevets/STDieArguments.java b/src/main/java/tech/nevets/STDieArguments.java new file mode 100644 index 0000000..0383204 --- /dev/null +++ b/src/main/java/tech/nevets/STDieArguments.java @@ -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()); + } +} \ No newline at end of file diff --git a/src/main/java/tech/nevets/STEmployeeHours.java b/src/main/java/tech/nevets/STEmployeeHours.java new file mode 100644 index 0000000..9bc7096 --- /dev/null +++ b/src/main/java/tech/nevets/STEmployeeHours.java @@ -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 employeeList = new ArrayList<>(); + private static final DataUtils EMPLOYEE_DATA_UTILS = new DataUtils<>(Employee.class); + private static final CastUtils 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"); + } + } +} diff --git a/src/main/java/tech/nevets/STMileageD.java b/src/main/java/tech/nevets/STMileageD.java new file mode 100644 index 0000000..3e8121a --- /dev/null +++ b/src/main/java/tech/nevets/STMileageD.java @@ -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); + } +} diff --git a/src/main/java/tech/nevets/STMileageS.java b/src/main/java/tech/nevets/STMileageS.java new file mode 100644 index 0000000..91104c2 --- /dev/null +++ b/src/main/java/tech/nevets/STMileageS.java @@ -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); + } +} diff --git a/src/main/java/tech/nevets/STValueReturn.java b/src/main/java/tech/nevets/STValueReturn.java new file mode 100644 index 0000000..18bc21f --- /dev/null +++ b/src/main/java/tech/nevets/STValueReturn.java @@ -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; + } +} diff --git a/src/main/java/tech/nevets/c5t9/Main.java b/src/main/java/tech/nevets/c5t9/Main.java new file mode 100644 index 0000000..d744591 --- /dev/null +++ b/src/main/java/tech/nevets/c5t9/Main.java @@ -0,0 +1,7 @@ +package tech.nevets.c5t9; + +public class Main { + public static void main(String[] args) { + StringBuilder sb = new StringBuilder(); + } +} diff --git a/src/main/java/tech/nevets/util/CastUtils.java b/src/main/java/tech/nevets/util/CastUtils.java new file mode 100644 index 0000000..40edfb7 --- /dev/null +++ b/src/main/java/tech/nevets/util/CastUtils.java @@ -0,0 +1,24 @@ +package tech.nevets.util; + +import java.lang.reflect.Array; + +@SuppressWarnings("unchecked") +public class CastUtils { + private Class clazz; + + public CastUtils(Class 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; + } +} diff --git a/src/main/java/tech/nevets/util/DataUtils.java b/src/main/java/tech/nevets/util/DataUtils.java new file mode 100644 index 0000000..1ac81d5 --- /dev/null +++ b/src/main/java/tech/nevets/util/DataUtils.java @@ -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 { + private Class clazz; + private final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + public DataUtils(Class clazz) { + this.clazz = clazz; + File dataDir = new File("./data/"); + if (!dataDir.exists()) { + dataDir.mkdirs(); + } + } + + public void saveToDataArray(T[] array, String fileName, Class 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; + } + } +} diff --git a/src/main/java/tech/nevets/util/Die.java b/src/main/java/tech/nevets/util/Die.java new file mode 100644 index 0000000..41c94a2 --- /dev/null +++ b/src/main/java/tech/nevets/util/Die.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/tech/nevets/util/Employee.java b/src/main/java/tech/nevets/util/Employee.java new file mode 100644 index 0000000..5798f83 --- /dev/null +++ b/src/main/java/tech/nevets/util/Employee.java @@ -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 + " }"; + } +}