代码如下:
package com.oracle.user;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 用户注册
* @author user
*
*/
public class Reg {
static Scanner sc = new Scanner(System.in);
static ArrayList<User> list = new ArrayList<User>();
/**
* 注册功能的方法
* @param args
*/
public static User zhuce(){
User u = new User();
System.out.println("请输入姓名:");
String name = sc.next();
u.setName(name);
System.out.println("请输入密码:");
String pwd = sc.next();
u.setPwd(pwd);
return u;
}
/**
* 查询集合中的用户
* @param args
*/
public static void findUser(){
for (User user : list) {
System.out.println(user);
}
}
/**
* 修改用户的方法
* @param args
*/
public static void updateUser(){
System.out.println("请输入要修改的用户名:");
String name = sc.next();
User u = null;
for (User user : list) {
if(user.getName().equals(name)){
u = user;
}
}
if(u == null){
System.out.println("查无此人");
return;
}
int index = list.indexOf(u);
System.out.println("请输入修改后的姓名:");
String name2 = sc.next();
System.out.println("请输入修改后的密码:");
String pwd = sc.next();
list.set(index, new User(name2,pwd));
System.out.println("修改成功");
}
/**
* 删除用户
* @param args
*/
public static void del(){
System.out.println("请输入要删除的用户名:");
String name = sc.next();
User u = null;
for (User user : list) {
if(user.getName().equals(name)){
u = user;
}
}
if(u == null){
System.out.println("查无此人");
return;
}
// int index = list.indexOf(u);
// list.remove(index);
boolean b = list.remove(u);
if(b){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
public static void main(String[] args) {
System.out.println("============用户注册============");
a: while(true){
System.out.println("请选择:1.注册用户;2.查看用户;3.修改用户;4.删除用户;0.退出");
int i = sc.nextInt();
switch(i){
case 1:
User u = zhuce();
list.add(u);
break;
case 2:
if(list.size()<=0){
System.out.println("当前没有用户数据,请先注册!");
}else{
findUser();
}
break;
case 3:
updateUser();
break;
case 4:
del();
break;
case 0:
break a;
}
}
System.out.println("============再见============");
}
}
package com.oracle.user;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<User> list = new ArrayList<User>();
//添加
User u1 = new User("tom","123");
User u2 = new User("jack","456");
list.add(u1);
list.add(u2);
User u = null;
for (User user : list) {
if(user.getName().equals("tom")){
u = user;//把从集合中遍历到的符合姓名的对象地址,赋给u对象
}
}
int i = list.indexOf(u);
System.out.println(i);
// System.out.println(list);
//查看集合的长度
// System.out.println(“长度=”+list.size());
//修改
// list.set(0, );
// System.out.println(list);
//删除
// boolean b = list.remove(u);
// System.out.println(b);
// User user = list.remove(0);
// System.out.println(user.getName()+”被删除了”);
// System.out.println(“删除后的长度=”+list.size());
}
}
package com.oracle.user;
public class User {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String pwd) {
super();
this.name = name;
this.pwd = pwd;
}
@Override
public String toString() {
return "User [name=" + name + ", pwd=" + pwd + "]";
}
}