利用数组功能完成学员管理系统

一.最基础的只存姓名的学员管理系统

数组的学员管理,要求对里面存储的学员姓名信息进行管理,可以对数据进行增加,删除,查询和修改的功能

import java.util.Scanner;

import javax.management.Query;

import org.omg.CosNaming.NamingContextExtPackage.AddressHelper;

public class Test {
	private static String[] stu = new String[100];
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		do {
			System.out.println("学员管理系统:\n1.添加学员信息\n2.删除学员信息\n3.查询学员信息\n4.修改学员信息\n5.退出\n请输入您的选择:");
			int num = sc.nextInt();
			switch (num) {
			case 1:
				System.out.println("添加学员信息");
				System.out.println("请输入学员姓名");
				String name = sc.next();
				add(name);
				break;
			case 2:
				System.out.println("删除学员信息");
				System.out.println("请输入学员姓名");
				name = sc.next();
				del(name);
				break;
			case 3:
				System.out.println("查询学员信息");
				query();
				
				break;
			case 4:
				System.out.println("修改学员信息");
				System.out.println("请输入学员姓名");
				name = sc.next();
				change(name);
				
				break;
			case 5:
				System.out.println("拜拜~~~");
				return;
			default:
				break;
			}
			
		} while (true);
	}

	private static void change(String name) {
		Scanner sc = new Scanner(System.in);
		int index = panDuan1(name);
		if (index==-1) {
			System.out.println("该学生不存在");
		}else {
			System.out.println("请输入你将改成的名字");
			String name1 = sc.next();
			int t = panDuan1(name1);
			if (t==-1) {
				stu[index]=name1;
				System.out.println("修改成功");
			}else {
				System.out.println("该学生已存在");
			}
		}
	}

	private static void del(String name) {
		int index = panDuan1(name);
		if (index==-1) {
			System.out.println("该学生不存在");
		}else{
			for (int i = index; i < stu.length-1; i++) {
				if (stu[i]==null) {
					break;
				}
				stu[i]=stu[i+1];
			}
			System.out.println("删除成功");
		}
	}

	private static int panDuan1(String name) {
		for (int i = 0; i < stu.length; i++) {
			if(stu[i]==null) {
				return -1;
			}
			if(stu[i].equals(name)){
				return i;
			}
		}
		return -1;
	}

	private static void query() {
		for (int i = 0; i < stu.length; i++) {
			if (stu[i]==null) {
				break;
			}
			System.out.println(stu[i]);
		}
	}

	private static void add(String name) {
		int index =panDuan(name);
		if (index==-1) {
			System.out.println("添加成功");
		}else {
			System.out.println("此名字已存在");
		}
	}

	private static int panDuan(String name) {
		for (int i = 0; i < stu.length; i++) {
			if (stu[i]==null) {
				stu[i]=name;
				return -1;
			}
			if (stu[i].equals(name)) {
				return i;
			}
		}
		return 0;
	}	
}

二.录用信息不止姓名一个变量的学员管理系统

由于录用信息不止一个 将每个数组元素当成一个对象,首先创建学生对象

public class Student {
	public  String name;
	public  String phone;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public Student(String name, String phone) {
		this.name = name;
		this.phone = phone;
	}
	public Student() {
	
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", phone=" + phone + "]";
	}
}

学员系统对象调用的实现

import java.awt.Checkbox;
import java.util.Scanner;
import java.util.concurrent.Delayed;

import javax.management.Query;

import org.omg.CosNaming.NamingContextExtPackage.AddressHelper;

public class Test2 {
	static Student[] student = new Student[100]; 
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		do {
			System.out.println("V2.学员管理系统");
			System.out.println("1.添加学员信息\n2.删除学员信息\n3.查询学员信息\n4.修改学员信息\n5.退出\n请输入您的选择:");
			
			int num = scanner.nextInt();
			switch (num) {
			case 1:
				System.out.println("请输入学员姓名");
				String name = scanner.next();
				System.out.println("请输入手机号");
				String phone = scanner.next();
				add(name,phone);
				break;
			case 2:
				System.out.println("请输入学员姓名");
				//String name1 = scanner.next();
				name = scanner.next();
				del(name);
				break;
			case 3:
				System.out.println("查询学生信息");
				query();
				break;
			case 4:
				System.out.println("请输入要修改的学员姓名");
				name = scanner.next();
				change(name);
				break;
			case 5:
				System.out.println("打扰了,告辞");
				return;

			default:
				System.out.println("输入有误");
				break;
			}
		} while (true);
	}
	
	private static void change(String name) {
		Scanner scanner = new Scanner(System.in);
		int index=panDuan1(name);
		if (index==-1) {
			System.out.println("该姓名不存在");
			
		}else{
			System.out.println("请输入你将改成的名字");
			String name1=scanner.next();
			int t = panDuan1(name1);
			if (t==-1) {
				student[index].name=name1;
				System.out.println("请输入你将改成的电话");
				student[index].phone=scanner.next();
				System.out.println("修改成功");
			}else {
				System.out.println("名字已存在");
			}
			
		} 
	}

	private static void del(String name) {		//删除
		int index=panDuan1(name);
		if (index==-1) {
			System.out.println("该姓名不存在");
			
		} else {
			for (int i = index; i < student.length-1; i++) {
				if (student[i]==null) {
					break;
				}
				student[i]=student[i+1];
			}
			System.out.println("删除成功");
		}	
	}
	private static int panDuan1(String name) {
		for (int i = 0; i < student.length; i++) {	//判断删除对象是否存在
			if (student[i]==null) {
				return -1;
			}
			if(student[i].name.equals(name)){
				return i;
			}
		}
		return -1;
	}

	private static void query() {
		for (int i = 0; i < student.length; i++) {	//查询
			if (student[i]==null) {
				break;
			}else {
				System.out.println(student[i].toString());
			}
		}
	}
	
	private static void add(String name, String phone) {  //添加
		int index=panDuan(name,phone);
		if (index==-1) {
			System.out.println("添加成功");
		}else {
			System.out.println("该姓名已存在");
		}
	}
	
	private static int panDuan(String name,String phone) {	//添加判断
		for (int i = 0; i < student.length; i++) {
			if (student[i]==null) {
				student[i]=new Student(name,phone);
				return -1;
			}
			
			if(student[i].name.equals(name)){
				return i;
			}
		}
		return -1;
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值