Java打卡系统
打卡系統
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; //構建一個學生類 public class Student {private String name;private int count;//打卡時間是隨時變動的,所以需要用一個集合存儲,并且時間是通過Date的形式進行獲取的private ArrayList<Date> arr = new ArrayList<>();@Overridepublic String toString() {//把時間按特定的格式輸出SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm;ss");//創建一個字符串集合,存儲修改后的內容StringBuffer stringBuffer = new StringBuffer();for (int i = 0;i<arr.size();i++){//獲取集合中的數據Date date = arr.get(i);//格式化數據,了解格式化的形式String format = sim.format(date);//把格式化的增加到字符串中,知道append方法stringBuffer.append(format+"\n");}return "Student{" +"姓名='" + name + '\'' +", 打卡數量=" + count +", 打卡時間=" + stringBuffer +'}';}//這里面我們是把姓名和打卡的數量看作一個整體,用于進行實例化這個學生類public Student(String name, int count) {this.name = name;this.count = count;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public ArrayList<Date> getArr() {return arr;}public void setArr(ArrayList<Date> arr) {this.arr = arr;} } //該類的功能主要是實現打卡的功能 public class Function {//此方法主要用于實現打卡功能public void dk() {HashMap<Integer, Student> map = new HashMap<>();//初始話三個對象Student wangwu = new Student("王五", 0);Student lisi = new Student("李四", 0);Student zhangsan = new Student("張三", 0);map.put(1, wangwu);map.put(2, lisi);map.put(3, zhangsan);while (true) {Scanner sc = new Scanner(System.in);System.out.println("1、王五2、李四3、張三4、查詢5、退出");System.out.println("請你輸入:");int key = sc.nextInt();if (key == 4) {//要實現查詢首先是先獲取到每個成員,需要遍歷輸出map集合System.out.println("查詢結果如下:");//獲取map集合中的所有的鍵,通過keyset方法獲得Set<Integer> set = map.keySet();//通過for循環遍歷輸出map中的內容for (Integer integer : set) {//通過鍵獲取map中的所有信息System.out.println(map.get(integer));}} else if (key == 5) {System.out.println("成功退出系統");System.exit(0);} else {Student stu = map.get(key);//對該集合成員的打卡數量加1stu.setCount(map.get(key).getCount() + 1);//在集合中增加時間stu.getArr().add(new Date());System.out.println("你已經打卡成功" + stu.getName());}}} } //測試函數 public class Program {public static void main(String[] args) {Function function = new Function();function.dk();} }總結
- 上一篇: ISP模块之RAW DATA去噪(一)
- 下一篇: python 自动划分训练集和测试集