import java.io.IOException;
import java.util.ArrayList;
public class App {
//主方法
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
ArrayList al = new ArrayList<>();//创建一个存数据的集合
Meau.function(al);//调用功能
Operation.writeTxt(al);//写出文件
}
}
学生类
定义学生的属性
import java.util.Objects;
public class Student {
private String id;//ID
private String name;//姓名
private String sex;//性别
private int age;//年龄
private String telephone;//电话号码
public Student() {}//无参构造方法
//有参构造方法
public Student(String id, String name, String sex, int age, String telephone) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.telephone = telephone;
}
public String getId() {
return id;
}
public void setId(String 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
//重写tostring方法
public String toString() {
return id + " " + name + " " + sex + " " + age + " " + telephone+"rn";
}
}
菜单类
主要是打印菜单
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Meau {
//主菜单
public static void meau() {
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("------0.退出------");
}
//查找菜单
public static void findMeau() {
System.out.println("------请选择功能------");
System.out.println("------1.按ID进行查找------");
System.out.println("------2.按姓名进行查找------");
System.out.println("------3.按性别进行查找------");
System.out.println("------4.按年龄进行查找------");
System.out.println("------0.返回上一级------");
}
//修改菜单
public static void madificationMeau() {
System.out.println("------请选择功能------");
System.out.println("------1.修改ID------");
System.out.println("------2.修改姓名------");
System.out.println("------0.返回上一级------");
}
//删除菜单
public static void deleteMeau() {
System.out.println("------请选择功能------");
System.out.println("------1.按ID进行删除------");
System.out.println("------2.按姓名进行删除------");
System.out.println("------0.返回上一级------");
}
//排序菜单
public static void rankMeau() {
System.out.println("------请选择功能------");
System.out.println("------1.按ID排序------");
System.out.println("------2.按姓名排序------");
System.out.println("------3.按性别排序------");
System.out.println("------4.按年龄排序------");
System.out.println("------0.返回上一级------");
}
//选择功能
public static void function(ArrayList al) throws IOException {
Operation.readTxt(al);
while(true) {
meau();//打印功能信息
Scanner r = new Scanner(System.in);
System.out.println("请输入您要选择的功能:");
int k=r.nextInt();
switch(k) {
case 1:
Operation.addStudent(al);//添加
break;
case 2:
Operation.findStudent(al);//查找
break;
case 3:
Operation.madificationStudent(al);//修改
break;
case 4:
Operation.deleteStudent(al);//删除
break;
case 5:
Operation.rankStudent(al);//排序
break;
case 0:
//退出提示
System.out.println("欢迎您的使用!");
break;
default:
System.out.println("您输入的功能有误!");
}
if(k==0) break;
}
}
}
验证类
验证用户输入的信息是否合法
import java.util.ArrayList;
import java.util.Scanner;
//校检
public class Check {
//验证ID
public static String checkId(String id,ArrayList al){
Scanner r = new Scanner(System.in);
while(true) {
int k = id.length();//获取ID长度
if(k<=0||k>8) {
System.out.println("请重新输入:");
id = r.next();
}else {
return id;
}
}
}
//姓名长度
static int namelength(String name) {
int length = 0;
String ch = "[u0391-uFFE5]";//正则表达式范围
for (int i = 0; i < name.length(); i++) {
String temp = name.substring(i,i+1);//获取一个字符
if(temp.matches(ch)) {//告知此字符串是否匹配给定的正则表达式。
length+=2;//含汉字
}else {
length+=1;
}
}
return length;
}
//验证name
public static String checkName(String name){
Scanner r = new Scanner(System.in);
while(true) {
int k = namelength(name);//获取name长度
//进行比较
if(k<=0||k>8) {
System.out.println("请重新输入:");
name = r.next();
}else {
return name;
}
}
}
//验证性别
public static String checkSex(String sex){
Scanner r = new Scanner(System.in);
while(true) {
//判断性别正确
if(sex.equals("M")||sex.equals("F")) {
return sex;
}else {
System.out.println("请重新输入:");
sex = r.next();
}
}
}
//验证年龄
public static int checkAge(int age){
Scanner r = new Scanner(System.in);
while(true) {
if(age<0||age>130) {//判断年龄是否合法
System.out.println("请重新输入:");
age = r.nextInt();
}else {
return age;
}
}
}
//验证电话
public static String checkTel(String tel){
Scanner r = new Scanner(System.in);
while(true) {
int k = tel.length();//获取tel长度
if(k!=11) {
System.out.println("请重新输入:");
tel = r.next();
}else {
return tel;
}
}
}
}
操作类
各种学生操作
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;
//操作
public class Operation {
//添加
public static void addStudent(ArrayList al) {
Scanner r = new Scanner(System.in);
System.out.println("请输入你要添加学生的ID:");
String id = r.nextLine();//输入ID
id = Check.checkId(id,al);//验证
System.out.println("请输入你要添加学生的姓名:");
String name = r.nextLine();//输入姓名
name = Check.checkName(name);//验证
System.out.println("请输入你要添加学生的性别:(男:M 女:F)");
String sex = r.nextLine();//输入性别
sex = Check.checkSex(sex);//验证
System.out.println("请输入你要添加学生的年龄:");
int age = r.nextInt();//输入年龄
age = Check.checkAge(age);//验证
System.out.println("请输入你要添加学生的电话:");
String tel = r.next();//输入电话
tel = Check.checkTel(tel);//验证
//创建新的Student对象用于存添加的用户信息
Student tian = new Student(id,name,sex,age,tel);
al.add(tian);//将创建用户添加到集合中
System.out.println("添加成功!");
}
//查找
public static void findStudent(ArrayList al) throws IOException {
Scanner r = new Scanner(System.in);
Meau.findMeau();//打印查找菜单
System.out.println("请输入你要选择的功能:");
String k = r.next();
switch(k) {
//id
case "1":
System.out.println("请输入查找人的ID:");
String id = r.next();//输入ID
//遍历集合
Iterator it1 = al.iterator();
System.out.println("Idt"+"姓名t"+"性别t"+"年龄t"+"电话n");
while(it1.hasNext()) {
Student stu = it1.next();//将每一行信息存到Student中用于查找
if(stu.getId().equals(id)) {//输入的id与集合中数据进行比较
System.out.println(stu.getId()+"t"+stu.getName()+"t"+stu.getSex()+"t"+stu.getAge()+"t"+stu.getTelephone()+"n");
}
}
break;
//name
case "2":
System.out.println("请输入查找人的姓名:");
String name = r.next();//输入name
//遍历集合
Iterator it2 = al.iterator();//将每一行信息存到Student中用于查找
System.out.println("Idt"+"姓名t"+"性别t"+"年龄t"+"电话n");
while(it2.hasNext()) {
Student stu = it2.next();
if(stu.getName().equals(name)) {//输入的name与集合中数据进行比较
System.out.println(stu.getId()+"t"+stu.getName()+"t"+stu.getSex()+"t"+stu.getAge()+"t"+stu.getTelephone()+"n");
}
}
break;
//sex
case "3":
System.out.println("请输入查找人的性别:");
String sex = r.next();//输入name
//遍历集合
Iterator it3 = al.iterator();
System.out.println("Idt"+"姓名t"+"性别t"+"年龄t"+"电话n");
while(it3.hasNext()) {
Student stu = it3.next();
if(stu.getSex().equals(sex)) {//输入的性别与集合中数据进行比较
System.out.println(stu.getId()+"t"+stu.getName()+"t"+stu.getSex()+"t"+stu.getAge()+"t"+stu.getTelephone()+"n");
}
}
break;
//age
case "4":
System.out.println("请输入查找人的年龄:");
int age = r.nextInt();//输入年龄
//遍历集合
Iterator it4 = al.iterator();
System.out.println("Idt"+"姓名t"+"性别t"+"年龄t"+"电话n");
while(it4.hasNext()) {
Student stu = it4.next();
if(stu.getAge()==age) {//输入的年龄与集合中数据进行比较
System.out.println(stu.getId()+"t"+stu.getName()+"t"+stu.getSex()+"t"+stu.getAge()+"t"+stu.getTelephone()+"n");
}
}
break;
case "0":
Meau.function(al);//返回上一级功能
break;
default:
System.out.println("您输入的功能有误!");
}
}
//修改
public static void madificationStudent(ArrayList al) throws IOException {
Scanner r = new Scanner(System.in);
Meau.madificationMeau();//打印修改菜单
System.out.println("请输入你要选择的功能:");
String k = r.next();
switch(k) {
//id
case "1":
System.out.println("请输入要修改人的ID:");
String id = r.next();
//遍历集合
Iterator it1 = al.iterator();
int i=0;//看在集合的位置
while(it1.hasNext()) {
Student stu = it1.next();
if(stu.getId().equals(id)) {//寻找用户
modificationFunction(al, stu,i);//调用修改功能
break;
}
i++;
}
break;
//name
case "2":
System.out.println("请输入要修改人的姓名:");
String name = r.next();
//遍历集合
Iterator it2 = al.iterator();
i=0;//看在集合的位置
while(it2.hasNext()) {
Student stu = it2.next();
if(stu.getName().equals(name)) {//寻找用户
modificationFunction(al, stu,i);//调用修改功能
break;
}
i++;
}
break;
case "0":
Meau.function(al);//返回上一级功能
break;
default:
System.out.println("您输入的功能有误!");
}
}
//修改功能
public static void modificationFunction(ArrayList al,Student stu,int i) {
Scanner r = new Scanner(System.in);
System.out.println("请输入要修改的信息:1.id 2.姓名 3.性别 4.年龄 5.电话");
String k = r.next();//信息选项
switch(k) {
//id
case "1":
System.out.println("请输入ID:");
String id = r.next();//用户输入修改信息
stu.setId(id);//改变ID值
al.set(i, stu);//覆盖集合中对应数值
System.out.println("修改成功!");
break;
//name
case "2":
System.out.println("请输入姓名:");
String name = r.next();//用户输入修改信息
stu.setName(name);//改变name值
al.set(i, stu);//覆盖集合中对应数值
System.out.println("修改成功!");
break;
//sex
case "3":
System.out.println("请输入性别:");
String sex = r.next();//用户输入修改信息
stu.setSex(sex);//改变sex值
al.set(i, stu);//覆盖集合中对应数值
System.out.println("修改成功!");
break;
//age
case "4":
System.out.println("请输入年龄:");
int age = r.nextInt();//用户输入修改信息
stu.setAge(age);//改变age值
al.set(i, stu);//覆盖集合中对应数值
System.out.println("修改成功!");
break;
//tel
case "5":
System.out.println("请输入电话:");
String tel = r.next();//用户输入修改信息
stu.setTelephone(tel);//改变电话值
al.set(i, stu);//覆盖集合中对应数值
System.out.println("修改成功!");
break;
default:
System.out.println("输入错误!");
}
}
//删除
public static void deleteStudent(ArrayList al) throws IOException {
Scanner r = new Scanner(System.in);
Meau.deleteMeau();//打印删除菜单
System.out.println("请输入你要选择的功能:");
int k = r.nextInt();
switch(k) {
//id
case 1:
System.out.println("请输入要删除人的ID:");
String id = r.next();//用户输入要删除人的信息
Iterator it1 = al.iterator();
int i=0;//集合中的位置
while(it1.hasNext()) {
Student stu = it1.next();
if(stu.getId().equals(id)) {//寻找用户
al.remove(i);//删除用户
System.out.println("删除成功!");
break;
}
i++;
}
break;
//name
case 2:
System.out.println("请输入要删除人的姓名:");
String name = r.next();//用户输入要删除人的信息
Iterator it2 = al.iterator();
i=0;//集合中的位置
while(it2.hasNext()) {
Student stu = it2.next();
if(stu.getName().equals(name)) {//寻找用户
al.remove(i);//删除用户
System.out.println("删除成功!");
break;
}
i++;
}
break;
case 0:
Meau.function(al);//返回上一级功能
break;
default:
System.out.println("您输入的功能有误!");
}
}
//排序
public static void rankStudent(ArrayList al) throws IOException {
Scanner r = new Scanner(System.in);
Meau.rankMeau();//打印排序菜单
System.out.println("请输入你要选择的功能:");
int k = r.nextInt();
switch(k) {
case 1:
//使用匿名内部类重写方法对集合进行排序
Collections.sort(al, new Comparator() {
//重写compare方法
public int compare(Student s1,Student s2) {
// TODO 自动生成的方法存根
return s1.getId().compareTo(s2.getId());
}
});
Iterator it1 = al.iterator();
System.out.println("Idt"+"姓名t"+"性别t"+"年龄t"+"电话n");
//打印
while(it1.hasNext()){
Student s = it1.next();
System.out.println(s);
}
break;
case 2:
//使用匿名内部类重写方法对集合进行排序
Collections.sort(al, new Comparator() {
//重写compare方法
public int compare(Student s1,Student s2) {
// TODO 自动生成的方法存根
return s1.getName().compareTo(s2.getName());
}
});
Iterator it2 = al.iterator();
System.out.println("Idt"+"姓名t"+"性别t"+"年龄t"+"电话n");
//打印
while(it2.hasNext()){
Student s = it2.next();
System.out.println(s);
}
break;
case 3:
//使用匿名内部类重写方法对集合进行排序
Collections.sort(al, new Comparator() {
//重写compare方法
public int compare(Student s1,Student s2) {
// TODO 自动生成的方法存根
return s1.getSex().compareTo(s2.getSex());
}
});
Iterator it3 = al.iterator();
System.out.println("Idt"+"姓名t"+"性别t"+"年龄t"+"电话n");
//打印
while(it3.hasNext()){
Student s = it3.next();
System.out.println(s);
}
break;
case 4:
//使用匿名内部类重写方法对集合进行排序
Collections.sort(al, new Comparator() {
//重写compare方法
public int compare(Student s1,Student s2) {
// TODO 自动生成的方法存根
return String.valueOf(s1.getAge()).compareTo(String.valueOf(s2.getAge()));
}
});
Iterator it = al.iterator();
System.out.println("Idt"+"姓名t"+"性别t"+"年龄t"+"电话n");
//打印
while(it.hasNext()){
Student s = it.next();
System.out.println(s);
}
break;
case 0:
Meau.function(al);//返回上一级功能
break;
default:
System.out.println("您输入的功能有误!");
}
}
//读取外部信息
public static void readTxt(ArrayList al) throws IOException {
Scanner r = new Scanner(System.in);
BufferedReader br = new BufferedReader(new FileReader("信息.txt"));//创建对象
String line;
//将文件数据添加到集合中
while((line=br.readLine())!=null) {
String[] s = line.split(" ");
Student stu = new Student(s[0],s[1],s[2],Integer.parseInt(s[3]),s[4]);
al.add(stu);
}
}
public static void writeTxt(ArrayList al) throws IOException {
BufferedWriter out = new BufferedWriter(new FileWriter("信息.txt"));//创建对象
Iterator key = al.iterator();
//重写文件
while(key.hasNext()) {
Student jia = key.next();
out.write(jia.toString());//写入文件
}
out.close();//关闭流
}
}
总结
第一写,不知道如何去写,就这吧!!!



