程序设计第一题

时间:2019-02-19
本文章向大家介绍程序设计第一题,主要包括程序设计第一题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

student.java

import java.util.ArrayList;

public class student {
	private String stu_id;   //学生id
	private String stu_name; //学生姓名
	private ArrayList<course> courses;  //课程
	
	public student(String stu_id,String stu_name,ArrayList<course> courses) {
		// TODO Auto-generated constructor stub
		this.courses=courses;
		this.stu_id=stu_id;
		this.stu_name=stu_name;
	}
	
	public ArrayList<course> getCourses() {
		return courses;
	}
	public String getStu_id() {
		return stu_id;
	}
	public String getStu_name() {
		return stu_name;
	}
	public void setCourses(ArrayList<course> courses) {
		this.courses = courses;
	}
	public void setStu_id(String stu_id) {
		this.stu_id = stu_id;
	}
	public void setStu_name(String stu_name) {
		this.stu_name = stu_name;
	}

}

 

 

course.java

public class course {
	private String courseName;//课程名
	private int score;   //分数
	
	course(String courseName,int score){
		this.courseName=courseName;
		this.score=score;
	}
	public String getCourseName() {
		return courseName;
	}
	public int getScore() {
		return score;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	public void setScore(int score) {
		this.score = score;
	}

}

 

Main.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class Main {
	private static ArrayList<String> readFile(String filename){
		//设置返回结果
		ArrayList<String> result=new ArrayList<>();
	    /* 读取文件*/
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filename))));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                result.add(lineTxt);
            }
            br.close();
        } catch (Exception e) {
           e.printStackTrace();
        }
        System.out.println("姓名:" );  
		return result;
		
	}
	
	//将信息写入xml文件
	private static void writeFile(Map<String,student> m,String filename){
		//创建文档
		Document document=new Document();
		//创建根元素,即students标签
		Element students=new Element("s1tudents");
		//根元素加入到document中
		document.addContent(students);
		Element student;
		
		for(String s:m.keySet()){//map.keySet()获取map全部的key值
			//创建学生标签
			student=new Element("s2tudent");
			//创建学号标签并加入到学生标签内
			Element e_stuid=new Element("学号");
			e_stuid.setText(m.get(s).getStu_id());
			student.addContent(e_stuid);
			
			//创建姓名标签并加入到学生标签内
			Element e_stuname=new Element("姓名");
			e_stuname.setText(m.get(s).getStu_name());
			student.addContent(e_stuname);
			
			//循环创建课程标签并加入到学生标签内
			for(course c:m.get(s).getCourses()){
				Element e_course=new Element("课程");
				e_course.setAttribute("课程名",c.getCourseName());//在e_course对象中加入名为课程名的属性并附值为c.getCourseName()的值
				e_course.setText(c.getScore()+"");//<姓名>张三</姓名> 就是填中间的那个名字
				student.addContent(e_course);//把上面那句话显示出来
			}
			
			//将单个学生标签加入到根标签
			students.addContent(student);
		}	
		//设置xml输出格式
                Format format = Format.getPrettyFormat();
                // format.setEncoding("utf-8");//设置编码
                //format.setIndent("    ");//设置缩进
               //得到xml输出流
               XMLOutputter out = new XMLOutputter(format);
               //把数据输出到xml中
               try {
			out.output(document, new FileOutputStream(filename));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	//用来测试输出map
	private static void showMap(Map<String,student> m){
		for(String s:m.keySet()){
			System.out.println(m.get(s).getStu_id()+" "+m.get(s).getStu_name()+"   ");
			for(course c:m.get(s).getCourses()){
				System.out.println(c.getCourseName()+"   "+c.getScore());
			}
			System.out.println("---------------------");
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//读取文件获取字符数组
		ArrayList<String> strs=readFile("D:\\test.txt");
		//因为要对学号进行降序排序,并且一个学生id对应学生类,使用Trrmap存储信息,java中TreeMap元素之间是有序的
		Map<String, student> students=new TreeMap<>(new Comparator<String>() {
			//自定义比较器,TreeMap默认为升序
			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				//降序
				return o2.compareTo(o1);
			}
		});
		//定义字符数组
		String []info;
		//学生学号
		String stu_id;
		System.out.println( strs.size());  
		for(int i=0;i<strs.size();i++){
			
			info=strs.get(i).split(",");//根据逗号进行字符串分割信息
			stu_id=info[0];//获取学号
			System.out.println( "i="+i+"    "+strs.size()); 
			if(!students.containsKey(stu_id)){//containKey 判断Map集合对象中是否包含指定的键名
				//如果map中还未有学号,创建学生对象
				student newstu=new student(stu_id, info[1], new ArrayList<course>());
				//课程数组加入课程
				newstu.getCourses().add(new course(info[2], Integer.parseInt(info[3])));//Integer.parseInt()方法将一个字符串转换成int型
				//加入到map
				students.put(stu_id, newstu);
			}
			else{
				//如果map中已有对象则加入对应学生的课程中即可
				students.get(stu_id).getCourses().add(new course(info[2], Integer.parseInt(info[3])));
			}
		}
		//showMap(students);
		//写入xml文件
		writeFile(students, "D:\\output.xml");
	}
 

}