使用数组,循环,排序,函数,做一个简单的用户管理系统
用来应付结课的大作业是再好不过了
写在一个类中,耦合度高,具体功能包括:
增加一个用户
删除一个用户
修改一个用户信息
查询所有用户信息
用户工资排名
保存用户信息到指定目录(这里简化为本人电脑桌面)
退出
package com.guanli;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
public class client {
public static void main(String[] args) {
//功能包括:添加用户,删除用户,修改用户,查询用户,工资排名,退出系统
//用户信息包括,id,姓名,年龄,性别,工资,邮箱
firstIndex();//首页面
int a=choiceIndex();//选择页面,并返回选择
int[] id = new int[100];//编号
String[] name = new String[100];//姓名
String[] sex = new String[100];//性别
int[] age = new int[100];//年龄
float[] money = new float[100];//工资
String[] email = new String[100];//邮箱
//给予初始值
id[0]=1;name[0]="轻微";sex[0]="男";age[0]=22;money[0]=1234.5f;email[0]="123456.com";
id[1]=2;name[1]="大黄";sex[1]="男";age[1]=252;money[1]=1884.5f;email[1]="533456.com";
id[2]=3;name[2]="沙发";sex[2]="女";age[2]=32;money[2]=3334.5f;email[2]="weewf3456.com";
int[] count=new int[1];
count[0]=3;//记录数量
toIndex(a, id, name, sex, age, money, email, count);//选择去向
}
public static void addEmployee(int[] id,String[] name,String[] sex,int[] age,float[] money,String[] email,int[] count) {//增加用户
System.out.println("增加用户");
System.out.println("----------------------");
Scanner scanner=new Scanner(System.in);
System.out.println("输入用户id");
int add = scanner.nextInt();
id[count[0]]=add;
System.out.println("输入用户姓名");
String add1 = scanner.next();
name[count[0]]=add1;
System.out.println("输入用户性别");
String add2 = scanner.next();
sex[count[0]]=add2;
System.out.println("输入用户年龄");
int add3 = scanner.nextInt();
age[count[0]]=add3;
System.out.println("输入用户工资");
Float add4 = scanner.nextFloat();
money[count[0]]=add4;
System.out.println("输入用户邮箱");
String add5 = scanner.next();
email[count[0]]=add5;
System.out.println("输入用户信息成功");
count[0]++;
int a=choiceIndex();
toIndex(a, id, name, sex, age, money, email, count);
}
public static void deleteEmployee(int[] id,String[] name,String[] sex,int[] age,float[] money,String[] email,int[] count) {//删除用户
System.out.println("删除用户");
System.out.println("--------------------");
System.out.println("请输入要删除的用户id");
Scanner scanner = new Scanner(System.in);
int id1 = scanner.nextInt();
boolean test1=false;//查询是否有输入id
for(int i=0;i<id.length-1;i++) {
if(id[i]==id1) {
test1=true;
break;
}
}
if(test1) {
int test=0;
for(int i=0;i<id.length-1;i++) {
if(id[i]==id1) {
test=i;
break;
}
}
for(int i=test;i<id.length-1;i++) {//count长度
//查到id,删除
id[i]=id[i+1];
name[i]=name[i+1];
sex[i]=sex[i+1];
age[i]=age[i+1];
money[i]=money[i+1];
email[i]=email[i+1];
}
System.out.println("删除id为"+id1+"的用户成功");
count[0]--;
}else {
System.out.println("未找到id为"+id1+"的用户");
}
int a=choiceIndex();
toIndex(a, id, name, sex, age, money, email, count);
}
public static void coutOnDesk(int[] id,String[] name,String[] sex,int[] age,float[] money,String[] email,int[] count) {//删除用户
System.out.println("输出所有用户信息到桌面");
System.out.println("--------------------");
PrintStream p = System.out;
try (PrintStream ps = new PrintStream(new FileOutputStream("C:\\Users\\11251\\Desktop\\12346.txt"))) {
// 将标准输出重定向到ps输出流
System.setOut(ps);
// 不在控制台显示
System.out.printf("%-10s","id");
System.out.printf("%-10s","name");
System.out.printf("%-10s","sex"+" ");
System.out.printf("%-10s","age");
System.out.printf("%-10s","money");
System.out.printf("%-10s","email");
System.out.println(" ");
for(int i=0;i<count[0];i++) {
//System.out.println(count[0]);
System.out.printf("%-10s",id[i]);
System.out.printf("%-10s",name[i]);
System.out.printf("%-10s",sex[i]);
System.out.printf("%-10s",age[i]);
System.out.printf("%-10s",money[i]);
System.out.printf("%-10s",email[i]);
System.out.println(" ");
}
// 重定向回到控制台输出
System.setOut(p);
System.out.println("成功保存所有用户信息到桌面");
toIndex(choiceIndex(), id, name, sex, age, money, email, count);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void changeEmployee(int[] id,String[] name,String[] sex,int[] age,float[] money,String[] email,int[] count) {//修改用户信息
System.out.println("修改用户信息");
System.out.println("--------------------");
System.out.println("请输入要修改的用户id");
Scanner scanner = new Scanner(System.in);
int id1 = scanner.nextInt();
boolean test1=false;//查询是否有输入id
for(int i=0;i<id.length-1;i++) {
if(id[i]==id1) {
test1=true;
break;
}
}
if(test1) {
for(int i=0;i<count[0];i++) {
if(id[i]==id1) {
id[i]=id[i];//编号不改
Scanner scanner1=new Scanner(System.in);
System.out.println("输入修改后的用户姓名");
String add1 = scanner1.next();
name[i]=add1;
System.out.println("输入修改后的用户性别");
String add2 = scanner1.next();
sex[i]=add2;
System.out.println("输入修改后的用户年龄");
int add3 = scanner1.nextInt();
age[i]=add3;
System.out.println("输入修改后的用户工资");
Float add4 = scanner1.nextFloat();
money[i]=add4;
System.out.println("输入修改后的用户邮箱");
String add5 = scanner1.next();
email[i]=add5;
System.out.println("修改用户信息成功");
}
}
}else {
System.out.println("未找到id为"+id1+"的用户");
}
int a=choiceIndex();
toIndex(a, id, name, sex, age, money, email, count);
}
public static void findEmployee(int[] id,String[] name,String[] sex,int[] age,float[] money,String[] email,int[] count) {//查询用户信息
System.out.println("查询用户信息");
System.out.printf("%-10s","id");
System.out.printf("%-10s","name");
System.out.printf("%-10s","sex"+" ");
System.out.printf("%-10s","age");
System.out.printf("%-10s","money");
System.out.printf("%-10s","email");
System.out.println(" ");
for(int i=0;i<count[0];i++) {
//System.out.println(count[0]);
System.out.printf("%-10s",id[i]);
System.out.printf("%-10s",name[i]);
System.out.printf("%-10s",sex[i]);
System.out.printf("%-10s",age[i]);
System.out.printf("%-10s",money[i]);
System.out.printf("%-10s",email[i]);
System.out.println(" ");
}
int a=choiceIndex();
toIndex(a, id, name, sex, age, money, email, count);
}
public static void moneySort(int[] id,String[] name,String[] sex,int[] age,float[] money,String[] email,int[] count) {//工资排名
System.out.println("工资排名结果如下");
if(count[0] > 0){
for(int i = 0;i<count[0];i++){
for(int j = 0;j<count[0] - 1 - i;j++){
if(money[j] < money[j+1]){//比较工资,排序
int temp = id[j];
id[j] = id[j+1];
id[j+1] = temp;
String temp1 = name[j];
name[j] = name[j+1];
name[j+1] = temp1;
String temp2 = sex[j];
sex[j] = sex[j+1];
sex[j+1] = temp2;
int temp3 = age[j];
age[j] = age[j+1];
age[j+1] = temp3;
Float temp4 = money[j];
money[j] = money[j+1];
money[j+1] = temp4;
String temp5 = email[j];
email[j] = email[j+1];
email[j+1] = temp5;
}
}
}
}
System.out.printf("%-8s","排名");
System.out.printf("%-10s","id");
System.out.printf("%-12s","name");
System.out.printf("%-10s","sex"+" ");
System.out.printf("%-10s","age");
System.out.printf("%-10s","money");
System.out.printf("%-10s","email");
System.out.println(" ");
int num=1;
for(int i=0;i<count[0];i++) {
//System.out.println(count[0]);
System.out.printf("%-10s",num++);
System.out.printf("%-10s",id[i]);
System.out.printf("%-10s",name[i]);
System.out.printf("%-10s",sex[i]);
System.out.printf("%-10s",age[i]);
System.out.printf("%-10s",money[i]);
System.out.printf("%-10s",email[i]);
System.out.println(" ");
}
int a=choiceIndex();
toIndex(a, id, name, sex, age, money, email, count);
}
public static void endSystem() {//退出系统
System.out.println(" ");
System.out.println("退出系统,欢迎下次使用");
}
public static void firstIndex() {//首页面
System.out.println("*************************************");
System.out.println("*******吴泉玮的 员工管理系统******");
System.out.println("*************************************");
System.out.println(" ");
}
public static int choiceIndex() {//选择页面
System.out.println(" ");
System.out.println(" ");
System.out.println("请选择您要进行的操作:");
System.out.println("1.添加员工 2.删除员工 3.修改员工");
System.out.println("4.查询所有员工 5.工资排名 6.退出系统");
System.out.println("7.保存所有员工信息到桌面");
Scanner scanner=new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
public static void toIndex(int a,int[] id,String[] name,String[] sex,int[] age,float[] money,String[] email,int[] count) {//选择判断去向页面
if(a==1) {
addEmployee(id, name, sex, age, money, email, count);
}else if(a==2) {
deleteEmployee(id, name, sex, age, money, email, count);
}else if(a==3) {
changeEmployee(id, name, sex, age, money, email, count);
}else if(a==4) {
findEmployee(id, name, sex, age, money, email, count);
}else if(a==5) {
moneySort(id, name, sex, age, money, email, count);
}else if(a==6) {
endSystem();
}else if(a==7) {
coutOnDesk(id, name, sex, age, money, email, count);
}else {
System.out.println("无效选择,请重新选择");
choiceIndex();
}
System.out.println(" ");
}
}