今天我们带领大家来实现人物对象的增、删、改、查(注:对象的删除以逻辑删除为主,在person类中设置“删除状态字段”,字删除该字段时,将状态改为有效。)
目录
定义person类:
类属性:
有参和无参构造:
tostring函数的重写:
get和set函数:
总:
添加人物函数:
删除人物函数:
查找人物函数:
修改人物属性函数:
输出人物属性函数:
总代码:
String name;
String sex;
LocalDate brithday;
boolean remove = false;
public person() {
super();
}
public person(String name, String sex, LocalDate brithday) {
super();
this.name = name;
this.sex = sex;
this.brithday = brithday;
}
@Override
public String toString() {
return "[name=" + name + ", sex=" + sex + ", brithday=" + brithday + "]\n";
}
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 LocalDate getBrithday() {
return brithday;
}
public void setBrithday(LocalDate brithday) {
this.brithday = brithday;
}
class person {
String name;
String sex;
LocalDate brithday;
boolean remove = false;
public person() {
super();
}
public person(String name, String sex, LocalDate brithday) {
super();
this.name = name;
this.sex = sex;
this.brithday = brithday;
}
@Override
public String toString() {
return "[name=" + name + ", sex=" + sex + ", brithday=" + brithday + "]\n";
}
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 LocalDate getBrithday() {
return brithday;
}
public void setBrithday(LocalDate brithday) {
this.brithday = brithday;
}
}
public static void add(Vector v) {
System.out.println("添加人物及属性");
System.out.println("请输入添加人物的姓名:");
String n = scan.next();
System.out.println("请输入添加人物的性别:");
String s = scan.next();
System.out.println("请输入添加人物的出生日期(格式:年 月 日)");
int y = scan.nextInt();
int m = scan.nextInt();
int d = scan.nextInt();
v.add(new person(n, s, LocalDate.of(y, m, d)));
System.out.println("添加成功");
}
public static void del(Vector v) {
System.out.println("请输入删除人物的姓名:");
B: for (;;) {
String n1 = scan.next();
int i;
for (i = 0; i < v.size(); i++) {
if (v.get(i).getName().equals(n1) && v.get(i).remove == false) {
v.get(i).remove = true;
System.out.println("删除成功!");
if (i == v.size() - 1)
break B;
}
}
if (i >= v.size())
System.out.println("未找到该人物!请重新输入:");
}
}
public static void find(Vector v) {
System.out.println("请输入要查找人物的姓名:");
C: for (;;) {
String n2 = scan.next();
int i1;
for (i1 = 0; i1 < v.size(); i1++) {
if (v.get(i1).getName().equals(n2) && v.get(i1).remove == false) {
System.out.println("查找成功!该人物的相关属性为:" + v.get(i1));
if (i1 == v.size() - 1)
break C;
}
}
if (i1 >= v.size())
System.out.println("查找失败!请重新输入:");
}
}
public static void chage(Vector v) {
System.out.println("请输入您要修改的人物的姓名:");
D: for (;;) {
String n3 = scan.next();
int i2;
for (i2 = 0; i2 < v.size(); i2++) {
if (v.get(i2).getName().equals(n3) && v.get(i2).remove == false) {
System.out.println("该用户存在,下面进入修改操作:");
System.out.println("请输入修改后人物的姓名:");
String n0 = scan.next();
System.out.println("请输入修改后人物的性别:");
String s0 = scan.next();
System.out.println("请输入修改后人物的出生日期(格式:年 月 日)");
int y0 = scan.nextInt();
int m0 = scan.nextInt();
int d0 = scan.nextInt();
v.get(i2).setName(n0);
v.get(i2).setSex(s0);
v.get(i2).setBrithday(LocalDate.of(y0, m0, d0));
System.out.println("恭喜你修改成功!!修改后的人物及其属性为:");
System.out.println(v.get(i2));
break D;
}
}
if (i2 >= v.size()) {
System.out.println("该人物不存在!请重新输入:");
}
}
}
public static void put(Vector v) {
int i3;
for (i3 = 0; i3 < v.size(); i3++) {
if ( v.get(i3).remove == false)
System.out.println(v.get(i3));
}
}
package one;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Scanner;
import java.util.Vector;
public class New {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
person p1 = new person("liubei", "男", LocalDate.of(1993, 8, 15));
person p2 = new person("guanyu", "男", LocalDate.of(1997, 9, 24));
person p3 = new person("diaochan", "女", LocalDate.of(1994, 2, 28));
person p4 = new person("miyue", "女", LocalDate.of(1995, 12, 8));
Vector v1 = new Vector();
v1.add(p1);
v1.add(p2);
v1.add(p3);
v1.add(p4);
A: for (;;) {
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("---------------------------------");
int choice = scan.nextInt();
switch (choice) {
case 1:
add(v1);
break;
case 2:
del(v1);
break;
case 3:
find(v1);
break;
case 4:
chage(v1);
break;
case 5:
put(v1);
break;
case 6:
System.out.println("您已退出!!再见!");
break A;
default:
System.out.println("输入错误,请重新输入:");
}
System.out.println("------------------------------------");
}
}
public static void add(Vector v) {
System.out.println("添加人物及属性");
System.out.println("请输入添加人物的姓名:");
String n = scan.next();
System.out.println("请输入添加人物的性别:");
String s = scan.next();
System.out.println("请输入添加人物的出生日期(格式:年 月 日)");
int y = scan.nextInt();
int m = scan.nextInt();
int d = scan.nextInt();
v.add(new person(n, s, LocalDate.of(y, m, d)));
System.out.println("添加成功");
}
public static void del(Vector v) {
System.out.println("请输入删除人物的姓名:");
B: for (;;) {
String n1 = scan.next();
int i;
for (i = 0; i < v.size(); i++) {
if (v.get(i).getName().equals(n1) && v.get(i).remove == false) {
v.get(i).remove = true;
System.out.println("删除成功!");
if (i == v.size() - 1)
break B;
}
}
if (i >= v.size())
System.out.println("未找到该人物!请重新输入:");
}
}
public static void find(Vector v) {
System.out.println("请输入要查找人物的姓名:");
C: for (;;) {
String n2 = scan.next();
int i1;
for (i1 = 0; i1 < v.size(); i1++) {
if (v.get(i1).getName().equals(n2) && v.get(i1).remove == false) {
System.out.println("查找成功!该人物的相关属性为:" + v.get(i1));
if (i1 == v.size() - 1)
break C;
}
}
if (i1 >= v.size())
System.out.println("查找失败!请重新输入:");
}
}
public static void chage(Vector v) {
System.out.println("请输入您要修改的人物的姓名:");
D: for (;;) {
String n3 = scan.next();
int i2;
for (i2 = 0; i2 < v.size(); i2++) {
if (v.get(i2).getName().equals(n3) && v.get(i2).remove == false) {
System.out.println("该用户存在,下面进入修改操作:");
System.out.println("请输入修改后人物的姓名:");
String n0 = scan.next();
System.out.println("请输入修改后人物的性别:");
String s0 = scan.next();
System.out.println("请输入修改后人物的出生日期(格式:年 月 日)");
int y0 = scan.nextInt();
int m0 = scan.nextInt();
int d0 = scan.nextInt();
v.get(i2).setName(n0);
v.get(i2).setSex(s0);
v.get(i2).setBrithday(LocalDate.of(y0, m0, d0));
System.out.println("恭喜你修改成功!!修改后的人物及其属性为:");
System.out.println(v.get(i2));
break D;
}
}
if (i2 >= v.size()) {
System.out.println("该人物不存在!请重新输入:");
}
}
}
public static void put(Vector v) {
int i3;
for (i3 = 0; i3 < v.size(); i3++) {
if ( v.get(i3).remove == false)
System.out.println(v.get(i3));
}
}
}
class person {
String name;
String sex;
LocalDate brithday;
boolean remove = false;
public person() {
super();
}
public person(String name, String sex, LocalDate brithday) {
super();
this.name = name;
this.sex = sex;
this.brithday = brithday;
}
@Override
public String toString() {
return "[name=" + name + ", sex=" + sex + ", brithday=" + brithday + "]\n";
}
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 LocalDate getBrithday() {
return brithday;
}
public void setBrithday(LocalDate brithday) {
this.brithday = brithday;
}
}
注:该程序存在部分bug,检查出来的友友可以放在评论区,感谢!!!总代码或分代码有部分不解的友友可以在评论区提问或者关注博主,私信提问!