java手把手教你写代码 java代码怎么写( 四 )


this.teacher = teacher;
}
public String getAssessment() {
return assessment;
}
public void setAssessment(String assessment) {
this.assessment = assessment;
}
@Override
public String toString() {
return " 课程号:" + classNum + ", 学分:" + credit
+ ", 上课时间:" + startClassDate + ", 下课时间:"
+ endClassDate + ", 教师:" + teacher + ",考核方式:"
+ assessment + "";
}

}
package main;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("创建学生请输入姓名:");
Student stu1 = new Student();
stu1.setName(sc.nextLine());
Scanner sc1 = new Scanner(System.in);
System.out.println("创建学生请输入学号:");
stu1.setNumber(sc1.nextLine());
System.out.println("请选择课程,课程如下:");
ListCourse list2 = new ArrayListCourse();
stu1.setCourses(list2);
ListCourse list = readTxt();
for (Course course : list) {
System.out.println(course.toString());
}
for (int i = 1; i5; i++) {
System.out.println("请输入第"+i+"个课程号:");
//输入第一个课程号
list2 = handleClass(list, list2);
System.err.println("已选择成功!");
list = notSelectClass(list, list2);
System.out.println("请选择课程,课程如下:");
if(i==4){
break;
}
for (Course course : list) {
System.out.println(course.toString());
}
}
System.out.println("课程已选完!结果如下:");
System.err.println("学生:"+stu1.getName()+":");
for (Course course : list2) {
System.err.println(course.toString());
}
}
/**
*
* 查看未选择可选课程
* @param list 全部可选课程
* @param list2 已选课程
* @return
*/
private static ListCourse notSelectClass(ListCourse list,ListCourse list2){
for (int i = 0; ilist.size(); i++) {
for (Course course : list2) {
//把已选课程剔除
if(course.getClassNum().equals(list.get(i).getClassNum())){
list.remove(i);
continue;
}
//把时间重合课程剔除startdate1 =enddate2 and enddate1=startdate2
if(daYuDengYu(course.getEndClassDate(),list.get(i).getStartClassDate())daYuDengYu(list.get(i).getEndClassDate(), course.getStartClassDate())){
list.remove(i);
}
}
}
return list;
}
public static boolean daYuDengYu(String first, String second){
BigDecimal bd1 = new BigDecimal(first);
BigDecimal bd2 = new BigDecimal(second);
return bd1.compareTo(bd2)=0?true:false;
}
/**
* 处理选择课程
* @param list
* @param list2
* @return
*/
private static ListCourse handleClass(ListCourse list,ListCourse list2){
while (true) {
Scanner sssi = new Scanner(System.in);
String num = sssi.nextLine().trim();
for (Course course : list) {
if(num.equals(course.getClassNum())){
list2.add(course);
return list2;
}
}
System.out.println("课程号输入错误,请重新输入:");
}
}
/**
* 读取txt获取课程每个课程按;分割每个属性按,分割
* @return
* @throws Exception
*/
private static ListCourse readTxt() throws Exception{
/* 读入TXT文件 */
String pathname = Main.class.getResource("/").toString();
pathname = pathname.substring(6, pathname.length()-4)+"/src/classs/class.txt"; //获取绝对路径

推荐阅读