Java多个类|返回显示类中信息的字符串(新手)

Java多个类|返回显示类中信息的字符串(新手),java,class,Java,Class,你好,stackoverflow用户!目前,我正在为Java编程课程做一项作业,要求我创建一个程序,允许用户为临时数据库输入数据。作业的基本大纲是必须包含三个类:驾驶员、学生和课程 驱动程序类必须向用户询问信息,并在用户希望退出后打印信息。用户输入信息后,将检查输入是否有效(例如确保小时变量低于17) 学生和课程类基本上用于验证用户的输入。我遇到的问题是如何将用户输入的信息返回给用户。我的教授希望我将方法命名为toString(),并从驱动程序类调用它,然后将信息打印回用户 这是用户在输入“2个

你好,stackoverflow用户!目前,我正在为Java编程课程做一项作业,要求我创建一个程序,允许用户为临时数据库输入数据。作业的基本大纲是必须包含三个类:驾驶员、学生和课程

驱动程序类必须向用户询问信息,并在用户希望退出后打印信息。用户输入信息后,将检查输入是否有效(例如确保小时变量低于17)

学生和课程类基本上用于验证用户的输入。我遇到的问题是如何将用户输入的信息返回给用户。我的教授希望我将方法命名为toString(),并从驱动程序类调用它,然后将信息打印回用户

这是用户在输入“2个按钮G1460”作为课程信息(科号、教授、房间号)和“Jimmie Bookson 3.85 16”作为学生信息(姓名、专业、GPA、学时)后应显示的示例。允许用户输入他/她想要的学生数量,直到用户希望退出,用户退出后,显示他们输入的信息

2个按钮G1460
Jimmie Bookson资讯科技署3.85 16 凯蒂·约翰斯ITDM 3.5 14


