自我练习V
package tx;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class GameMain {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
//申请一个内存空间用来保存英雄
//创建list
//List list = new ArrayList();//多态向下转型
List list=HonronUtil.readFromFile();
Map map = new HashMap();
//Honron honron = null ;
//
System.out.println("--------------欢迎使用V 1.0 英雄在线管理系统------------------");
System.out.println(" 功能菜单如下 ");
System.out.println("--1.查询所有英雄");
System.out.println("--2.增加英雄");
System.out.println("--3.英雄pk");
System.out.println("--4.退出系统");
System.out.println("--5.英雄删除");
System.out.println("--6.一键清空英雄");
System.out.println("--7.注册");
System.out.println("--8.登录");
System.out.println("--9.管理员登陆");
System.out.println("--提示:请输入序号进行下一步操作!");
String cmd =scanner.next();
boolean startGame=true;
boolean inputIn = false;//等待输入,状态 true 可以接受参数继续操作
while (startGame) {
if(cmd.equals("1")) {
//System.out.println("英雄名称:"+honron.getName()+"攻击力:"+honron.getGongjili());
//System.out.println("该功能暂未上线!");
//list.size() 集合的长度
if(list.size()>1) {
for(int i=0;i< list.size();i++) {
Honron honron = (Honron) list.get(i);//从集合中遍历
System.out.println("英雄名称:"+honron.getName()+"攻击力:"+honron.getGongjili());
}
}else {
System.out.println("暂无信息");
}
inputIn = true;//改变输入状态
}else if(cmd.equals("2")) {
//增加英雄
//创建一个英雄
System.out.println("请输入英雄名称:");
String hname =scanner.next();
boolean isname= false;
//判断英雄名称是否存在
for (int i =0; i < list.size(); i++) {
Honron honron = (Honron) list.get(i);//获取集合中保存的英雄对象
if(hname.equals(honron.getName())) {//说明已经存在重复名称英雄
System.out.println("名称已经存在请从新操作!");
isname = true;
break;
}
}
if(!isname) {
System.out.println("请输入英雄攻击力:");
int gongjili = scanner.nextInt();
Honron honron =new Honron();
honron.setName(hname);
honron.setGongjili(gongjili);
list.add(honron);//把英雄存储到list内存中
System.out.println("添加英雄成功!");
}else {
System.out.println("请输入菜单命令,继续操作!");
}
inputIn = true;
}else if(cmd.equals("3")) {
System.out.println("该功能暂未上线!");
inputIn = true;
}else if(cmd.equals("4")) {
startGame = false;
inputIn = false;
System.out.println("欢迎下一次使用!");
}
else if(cmd.equals("5")) {
System.out.println("输入需要删除英雄编号");
//接受输入的英雄编号
String id = scanner.next();
//在list中移除英雄
//转换接收到的英雄编号
int honnor = Integer.parseInt(id)-1;
list.remove(honnor);//操作移除英雄
HonronUtil.writeToFile(list);
startGame = true;
inputIn = true;
}else if(cmd.equals("6")) {
list.clear();//清空集合
System.out.println("清空英雄成功!");
startGame = true;
inputIn = true;
}else if(cmd.equals("7")) {//普通用户注册
System.out.println("注册普通用户");
System.out.println("请输入用户名:");
String name= scanner.next();
System.out.println("请输入密码:");
String pwd1= scanner.next();
System.out.println("请再次输入密码:");
String pwd2= scanner.next();
//判断map中是否已经存在
if(map.containsKey(name)) {
System.out.println("该用户已被注册,你来晚一步!");
}else {
if(pwd1.equals(pwd2)) {//两次输入的密码是否一致
map.put(name, pwd2);//往map中增加元素内容
System.out.println("欢迎"+name+"注册成功");
}else {
System.out.println("两次输入的密码不一致!");
}
}
startGame = true;
inputIn = true;
}else if(cmd.equals("8")) {
System.out.println("普通用户登录");
System.out.println("请输入用户名:");
String name= scanner.next();
System.out.println("请输入密码:");
String pwd1= scanner.next();
String pdwMap= (String) map.get(name);
if(pwd1.equals(pdwMap)) {
System.out.println("欢迎"+name+"登陆成功!");
}else {
System.out.println("账号或者密码错误!");
}
startGame = true;
inputIn = true;
}else if(cmd.equals("9")) {
//用户名密码是 admin 1234
String username = scanner.next();
System.out.println("请输入密码");
String password = scanner.next();
if(username.equals("admin") && password.equals("1234")) {
System.out.println("管理员登陆成功");
}
startGame = true;
inputIn = true;
}
if(inputIn) {
cmd =scanner.next();
}
}
//}
}
}
package tx;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class HonronUtil {
public static List readFromFile() {
List honors = null;
try {
FileInputStream fileInputStream = new FileInputStream("D:\\mk.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(
fileInputStream);
honors = (List) objectInputStream.readObject();
if (honors == null) {
honors = new ArrayList();
} else {
}
objectInputStream.close();
fileInputStream.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return honors;
}
public static void writeToFile(List list) throws IOException {
// TODO Auto-generated method stub
FileOutputStream outputStream = new FileOutputStream("D:\\mk.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
outputStream);
objectOutputStream.writeObject(list);
objectOutputStream.close();
outputStream.close();
}
public static void main(String[] args) throws IOException {
List list = new ArrayList();
writeToFile(list);
}
}