Course.java
package com.course;
public class Course {
private String id;
private String name;
public Course(String id,String name){
this.id =id;
this.name = name;
}
public Course(){
}
public void setId(String id){
this.id =id;
}
public String getId(){
return id;
}
public void setName(String pname){
name = pname;
}
public String getName(){
return name;
}
}
Student.java
package com.student;
public class Student {
private String id;
private String name;
public Student(String id,String name){
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
SelectCourse.java
package com.manager;
public class SelectCourse {
private String courseId;
private String studentId;
public SelectCourse(String courseId,String studentId){
this.courseId =courseId;
this.studentId =studentId;
}
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
}
Manager.java
package com.manager;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.course.Course;
import com.student.Student;
public class Manager {
private static List<SelectCourse> courseList = new ArrayList<>();
private static Course[] courseData = new Course[3];
private static Student[] students = new Student[3];
public static void main(String[] args) {
init();
Scanner scanner = new Scanner(System.in);
System.out.print(">");
while (scanner.hasNext()) {
String value = scanner.nextLine();
if(value.equals("exit")){
break;
}
else if(value.startsWith("add")){
selCourse(value);
}
else if(value.startsWith("query")){
query();
}
else{
System.out.println(value);
}
System.out.print(">");
}
}
private static void init(){
courseData[0]= new Course("1"," 语文");
courseData[1]= new Course("2"," 数学");
courseData[2]= new Course("3"," 英文");
students[0] = new Student("1", "张三");
students[1] = new Student("2", "李四");
students[2] = new Student("3", "王五");
}
private static void selCourse(String value){
String[] cmds = value.split(" ");
if(!cmds[0].equals("add")){
System.out.println("this is invalid add cmd:"+value);
}
else if(cmds.length!=3){
System.out.println("this is invalid add cmd:"+value);
}
else{
SelectCourse obj = new SelectCourse(cmds[1],cmds[2]);
boolean find =false;
for(Course item:courseData){
if(item.getId().equals(cmds[2])){
find = true;
}
}
if(!find){
System.out.println("the course is not exist");
return;
}
find =false;
for(Student item:students){
if(item.getId().equals(cmds[1])){
find = true;
}
}
if(!find){
System.out.println("the syudent is not exist");
return;
}
courseList.add(obj);
}
}
private static void query(){
if(courseList.size()==0){
System.out.println("no select course");
}
else{
for(SelectCourse item:courseList){
System.out.println(item.getCourseId()+","+item.getStudentId());
}
}
}
}