项目简介
本项目是一个话费充值信息管理系统,主要架构为:
本项目分为三个使用主体:
- 普通用户
- 话费充值
- 普通管理员
- 查看系统所有用户信息
- 查看指定用户详细信息
- 话费充值
- 退出系统
- 超级管理员
- 查看系统所有用户信息
- 查看指定用户详细信息
- 在线选号
- 修改用户信息(根据字段 phoneNum 修改)
- 话费充值
- 用户注销(根据字段 phoneNum 删除)
- 导出数据(TXT)
- 退出系统
项目代码
Phone.java 话费实体类
package com.zwy;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author :zwy
* @Date :2020/12/30
* @Version :1.0
* @Description :话费充值系统 - 电话实体类
**/
public class Phone implements Serializable {
private static final long serialVersionUID = 1L;
private String phoneNum; //电话号码
private double currentFee; //当前话费
private String name; //持卡人姓名
private Date startDate; //开户时间
private Date updateDate; //修改时间
private String operator; //运营商
private int state = 1; //账户状态 1 - 正在使用;0 - 已注销
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public double getCurrentFee() {
return currentFee;
}
public void setCurrentFee(double currentFee) {
this.currentFee = currentFee;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public String getState() {
return state == 1 ? "正在使用" : "已注销";
}
public void setState(int state) {
this.state = state;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
//带参数的构造方法
public Phone(String phoneNum, double currentFee, String name, Date startDate,
Date updateDate, String operator) {
this.phoneNum = phoneNum;
this.currentFee = currentFee;
this.name = name;
this.startDate = startDate;
this.updateDate = updateDate;
this.operator = operator;
}
//充话费
public void addFee(double fee){
if (fee > 0){
currentFee = currentFee + fee;
return;
}
System.out.println("充值数额输入错误!");
}
//显示全部详细信息
public void showAllInfo(){
System.out.println("持卡人姓名:\t" + name);
System.out.println("手机号:\t" + phoneNum);
System.out.println("当前话费:\t" + currentFee);
System.out.println("运营商:\t" + operator);
System.out.println("开户时间:\t" + getTime(startDate));
System.out.println("当前状态:\t" + getState());
}
//导出个人信息
public String exportInfo(){
return name + "\t\t\t" + phoneNum + "\t\t\t" + currentFee+ "\t\t\t" +
operator+ "\t\t\t" + getTime(startDate)+ "\t\t\t" + getState();
}
//显示个人信息
public void showInfo(){
System.out.println(name + "\t\t\t" + phoneNum + "\t\t\t" + currentFee+ "\t\t" + getState());
}
//日期格式转为字符串格式
public static String getTime(Date date){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(date);
}
}
PhoneUtils.java 处理信息的工具类
package com.zwy;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* @Author :zwy
* @Date :2020/12/30
* @Version :1.0
* @Description :话费充值系统 - 处理信息的工具类
**/
public class PhoneUtils {
private List<Phone> list;
private static PhoneUtils utils;
public List<Phone> getList() {
return list;
}
//单例模式 - 懒汉式
public synchronized static PhoneUtils getInstance(List<Phone> list){
if (utils == null)
utils = new PhoneUtils();
utils.list = list;
return utils;
}
//添加信息
public void addPho(Phone pho){
if(list.contains(pho)){
System.out.println("该信息已存在");
return;
}
list.add(pho);
System.out.println("添加成功");
}
//删除(修改用户状态)
public void deletePhoById(String phoneNum){
for(Phone p : list){
if(phoneNum.equals(p.getPhoneNum()) ){
p.setState(0);// 修改用户状态为已注销
p.setUpdateDate(new Date());//更新修改时间
System.out.println("注销成功");
return;
}
}
System.out.println("该信息不存在");
}
//修改用户信息
public void updatePho(String phoneNum, String PhName){
for(Phone p : list){
if(phoneNum.equals(p.getPhoneNum())){
p.setName(PhName);
p.setUpdateDate(new Date());
System.out.println("修改成功");
return;
}
}
System.out.println("该信息不存在");
}
//显示所有信息
public void getAllInfo(){
System.out.println("持卡人姓名\t电话号码\t\t\t\t当前话费\t\t当前状态");
list.forEach(Phone::showInfo);
}
//显示个人详细信息
public void getInfo(String phoneNum){
for(Phone p : list){
if(phoneNum.equals(p.getPhoneNum())){
p.showAllInfo();
return;
}
}
System.out.println("该信息不存在!");
}
//充值话费
public void addPhoFee(String phoneNum , double fee){
for(Phone p : list){
if(phoneNum.equals(p.getPhoneNum())){
p.addFee(fee);
System.out.println("充值成功!当前余额为:" + p.getCurrentFee());
return;
}
}
System.out.println("充值失败!该手机号不在该系统中。");
}
//随机生成一组(5个)手机号码
public static void getPhoneNumber(){
for (int i = 0; i < 5; i++) {
System.out.println(getTel());
}
}
//随机生成手机号码
public static int getNum(int start,int end) {
return (int)(Math.random()*(end-start+1)+start);
}
private static String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
public static String getTel() {
int index=getNum(0,telFirst.length-1);
String first=telFirst[index];
String second=String.valueOf(getNum(1,888)+10000).substring(1);
String third=String.valueOf(getNum(1,9100)+10000).substring(1);
return first+second+third;
}
//导出数据
public void exportTxt(){
String fileUrl = "exportInfo.txt";
FileWriter fw = null;
try {
fw = new FileWriter(fileUrl,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("姓名\t\t\t电话号码\t\t\t当前话费\t\t\t运营商" +
"\t\t\t开户时间\t\t\t\t当前状态");
bw.newLine();
for (Phone p: list) {
bw.write(p.exportInfo());
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("导出成功!");
}
}
DataInit.java 数据初始化;添加测试数据;保存数据
package com.zwy;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @Author :zwy
* @Date :2020/12/30
* @Version :1.0
* @Description :话费充值系统 - 数据初始化;添加测试数据;保存数据
**/
public class DataInit {
// 保存对象,序列化
public static void saveObject(Object object) throws Exception {
try (FileOutputStream fout = new FileOutputStream("data.txt");
ObjectOutputStream out = new ObjectOutputStream(fout)) {
out.writeObject(object);
}
}
// 读取对象,反序列化
public static Object readObject() throws Exception {
ObjectInputStream in = null;
FileInputStream fin = null;
Object object = null;
try {
fin = new FileInputStream("data.txt");
in = new ObjectInputStream(fin);
object = in.readObject();
}catch (Exception e){
e.printStackTrace();
}finally {
assert fin != null;
fin.close();
assert in != null;
in.close();
}
return object;
}
//随机生成运营商
public static String getOperater(){
String[] doc = {"电信", "移动", "联通"};
int index = (int) (Math.random() * doc.length);
return doc[index];
}
/*public static List<Phone> PhoneInit(){
List<Phone> list = new ArrayList<Phone>();
Phone p1 = new Phone(PhoneUtils.getTel() , 58.8,
"zwy",getDateTime("2020-06-22 15:26:33"),
getDateTime("2020-06-22 15:26:33"),getOperater());
Phone p2 = new Phone(PhoneUtils.getTel(), 32.5,
"zfj",getDateTime("2020-08-24 12:44:43"),
getDateTime("2020-08-24 12:44:43"),getOperater());
Phone p3 = new Phone("15037856340" , 15.3,
"cx",getDateTime("2020-05-12 15:26:45"),
getDateTime("2020-05-12 15:26:45"),getOperater());
Phone p4 = new Phone(PhoneUtils.getTel() , 22.6,
"dhf",getDateTime("2020-11-23 14:34:13"),
getDateTime("2020-11-23 14:34:13"),getOperater());
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
return list;
}
public static Date getDateTime(String dateStr){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = formatter.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static void main(String[] args) throws Exception {
saveObject(PhoneInit());
List<Phone> list = (List<Phone>) DataInit.readObject();
list.forEach(Phone::showAllInfo);
}*/
}
PhoneSystem.java 系统总流程
package com.zwy;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
* @Author :zwy
* @Date :2020/12/30 16:22
* @Version :1.0
* @Description :话费充值系统 - 系统总流程
**/
public class PhoneSystem {
//从 TXT文档中读取数据
static List<Phone> list;
static {
try {
list = (List<Phone>) DataInit.readObject();
} catch (Exception e) {
e.printStackTrace();
}
}
//实例化工具类对象
static PhoneUtils utils = PhoneUtils.getInstance(list);
static Scanner sc = new Scanner(System.in);
//超级管理员账号信息
static String adminUser = "zzz";
static String adminPwd = "111";
//普通管理员账号信息
static String user = "www";
static String pwd = "222";
public static void systemSleep(String msg) throws InterruptedException {
System.out.print(msg);
for (int i = 0; i < 6; i++) {
Thread.sleep((long)(Math.random() * 500));
System.out.print("·");
}
System.out.println();
}
//超级管理员流程
public static void adminUser() throws Exception {
systemSleep("登录中");
System.out.println("***************超级管理员登录成功**************");
while (true){
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("7.导出数据");
System.out.println("8.退出系统");
System.out.println("请输入操作码:");
String nn = sc.next();
switch (nn){
case "1":
systemSleep("查询中");
utils.getAllInfo();
break;
case "2":
System.out.println("输入查看人的手机号码:");
String phoneNum = sc.next();
System.out.println("以下为该手机号的详细信息");
systemSleep("查询中");
utils.getInfo(phoneNum);
break;
case "3":
System.out.println("请从以下号码中挑选:");
while (true){
PhoneUtils.getPhoneNumber();
System.out.println("是否继续下一页 (n 结束,任意键继续)");
String ifCon = sc.next();
if ("n".equals(ifCon)){
break;
}
}
System.out.println("请输入挑选的号码:");
String xuanNumber = sc.next();
System.out.println("请输入持卡人的姓名:");
String phoneName = sc.next();
Phone pp = new Phone(xuanNumber, 50,
phoneName,new Date(),new Date(),DataInit.getOperater());
systemSleep("添加中");
utils.addPho(pp);
break;
case "4":
System.out.println("输入修改的手机号码:");
String updatePh = sc.next();
System.out.println("输入修改后的持卡人:");
String updateName = sc.next();
systemSleep("修改中");
utils.updatePho(updatePh,updateName);
break;
case "5":
System.out.println("输入充值的手机号码:");
String chongPh = sc.next();
System.out.println("请输入充值的金额:");
double fff = sc.nextDouble();
systemSleep("充值中");
utils.addPhoFee(chongPh,fff);
break;
case "6":
System.out.println("输入注销的手机号码:");
String zhuxiao = sc.next();
utils.deletePhoById(zhuxiao);
break;
case "7":
utils.exportTxt();
break;
case "8":
DataInit.saveObject(utils.getList());
System.out.println("系统退出!");
System.exit(0);
default:
System.out.println("操作失误,请重新输入!");
adminUser();
}
}
}
//普通管理员流程
public static void user() throws Exception {
systemSleep("登录中");
System.out.println("***************普通管理员登录成功**************");
while (true){
System.out.println("1.查看系统所有信息");
System.out.println("2.查看指定用户信息");
System.out.println("3.话费充值");
System.out.println("4.退出系统");
System.out.println("请输入操作码:");
String nn = sc.next();
switch (nn){
case "1":
systemSleep("查询中");
utils.getAllInfo();
break;
case "2":
System.out.println("输入查看人的手机号码:");
String phoneNum = sc.next();
System.out.println("以下为该手机号的详细信息");
systemSleep("查询中");
utils.getInfo(phoneNum);
break;
case "3":
System.out.println("输入充值的手机号码:");
String phoneNumber = sc.next();
System.out.println("输入充值的数额:");
double fee = sc.nextDouble();
systemSleep("查询中");
utils.addPhoFee(phoneNumber,fee);
break;
case "4":
DataInit.saveObject(utils.getList());
System.out.println("系统退出!");
System.exit(0);
default:
System.out.println("操作失误,请重新输入!");
user();
}
}
}
//系统开始
public static void start() throws Exception {
System.out.println("***************欢迎使用话费充值信息管理系统**************");
while (true){
System.out.println("1.管理员登录系统");
System.out.println("2.用户话费充值");
System.out.println("3.退出系统");
System.out.println("请输入操作码:");
String mun = sc.next();
switch (mun){
case "1":
for (int i = 0; i < 3; i++) {
System.out.println("请输入登录的用户名:");
String u = sc.next();
if (adminUser.equals(u)){ // 判断是否为超级管理员用户名
System.out.println("请输入登录的密码:");
String p = sc.next();
if (adminPwd.equals(p)){
adminUser(); // 用户名和密码都对进入超级管理员流程
}
System.out.println("密码错误!");
}else if (user.equals(u)){ // 判断是否是普通管理员用户名
System.out.println("请输入登录的密码:");
String p = sc.next();
if (pwd.equals(p)){
user(); // 用户名和密码都对进入普通管理员流程
}
System.out.println("密码错误!");
}else { //都不是则需重新输入,但只有 3 次机会,3 次之后系统退出
System.out.println("用户名错误,请重新登录!");
}
}
System.out.println("3次登录机会已用完,系统退出!");
DataInit.saveObject(utils.getList());
System.exit(0);
case "2":
System.out.println("输入充值的手机号码:");
String phoneNumber = sc.next();
System.out.println("输入充值的数额:");
double fee = sc.nextDouble();
systemSleep("充值中");
utils.addPhoFee(phoneNumber,fee);
break;
case "3":
DataInit.saveObject(utils.getList());
System.out.println("系统退出!");
System.exit(0);
default:
System.out.println("操作失误,请重新输入!");
start();
}
}
}
public static void main(String[] args) throws Exception {
start();
}
}
代码运行
第一次执行时,先把 DataInit.Java 注释的代码解掉注释,并运行里面的 main 方法,进行系统测试数据的写入,运行时候会新生成新的 data.txt 文件
运行之后可以把代码重新注释,不注释也没事,然后运行 PhoneSystem.java中的 main 方法即可。
普通管理员账号密码是 www、222;超级管理员密码是 zzz 、111,用户名和密码是代码写好的,想要修改的话修改 PhoneSystem.java 以下代码就可以啦。