package com.neuedu.manager;
import java.sql.Date;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
- 主程序类
- @author Administrator
*/
public class Start {
private static UserInfo dbUser=null;
public static void main(String[] args) {
TimerUtil.getU();
Scanner scan=new Scanner(System.in);
while(true){
System.out.println("1.注册 2.登录 3.查询个人信息 4.修改密码 5.修改昵称"
+ " 6.查询余额 7.账户充值 8.VIP升级 9.申请解冻 10.查询交易记录 11.退出");
int num=scan.nextInt();
switch(num){
case 1:
regUser();
break;
case 2:
loginUser();
break;
case 3:
findInfo();
break;
case 4:
updatePwd();
break;
case 5:
updateNikename();
break;
case 6:
getBalance();
break;
case 7:
addBalance();
break;
case 8:
setVip();
break;
case 9:
reminfo();
break;
case 10:
findPay();
break;
case 11:
out();
break;
default:
System.out.println("选项无效...");
}
}
}
//选退出后 获取当前时间 将该时间加入userinfo表中
public static void out(){
UserDao dao=new UserDao();
dao.putOutTime(dbUser.getUsername());
dbUser=null;
}
//申请解冻
public static void reminfo() {
System.out.println("请输入需要解冻的用户名");
Scanner scan=new Scanner(System.in);
String username=scan.next();
UserDao dao=new UserDao();
if(dao.login(username)==null){
System.out.println("没有该用户");
return;
}
if(dao.outTime(username)==0){
System.out.println("该用户不处于冻结状态,不需要解冻");
return;
}else if(dao.outTime(username)==1){
System.out.println("申请解冻,请输入原密码");
String a=scan.next();
if(!a.equals(dbUser.getPassword())){
System.out.println("原密码有误");
return;
}else{
System.out.println("输入正确");
dao.updateOutTime(username);
dbUser.setOuttime(0);
System.out.println("解冻成功");
}
}
}
//VIP升级方法
public static void setVip() {
if(dbUser==null){
System.out.println("请先登录再进行操作");
return;
}
Scanner scan=new Scanner(System.in);
System.out.println("请输入充值约束 20 RMB/月");
int num=scan.nextInt();
UserDao dao=new UserDao();
boolean flag=dao.enoughMoney(dbUser.getUsername(), num*20);
//如果为false则说明余额不足
if(flag==false){
System.out.println("余额不足请充值后再进行办理");
return;
}
//不执行if语句块 则说明余额充足 进行消费 vip增加月数 并且balance减少
dao.vip(dbUser.getUsername(), num);
dbUser.setVip(num);
dao.buy(dbUser.getUsername(), num*20);
dbUser.setBalance(-num*20);
//将该消费记录加入到userpay表中
dao.putUserPay(dbUser.getUserid(),-num*20);
System.out.println("充值成功");
}
//查询消费记录
public static void findPay(){
if(dbUser==null){
System.out.println("请先登录在进行操作");
return;
}
UserDao dao=new UserDao();
List<UserPay> list=dao.getPay(dbUser.getUserid());
for(UserPay u:list){
System.out.println(u);
}
}
//账户充值功能
public static void addBalance() {
if(dbUser==null){
System.out.println("请先登录在进行操作");
return;
}
Scanner scan=new Scanner(System.in);
System.out.println("请输入充值金额");
double money=scan.nextDouble();
UserDao dao=new UserDao();
//修改表中的余额 balance
dbUser.setBalance(money);
dao.putMoney(dbUser.getUsername(), money);
//插入消费记录
dao.putUserPay(dbUser.getUserid(),money);
System.out.println("充值成功");
}
//查询余额功能
public static void getBalance() {
if(dbUser==null){
System.out.println("请先登录在进行操作");
return;
}
UserDao dao=new UserDao();
System.out.println(dao.findBalance(dbUser.getUsername()));
}
//修改昵称功能
public static void updateNikename() {
if(dbUser==null){
System.out.println("请先登录在进行操作");
return;
}
Scanner scan=new Scanner(System.in);
System.out.println("请输入新昵称");
String nikename=scan.next();
dbUser.setNikename(nikename);
UserDao dao=new UserDao();
dao.updateNikeName(dbUser.getUsername(),nikename);
}
//修改密码功能
public static void updatePwd() {
if(dbUser==null){
System.out.println("请先登录再进行操作");
return;
}
Scanner scan=new Scanner(System.in);
System.out.println("请输入原密码");
String a=scan.next();
if(!a.equals(dbUser.getPassword())){
System.out.println("原密码有误");
return;
}else{
System.out.println("输入正确");
}
System.out.println("请输入新密码");
String b=scan.next();
System.out.println("请再次输入新密码");
String c=scan.next();
if(b.equals(c)&&b.length()>=6&&b.length()<=20){
System.out.println("修改密码成功");
}else{
System.out.println("两次输入密码不一致或密码长度不符合规则");
}
//修改标志密码
dbUser.setPassword(b);
//修改表的密码
UserDao dao=new UserDao();
dao.updatePwd(dbUser.getUsername(), b);
//重新登录
System.out.println("请重新登录");
loginUser();
}
//查询个人信息功能
public static void findInfo() {
if(dbUser==null){
System.out.println("请先登录在进行操作");
return;
}
System.out.println("用户名: "+dbUser.getUsername()+" 用户密码: "+dbUser.getPassword()
+" 昵称: "+dbUser.getNikename()+" 权限: "+(dbUser.getVip()==0?"普通用户":"VIP"));
}
//登录功能
public static void loginUser() {
//先判断是否已经登录了
if(dbUser!=null){
System.out.println("已登录,请误重复操作");
return;
}
Scanner scan=new Scanner(System.in);
System.out.println("请输入用户名");
String username=scan.next();
System.out.println("请输入密码");
String password=scan.next();
UserDao dao=new UserDao();
//先判断账户是否被冻结
if(dao.outTime(username)==1){
System.out.println("该账户被冻结,请解除冻结");
reminfo();
return;
}
UserInfo userinfo=dao.login(username, password);
if(userinfo==null){
System.out.println("用户名或密码错误");
return;
}else{
System.out.println("登陆成功");
}
dbUser=userinfo;
}
//注册功能
public static void regUser() {
Scanner scan=new Scanner(System.in);
System.out.println("请输入用户名");
String username=scan.next();
System.out.println("请输入密码");
String password=scan.next();
System.out.println("请输入昵称(随机名称请输入0)");
String nikename=scan.next();
UserDao dao=new UserDao();
String DBUser=dao.login(username);
if(DBUser!=null){
System.out.println("用户名重复");
return;
}
if(password.length()<6||password.length()>20){
System.out.println("密码不符合规则");
return;
}
//获取随机姓名
if(nikename.equals("0")){
Random ran=new Random();
int num=ran.nextInt(9)+1;
nikename=dao.getNikeName(num);
dao.putUser(username, password, nikename);
System.out.println("注册成功");
return;
}else{
dao.putUser(username, password, nikename);
System.out.println("注册成功");
return;
}
}
}