本帖最后由 洋葱头头 于 2016-2-16 12:34 编辑
LiHuan.zip
(8.39 KB, 下载次数: 62)
老板,求打赏!!!
目前还没学集合什么的,用的是字符串连接的方法做的。这不是压缩包,将后缀名.zip改成.java即可。
注释就不写得特别详细了。
坐等打赏!{:2_31:}
- /*
- *花了好多时间考虑,写了几遍,调试了也不知道多少次。
- *第一遍不是通过控制器的方式,是在主函数中按照面向过程的方式写的,但是觉得这样写不怎么好,
- *然后突然想起以前见到的使用一个整型数来代表界面,然后又开始从新写了一遍。
- *目前没有学集合,要储存不确定的用户数量是一个难题,于是后来想要通过字符串拼接的方式,
- *是我目前能想到的最好的方法了。
- *功能基本实现了!如遇BUG请反馈!!!
- *具体注释我就不写了,大致的注释就简单的写了一些。
- */
- import java.util.Scanner;
- import java.io.*;
- class LiHuan {
- public static int control = 0; //定义控制器参数
- public static int countUser = 0; //统计用户数量
- public static String[] userInfo = new String[3]; //存储当前登录的用户信息
- public static String userList = "#"; //存储所有用户数据
- public static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) throws IOException {
- while (true) { //无限循环,根据控制器的参数进行操作
- if (control == -1) {
- break;
- } else {
- control();
- }
- }
- }
- public static void control() throws IOException {
- switch (control) {
- case 0: //主界面
- iMain();
- break;
- case 1: //注册界面
- iRegister();
- break;
- case 2: //登录界面
- iLogin();
- break;
- case 3: //用户界面
- iUser();
- break;
- default: //默认界面
- control = -1;
- break;
- }
- }
- public static void iMain() { //主界面函数
- System.out.println("------------------------------------");
- System.out.println("----------欢迎来到黑马论坛----------");
- System.out.println("-----------本站共有0名用户----------");
- System.out.println("0.退出");
- System.out.println("1.注册");
- System.out.println("2.登录");
- while(true) {
- System.out.println("\r\n请选择你想要的操作选项:");
- String str = sc.nextLine();
- if (str.equals("0") || str.equals("1") || str.equals("2")) { //不能使用==
- switch (str) {
- case "0":
- control = -1;
- break;
- case "1":
- control = 1;
- break;
- case "2":
- control = 2;
- break;
- default:
- control = -1;
- break;
- }
- break;
- } else {
- System.out.println("<---你操作错误,请重新输入选项!--->");
- System.out.println("------------------------------------");
- }
- }
- }
- public static void iRegister() { //注册界面函数
- String userName;
- String passWord;
- System.out.println("------------------------------------");
- System.out.println("----------欢迎来到注册页面----------");
- while (true) {
- System.out.println("输入你要注册的帐号:");
- userName = sc.nextLine().toLowerCase();
- if (!userList.contains(userName)) {
- while (true) {
- System.out.println("请输入你要注册的密码:");
- passWord = sc.nextLine();
- if (passWord.matches("[a-zA-Z0-9_]{6,20}")) {
- userList += (userName + "," + passWord + ",0#");
- countUser++;
- control = 0;
- System.out.println("注册成功!");
- break;
- } else {
- System.out.println("<---密码只能为英文字母大小写、数字以及下划线,且密码长度在6-20之间!--->");
- System.out.println("------------------------------------");
- }
- }
- break;
- } else {
- System.out.println("<-你输入的帐号已存在,请重新尝试!->");
- System.out.println("------------------------------------");
- control = 0;
- break;
- }
- }
- }
- public static void iLogin() { //登录界面函数
- if (countUser == 0) {
- control = 1;
- return;
- } else {
- System.out.println("------------------------------------");
- System.out.println("----------欢迎来到登录界面----------");
- System.out.println("--------------请按1返回-------------");
- System.out.println("--------------请按2继续-------------");
- while (true) {
- String tempSelect = sc.nextLine();
- if (tempSelect.equals("1")) {
- control = 0;
- break;
- } else if (tempSelect.equals("2")) {
- while (true) {
- System.out.println("请输入账号:");
- String userNameTemp = sc.nextLine();
- System.out.println("请输入密码:");
- String passWordTemp = sc.nextLine();
- if (userList.contains("#" + userNameTemp + "," + passWordTemp + ",0#")) {
- setUserInfo(userNameTemp, passWordTemp, "0");
- break;
- } else if (userList.contains("#" + userNameTemp + "," + passWordTemp + ",1#")) {
- setUserInfo(userNameTemp, passWordTemp, "1");
- break;
- } else {
- System.out.println("<------------登录失败!------------->\r\n");
- control = 0;
- break;
- }
- }
- break;
- } else {
- System.out.println("<------------输入有误!------------>");
- System.out.println("------------------------------------");
- control = 0;
- break;
- }
- }
- }
- }
- public static void setUserInfo(String userName, String passWord, String isVip) { //设置当前登录用户信息
- System.out.println("登陆成功!");
- control = 3;
- userInfo[0] = userName;
- userInfo[1] = passWord;
- userInfo[2] = isVip;
- }
- public static void iUser() throws IOException { //用户界面函数
- System.out.println("------------------------------------");
- System.out.print("欢迎" + userInfo[0]);
- if (userInfo[2].equals("1")) {
- System.out.println("会员!");
- } else {
- System.out.println("游客!");
- }
- System.out.println("欢迎使用以下服务:");
- switch (userInfo[2]) {
- case "0":
- System.out.println("1.升级会员服务!");
- System.out.println("2.返回登录界面!");
- System.out.println("3.返回注册界面!");
- System.out.println("4.返回主界面!");
- break;
- case "1":
- System.out.println("1.打印会员信息!");
- System.out.println("2.撤销会员服务!");
- System.out.println("3.返回登录界面!");
- System.out.println("4.返回注册界面!");
- System.out.println("5.返回主界面!");
- break;
- }
- String selectOperater = sc.nextLine() + userInfo[2];
-
- switch (selectOperater) { //通过在用户输入的选项后面加入0或1来加以判断用户是游客还是会员操作的操作
- case "10":
- String securityCode = securityCode() + "";
- System.out.println("------------------------------------");
- System.out.println("请输入验证码确认操作:" + securityCode);
- String inputCode = sc.nextLine();
- if (inputCode.equals(securityCode)) {
- changeVip();
- } else {
- System.out.println("<--------输入的验证码有误!-------->");
- System.out.println("------------------------------------");
- control = 3;
- }
- break;
- case "20":
- case "31":
- control = 2;
- break;
- case "30":
- case "41":
- control = 1;
- break;
- case "40":
- case "51":
- control = 0;
- break;
- case "11":
- printUserInfo();
- break;
- case "21":
- changeVip();
- break;
- default:
- control = 0;
- break;
- }
- }
- public static void changeVip() { //修改用户会员信息
- StringBuilder sb = new StringBuilder(userList);
- if (userInfo[2].equals("0")) { //如果为0,即游客,则执行
- userInfo[2] = "1";
- int startIndex = userList.indexOf("#" + userInfo[0] + "," + userInfo[1] + ",0#");
- int endIndex = ("#" + userInfo[0] + "," + userInfo[1] + ",0#").length() + startIndex;
- userList = sb.replace(startIndex, endIndex, "#" + userInfo[0] + "," + userInfo[1] + ",1#").toString();
- //userList.replace("#" + userInfo[0] + "," + userInfo[1] + ",0#", "#" + userInfo[0] + "," + userInfo[1] + ",1#");
- } else if (userInfo[2].equals("1")) { //如果为1,即会员,则执行
- userInfo[2] = "0";
- int startIndex = userList.indexOf("#" + userInfo[0] + "," + userInfo[1] + ",1#");
- int endIndex = ("#" + userInfo[0] + "," + userInfo[1] + ",1#").length() + startIndex;
- userList = sb.replace(startIndex, endIndex, "#" + userInfo[0] + "," + userInfo[1] + ",0#").toString();
- //userList.replace("#" + userInfo[0] + "," + userInfo[1] + ",1#", "#" + userInfo[0] + "," + userInfo[1] + ",0#");
- } else {
- control = 0;
- System.out.println("<----------会员操作失败!---------->");
- System.out.println("------------------------------------");
- }
- }
- public static void printUserInfo() throws IOException { //打印用户信息
- String printUserInfo = "用户帐号:" + userInfo[0] + "\r\n用户密码:" + userInfo[1];
- if (userInfo[2].equals("1")) {
- printUserInfo += "\r\n是否是会员:是";
- } else {
- printUserInfo += "\r\n是否是会员:否";
- }
- FileWriter fw = new FileWriter(userInfo[0] + ".txt");
- fw.write(printUserInfo);
- System.out.println("------------------------------------");
- System.out.println("已完成会员信息打印!");
- fw.close();
- }
- public static int securityCode() { //获取验证码
- return (int)(Math.random() * 10000);
- }
- }
复制代码
|