JAVA实现一个简单的用户管理系统
java练习的一个小项目,使用面向对象和IO流实现用户管理操作,用户信息包括【用户编号、用户名称、工资值和年龄,随机生成若干个(>10)用户信息写入到一个二进制文件中
·
用户管理操作
用户信息包括【用户编号、用户名称、工资值和年龄】
随机生成若干个(>10)用户信息写入到一个二进制文件中,不允许id值重复,id也是随机生成的
长整型数值,在控制台上显示所有用户信息,删除第3个用户,将用户按照用户工资值从小到大排序后写回原文件
User类
import java.io.Serializable;
public class User implements Serializable, Comparable<User> {
private static final long serialVersionUID = 3492921525397060568L;
private Long id;// 用户编号
private String username;// 用户名称
private Double salary; // 工资
private Integer age; // 年龄
@Override
/*
Math.abs(bb):return (a <= 0.0D) ? 0.0D - a : a;
Math.abs(x)<1e-6其实相当于x == 0
1e-6(也就是0.000001)叫做epslon,用来抵消浮点运算中因为误差造成的相等无法判断的情况。
它通常是一个非常小的数字(具体多小要看你的运算误差)
*/
public int compareTo(User o) {
double bb = this.salary - o.getSalary();
if (Math.abs(bb) > 1e-6) {//
if (bb > 0)
return 1;
else
return -1;
}
return 0;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", salary=" + salary + ", age=" + age + "]";
}
}
UserManger类
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;
public class UserManager {
private static File file = new File("data/users.data");
static {
File ff = new File("data/");
if (!ff.exists())
ff.mkdirs();
ObjectOutputStream oos = null;
try {
if (!file.exists()) {
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
Random r = new Random();
int num = r.nextInt(91) + 10;
long[] idArr = new long[num];
for (int i = 0; i < num; i++) {
User tmp = new User();
long kk = generateId(idArr);
tmp.setId(kk);
idArr[i] = kk;
tmp.setUsername("name" + i);
tmp.setSalary(r.nextDouble() * 10000);
tmp.setAge(r.nextInt(10) + 10);
oos.writeObject(tmp);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (oos != null)
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void sort() throws IOException, ClassNotFoundException {
Object[] res = null;
ArrayList list = getAllUsers();
int len = 0;
if (list != null) {
res = list.getData();
// 这个方法不唯一
for (int i = 0; i < res.length; i++) {
if (res[i] != null) {
len++;
continue;
} else
break;
}
for (int i = 1; i < len; i++) {
for (int k = 0; k < len - i; k++) {
if (res[k] != null && res[k] instanceof User) {
User tmp1 = (User) res[k];
if (res[k + 1] != null && res[k + 1] instanceof User) {
User tmp2 = (User) res[k + 1];
if (tmp1.compareTo(tmp2) > 0) {
Object tmp = res[k];
res[k] = res[k + 1];
res[k + 1] = tmp;
}
}
}
}
}
}
try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));) {
if (res != null && res.length > 0) {
for (Object tmp : res) {
if(tmp!=null) {
oos.writeObject(tmp);
}
}
}
}
}
public ArrayList getAllUsers() throws IOException, ClassNotFoundException {
ArrayList res = new ArrayList();
if (file.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) {
while (true) {
try {
Object obj = ois.readObject();
res.add(obj);
} catch (EOFException e) {
break;
}
}
}
}
return res;
}
public void remove(int index) throws IOException, ClassNotFoundException {
ArrayList list = this.getAllUsers();
list.remove(index);
try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));) {
Object[] array = list.getData();
for (Object tmp : array) {
if (tmp != null)
oos.writeObject(tmp);
}
}
}
private static long generateId(long[] idArr) {
long res = 0L;
Random r = new Random();
while (true) {
// id>0
long kk = r.nextLong();
if (kk > 0) {
boolean exists = false;
for (int k = 0; k < idArr.length; k++) {
if (idArr[k] == kk) {
exists = true;
break;
}
}
if (!exists) {
res = kk;
break;
}
}
}
return res;
}
public static void main(String[] args) throws Exception {
UserManager um = new UserManager();
for (Object tmp : um.getAllUsers().getData())
System.out.println(tmp);
}
}
Arraylist类
public class ArrayList {
private Object[] array;
private static final int CAPACITY = 10;
public ArrayList() {
this(CAPACITY);
}
public ArrayList(int capacity) {
array = new Object[capacity];
}
public void remove(int index) {
if (index > array.length)
throw new ArrayIndexOutOfBoundsException();
Object end = array[array.length - 1];
System.arraycopy(array, index, array, index - 1, array.length - index);
if (end != null) {
array[array.length - 1] = null;
}
}
public void add(Object obj) {
if (obj != null) {
boolean flag = false;
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
array[i] = obj;
flag = true;
break;
}
}
int len = array.length;
if (!flag) {
int newLen = len * 2;
Object[] newArr = new Object[newLen];
System.arraycopy(array, 0, newArr, 0, array.length);
this.array = newArr;
array[len] = obj;
}
}
}
public Object[] getData() {
return this.array;
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)