简体   繁体   English

如何实现将文件加载到程序中的其他类可以使用的类?

[英]How do I implement a class that loads a file into my program that other classes can use?

Okay so I am building a program that will sort grades and records of students. 好吧,所以我正在构建一个程序,该程序将对学生的成绩和成绩进行排序。 It is a command-line program and when run it will start by asking for user input. 它是一个命令行程序,在运行时会从询问用户输入开始。 there are several commands such as exit(exits program), load [file name](loads a file name), student [student name] (loads student records), etc. the others are not important. 有几个命令,例如退出(退出程序),加载[文件名](加载文件名),学生[学生名](加载学生记录)等,其他命令并不重要。 Well basically what I am wondering and what I am stuck on is all those functions will be in separate classes and will be called when the user inputs a specific command, but if I put the "load" command in its own class, then how do I get it to share its information with the other classes? 好吧,基本上我想知道的是,我坚持的是所有这些功能都将在单独的类中,并且在用户输入特定命令时将被调用,但是如果我将“ load”命令放在其自己的类中,那么该怎么办我可以与其他班级分享其信息吗? I know I have to use BufferReader to read in the files, but how would I go implementing my load class, or if there is a better way feel free to say so. 我知道我必须使用BufferReader读取文件,但是我将如何实现我的加载类,或者是否有更好的方式随意声明。 here is my code so far. 到目前为止,这是我的代码。 there isn't much on my other classes because I feel like I need to figure out how to read in and share the file with the other classes first. 我的其他课程没有太多内容,因为我觉得我需要先弄清楚如何读入文件并与其他课程共享文件。

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 was my main class here are the function classes so far. 那是我主要的类,到目前为止是函数类。

public class Load
{
    public Load()
    {
        BufferReader in = new BufferReader
    }

}




public class Student
{
    public Student()
    {

    }

}


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

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

    public String Splitter()
    {

    }
    public String Printer()
    {

    }

}

Well basically what I am wondering and what I am stuck on is all those functions will be in separate classes and will be called when the user inputs a specific command, 好吧,基本上,我想知道的是,我坚持的是所有这些功能都将放在单独的类中,并在用户输入特定命令时被调用,

but if I put the "load" command in its own class, then how do I get it to share its information with the other classes? 但是,如果我将“ load”命令放在其自己的类中,那么如何使它与其他类共享信息呢?

If your Load class has a public getter method then you can retrieve it that way. 如果您的Load类有一个公共的getter方法,那么您可以用这种方法检索它。

For example, with a method inside your Load class of 例如,在您的Load类中使用

public String getFileContents() {
/* your BufferedFileReader and operations here */
    return s;
}

You can access it by making a Load object in your main class with 您可以通过在主类中创建一个Load对象来访问它

Load load = new Load();
String s = load.getFileContents();

This way you have made a String in your main program7 class and pulled the data from your Load class. 这样,您就可以在主program7类中创建一个String并从Load类中提取数据。

I hope this helps and has answered your question. 希望对您有所帮助,并已回答您的问题。

Can I access it in my other classes too, using the Load object? 我也可以使用Load对象在其他类中访问它吗? My other classes need some of the information because they process it differently. 我的其他班级需要一些信息,因为他们对信息的处理方式不同。 For example my students class, prints out the Students and their grades, while Student class will print out a certain student as specified by the user 例如,我的学生班级,打印出学生及其成绩,而学生班级将打印出用户指定的特定学生

So you might have a method in your Student class like 因此,您的学生类中可能会有一个方法,例如

public void printGrades(String fileContents) {
    /* do stuff */
    System.out.println("Grades");
}

and then call it from your main program7 class by 然后从您的主program7类中调用它

Student student = new Student();
student.printGrades(s); // s is the String from load.getFileContents(); up above.

So you are passing the file contents from the Load class to the Student class through these public methods within each class. 因此,您正在通过每个类中的这些公共方法将文件内容从Load类传递到Student类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM