#Coding assignment for school

1 messages · Page 1 of 1 (latest)

sage galleon
#

boys im kind of fucked here, this assignment is due tomorrow and I have been stuck on the same god damn test case for 3 days now. its not really a specific issue with the code itself and i have checked ALL of the possible issues and it just wont pass the damn test case. if anyone and i mean ANYONE wants to hop in a call and just look at the fucking mess of code i have it would be GREATLY APPRECIATED.

cobalt swanBOT
#

<@&987246584574140416> please have a look, thanks.

sage galleon
#

i can send a screen shot of the question and what the output is but it would be much easier to explain it in a call i think

#

this is the code I have done, this project is being done through a website called moodle so I dont have the csv file needed to run it externally so i have to run it on the website. but the code looks the same

cobalt swanBOT
gritty fulcrum
#

honestly it doesn't seem too bad just needs data parsing

#

But I have a feel you wrote this coding using AI

sage galleon
#

BROTHER WHATTTTT

gritty fulcrum
sage galleon
#

i have spent the past week and a half working on this code dudee im fucking tweaking

#

i wish i could use ai but i dont know what question to ask it in order for it to help me

gritty fulcrum
#

why is everything in 1 class?

#

ah I forgot its moodle

sage galleon
#

its all in one file but there are seperate classes, also on the website moodle they cant make other files, its just one file with seperate classes

gritty fulcrum
#

Yeah we use moodle too mb

sage galleon
#

its confusing to use so ive just been using another IDE and then copy and pasting the code

sage galleon
gritty fulcrum
#

parsing csv file

sage galleon
#

oh okay i just googled it

#

I think i am doing that already

hidden nimbus
#

example:

cobalt swanBOT
#
John Doe 20
Jane Doe 17
Bob Doe 30

In order to read and work with that data, we create a simple Person class:

public class Person {
  private final String firstName;
  private final String lastName;
  private final int age;

  // Constructor, getter omitted 
}

We want to read the file line by line, so we add a method parseLine to the Person class that, given a line, creates the corresponding Person:

public static Person parseLine(String line) {
  String[] data = line.split(" ");

  String firstName = data[0];
  String lastName = data[1];
  int age = Integer.parseLine(data[2]);

  return new Person(firstName, secondName, age);
}

We read the file line by line, call parseLine and collect the resulting people into a List:

// Stream solution
List<Person> persons = Files.lines(Paths.get("myFile.txt"))
  .map(Person::parseLine)
  .collect(Collectors.toList());

// Classic solution
List<String> lines = Files.readAllLines(Paths.get("myFile.txt"));
List<Person> persons = new ArrayList<>();
for (String line : lines) {
  persons.add(Person.parseLine(line));
}

We could now easily work with that data, for example increasing the age of John:

persons.get(0).setAge(25);

For updating the file, we add a toLine() method to Person:

public String toLine() {
  return String.join(" ", firstName, lastName, String.valueOf(age));
}

Lastly, we create a list of the lines we want to write:

List<String> lines = persons.stream()
  .map(Person::toLine)
  .collect(Collectors.toList());

and write them back to the file:

Files.write(Paths.get("myFile.txt"), lines);
hidden nimbus
#

(almost csv but u get the idea)

gritty fulcrum
sage galleon
#

fair

#

theres a lot and its messy

#

all of the data is going where it needs to go but i think that my department list isnt being filled correctly, i just dont know how to fix it or where its going wrong

gritty fulcrum
#

I see some problems

#

you re-init the lists

#

do java if (dpNames == null) { dpNames = new ArrayList<String>(); } if (dpIds == null) { dpIds = new ArrayList<Integer>(); } instead

sage galleon
#

oh man

#

thats really obvious now that im looking at it

#

i knew it was being emptied at some point and i didnt know why

gritty fulcrum
#

I feel you implemented this whole class wrong

sage galleon
#

really?

#

how so

gritty fulcrum
#

You are trying to make a singleton but you arent?

#

I would sort of seperate the logic

sage galleon
#

i dont know what a singleton is

gritty fulcrum
#

Singleton is a class that is only initialized once

#

its fine forget it

sage galleon
#

ohh okay

gritty fulcrum
#

I don't wanna make this more complex for no reason

sage galleon
#

i actually wanna know as much as I can because my lectures are just talking about the same stuff i learned in high school

gritty fulcrum
#

Ok so if this was me

#

I would really just seperate everything

#

Nothing would be static

sage galleon
#

yeah i would too, but the class types are specified unfortunatly

