粗略版JavaIo投票每次都会存储数据

package com.zhang.IoDemo2.IoTouPiao;

import javax.persistence.criteria.CriteriaBuilder;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class TouPiao {
public static HashMap<String , Integer> hashMap = new HashMap<>();
public static void main(String[] args) throws IOException, ClassNotFoundException {

File file = new File("D:\\AWork\\Iochengxu.txt");

if (file . length() !=0){
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\AWork\\Iochengxu.txt"));
HashMap<String , Integer> se =( HashMap<String , Integer> ) ois.readObject();
hashMap = se;
ois.close();
}else{
hashMap.put("[001]小刘" ,0);
hashMap.put("[002]小王" , 0);
hashMap.put("[003]小张" , 0);
}





Scanner scanner = new Scanner(System.in);
System.out.println("================================");
System.out.println("欢迎使用张氏投票系统");
System.out.println("-------欢迎为以下人员投票-------)");
System.out.println("[001]小刘\t[002]小王\t[003]小赵");

while (true) {
System.out.println("请输入你选择的干部编号(输入end则投票结束)");
String s = scanner.nextLine();
if (s.equals("end")){
break;
}
if (s.equals("001")){
hashMap.put("[001]小刘" ,hashMap.get("[001]小刘") +1) ;
}
if (s.equals("002")){
hashMap.put("[002]小王" ,hashMap.get("[002]小王") +1) ;
}
if (s.equals("003")){
hashMap.put("[003]小张" ,hashMap.get("[003]小张") +1) ;
}

}
Set<Map.Entry<String, Integer>> entries = hashMap.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
System.out.println("姓名" +entry.getKey());
System.out.println("票数" +entry.getValue());

}
try(
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\AWork\\Iochengxu.txt"))

){
oos.writeObject(hashMap);
System.out.println("写入成功");
}catch (Exception e){
e.printStackTrace();
}

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值