構造一個學生類Student, 每一個學生對象有id, name, sex, address等
基本信息,同時還有java語言、C語言、編譯原理和Java EE等課程考試
成績信息。利用集合框架類完成如下功能:(考試成績用一個類實現)
1. 讀取存放學生信息的文件,存儲到一個集合框架類對象中,文件格式如下:
id name sex address java語言成績 C語言成績 編譯原理成績 Java EE成績
1 aaa f nwsuaf 98 92 32 87
2 bbb m yangling 99 91 86 88
……………
2. 增加一個學生對象(id是唯一的不允許重複,如果有重複的給出提示,並
重新輸入),要求按照學號id升序排序,並打印輸出。
3. 查找一個學生,根據學生的姓名查找集合中是否存在該學生,存在打印輸
出該學生信息,不存在給出提示;
4. 刪除一個學生,輸入一個學生的姓名,刪除集合中該學生。
5. 創建一個Map對象map, map = newHashMap<Integer, TreeSet<Student>>,
其中map 的key存儲爲id%10, value對象TreeSet<Student>存儲的key相同的所
有學生對象。要求TreeSet <Student>中的學生對象按照學號升序排序,打印輸出
map中所有的信息。
***********************************************************************
Student類,代碼如下:
package collection;
public class Student extends Scores implements Comparable<Student>{
//學生信息類
private int id;//學號
private String name;//姓名
private String sex;//性別
private String address;//地址
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void Student(int id,String name,String sex,String address,int javayuyan,int cyuyan,int bianyi,int javaee){
this.id=id;
this.name =name;
this.sex=sex;
this.address =address;
this.javayuyan=javayuyan;
this.cyuyan=cyuyan;
this.bianyi=bianyi;
this.javaee=javaee;
}
@Override
public String toString() {
return id + " "+ name + " " + sex + " " + address + " "+
javayuyan+ " " + cyuyan + " "+ bianyi + " " + javaee ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
@Override
public int compareTo(Student o) {
int result =this.getId()>o.getId()? 1:
(this.getId()==o.getId()?0:-1);
return result;
}
}
Scores類,代碼如下:
package collection;
public class Scores {
//學生的分數類
protected int javayuyan;//Java語言
protected int cyuyan;//c語言
protected int bianyi;//編譯原理
protected int javaee;//JavaEE
public int getJavayuyan() {
return javayuyan;
}
public void setJavayuyan(int javayuyan) {
this.javayuyan = javayuyan;
}
public int getCyuyan() {
return cyuyan;
}
public void setCyuyan(int cyuyan) {
this.cyuyan = cyuyan;
}
public int getBianyi() {
return bianyi;
}
public void setBianyi(int bianyi) {
this.bianyi = bianyi;
}
public int getJavaee() {
return javaee;
}
public void setJavaee(int javaee) {
this.javaee = javaee;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + bianyi;
result = prime * result + cyuyan;
result = prime * result + javaee;
result = prime * result + javayuyan;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Scores other = (Scores) obj;
if (bianyi != other.bianyi)
return false;
if (cyuyan != other.cyuyan)
return false;
if (javaee != other.javaee)
return false;
if (javayuyan != other.javayuyan)
return false;
return true;
}
@Override
public String toString() {
return "Scores [bianyi=" + bianyi + ", cyuyan=" + cyuyan + ", javaee="
+ javaee + ", javayuyan=" + javayuyan + "]";
}
}
Operation類,代碼如下:
package collection;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeSet;
public class Operation {
public void Read(Map<Integer, TreeSet<Student>> map){
//讀入數據
String fileName="data/11.txt";
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
while((line=reader.readLine())!=null){//循環賦值讀入
String str[] = line.split("\\s+");
Student op=new Student();
op.setId( Integer.parseInt(str[0].trim()));
op.setName( str[1].trim());
op.setSex(str[2].trim());
op.setAddress(str[3].trim());
op.setJavayuyan(Integer.parseInt(str[4].trim()));
op.setCyuyan(Integer.parseInt(str[5].trim()));
op.setBianyi(Integer.parseInt(str[6].trim()));
op.setJavaee(Integer.parseInt(str[7].trim()));
if(map.containsKey(op.getId()%10)){
map.get(op.getId()%10).add(op);
}else{
TreeSet<Student> ts=new TreeSet<Student>();
ts.add(op);
map.put(op.getId()%10, ts);
}
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public void OutPrint(Map<Integer, TreeSet<Student>> map){
//按一定的順序輸出信息
System.out.println("學號 姓名 性別 地址 java語言成績 C語言成績 編譯原理成績 JavaEE成績");
for(Iterator<Integer> it = map.keySet().iterator();
it.hasNext();)
{
TreeSet<Student> tp2=new TreeSet<Student>();
tp2.addAll(map.get(it.next()));
for(Iterator<Student> it1 = tp2.iterator();
it1.hasNext();)
{
System.out.println(it1.next().toString());
}
}
}
public void AddStudent(Map<Integer, TreeSet<Student>> map){
//添加學生信息,如果相同則重新添加
System.out.println("輸入學生信息:");
System.out.println("輸入格式爲:學號 姓名 性別 地址 java語言成績 C語言成績 編譯原理成績 JavaEE成績");
Scanner in=new Scanner(System.in);
String line=in.nextLine();
String str[] = line.split("\\s+");
Student op=new Student();
op.setId( Integer.parseInt(str[0].trim()));
op.setName( str[1].trim());
op.setSex(str[2].trim());
op.setAddress(str[3].trim());
op.setJavayuyan(Integer.parseInt(str[4].trim()));
op.setCyuyan(Integer.parseInt(str[5].trim()));
op.setBianyi(Integer.parseInt(str[6].trim()));
op.setJavaee(Integer.parseInt(str[7].trim()));
if(map.containsKey(op.getId()%10)){
if(map.get(op.getId()%10).contains(op)){
System.out.println("學號信息已經存在,不能插入!");
}else{
map.get(op.getId()%10).add(op);
System.out.println("信息添加成功!");
}
}else{
TreeSet<Student> ts=new TreeSet<Student>();
ts.add(op);
map.put(op.getId()%10, ts);
System.out.println("信息添加成功!");
}
}
public void FindStudent(Map<Integer, TreeSet<Student>> map){
//按學生的姓名進行查找學生,如果找到則打印輸出信息
System.out.println("輸入要查找的學生姓名:");
Scanner in=new Scanner(System.in);
String line=in.nextLine();
String str= line.trim();
boolean a=true;
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext();) {
TreeSet<Student> tp2 = new TreeSet<Student>();
tp2.addAll(map.get(it.next()));
for (Iterator<Student> it1 = tp2.iterator(); it1.hasNext();) {
Student stu=new Student();
stu=it1.next();
if(stu.getName().equals(str)){
System.out.println("要查找的姓名爲"+str+"的學生已找到,信息打印如下:");
System.out.println(stu.toString());
a=false;
}
}
}
if(a){
System.out.println("要查找的姓名爲"+str+"的學生姓名不存在!");
}
}
public void DelStudent(Map<Integer, TreeSet<Student>> map){
//按照學生姓名刪除學生信息,如果存在則刪除,如果不存在則提示輸入的信息不存在
System.out.println("輸入要刪除的學生姓名:");
Scanner in=new Scanner(System.in);
String line=in.nextLine();
String str= line.trim();
boolean a=true;
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext();) {
TreeSet<Student> tp2 = new TreeSet<Student>();
tp2.addAll(map.get(it.next()));
for (Iterator<Student> it1 = tp2.iterator(); it1.hasNext();) {
Student stu=new Student();
stu=it1.next();
if(stu.getName().equals(str)){
System.out.println("要查找的姓名爲"+str+"的學生已刪除!");
map.get(stu.getId()).remove(stu);
a=false;
}
}
}
if(a){
System.out.println("要刪除的姓名爲"+str+"的學生姓名不存在!");
}
}
}
Main類,代碼如下:
package collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Map<Integer, TreeSet<Student>> map=new HashMap<Integer, TreeSet<Student>>();
Operation op=new Operation();
op.Read(map);
int i;
while(true){
System.out.println("1:打印學生信息\t"+"2:查找學生信息\t"+"3:插入學生信息\t"+"4:刪除學生信息\t"+"5:退出系統\n");
i=(new Scanner(System.in)).nextInt();
switch (i)
{
case 1:op.OutPrint(map);break;
case 2:op.FindStudent(map);break;
case 3:op.AddStudent(map);break;
case 4:op.DelStudent(map);break;
}
if(i==5){
break;
}
}
}
}
main類爲主函數類,其他的均爲執行類或者對象類。以上代碼可能存在一些冗餘的部分,沒有進行修改,如果需要可以自行拷貝和修改。