finish assignment 3

This commit is contained in:
SowinskiBraeden committed 2025-04-07 17:42:36 -07:00
1 parent 26343de294
commit 49dc7cd326
7 files changed
+272 -49

No files matched your search

@@ -3,14 +3,14 @@ package ca.bcit.comp1510.assignment3.q2;
import java.time.LocalDate;
import java.util.List;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class TestTimeSheet {
class TestTimesheet {
@Test
void testGetEmpNum() {
TimeSheet sheet = new TimeSheet(
Timesheet sheet = new Timesheet(
"984757",
null
);
@@ -19,8 +19,11 @@ class TestTimeSheet {
@Test
void testGetEndWeek() {
LocalDate now = LocalDate.now();
TimeSheet sheet = new TimeSheet(
final int year = 2025;
final int month = 4;
final int day = 4;
LocalDate now = LocalDate.of(year, month, day);
Timesheet sheet = new Timesheet(
"",
now
);
@@ -29,7 +32,7 @@ class TestTimeSheet {
@Test
void testGetDetails() {
TimeSheet sheet = new TimeSheet("123", null);
Timesheet sheet = new Timesheet("123", null);
final float hours1 = 2;
final float hours2 = 32 / 10;
@@ -37,19 +40,19 @@ class TestTimeSheet {
final int id3 = 3;
TimeSheetRow r1 = new TimeSheetRow(
TimesheetRow r1 = new TimesheetRow(
1,
"assignment1",
hours1, hours2, hours3
);
TimeSheetRow r2 = new TimeSheetRow(
TimesheetRow r2 = new TimesheetRow(
2,
"assignment2",
hours3, hours2, hours1
);
TimeSheetRow r3 = new TimeSheetRow(
TimesheetRow r3 = new TimesheetRow(
id3,
"assignment3",
hours2, hours3, hours1
@@ -59,7 +62,7 @@ class TestTimeSheet {
sheet.addRow(r2);
sheet.addRow(r3);
List<TimeSheetRow> correct = new ArrayList<TimeSheetRow>();
List<TimesheetRow> correct = new ArrayList<TimesheetRow>();
correct.add(r1);
correct.add(r2);
correct.add(r3);
@@ -69,7 +72,7 @@ class TestTimeSheet {
@Test
void testSetEmpNum() {
TimeSheet sheet = new TimeSheet(
Timesheet sheet = new Timesheet(
"",
null
);
@@ -80,11 +83,14 @@ class TestTimeSheet {
@Test
void testSetEndWeek() {
TimeSheet sheet = new TimeSheet(
Timesheet sheet = new Timesheet(
"",
null
);
LocalDate now = LocalDate.now();
final int year = 2025;
final int month = 4;
final int day = 4;
LocalDate now = LocalDate.of(year, month, day);
sheet.setEndWeek(now);
@@ -93,13 +99,13 @@ class TestTimeSheet {
@Test
void testToString() {
TimeSheet sheet = new TimeSheet("123", null);
Timesheet sheet = new Timesheet("123", null);
final float hours1 = 2;
final float hours2 = 32 / 10;
final float hours3 = 38 / 10;
TimeSheetRow r1 = new TimeSheetRow(
TimesheetRow r1 = new TimesheetRow(
1,
"assignment1",
hours1, hours2, hours3
@@ -114,13 +120,13 @@ class TestTimeSheet {
@Test
void testAddRow() {
TimeSheet sheet = new TimeSheet("123", null);
Timesheet sheet = new Timesheet("123", null);
final float hours1 = 2;
final float hours2 = 32 / 10;
final float hours3 = 38 / 10;
TimeSheetRow r1 = new TimeSheetRow(
TimesheetRow r1 = new TimesheetRow(
1,
"assignment1",
hours1, hours2, hours3
@@ -128,7 +134,7 @@ class TestTimeSheet {
sheet.addRow(r1);
List<TimeSheetRow> correct = new ArrayList<TimeSheetRow>();
List<TimesheetRow> correct = new ArrayList<TimesheetRow>();
correct.add(r1);
assertEquals(sheet.getDetails(), correct);
@@ -1,17 +1,18 @@
package ca.bcit.comp1510.assignment3.q2;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class TestTimeSheetRow {
class TestTimesheetRow {
@Test
void testEmptyConstructor() {
TimeSheetRow row = new TimeSheetRow();
TimesheetRow row = new TimesheetRow();
boolean c1 = row.getProject() == 0;
boolean c2 = row.getWorkPackage() == "";
boolean c2 = row.getWorkPackage().equals("");
boolean c3 = row.getHours() == 0;
assertTrue(c1 && c2 && c3);
@@ -22,7 +23,7 @@ class TestTimeSheetRow {
final int id = 984757;
final String pack = "abc2xyz";
TimeSheetRow row = new TimeSheetRow(
TimesheetRow row = new TimesheetRow(
id,
pack,
0
@@ -39,7 +40,7 @@ class TestTimeSheetRow {
final int id = 984757;
final String pack = "abc2xyz";
TimeSheetRow row = new TimeSheetRow(
TimesheetRow row = new TimesheetRow(
id,
pack,
0
@@ -53,7 +54,7 @@ class TestTimeSheetRow {
final int id = 984757;
final String pack = "abc2xyz";
TimeSheetRow row = new TimeSheetRow(
TimesheetRow row = new TimesheetRow(
id,
pack,
0
@@ -71,7 +72,7 @@ class TestTimeSheetRow {
final float hours2 = 32 / 10;
final float hours3 = 38 / 10;
TimeSheetRow row = new TimeSheetRow(
TimesheetRow row = new TimesheetRow(
id,
pack,
hours1, hours2, hours3
@@ -84,7 +85,7 @@ class TestTimeSheetRow {
@Test
void testSetProject() {
TimeSheetRow row = new TimeSheetRow();
TimesheetRow row = new TimesheetRow();
final int id = 984757;
@@ -95,7 +96,7 @@ class TestTimeSheetRow {
@Test
void testSetWorkPackage() {
TimeSheetRow row = new TimeSheetRow();
TimesheetRow row = new TimesheetRow();
final String pack = "abc2xyz";
@@ -106,7 +107,7 @@ class TestTimeSheetRow {
@Test
void testSetHours() {
TimeSheetRow row = new TimeSheetRow();
TimesheetRow row = new TimesheetRow();
final long hours = 12345678;
@@ -124,7 +125,7 @@ class TestTimeSheetRow {
final float hours2 = 32 / 10;
final float hours3 = 38 / 10;
TimeSheetRow row = new TimeSheetRow(
TimesheetRow row = new TimesheetRow(
id,
pack,
hours1, hours2, hours3
@@ -145,7 +146,7 @@ class TestTimeSheetRow {
final float hours2 = 32 / 10;
final float hours3 = 38 / 10;
TimeSheetRow row = new TimeSheetRow(
TimesheetRow row = new TimesheetRow(
id,
pack,
hours1, hours2, hours3
@@ -163,7 +164,7 @@ class TestTimeSheetRow {
final float hours2 = 32 / 10;
final float hours3 = 38 / 10;
TimeSheetRow row = new TimeSheetRow(
TimesheetRow row = new TimesheetRow(
id,
pack,
hours1, hours2, hours3
@@ -6,11 +6,12 @@ import java.util.List;
import java.util.ArrayList;
/**
* TimeSheet data.
* TimeSheet stores the amount of hours worked
* during a given week period for an employee.
* @author Braeden Sowinski
* @version 1.0.0
*/
public class TimeSheet {
public class Timesheet {
/** empNum is employee number. */
private String empNum;
@@ -19,15 +20,15 @@ public class TimeSheet {
private LocalDate endWeek;
/** details of TimeSheetRows. */
private List<TimeSheetRow> details;
private List<TimesheetRow> details;
/**
* TimeSheet no-argument constructor.
*/
public TimeSheet() {
public Timesheet() {
this.empNum = "";
this.endWeek = null;
this.details = new ArrayList<TimeSheetRow>();
this.details = new ArrayList<TimesheetRow>();
}
/**
@@ -35,10 +36,10 @@ public class TimeSheet {
* @param empNum string of employee number
* @param endWeek date
*/
public TimeSheet(String empNum, LocalDate endWeek) {
public Timesheet(String empNum, LocalDate endWeek) {
this.empNum = empNum;
this.endWeek = endWeek;
this.details = new ArrayList<TimeSheetRow>();
this.details = new ArrayList<TimesheetRow>();
}
/**
@@ -61,7 +62,7 @@ public class TimeSheet {
* getDetails.
* @return details List of TimeSheetRow
*/
public List<TimeSheetRow> getDetails() {
public List<TimesheetRow> getDetails() {
return this.details;
}
@@ -103,7 +104,7 @@ public class TimeSheet {
* addRow to details array.
* @param row TimeSheetRow
*/
public void addRow(TimeSheetRow row) {
public void addRow(TimesheetRow row) {
this.details.add(row);
}
@@ -112,7 +113,7 @@ public class TimeSheet {
* @param args unused
*/
public static void main(String[] args) {
TimeSheet sheet = new TimeSheet();
Timesheet sheet = new Timesheet();
final float hours1 = 2;
final float hours2 = 32 / 10;
@@ -120,19 +121,19 @@ public class TimeSheet {
final int id3 = 3;
sheet.addRow(new TimeSheetRow(
sheet.addRow(new TimesheetRow(
1,
"assignment1",
hours1, hours2, hours3
));
sheet.addRow(new TimeSheetRow(
sheet.addRow(new TimesheetRow(
2,
"assignment2",
hours3, hours2, hours1
));
sheet.addRow(new TimeSheetRow(
sheet.addRow(new TimesheetRow(
id3,
"assignment3",
hours2, hours3, hours1
@@ -1,11 +1,12 @@
package ca.bcit.comp1510.assignment3.q2;
/**
* TimeSheetRow is a row of TimeSheet.
* TimeSheetRow contains hourly information
* for a given week.
* @author Braeden Sowinski
* @version 1.0.0
*/
public class TimeSheetRow {
public class TimesheetRow {
/** MASK of bits for days of week represented inside hours long. */
private static final long[] MASK = {
@@ -43,7 +44,7 @@ public class TimeSheetRow {
/**
* TimeSheetRow no-argument constructor.
*/
public TimeSheetRow() {
public TimesheetRow() {
this.project = 0;
this.workPackage = "";
this.hours = 0;
@@ -55,7 +56,7 @@ public class TimeSheetRow {
* @param workPackage String
* @param hours float...
*/
public TimeSheetRow(int projectNumber, String workPackage, float... hours) {
public TimesheetRow(int projectNumber, String workPackage, float... hours) {
this.project = projectNumber;
this.workPackage = workPackage;
@@ -0,0 +1,106 @@
package ca.bcit.comp1510.assignment3.q3;
/**
* MIXChar represents a MIX Character.
* @author Braeden Sowinski
* @version 1.0.0
*/
public class MIXChar {
/** MIX character array. */
public static final char[] CHARS = {
' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'Δ', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'Σ', 'Π', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '.', ',', '(', ')', '+', '-', '*', '/',
'=', '$', '<', '>', '@', ';', ':', '\''
};
/** character. */
private final int value;
/**
* MixChar constructor.
* @param c character
*/
public MIXChar(char c) throws IllegalArgumentException {
if (!isMIXChar(c)) {
throw new IllegalArgumentException(
"Invalid MIX Character provided."
);
}
this.value = c;
}
/**
* isMIXChar check if a given character
* is a valid MIX character.
* @param c char to check
* @return boolean
*/
static boolean isMIXChar(char c) {
for (int i = 0; i < CHARS.length; i++) {
if (CHARS[i] == c) {
return true;
}
}
return false;
}
/**
* toChar converts this MIXChar character
* to corresponding Java char.
* @return character
*/
public char toChar() {
return (char) this.value;
}
/**
* convert MIXChar array to string.
* @param c MIXChar array.
* @return string
*/
static String toString(MIXChar[] c) {
char[] chars = new char[c.length];
for (int i = 0; i < c.length; i++) {
chars[i] = (char) c[i].ordinal();
}
return new String(chars);
}
/**
* toMIXChar converts string to MIXChar array.
* @param s string to convert.
* @return MIXChar array
*/
static MIXChar[] toMIXChar(String s) {
MIXChar[] mix = new MIXChar[s.length()];
for (int i = 0; i < mix.length; i++) {
mix[i] = new MIXChar(s.charAt(i));
}
return mix;
}
/**
* ordinal returns numeric valid of character.
* @return numeric value
*/
public int ordinal() {
for (int i = 0; i < CHARS.length; i++) {
if (this.value == CHARS[i]) {
return i;
}
}
return -1;
}
/**
* toString converts MIXChar to
* corresponding Java char as a String.
* @return mix char as string.
*/
public String toString() {
return "" + toChar();
}
}
@@ -0,0 +1,103 @@
package ca.bcit.comp1510.assignment3.q3;
/**
* Message of MIXChars.
* @author Braeden Sowinski
* @version 1.0.0
*/
public class Message {
/** maximum number of chars stored per segment. */
public static final int MAXCHARPERSEGMENT = 11;
/** base of MIX character set 0 to 55. */
public static final int BASE = 56;
/** segments of a MIXChar[] where each long holds 11 MIXChar characters. */
private long[] segments;
/** number of characters stored in segments. */
private int numChars;
/**
* Message constructor.
* @param m message to store
*/
public Message(MIXChar[] m) {
int segmentsLength = (int) Math.ceil(m.length / MAXCHARPERSEGMENT);
this.segments = new long[segmentsLength];
generateSegments(m);
}
/**
* Message constructor.
* @param s message as string
*/
public Message(String s) {
MIXChar[] m = MIXChar.toMIXChar(s);
int segmentsLength = (int) Math.ceil(m.length / MAXCHARPERSEGMENT);
this.segments = new long[segmentsLength];
generateSegments(m);
}
/**
* generateSegments loads MIXChar values into
* segments.
* @param m MIXChar[]
*/
private void generateSegments(MIXChar[] m) {
this.segments = m.length % MAXCHARPERSEGMENT == 0
? new long[(m.length / MAXCHARPERSEGMENT)]
: new long[(m.length / MAXCHARPERSEGMENT) + 1];
for (int i = 0; i < this.segments.length; i++) {
for (
int j = 0;
j < MAXCHARPERSEGMENT && this.numChars < m.length;
j++
) {
MIXChar c = m[this.numChars];
long value = c.ordinal();
value *= (long) Math.pow(BASE, j);
this.segments[i] += value;
this.numChars++;
}
}
}
/**
* toString returns string message.
* @return string
*/
public String toString() {
String message = "";
for (int i = 0; i < this.segments.length; i++) {
long segment = this.segments[i];
while (segment != 0) {
int value = (int) Long.remainderUnsigned(
segment,
BASE
);
char c = MIXChar.CHARS[value];
message += c;
segment = Long.divideUnsigned(segment, BASE);
}
}
return message;
}
/**
* toLongs shows encoded longs.
* @return longs string
*/
public String toLongs() {
String longs = "";
for (int i = 0; i < this.segments.length; i++) {
longs += Long.toUnsignedString(this.segments[i]) + " ";
}
return longs;
}
}
@@ -0,0 +1,5 @@
package ca.bcit.comp1510.assignment3.q3;
public class TestMixChar {
}