mirror of
https://github.com/RustyRaptor/CS-272.git
synced 2026-12-03 15:55:22 -07:00
moved files out of fileops package and added new Lab
This commit is contained in:
1 parent
1bad676c57
commit
fdeb6bee53
3 files changed
+120
-187
No files matched your search
@@ -0,0 +1,120 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author ziad
|
||||
*
|
||||
*/
|
||||
public class EmployeeSet {
|
||||
private int employeeCnt;
|
||||
private Employee[] employeeArr;
|
||||
|
||||
public EmployeeSet() {
|
||||
this.employeeCnt = 0;
|
||||
this.employeeArr = new Employee[10];
|
||||
}
|
||||
|
||||
public EmployeeSet(Object obj) {
|
||||
if (obj == null) {
|
||||
return;
|
||||
}
|
||||
if (obj instanceof EmployeeSet) {
|
||||
EmployeeSet emps = (EmployeeSet) obj;
|
||||
employeeCnt = emps.employeeCnt;
|
||||
employeeArr = emps.employeeArr;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (!(obj instanceof EmployeeSet))
|
||||
return false;
|
||||
EmployeeSet other = (EmployeeSet) obj;
|
||||
if (!Arrays.equals(employeeArr, other.employeeArr))
|
||||
return false;
|
||||
if (employeeCnt != other.employeeCnt)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
int size = 0;
|
||||
for (int i = 0; i < this.employeeArr.length; i++) {
|
||||
if (this.employeeArr[i] != null) {
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
public int capacity() {
|
||||
return this.employeeArr.length;
|
||||
}
|
||||
|
||||
public void add(Employee a) {
|
||||
this.employeeArr[this.employeeCnt] = a;
|
||||
}
|
||||
|
||||
public boolean contains(Employee a) {
|
||||
if (a == null) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i <= this.employeeCnt; i++) {
|
||||
if (this.employeeArr[i].equals(a)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean remove(int eno) {
|
||||
if (eno == 0) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i <= this.employeeCnt; i++) {
|
||||
if (this.employeeArr[i].getNo() == eno) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void ensureCapacity(int minimumCapacity) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EmployeeSet [employeeCnt=" + employeeCnt + ", employeeArr=" + Arrays.toString(employeeArr) + "]";
|
||||
}
|
||||
|
||||
public int getEmployeeCnt() {
|
||||
return employeeCnt;
|
||||
}
|
||||
|
||||
public void setEmployeeCnt(int employeeCnt) {
|
||||
this.employeeCnt = employeeCnt;
|
||||
}
|
||||
|
||||
public Employee[] getEmployeeArr() {
|
||||
return employeeArr;
|
||||
}
|
||||
|
||||
public void setEmployeeArr(Employee[] employeeArr) {
|
||||
this.employeeArr = employeeArr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Employee John = new Employee();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,4 @@
|
||||
import java.io.IOException;
|
||||
|
||||
/*
|
||||
* Ziad Arafat
|
||||
* I just imported my strToFile function from the fileops package I created
|
||||
*/
|
||||
|
||||
import fileops.fileops;
|
||||
|
||||
|
||||
public class Welcome {
|
||||
/**
|
||||
* @param args
|
||||
|
||||
@@ -1,178 +0,0 @@
|
||||
package fileops;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
|
||||
/*
|
||||
* Ziad Arafat
|
||||
* Used powerpoint functions as reference
|
||||
* I then created seperate functions to get and store the column names and the data
|
||||
* I used switch statements to only print the specific columns and checked the age
|
||||
* IMPORTANT: Check the README
|
||||
*/
|
||||
public class fileops {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
writeData(readCSV("core_dataset.csv"),
|
||||
readCSVCols("core_dataset.csv"),"young_employee.csv");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Read the data from the CSV and return as a 2D String array.
|
||||
public static String[][] readCSV(String inFname) throws IOException {
|
||||
BufferedReader inputStream = new BufferedReader(new FileReader(inFname));
|
||||
String line;
|
||||
int lineno = 1;
|
||||
String[][] data = new String[302][20];
|
||||
while ((line = inputStream.readLine()) != null) {
|
||||
String[] numbers = line.split(",");
|
||||
if (lineno == 1) {
|
||||
System.out.println();
|
||||
}else {
|
||||
data[lineno-2] = numbers;
|
||||
}
|
||||
lineno++;
|
||||
}
|
||||
inputStream.close();
|
||||
|
||||
return(data);
|
||||
}
|
||||
|
||||
|
||||
// Read only the column labels from the fiile and store in it's own 1D array for later use
|
||||
public static String[] readCSVCols(String inFname) throws IOException {
|
||||
BufferedReader inputStream = new BufferedReader(new FileReader(inFname));
|
||||
String line = inputStream.readLine();
|
||||
String[] cols = new String[25];
|
||||
String[] labels = line.split(",");
|
||||
cols = labels;
|
||||
inputStream.close();
|
||||
return(cols);
|
||||
}
|
||||
|
||||
// Write data from 2D String array containing data and 1D String array containing labels
|
||||
public static void writeData(
|
||||
String[][] inData, String[] inDataLabels, String outFname
|
||||
) throws IOException {
|
||||
PrintWriter outputStream = null;
|
||||
Integer ageInt;
|
||||
String ageStr;
|
||||
|
||||
// print the data labels to the file
|
||||
try {
|
||||
outputStream = new PrintWriter(new FileWriter(outFname));
|
||||
for (int j = 0; j < inDataLabels.length
|
||||
&& (inDataLabels[j] != null); j++) {
|
||||
|
||||
// This switch case is to ensure only the correct column labels are used in the labels array.
|
||||
switch (inDataLabels[j]) {
|
||||
case "Employee Fname":
|
||||
outputStream.printf(inDataLabels[j] + ",");
|
||||
break;
|
||||
case "Employee Lname":
|
||||
outputStream.printf(inDataLabels[j] + ",");
|
||||
break;
|
||||
case "Employee Number":
|
||||
outputStream.printf(inDataLabels[j] + ",");
|
||||
break;
|
||||
case "State":
|
||||
outputStream.printf(inDataLabels[j] + ",");
|
||||
break;
|
||||
case "Zip":
|
||||
outputStream.printf(inDataLabels[j] + ",");
|
||||
break;
|
||||
case "Age":
|
||||
outputStream.printf(inDataLabels[j] + ",");
|
||||
break;
|
||||
case "Sex":
|
||||
outputStream.printf(inDataLabels[j] + ",");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
// print the data to the file
|
||||
outputStream.println();
|
||||
for (int i = 0; (i < inData.length-1) && (inData[i] != null); i++) {
|
||||
// I parse the age value to an integer for checking age.
|
||||
ageStr = new String(inData[i][6]);
|
||||
ageInt = Integer.parseInt(ageStr);
|
||||
if (ageInt <= 30) {
|
||||
for (int j = 0; j < inDataLabels.length
|
||||
&& (inData[i][j] != null); j++) {
|
||||
|
||||
// This switch case is to ensure only the correct column labels are used in the data array.
|
||||
switch (inDataLabels[j]) {
|
||||
case "Employee Fname":
|
||||
System.out.println(inData[i][j]);
|
||||
System.out.println();
|
||||
outputStream.printf(inData[i][j] + ",");
|
||||
break;
|
||||
case "Employee Lname":
|
||||
System.out.println(inData[i][j]);
|
||||
outputStream.printf(inData[i][j] + ",");
|
||||
break;
|
||||
case "Employee Number":
|
||||
System.out.println(inData[i][j]);
|
||||
outputStream.printf(inData[i][j] + ",");
|
||||
break;
|
||||
case "State":
|
||||
System.out.println(inData[i][j]);
|
||||
outputStream.printf(inData[i][j] + ",");
|
||||
break;
|
||||
case "Zip":
|
||||
System.out.println(inData[i][j]);
|
||||
outputStream.printf(inData[i][j] + ",");
|
||||
break;
|
||||
case "Age":
|
||||
System.out.println(inData[i][j]);
|
||||
outputStream.printf(inData[i][j] + ",");
|
||||
break;
|
||||
case "Sex":
|
||||
System.out.println(inData[i][j]);
|
||||
outputStream.printf(inData[i][j] + ",");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
// print a newline when we finish a row
|
||||
outputStream.println();
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
if (outputStream != null) {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// This function is not used here it is for later use. It's a simple function to write strings to a file.
|
||||
public static void strToFile(String inStr, String outFname)
|
||||
throws IOException {
|
||||
String inputStr;
|
||||
PrintWriter outputStream = null;
|
||||
try {
|
||||
inputStr = new String(inStr);
|
||||
outputStream = new PrintWriter(new FileWriter(outFname));
|
||||
outputStream.println(inputStr);
|
||||
} finally {
|
||||
if (outputStream != null) {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user