当前位置:   article > 正文

java基础学生管理系统_java系统中工号存在后台怎么判定的

java系统中工号存在后台怎么判定的

java实现简单的学生管理系统

文件搭建:直接复制到项目下
	Admin.txt
				357886-张三-12345
				357887-李四-12345
				357888-王五-12345
	Student.txt
				101-乔峰-99.0
				102-虚竹-99.2
				103-段誉-98.0
## 实体类的封装
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
Admin.java
package com.batis.beans;
/*
 * 	学生管理系统:
 * 		管理员类。
 * 	管理员实体类分析:
 * 		管理员具有
 * 			工号	|	姓名	|	密码
 */
public class Admin {
	private Integer id;
	private String name;
	private String pass;
	public Admin() {
		super();
	}

	public Admin(Integer id, String name, String pass) {
		super();
		this.id = id;
		this.name = name;
		this.pass = pass;
	}

	public int getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	@Override
	public String toString() {
		return this.id + "-" + this.name + "-" + this.pass;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
Student.java
package com.batis.beans;
/*
 * 	学生管理系统:
 * 		学生类。
 * 	学生类分析:
 * 		学生具有的一些属性:
 * 		学号	|	姓名	|	成绩
 */
public class Student {
	private Integer id;
	private String name;
	private double score;
	
	public Student() {
		super();
	}
	
	public Student(Integer id, String name, double score) {
		super();
		this.id = id;
		this.name = name;
		this.score = score;
	}

	public int getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	@Override
	public String toString() {
		return this.id + "-" + this.name + "-" + this.score;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

测试类

package com.batis.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import com.batis.beans.Admin;
import com.batis.beans.Student;

/*
 * 	学生管理系统:主函数
 * 		这个简单程序的主入口。
 */
public class Test {
	// 静态的全局变量,供全局使用
	private static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		menu1();
	}

	// 定义一个方法,要完成管理员的登录和注册
	public static void menu1() {
		System.out.println("==学生管理系统==");
		System.out.println("1:登录");
		System.out.println("2:注册");
		System.out.println("3:退出");
		System.out.println("请输入你的选择");
		int choose = sc.nextInt();
		switch (choose) {
		case 1:
			login();
			break;
		case 2:
			register();
			break;
		case 3:
			System.out.println("Bye~~");
			System.exit(0);// 固定写法
			break;
		default:
			System.out.println("输入的信息有误");
			break;
		}
		menu1();// 跳出switch循环之后再次近路菜单一。
	}

	public static void login() {
		/*
		 * 实现登录的功能 验证账号和密码是否相等 从上面你的数组中我们得知 账号和密码分别是数组中的第一位和第三位
		 */
		System.out.println("请输入您的工号");
		int id = sc.nextInt();
		System.out.println("请输入您的密码");
		String pass = sc.next();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("Admin.txt"));
			String str = null;
			while ((str = br.readLine()) != null) {
				// 对数据进行下一步的处理,对数据进行切割之后存储到一个数组之中
				String[] split = str.split("-");
				/*
				 * 对数据进行判断 因为数据是字符串类型和基本类型进行比较 需要使用基本类型中包装类的方法将字符串转换为基本数据类型载进行比较
				 * 对于基本数据类型的比较实用的是== 而equals比较的是字符串的是否相等。
				 */
				if (Integer.parseInt(split[0]) == id && split[2].equals(pass)) {
					System.out.println("登录成功");
					// 跳转二级菜单之后,一定要使用return结束现在这个方法的调用,不然方法走完循环还会继续执行下去
					menu2(split[1]);
					return;
				}
			}
			System.out.println("登入失败");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 注册:需要知道注册都需要填写什么,然后通过读写操作对数据的写入 需要对管理员进行判断,如果已经存在在了这个文件当中 工号是管理员唯一的
	 * 信息,只需要判断输入的工号是否存在即可 则输出管理员已存在 不存在则进行写入管理员信息 关闭资源时,先开的后关,后开的先关
	 * 追加写是在这个文本后面加一个true
	 */
	public static void register() {
		System.out.println("请输入您的工号:");
		int id = sc.nextInt();
		System.out.println("请输入姓名:");
		String name = sc.next();
		System.out.println("请输入密码:");
		String pass = sc.next();
		BufferedReader br = null;
		BufferedWriter bw = null;

		try {

			// 1.实例化两个流对象
			br = new BufferedReader(new FileReader("Admin.txt"));
			bw = new BufferedWriter(new FileWriter("Admin.txt", true));

			// 2.判断输入的id是否已存在

			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				if (Integer.parseInt(split[0]) == id) {
					System.out.println("该工号已注册!");
					return;
				}
			}

			// 3.向文件中写出数据
			bw.write(new Admin(id, name, pass).toString());
			bw.newLine();
			bw.flush();
			System.out.println("注册成功!");

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public static void menu2(String x) {
		System.out.println("欢迎" + x + "用户登录");
		System.out.println("1:根据学号查找学生");
		System.out.println("2:展示所有学生");
		System.out.println("3:新增一个学生");
		System.out.println("4:移除一个学生");
		System.out.println("5:修改学生成绩");
		System.out.println("6:返回上级菜单");
		int choose = sc.nextInt();
		switch (choose) {
		case 1:
			findOneStudentById();
			break;
		case 2:
			showAll();
			break;
		case 3:
			addOne();
			break;
		case 4:
			removeOne();
			break;
		case 5:
			changeStudentScore();
			break;
		case 6:
			menu1();
			break;
		default:
			System.out.println("输入有误");
			break;
		}
		menu2(x);
	}

	/**
	 * 根据学号查找学生 : 读取学生信息的文本 对文本进行切割,切割到的第一位就是我们想要的内容 对内容进行判定,如果不存在则输出该生不存在
	 * 如果查找到,则输出该学生的信息
	 * 
	 * @return
	 */
	public static Student findOneStudentById() {
		System.out.println("请输入要查找的学生编号:");
		int id = sc.nextInt();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				if (Integer.parseInt(split[0]) == id) {
					System.out.println("学号\t姓名\t成绩");
					System.out.println(split[0] + "\t" + split[1] + "\t" + split[2]);
					return new Student(Integer.parseInt(split[0]), split[1], Double.parseDouble(split[2]));
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		System.out.println("该学生不存在!");
		return null;
	}

	/**
	 * 展示所有学生信息: 读取文件,输出文件
	 */
	public static void showAll() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			String str = null;
			System.out.println("学号\t姓名\t成绩");
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				System.out.println(split[0] + "\t" + split[1] + "\t" + split[2]);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/*
	 * 添加一个学生信息,需要读取文件,判断学生是否已经存在这个文件之中 如果存在输出该学生已经存在
	 * 不存在则进行天添加的动作,添加应该是追加写,所以在后面需要加一个参数true
	 */
	public static void addOne() {
		System.out.println("请输入添加的学生id");
		int stuId = sc.nextInt();
		System.out.println("请输入添加的学生的姓名");
		String name = sc.next();
		System.out.println("请输入添加的学生的成绩");
		double score = sc.nextDouble();
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			bw = new BufferedWriter(new FileWriter("Student.txt", true));
			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				if (Integer.parseInt(split[0]) == stuId) {
					System.out.println("该学生已经存在");
					return;
				}
			}
			bw.write(new Student(stuId, name, score).toString());
			bw.newLine();
			bw.flush();
			System.out.println("添加成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 删除一个学生,则这个学生可以调用根据id查找学生的方法来使用 查找到之后在进行删除的
	 */
	public static void removeOne() {
		Student stu = findOneStudentById();
		/*
		 * 删除:如果找到的这个对象为空直接结束掉方法 不为空才能进行删除 删除的时候需要进行确认
		 */
		if (stu == null) {
			return;
		}
		System.out.println("是否确定删除(Y/N)");
		String choose = sc.next();
		if (choose.equals("N")) {
			return;
		}
		BufferedReader br = null;
		BufferedWriter bw = null;
		// 定义一个集合用来存储删除之后的其他元素
		ArrayList<Student> list = new ArrayList<Student>();
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				if (stu.getId() == Integer.parseInt(split[0])) {
					continue;
				}
				list.add(new Student(Integer.parseInt(split[0]), split[1], Double.parseDouble(split[2])));
			}
			/*
			 * 已经把其他的学生添加到集合当中,所以只需要遍历集合 将集合中的元素重新写入到文本之中
			 */
			bw = new BufferedWriter(new FileWriter("student.txt"));
			for (Student student : list) {
				bw.write(student.toString());
				bw.newLine();
				bw.flush();
				;
			}
			System.out.println("移除成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/*
	 * 修改学生成绩 可以根据查找到的学生来对他的成绩进行修改
	 * 
	 */
	public static void changeStudentScore() {
		Student student = findOneStudentById();
		if (student == null) {
			return;
		}
		System.out.println("请输入学生新的成绩");
		double newScore = sc.nextDouble();
		BufferedReader br = null;
		BufferedWriter bw = null;
		ArrayList<Student> list = new ArrayList<Student>();
		try {
			br = new BufferedReader(new FileReader("Student.txt"));
			String str = null;
			while ((str = br.readLine()) != null) {
				String[] split = str.split("-");
				//找到这个学生
				if (student.getId() == Integer.parseInt(split[0])) {
					//先添加到集合,即保证这一个修改到。
					list.add(new Student(Integer.parseInt(split[0]), split[1], newScore));
					continue;
				}
				list.add(new Student(Integer.parseInt(split[0]), split[1], Double.parseDouble(split[2])));
			}

			// 将集合中的数据写出到Student.txt中
			bw = new BufferedWriter(new FileWriter("Student.txt"));
			for (Student stu : list) {
				bw.write(stu.toString());
				bw.newLine();
				bw.flush();
			}
			System.out.println("修改成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/776777
推荐阅读
相关标签
  

闽ICP备14008679号