import java.util.Scanner;
公务舱司机
{
课程输入=新课程();
学生输入=新学生();
公共静态void main(字符串[]args)
{
Driver driv=新的驱动程序();
driv.getCourseInfo();
布尔值=假;
课程课程=新课程();
做
{
另一个=driv.getStudentInfo();
}而(另),;
/**需要从课程类调用toString方法,并返回用户为getCourseInfo()和getStudentInfo()中的提示输入的信息
*上课。
*/
}
公共无效getCourseInfo()
{
int sectionValid=0;
int-instructorValid=0;
int-roomValid=0;
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入节号(1到15)”);
sectionValid=courseInputs.setSectionNumber(input.nextInt());
while(sectionValid==0)
{
做
{
System.out.println(“您的输入无效。请输入节号(1到15)”;
sectionValid=courseInputs.setSectionNumber(input.nextInt());
}而(!(sectionValid==1));
}
input.nextLine();
System.out.println(“请输入本节的讲师”);
讲师有效=courseInputs.set讲师(input.nextLine());
while(讲师有效==0)
{
做
{
System.out.println(“您的输入不能为空,请输入讲师。”);
讲师有效=courseInputs.set讲师(input.nextLine());
}while(instructorValid==0);
}
System.out.println(“请输入房间号”);
roomValid=courseInputs.setRoom(input.nextLine());
while(roomValid==0)
{
做
{
System.out.println(“房间号不能为空,请输入房间号。”);
roomValid=courseInputs.setRoom(input.nextLine());
}while(roomValid==0);
}
}
公共布尔getStudentInfo()
{
布尔值=真;
int nameValid=0;
int-majorValid=0;
int gradePointAverageValid=0;
int hoursValid=0;
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入名称,按enter键退出”);
nameValid=studentInputs.setName(input.nextLine());
如果(nameValid==1)
{
另一个=正确;
System.out.println(“请输入专业”);
majorValid=studentInputs.setName(input.nextLine());
如果(主有效==0)
{
做
{
System.out.println(“专业不能为空,请输入专业”);
majorValid=studentInputs.setName(input.nextLine());
}而(majorValid==0);
}
System.out.println(“请输入平均分数”);
gradePointAverageValid=studentInputs.setGradePointAverage(input.nextDouble());
如果(gradePointAverageValid==0)
{
System.out.println(“平均分数必须介于0.00和4.00之间。请输入平均分数”);
gradePointAverageValid=studentInputs.setGradePointAverage(input.nextDouble());
}而(gradePointAverageValid==0);
System.out.println(“请输入小时”);
hoursValid=studentInputs.setHours(input.nextInt());
如果(小时有效==0)
{
做
{
System.out.println(“小时数不能大于17,请输入小时数”);
hoursValid=studentInputs.setHours(input.nextInt());
}而(hoursValid==0);
}
课程输入。添加学生(学生输入);
}
其他的
{
另一个=假;
}
返回另一个;
}
}
导入java.util.ArrayList;
公共课
{
私家车号码;
私人弦乐教练;
私人弦乐室;
private ArrayList students=新建ArrayList();
public int getSectionNumber()
{
返回节号;
}
公共int集合区段编号(int区段编号)
{
int sectionValid=0;
如果(sectionNumber>=1&§ionNumber=0.00&&gradePointAverage=17))
{
这个小时=小时
import java.util.Scanner;
public class Driver 
{
    Course courseInputs = new Course();
    Student studentInputs = new Student();

    public static void main(String[] args) 
    {
        Driver driv = new Driver();
        driv.getCourseInfo();
        boolean another = false;
        Course cour = new Course();


        do
        {
         another = driv.getStudentInfo();
        }while(another);

        /** need to call the toString method from the Course class and return the information the user has inputed for the prompts in the getCourseInfo() and getStudentInfo() 
         * classes.
         */
    }

    public void getCourseInfo()
    {
        int sectionValid = 0;
        int instructorValid = 0;
        int roomValid = 0;
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the section number (1 to 15)");
        sectionValid = courseInputs.setSectionNumber(input.nextInt());

        while (sectionValid == 0)
        {
            do
            {
                System.out.println("Your input was invalid. Please enter the section number (1 to 15)");
                sectionValid = courseInputs.setSectionNumber(input.nextInt());
            }while(!(sectionValid == 1));
        }

        input.nextLine();

        System.out.println("Please enter the instructor for this section");
        instructorValid = courseInputs.setInstructor(input.nextLine());

        while (instructorValid == 0)    
        {
            do
            {
                System.out.println("Your input cannot be blank. Please enter an instructor.");
                instructorValid = courseInputs.setInstructor(input.nextLine());
            }while(instructorValid == 0);
        }

        System.out.println("Please enter the room number.");
        roomValid = courseInputs.setRoom(input.nextLine());

        while (roomValid == 0)
        {
            do
            {
                System.out.println("The room number cannot be blank. Please enter a room number.");
                roomValid = courseInputs.setRoom(input.nextLine());
            }while(roomValid == 0);
        }

    }

    public boolean getStudentInfo()
    {
        boolean another = true;
        int nameValid = 0;
        int majorValid = 0;
        int gradePointAverageValid = 0;
        int hoursValid = 0;
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter a name. Press enter to quit");
        nameValid = studentInputs.setName(input.nextLine());

    if (nameValid == 1)
    {
        another = true;
        System.out.println("Please enter a major");
        majorValid = studentInputs.setName(input.nextLine());

        if (majorValid == 0)
        {
            do
            {
                System.out.println("The major cannot be blank. Please enter a major");
                majorValid = studentInputs.setName(input.nextLine());
            }while(majorValid == 0);
        }


        System.out.println("Please enter a grade point average");
        gradePointAverageValid = studentInputs.setGradePointAverage(input.nextDouble());

        if (gradePointAverageValid == 0)
        {
            System.out.println("The grade point average has to be between 0.00 and 4.00. Please enter a grade point average");
            gradePointAverageValid = studentInputs.setGradePointAverage(input.nextDouble());
        }while(gradePointAverageValid == 0);


        System.out.println("Please enter the hours");
        hoursValid = studentInputs.setHours(input.nextInt());

        if (hoursValid == 0)
        {
            do
            {
                System.out.println("Hours cannot be greater than 17. Please enter the hours");
                hoursValid = studentInputs.setHours(input.nextInt());
            }while(hoursValid == 0);
        }
        courseInputs.addStudent(studentInputs);
    }
    else
    {
        another = false;
    }
    return another;
    }
}










import java.util.ArrayList;


    public class Course 
    {
        private int sectionNumber;
        private String instructor;
        private String room;
        private ArrayList<Student> students = new ArrayList<Student>();


        public int getSectionNumber()
        {
            return sectionNumber;
        }

        public int setSectionNumber(int sectionNumber)
        {
            int sectionValid = 0;

            if (sectionNumber >= 1 && sectionNumber <= 15)
            {
                this.sectionNumber = sectionNumber;
                sectionValid = 1;
            }
            return sectionValid;
        }

        public String getInstructor()
        {
            return instructor;
        }

        public int setInstructor(String instructor)
        {
            int instructorValid = 0;

            if (instructor.length() != 0)
            {
                this.instructor = instructor;
                instructorValid = 1;
            }
            return instructorValid;
        }

        public String getRoom()
        {
            return room;
        }

        public int setRoom(String room)
        {
            int roomValid = 0;

            if (room.length() != 0)
            {
                this.room = room;
                roomValid = 1;
            }
            return roomValid;
        }


        public void addStudent(Student studentInputs)
        {
         students.add(studentInputs);
        }

        public String toString()
        {
         //return String here with the information the user has inputed for sectionNumber, instructor, room, and students (an array of the Student Class)
         //need to return this to the Driver class and print the information back to the user
         //will need to call the Student class to get the toString method from it print the students array list

        }
    }









public class Student 
{
    private String name;
    private String major;
    private double gradePointAverage;
    private int hours;


    public String getName()
    {
        return name;
    }

    public int setName(String name)
    {
        int nameValid = 0;
        if (name.length() == 0)
        {
            nameValid = 0;
        }
        else
        {
            nameValid = 1;
        }
        return nameValid;
    }

    public String setMajor()
    {
        return major;
    }

    public int setMajor(String major)
    {
        int majorValid = 0;

        if (major.length() != 0)
        {
            this.major = major;
            majorValid = 1;
        }
        return majorValid;
    }

    public double getGradePointAverage()
    {
        return gradePointAverage;
    }

    public int setGradePointAverage(double gradePointAverage)
    {
        int gradePointAverageValid = 0;
        if (gradePointAverage >= 0.00 && gradePointAverage <= 4.00)
        {
            this.gradePointAverage = gradePointAverage;
            gradePointAverageValid = 1;
        }
        return gradePointAverageValid;
    }

    public int getHours()
    {
        return hours;
    }

    public int setHours(int hours)
    {
        int hoursValid = 0;
        if (!(hours >= 17))
        {
            this.hours = hours;
            hoursValid = 1;
        }
        return hoursValid;
    }

    public String toString(int hours, double gradePointAverage)
    {
    // Need to return a String that holds the values of name, major, gradePointAverage, and hours here
    }
}