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();
}
}
}
粗略版JavaIo投票每次都会存储数据
最新推荐文章于 2024-02-28 20:45:16 发布