Java学生信息增删改查(并没用数据库)
Posted 邗影
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java学生信息增删改查(并没用数据库)相关的知识,希望对你有一定的参考价值。
一个泛型的应用,Java版本增删改查,写的简陋,望批评指正
2016-07-02
很久前写的一个程序了。拿出来存一下,不是为了展示啥,自己用的时候还可以看看。写的很粗糙。
1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileNotFoundException; 4 import java.io.FileReader; 5 import java.io.IOException; 6 import java.util.Comparator; 7 import java.util.HashMap; 8 import java.util.HashSet; 9 import java.util.Iterator; 10 import java.util.Map; 11 import java.util.Scanner; 12 import java.util.Set; 13 import java.util.TreeSet; 14 15 16 public class studen implements Comparable<studen>{ 17 public int id; 18 public String name; 19 public char sex; 20 public String address; 21 public int javascore; 22 public int cscore; 23 public int byscore; 24 public int j2eescore; 25 public int getId() { 26 return id; 27 } 28 public void setId(int id) { 29 this.id = id; 30 } 31 public String getName() { 32 return name; 33 } 34 public void setName(String name) { 35 this.name = name; 36 } 37 public char getSex() { 38 return sex; 39 } 40 public void setSex(char sex) { 41 this.sex = sex; 42 } 43 public String getAddress() { 44 return address; 45 } 46 public void setAddress(String address) { 47 this.address = address; 48 } 49 public int getJavascore() { 50 return javascore; 51 } 52 public void setJavascore(int javascore) { 53 this.javascore = javascore; 54 } 55 public int getCscore() { 56 return cscore; 57 } 58 public void setCscore(int cscore) { 59 this.cscore = cscore; 60 } 61 public int getByscore() { 62 return byscore; 63 } 64 public void setByscore(int byscore) { 65 this.byscore = byscore; 66 } 67 public int getJ2eescore() { 68 return j2eescore; 69 } 70 public void setJ2eescore(int j2eescore) { 71 this.j2eescore = j2eescore; 72 } 73 public void setall(int id, String name,char sex,String address,int javascore,int cscore, int byscore,int j2eescore) 74 { 75 this.id=id; 76 this.address=address; 77 this.name=name; 78 this.byscore=byscore; 79 this.cscore=cscore; 80 this.j2eescore=j2eescore; 81 this.sex=sex; 82 this.javascore=javascore; 83 } 84 85 @Override 86 public String toString() { 87 return "studen [id=" + id + ", name=" + name + ", sex=" + sex 88 + ", address=" + address + ", javascore=" + javascore 89 + ", cscore=" + cscore + ", byscore=" + byscore 90 + ", j2eescore=" + j2eescore + "]"; 91 } 92 93 public static void main(String[] args) { 94 // TODO Auto-generated method stub 95 Set<studen> A=new HashSet<studen>(); 96 //// 97 98 ///// 99 File file = new File("infor.txt"); 100 if(file.exists()&&file.isFile()) 101 { 102 try { 103 FileReader read=new FileReader("infor.txt"); 104 BufferedReader re=new BufferedReader (read); 105 String str; 106 while((str=re.readLine())!=null) 107 { 108 //System.out.println(str); 109 110 String arr[]=str.split(" "); 111 studen st=new studen(); 112 st.setId(Integer.parseInt(arr[0])); 113 st.setName(arr[1]); 114 st.setSex(arr[2].charAt(0)); 115 st.setAddress(arr[3]); 116 st.setJavascore(Integer.parseInt(arr[4])); 117 st.setJavascore(Integer.parseInt(arr[5])); 118 st.setByscore(Integer.parseInt(arr[6])); 119 st.setJ2eescore(Integer.parseInt(arr[7])); 120 System.out.println(st.toString()); 121 A.add(st); 122 123 //System.out.println(); 124 } 125 126 for(Iterator<studen> it=A.iterator();it.hasNext();) 127 { 128 String ss=it.next().toString(); 129 System.out.println("HashSet1***:"+ss+" "); 130 } 131 } catch (FileNotFoundException e) { 132 // TODO Auto-generated catch block 133 System.out.print("can not read the txt"); 134 e.printStackTrace(); 135 } catch (IOException e) { 136 // TODO Auto-generated catch block 137 e.printStackTrace(); 138 } 139 140 } 141 //插入一个新的student对象给集合SET 142 System.out.print("插入一个学生 2, GOQI, f,beijing, 90, 89, 78,94:"); 143 studen stu2=new studen(); 144 stu2.setall(2, "GOQI", ‘f‘,"beijing", 90, 89, 78,94); 145 A.add(stu2); 146 TreeSet<studen> ts=new TreeSet<studen>(); 147 for(Iterator<studen> it=A.iterator();it.hasNext();) 148 { 149 studen cad=it.next(); 150 ts.add(cad); 151 } 152 for(Iterator<studen> it2=ts.iterator();it2.hasNext();) 153 { 154 System.out.println("Treeset(按ID排序结果):"+it2.next().toString()); 155 } 156 //serach student 157 System.out.println("输入待查学生姓名:"); 158 Scanner cin=new Scanner(System.in); 159 if(cin.hasNext()) 160 { 161 String name=cin.next(); 162 studen s1=new studen(); 163 164 ///? 165 int flage=0; 166 studen Temp; 167 for(Iterator<studen> it=A.iterator();it.hasNext();) 168 { 169 Temp = it.next(); 170 if(Temp .getName().equals(name)) 171 { 172 flage=1; 173 System.out.println("exist this student."); 174 System.out.println("该生信息为:"+Temp.toString()); 175 break; 176 } 177 else 178 { 179 ; 180 } 181 } 182 if(flage==0) 183 { 184 System.out.println("Not exist this student."); 185 } 186 } 187 //删除学生 188 189 System.out.println("输入待删除学生姓名:"); 190 Scanner cin2=new Scanner(System.in); 191 if(cin2.hasNext()) 192 { 193 String name2=cin2.next(); 194 195 int flage=0; 196 studen Temp; 197 for(Iterator<studen> it=A.iterator();it.hasNext();) 198 { 199 Temp = it.next(); 200 if(Temp .getName().equals(name2)) 201 { 202 flage=1; 203 it.remove(); 204 System.out.println("removed this student."); 205 206 break; 207 } 208 209 } 210 if(flage==0) 211 { 212 System.out.println("Not exist this student so we can not remove it."); 213 } 214 } 215 //map 216 Map<Integer,TreeSet<studen>> map=new HashMap<Integer, TreeSet<studen>>(); 217 TreeSet<studen> tm0=new TreeSet<studen>(); 218 TreeSet<studen> tm1=new TreeSet<studen>(); 219 TreeSet<studen> tm2=new TreeSet<studen>(); 220 TreeSet<studen> tm3=new TreeSet<studen>(); 221 TreeSet<studen> tm4=new TreeSet<studen>(); 222 Iterator<studen> treeit=ts.iterator(); 223 224 studen sno=new studen(); 225 //将学生信息按照%5ID对应treeset 依次放入。 226 while(treeit.hasNext()) 227 { 228 sno=(studen) treeit.next(); 229 if(sno.getId()%5==0) 230 { 231 tm0.add(sno); 232 } 233 else if(sno.getId()%5==1) 234 { 235 tm1.add(sno); 236 } 237 else if(sno.getId()%5==2) 238 { 239 tm2.add(sno); 240 } 241 else if(sno.getId()%5==3) 242 { 243 tm3.add(sno); 244 } 245 else 246 { 247 tm4.add(sno); 248 } 249 } 250 251 map.put(0,tm0); 252 map.put(1,tm1); 253 map.put(2,tm2); 254 map.put(3,tm3); 255 map.put(4,tm4); 256 ////print map 257 System.out.println("map id:0~4(%5):"); 258 for(Object o : map.keySet()){ 259 260 System.out.println( "map输出:"+map.get(o).size()+"个:"+map.get(o).toString()); } 261 }//main 262 263 264 265 @Override 266 public int hashCode() { 267 final int prime = 31; 268 int result = 1; 269 result = prime * result + id; 270 return result; 271 } 272 @Override 273 public boolean equals(Object obj) { 274 if (this == obj) 275 return true; 276 if (obj == null) 277 return false; 278 if (getClass() != obj.getClass()) 279 return false; 280 studen other = (studen) obj; 281 if (id != other.id) 282 return false; 283 System.out.println("!!!id 重复无法插入,重复ID为:"+id); 284 return true; 285 } 286 @Override 287 public int compareTo(studen o) { 288 // TODO Auto-generated method stub 289 int result=this.getId()>o.getId()?1: 290 (this.getId()==o.getId()?0:-1); 291 return result; 292 } 293 294 }
也是泛型的应用
以上是关于Java学生信息增删改查(并没用数据库)的主要内容,如果未能解决你的问题,请参考以下文章
基于Java web 学生管理系统 最简单的就行 要求实现数据库的增删改查 要求个源代码和包
使用mybatis框架完成学生信息的增删改查操作的完整代码书写-代码拿走即用-看完秒懂哦
使用mybatis框架完成学生信息的增删改查操作的完整代码书写-代码拿走即用-看完秒懂哦