📄 course.java
字号:
package baseClass;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class Course {
private String courseId;
private String courseName;
private String courseMark;
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseMark() {
return courseMark;
}
public void setCourseMark(String courseMark) {
this.courseMark = courseMark;
}
public Course()
{
}
public Course(String pcid,String pcname,String pcmark)
{
courseId=pcid;
courseName=pcname;
courseMark=pcmark;
}
public ArrayList loadAllCourse()
{
ArrayList alldate=new ArrayList();
try
{
String[] classmessage=new String[2];
File read = new File(GlobalValue.COURSEDATESOURSE);//第一行表示有多少条数据,后面行表示实际数据
if(!read.exists())
read.createNewFile();
BufferedReader br = new BufferedReader(
new FileReader(read));
String temp = null;
temp = br.readLine();
int i=0,j=0;
if(temp != null)//读取数据条数
{
j=Integer.parseInt(temp);
temp = br.readLine();
}
while(temp != null)//读取数据
{
classmessage=temp.split(GlobalValue.SPLITCHAR);
Course tempstudent=new Course();
tempstudent.courseId=classmessage[0];
tempstudent.courseName=classmessage[1];
tempstudent.courseMark=classmessage[2];
alldate.add(tempstudent);
temp = br.readLine();
}
br.close();
alldate.trimToSize();
//showMessage(alldate);
return alldate;
}
catch(FileNotFoundException e){ //文件未找到
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
catch(Exception e){
System.out.println (e);
}
return null;
}
public int addnewcourse(Course newclass)
{
//1为可添加 0为数据为空可添加 2为数据已有 3为新添加成功
ArrayList allcourse=loadAllCourse();
for(int i=0;i<allcourse.size();i++)
{
if(newclass.equals((Class)allcourse.get(i)))
{
return 2;
}
}
allcourse.add(newclass);
allcourse.trimToSize();
Couresecomparator classco=new Couresecomparator();
Collections.sort(allcourse,classco);
saveAllStudent(allcourse);
return 3;
}
public boolean equals(Course newclass)
{
//if(courseId==newclass.courseId&&courseName==newclass.courseName&&newclass.courseMark==courseMark)
if(courseId==newclass.courseId)
{
return true;
}
else
{
return false;
}
}
public void saveAllStudent(ArrayList allclass)
{
try
{
File write = new File(GlobalValue.COURSEDATESOURSE);
if(!write.exists())
write
.createNewFile();
BufferedWriter bw = new BufferedWriter(
new FileWriter(write,false));
int i=0;
if(allclass!=null)//写入数据行数
{
bw.write(allclass.size()+"\n");
for(int j=0;j<allclass.size();j++)//读取数据
{
Course tempstudent=(Course)allclass.get(i);
String temp=tempstudent.courseId+"GlobalValue.SPLITCHAR"+tempstudent.courseName+"GlobalValue.SPLITCHAR"+tempstudent.courseMark+"\n";
bw.write(temp);
}
}
else
{
bw.write(0+"\n");
}
bw.close();
}
catch(FileNotFoundException e){ //文件未找到
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
catch(Exception e){
System.out.println (e);
}
}
public void showMessage(ArrayList classArray)
{
if(classArray!=null)
{
for(int i=0;i<classArray.size();i++)
{
System.out.println("课程编号:"+((Course)classArray.get(i)).courseId+" 课程名称:"+((Course)classArray.get(i)).courseName+" 课程学分:"+((Course)classArray.get(i)).courseMark);
}
}
else
{
System.out.println("没有数据!");
}
}
public static void main(String[] arg)
{
Course temp=new Course();
ArrayList message=temp.loadAllCourse();
//temp.saveAllStudent(message);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -