本帖最后由 渝小妹 于 2018-2-4 11:52 编辑
升级的内容为:1.使用io流简单的吧存储的学生信息和管理员的账号密码信息存到了本地
2.对管理员的账号密码存储在本地的信息进行了一个简单的加密和解密。
3.实在不知道在加什么功能了,就先这样吧
现在版本等级应该为:1.1版
package MyStudentManager;
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;
/*
* 学生管理系统的主类
* */
public class MyStudentManager {
//登陆入口
public static void main(String[] args) throws IOException {
ArrayList<Student> aList = new ArrayList<>();
ArrayList<User> aList2 = new ArrayList<>();
String fileName = "学生信息表";
String fileName1 = "管理员表";
Scanner scanner = new Scanner(System.in);
System.out.println("----欢迎使用上午茶的学生管理系统-------");
System.out.println("----1.请选择注册----2.请选择登陆----");
int a = scanner.nextInt();
boolean flag = false;
switch (a) {
case 1:
registrStudent(fileName1);
flag = loginStudent(fileName1);
if (flag) {
inter(aList,aList2,fileName,fileName1);
}
break;
case 2:
flag = loginStudent(fileName1);
if (flag) {
inter(aList,aList2,fileName,fileName1);
}
break;
default:
System.out.println("谢谢你的使用");
System.exit(0);
break;
}
}
//功能界面
public static void inter(ArrayList<Student> aList,ArrayList<User> aList2,String fileName,String fileName1) throws IOException {
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("----欢迎来到上午茶的学生管理系统界面功能-");
System.out.println("----1.查看所有学生信息-------------");
System.out.println("----2.添加学生------------------");
System.out.println("----3.删除学生------------------");
System.out.println("----4.修改学生------------------");
System.out.println("----5.找出所有女同学--------------");
System.out.println("----6.重登陆系统------------------");
System.out.println("----7.修改密码------------------");
System.out.println("----8.退出系统------------------");
System.out.println("请输入你选择:");
String choiString = scanner.nextLine();
//使用switch语句选择功能
switch (choiString) {
case "1":
//查看所有学生
findAllStudent(fileName);
break;
case "2":
//添加学生
addStudent(fileName);
break;
case "3":
//删除学生
deletStudent(fileName);
break;
case "4":
updateStudent(fileName);
//修改学生
break;
case "5":
//找出所有女同学
checkStudent(fileName);
break;
case "6":
//重登系统
boolean flag = loginStudent(fileName1);
if (flag) {
inter(aList,aList2,fileName,fileName1);
}
break;
case "7":
//修改用户密码
updatePassword(fileName1);
break;
default:
System.out.println("谢谢你的使用");
System.exit(0);
break;
}
}
}
//查看学生
public static void findAllStudent(String fileName) throws IOException {
ArrayList<Student> aList = new ArrayList<>();
readerData(fileName,aList);
if (aList.size()==0) {
System.out.println("不好意思,没有任何学生信息可查,请返回重新选择。");
return;
}
System.out.println("学号\t\t姓名\t性别\t年龄\t学科\t居住地");
for (int i = aList.size()-1; i >=0; i--){
Student s = aList.get(i);
System.out.println(s.getId( )+"\t"+s.getName()+"\t"+s.getSex()+"\t"+s.getAge()+"\t"+s.getSubject()+"\t"+s.getAdderss());
}
}
//添加学生
public static void addStudent(String fileName) throws IOException{
Scanner scanner = new Scanner(System.in);
ArrayList<Student> aList = new ArrayList<>();
readerData(fileName, aList);
while(true) {
boolean flag = false;
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
for (int i = aList.size()-1; i >= 0; i--) {
Student s1 = aList.get(i);
if (id.equals(aList.get(i).getId())) {
flag = true;
break;
}
}
if (!flag) {
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入性别:");
String sex= scanner.nextLine();
System.out.println("请输入学生年龄:");
String age = scanner.nextLine();
System.out.println("请输入学生学科:");
String subj = scanner.nextLine();
System.out.println("请输入居住地:");
String address= scanner.next();
Student s1 = new Student(id, name, sex, age, subj, address);
aList.add(s1);
System.out.println("添加成功");
break;
}else {
System.out.println("该学生已经存在,请重新选择");
}
}
WriterData(fileName, aList);
}
//删除学生
public static void deletStudent(String fileName) throws IOException {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> aList = new ArrayList<>();
readerData(fileName, aList);
System.out.println("请输入你要删除的学生的学号:");
String id = scanner.nextLine();
int index = -1;
for (int i = aList.size()-1; i >=0; i--){
Student s1 = aList.get(i);
if (s1.getId().equals(id)) {
index = i;
break;
}
}
if (index==-1) {
System.out.println("不好意思,你要删除的学号的信息不存在,请重新选择。");
}else {
aList.remove(index);
System.out.println("删除学生成功");
}
WriterData(fileName, aList);
}
//修改学生
public static void updateStudent(String fileName) throws IOException {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> aList = new ArrayList<>();
readerData(fileName, aList);
System.out.println("请输入你要修改的学生的学号:");
String id = scanner.nextLine();
int index = -1;
for (int i = aList.size()-1; i >=0; i--) {
Student student = aList.get(i);
if (student.getId().equals(id)){
index = i;
break;
}
}
if (index ==-1) {
System.out.println("不好意思,你要修改的学号的信息不存在,请重新选择。");
}else {
System.out.println("请输入修改的学号:");
String id1 = scanner.nextLine();
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入性别:");
String sex= scanner.nextLine();
System.out.println("请输入学生年龄:");
String age = scanner.nextLine();
System.out.println("请输入学生学科:");
String subj = scanner.nextLine();
System.out.println("请输入居住地:");
String address= scanner.next();
Student s1 = new Student(id1, name, sex, age, subj, address);
aList.set(index, s1);
System.out.println("修改成功");
}
WriterData(fileName, aList);
}
//找出所有女同学
public static void checkStudent(String fileName) throws IOException {
System.out.println("学号\t\t姓名\t性别\t年龄\t学科\t居住地");
ArrayList<Student> aList = new ArrayList<>();
readerData(fileName, aList);
for (int i = aList.size()-1; i >= 0; i--) {
Student student = aList.get(i);
if (student.getSex().equals("女")) {
System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getSex()+"\t"+student.getAge()+"\t"+student.getSubject()+"\t"+student.getAdderss());
}
}
}
//注册功能加密
public static void registrStudent(String fileName1) throws IOException {
ArrayList<User> aList2 = new ArrayList<>();
int a = 1164935710;
int b = 1350945678;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的用户名:");
String admin = scanner.nextLine();
System.out.println("请输入你的密码:");
String password = scanner.nextLine();
int admin1 = Integer.parseInt(admin);
int num1 = admin1^a;
int password1 = Integer.parseInt(password);
int num2 = password1^b;
User user = new User(num1+"", num2+"");
aList2.add(user);
WriterData1(fileName1, aList2);
}
//登陆功能解密
public static boolean loginStudent(String fileName1) throws IOException {
ArrayList<User> aList2 = new ArrayList<>();
readerData1(fileName1, aList2);
boolean flag = false;
int count = 2;
while(count>=0) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的注册时的用户名:");
String string = scanner.nextLine();
System.out.println("你的用户名已被加密,请输入解密密码:");
int a = scanner.nextInt();
System.out.println("请输入你的注册时的密码:");
String string1 = scanner.next();
System.out.println("你的密码已被加密,请输入解密密码:");
int a1 = scanner.nextInt();
for (int i = aList2.size()-1; i >=0; i--) {
User aString2 = aList2.get(i);
int num = Integer.parseInt(aString2.getUsrename());
int stringa = num^a;
String aString = stringa+"";
int num1 = Integer.parseInt(aString2.getPaswword());
int stringa1 = num1^a1;
String bString = stringa1+"";
if (aString.equals(string)&&string1.equals(bString)) {
flag =true;
return flag;
}
if (count==0) {
System.out.println("已输错三次,退出系统,请联系管理员");
System.exit(0);
}else {
System.out.println("账户密码错误,你还剩"+count+"次");
count--;
}
}
}
return flag;
}
//修改账户密码
public static void updatePassword(String fileName1) throws IOException {
ArrayList<User> aList2 = new ArrayList<>();
readerData1(fileName1, aList2);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要修改密码的用户名");
String aString = scanner.nextLine();
System.out.println("请输入你的解密密码:");
int a = scanner.nextInt();
int index = -1;
for (int i = aList2.size()-1; i >= 0; i--) {
User user = aList2.get(i);
int num =Integer.parseInt(user.getUsrename());
int num1 =num^a;
if (aString.equals(num1+"")) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("不好意思,没有该用户名,请重新选择");
}else {
System.out.println("请输入用户名:");
String str = scanner.nextLine();
System.out.println("请输入密码:");
String str1 = scanner.nextLine();
User user = new User(str, str1);
aList2.set(index, user);
System.out.println("恭喜你,修改成功");
}
WriterData1(fileName1, aList2);
}
//用于读取学生信息表
public static void readerData(String fileName,ArrayList<Student> aList) throws IOException {
BufferedReader aReader = new BufferedReader(new FileReader(fileName));
String line;
while((line= aReader.readLine())!=null) {
String []arr = line.split(",");
Student s = new Student();
s.setId(arr[0]);
s.setName(arr[1]);
s.setSex(arr[2]);
s.setAge(arr[3]);
s.setSubject(arr[4]);
s.setAdderss(arr[5]);
aList.add(s);
}
aReader.close();
}
//用于写数据进学生信息表
public static void WriterData(String fileName,ArrayList<Student> aList) throws IOException {
BufferedWriter aWriter = new BufferedWriter(new FileWriter(fileName));
for (int i = 0; i <aList.size(); i++) {
Student s1 = aList.get(i);
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(s1.toString());
String string = new String(sBuilder);
String string2 = string.toString();
aWriter.write(string2);
aWriter.newLine();
aWriter.flush();
}
aWriter.close();
}
//用于读取管理员数据
public static void readerData1(String fileName1,ArrayList<User> aList2) throws IOException {
BufferedReader aReader = new BufferedReader(new FileReader(fileName1));
String line;
while((line= aReader.readLine())!=null) {
String []arr = line.split(",");
User s = new User();
s.setUsrename(arr[0]);
s.setPaswword(arr[1]);
aList2.add(s);
}
aReader.close();
}
//用于写数据进管理员表
public static void WriterData1(String fileName1,ArrayList<User> aList) throws IOException {
BufferedWriter aWriter = new BufferedWriter(new FileWriter(fileName1));
for (int i = 0; i <aList.size(); i++) {
User s1 = aList.get(i);
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(s1.toString());
String string = new String(sBuilder);
String string2 = string.toString();
aWriter.write(string2);
aWriter.newLine();
aWriter.flush();
}
aWriter.close();
}
}
|
|