java - How do I implement a class that loads a file into my program that other classes can use? -
okay building program sort grades , records of students. command-line program , when run start asking user input. there several commands such exit(exits program), load [file name](loads file name), student [student name] (loads student records), etc. others not important. wondering , stuck on functions in separate classes , called when user inputs specific command, if put "load" command in own class, how share information other classes? know have use bufferreader read in files, how go implementing load class, or if there better way feel free so. here code far. there isn't on other classes because feel need figure out how read in , share file 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 "); 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 class database specified in [filename]."); system.out.println("students - prints out list of students class, along "); system.out.println(" total points, , final grades each student."); system.out.println("assignments - prints out list of assignments file, along points possible"); system.out.println("student [student name] - prints report 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 class database specified in [filename]."); system.out.println("students - prints out list of students class, along "); system.out.println(" total points, , final grades each student."); system.out.println("assignments - prints out list of assignments file, along points possible"); system.out.println("student [student name] - prints report student"); system.out.print(">"); } } } }
that main class here function classes 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 wondering , stuck on functions in separate classes , called when user inputs specific command,
but if put "load" command in own class, how share information other classes?
if load class has public getter method can retrieve way.
for example, method inside load class of
public string getfilecontents() { /* bufferedfilereader , operations here */ return s; }
you can access making load object in main class with
load load = new load(); string s = load.getfilecontents();
this way have made string in main program7 class , pulled data load class.
i hope helps , has answered question.
can access in other classes too, using load object? other classes need of information because process differently. example students class, prints out students , grades, while student class print out student specified user
so might have method in student class like
public void printgrades(string filecontents) { /* stuff */ system.out.println("grades"); }
and call main program7 class by
student student = new student(); student.printgrades(s); // s string load.getfilecontents(); above.
so passing file contents load class student class through these public methods within each class.
Comments
Post a Comment