gritty fulcrum
#

oh yeah I forgot that mb

#

💀 this design is so bad they should let students design it

#
    private static ArrayList<Integer> dpIds; // maintains a list of department IDs
    private static ArrayList<String> dpNames; // maintains a list of department names - dpIds.get(i) corresponds to dpNames.get(i)```
#

I would most likely just DI this into classes

sage galleon
#

excuse my ignorance but what does DI stand for

gritty fulcrum
#

Have parameter in each class to accept these ArrayLists

#

Dependency Injection

gritty fulcrum
sage galleon
gritty fulcrum
#

You never write ArrayList<Integer> dpIds

#

Just do List<Integer>

#

But behind scenes it is an ArrayList

sage galleon
#

ah okay

#

any other issues you noticed?

gritty fulcrum
#

1 thing but it is more of a preference

#
public int get_dep_ip() {
  return depId;
}``` is fine
sage galleon
#

i like putting the brackets on the line after because for me it is easier to see where the method starts and ends

gritty fulcrum
#

like I said preference

sage galleon
#

does everything else look fine?

gritty fulcrum
#

I can get slightly brutal if you want

sage galleon
#

go for it

gritty fulcrum
#

I am not gonna say anything on design issues because we are incredibly handicapped by the question itself

sage galleon
#

real

gritty fulcrum
#

are you asked to put these comments

sage galleon
#

yes

gritty fulcrum
#

bruh

#

no one really puts comments like these

#

these are incredibly useless

#

if you really want to put an actual java doc

sage galleon
#

thats just me because in my frantic attempts to fix things, i forgot what other things did

#

and wanted to make sure i didnt for get them

gritty fulcrum
#

I would maybe do

#
/**
 * Increments and returns the next department ID.
 * 
 * @return the next unique department ID
 */
public static int getNextDepID()
{
    dpId++;
    return dpId;
}````
sage galleon
#

you know that does actually look better

gritty fulcrum
#

it is better.

#
    /**
     * Constructs a Department object with the specified name and join date.
     * If a department with the given name already exists, uses its existing ID.
     * Otherwise, creates a new unique ID for this department.
     * 
     * @param depName the name of the department
     * @param joinDate the date of joining in YYYY-MM-DD format
     */
    public Department(String depName, String joinDate)
    {```
#

you get the idea?

sage galleon
#

once im done getting this thing to actually work ill see about changing the comments because this stuff is crazy messy and stupid to read

gritty fulcrum
#

are you explicitly told to put comments and java docs everywhere?

#

no one really puts docs on toString

sage galleon
#

idk i wrote most of the comments at like 2am last night

gritty fulcrum
#

💀 I can see that

#

would u agree 0000 = 0 ?

sage galleon
#

yes

gritty fulcrum
#

you aren't actually padding it

sage galleon
#

dawg no way i wrote that

#

jesus

#

i did write that

gritty fulcrum
#

public Department(String depName, String joinDate)

#

Department dep2 = new Department(emp_rows.get(j).get(2), emp_rows.get(j).get(3));

#

I have a feel this is putting employ name

#

should be 3 and 4?

#

nvm its correct

#

mb mb

sage galleon
#

emp_rows is a list of first names(0), last names(1), department name(2) and join date(3) i did not write the code to get that it was given to me

gritty fulcrum
#

yeah I just realized that

#

rest seems fine to me

sage galleon
#

okay, well thanks for the help, i posted the question in another server and it seems they found something

peak falcon
gritty fulcrum
peak falcon
#

@sage galleon How is your code currently? Do all tests pass, or is it still 1 test that fail

#

the number of day between first and last test

sage galleon
#

im working it out with another guy in another server

#

we are getting somewhere

gritty fulcrum
#

answer their question

peak falcon
# gritty fulcrum subjective

If you do static code, you failed in your structure somehow and you will feel it later on.
It will be in twisted logic, or in weird bug.

sage galleon
#

i found out that it was testing for the number of employees who are cross appointed NOT how many days between certain start dates

gritty fulcrum
sage galleon
peak falcon
#

and calculate the difference in day between them.

#
Instant start = Instant.parse("2025-10-01T00:00:00Z");
Instant end = Instant.parse("2025-10-10T00:00:00Z");

long daysBetween = Duration.between(start, end).toDays();
#

The hard part is parsing your dates.

sage galleon
#

i think thats already happening but its using localDate

#

and i cant change that unfortunatly

peak falcon
#

what is the format of the dates in the csv?

sage galleon
#

yyyy-mm-dd