Search code examples
javacsvcommand-linebufferedreadervoid

How do I get my program to load a file that will be communicate with my other classes?


okay so I am creating a program that will organize and display according to what the user inputs. The user will Load a file then use those commands. The user may only load a file with that is a Comma Separated Value text file. The first line will contain the word "name", followed by the names of all the assignments in class, separated by commas. The second line will give the name of the class, followed by the number of points possible for each assignment.Any line after that will have the student name followed by the number of points achieved on each assignment.

For example:
name,essay 1,test 1,essay 2,test 2,final
Philosophy 101,5,20,5,20,50
Aristotle,4,18,3,15,40
Euclid,3,15,2,10,35
Immanuel Kant,4,18,4,20,48
Ben Franklin,5,20,4,19,49

the program will start in a "command" mode which allows the user to type various commands and get results from the system. The commands are :

1. exit
Causes the program to exit.
2. help
Causes the program to print a list of commands accepted by the system.
3. load [filename]
Causes the program to load a class database specified in the file [filename]. If a database is
currently loaded, the program should discarded it and replace it with the new one.
4. students
Prints out a list of students from the class, along with total points, and final grades (A, B, C, D,
F) for each student. These should be properly aligned in columns, so that the grades line up
above one another, and the student names are left aligned.
5. assignments
Prints out a list of assignments from the file, along with how many points are possible for each
assignment. As with students, the assignments should be properly aligned on columns.
6. student [student name]
Prints a report for the student. If the student does not exist, it prints a message stating that the
student does not exist, and continues without crashing. The report should contain the following
information:
1. Student name
2. A list of all assignments, and the grades the student achieved for them and points possible
3. Overall points made in the course and total points possible.
4. Final letter grade for the student, given the grading scale
A >= 90%
B >= 80%, < 90%
C >= 70%, < 80%
D >= 60%, < 70%
F < 60%
5. The report should be formatted reasonably, so that assignments and grades line up in
columns, for example.
7. assignment [assignment name]
Prints a report about a particular assignment, which includes the following information:
1. The assignment name, and points possible
2. The low, high, and average for the assignment
3. How many students achieved different grades (A-F) for the assignment.
8. grades
Prints a report about overall grades in the class. The report must include the following
information:
1. low, high, and average percentages in the class
2. how many students achieved the different grades in the class (A-F)

Now what im confused on is the Load class. It basically dictates the information of the other classes, but I havent had much luck on getting it to work. Here is all my code so far.

import java.util.*;
import java.io.*;
public class program7
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Grade Stats by ");
        System.out.print(">");
        while(scan.hasNextLine())
        {

            String input = scan.nextLine();

            if(input.equals("exit"))
            {
                System.exit(0);
            }
            else if(input.equals("help"))
            {
                System.out.println("exit                   - program closes.");
                System.out.println("load [filename]        - loads a class database specified in [filename].");
                System.out.println("students               - prints out a list of students from the class, along ");
                System.out.println("                         with total points, and final grades for each student.");
                System.out.println("assignments            - prints out a list of assignments from the file, along with points possible");
                System.out.println("student [student name] - Prints report for the student");
                System.out.print(">");
            }
            else if(input.contains("load"))
            {
                String[] split = input.split(" ");
                Load load = new Load(split[1]);
                System.out.print(">");
            }
            else if(input.equals("students"))
            {

                System.out.print(">");
            }
            else if(input.equals("assignments"))
            {

                System.out.print(">");
            }
            else if(input.contains("student"))
            {
                String[] split = input.split(" ");
                Student student = new Student(split[1]);
                System.out.print(">");
            }
            else if(input.contains("assignment"))
            {

            }
            else if(input.equals("grades"))
            {

            }
            else
            {
                System.out.println("exit                   - program closes.");
                System.out.println("load [filename]        - loads a class database specified in [filename].");
                System.out.println("students               - prints out a list of students from the class, along ");
                System.out.println("                         with total points, and final grades for each student.");
                System.out.println("assignments            - prints out a list of assignments from the file, along with points possible");
                System.out.println("student [student name] - Prints report for the student");
                System.out.print(">");
            }
        }
    }

}

That is my main class btw. Here are the other classes.

import java.util.*;
import java.io.*;
public class Load
{
    public String inputFile;
    public List<String> info = new ArrayList<String>();

    public Load(String inputFile)
    {

        this.inputFile = inputFile;
    }
    public void getFileContent()
    {
        try
        {
            BufferedReader in = new BufferedReader(new FileReader(inputFile));
            try
            {
                String line =  "";

                while(in.readLine() != null)
                {
                    line = in.readLine();
                    info.add(line);

                }


            }
            catch(IOException e)
            {
                System.err.println("Exception, man");
            }
            finally
            {
            in.close();
            }
        }
        catch(FileNotFoundException e)
        {
            System.err.println("File wasnt found bro");
        }
        catch(IOException e)
        {
            System.err.println("Exception, man");
        }
    }

}

That was my Load class and this following one is my Students class

public class Student
{
    public Student()
    {

    }

}

I couldnt get Load to work so I havent messed much with these. And here is the Student class.

import java.util.*;
import java.io.*;
public class Student
{

    private String student;
    public Student(String student)
    {
        this.student = student;
    }

    public String Splitter()
    {

        return "";

    }
    public String Printer()
    {

        return "";
    }

}

Now I understand I have to have a constructor and call the class by creating an instance of it in the main class, but it doesn't seem to be running my void method in my Load class. Is there a way i could fix this? And if there are any better ideas Id like to hear them, I am just trying to think of a way I could easily transfer information(user inputFile) to all my classes so they can be manipulated at the users will, or according to his or hers input. Oh yeah and this program is just run on command-line.


Solution

  • Looks like you never call getFileContent() in main. Should look something like this:

    else if(input.contains("load"))
    {
        String[] split = input.split(" ");
        Load load = new Load(split[1]);
        load.getFileContent();    // forgot to call
        System.out.print(">");
    }
    

    If the provided file is valid, there should be content added to the ArrayList specified in the Load class.