1. 程式人生 > >用程式碼實現以下需求

用程式碼實現以下需求

(1)定義學生類,包含姓名(String name),性別(String gender),年齡(int age)三個屬性,生成空參有參構造,set和get方法,toString方法
(2)鍵盤錄入6個學員資訊(錄入格式:張三,男,25),要求有兩個相同的資訊,將6個學員資訊存入到ArrayList集合中
(3)將存有6個學員資訊的ArrayList集合物件寫入到D:\\StudentInfo.txt檔案中
(4)讀取D:\\StudentInfo.txt檔案中的ArrayList物件
(5)對ArrayList集合中的6個學生物件進行去重並按照年齡從小到大的順序排序

(6)將ArrayList集合中排序後的結果利用PrintWriter流寫入到E:\\StudentInfo.txt檔案中(寫入格式:張三-男-25)

package ceShi;

import java.io.Serializable;

public class student implements Serializable {
	private String name;//姓名
	private String gender;//性別
	private int age;//年齡
	public String getName() {
		return name;
	}
	public student(String name, String gender, int age) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
	}
	@Override
	public String toString() {
		return "student [name=" + name + ", gender=" + gender + ", age=" + age
				+ "]";
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
package ceShi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class cunChu {
	public static void main(String[] args)throws IOException, ClassNotFoundException {
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入你要輸入的學生數量");
		int num = sc.nextInt();
		System.out.println("請安如下格式輸入你要錄入的學生資訊(錄入格式:張三,男,25)");
		String[] stuStr = new String[num];
		List<student> stuList = new ArrayList<student>();
		for(int i = 0;i<num;i++){
			System.out.println("請輸入第"+(i+1)+"個:");
			stuStr[i] = sc.next();
		}
		Split(stuStr,stuList);
	//	System.out.println(stuList);
		Write(stuList);
		Read();
		quChongBiJiao(stuList);
		printWriter(stuList);
	}


	private static void printWriter(List<student> stuList) throws IOException {
		// 將ArrayList集合中排序後的結果利用PrintWriter流寫入到碟符
		PrintWriter pw = new PrintWriter(new File("e:"+File.separator+"StudentInfo.txt"));
		for(int i = 0;i<stuList.size();i++){
			pw.println(stuList.get(i).toString().replace(",", "-"));
		}
		pw.close();
	}


	private static void quChongBiJiao(List<student> stuList) {
		// 將ArrayList中studen物件去重比較排序
		ArrayList<student> newStuList =new ArrayList<student>();
		Collections.sort(stuList,new Comparator<student>(){

			@Override
			public int compare(student o1, student o2) {
				// TODO Auto-generated method stub
				return o1.getAge()-o2.getAge();
			}
			

		});
		
		//去重
		for(int i = 0;i<stuList.size();i++){
			for(int j =i+1;j<stuList.size();j++){
				if((stuList.get(i).getName()).equals(stuList.get(j).getName())&&
					(stuList.get(i).getGender()).equals(stuList.get(j).getGender())&&
					(stuList.get(i).getAge())==(stuList.get(j).getAge())
					){
					stuList.remove(i);
					}
			}
		}
		System.out.println(stuList);
		
	}


	private static void Read() throws IOException, ClassNotFoundException {
		// 將stuList反序列化
		FileInputStream fis = new FileInputStream("D:"+File.separator+"StudentInfo.txt");
		ObjectInputStream ois = new ObjectInputStream(fis);
		ArrayList a = (ArrayList) ois.readObject();
		System.out.println(a);
	}


	private static void Write(List<student> stuList) throws IOException {
		// 將stuList序列化
		//建立位元組輸出,封裝檔案
		FileOutputStream fos = new FileOutputStream("D:"+File.separator+"StudentInfo.txt");
		//建立寫出物件的序列化流的物件,構造方法傳遞位元組輸出流
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(stuList);
		oos.close();
	}


	private static void Split(String[] stuStr, List<student> stuList) {
		// 將輸入資訊切割成物件屬性存入ArrayList集合中
		for(int i = 0;i<stuStr.length;i++){
//			String name,gender;
//			int age;
			String[] a =stuStr[i].split(",");
			student s = new student(a[0],a[1],Integer.parseInt(a[2]));
			stuList.add(s);
		}
	}
	
}