【JAVA】电话簿管理及学习提醒系统实现

180 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

【JAVA】实现电话簿管理及学习提醒系统

一、系统主要功能

实现简单的电话信息的写入、读取、查看等管理操作与学习的时刻提醒。

二、系统体系结构

image.png 图1.系统结构UML图

三、编码说明

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import thread.ThreadAdd;
import thread.ThreadAdd.ThreadTest;

public class Main {

	public static void main(String[] args) throws InterruptedException {

	    Information[] inf = new Information[4];
	    inf[0] = new Information();
	    inf[1] = new Information("JMU","CHINA",101);
	    inf[2] = new Other("计算机工程学院","陆大楼",15);
	    inf[3] = new Major();

	    System.out.println(inf[0].getInformation1());
	    System.out.println(inf[1].getInformation2());
	    System.out.println(inf[2].getInformation2());
	    System.out.println(inf[3].getInformation2());

        System.out.print("\n想不想判断上面这两个东西的相似度?\n想就扣1!\n");
        
        Scanner s1 = new Scanner(System.in);              	 
        int num1 = s1.nextInt();
        if (num1==1) {
    	    System.out.println(inf[0].equals(inf[1]));
        }
        System.out.print("\n想不想要这个学校的联系方式?\n来吧!打个1!\n");
        int num2 = s1.nextInt();
        if (num2==1) {
    	    readFile();
    	    writeFile();
        }
}
    public static void readFile() {
        String pathname = "university.txt"; 
 
        try (FileReader reader = new FileReader(pathname);
             BufferedReader br = new BufferedReader(reader) 
        ) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeFile() {
        try {
        	
            File writeName = new File("university.txt"); 
            writeName.createNewFile(); // 创建文件,同名直接覆盖
            try (FileWriter writer = new FileWriter(writeName,true);
                 BufferedWriter out = new BufferedWriter(writer)
            ) {
                Scanner s = new Scanner(System.in);
                String num1 = s.nextLine();
                System.out.print("\n是否想给不学习的人打骚扰电话?\n");
                String num2 = s.nextLine();
                System.out.print("\n输入0:超级想!\n输入1:我也不爱学习,不想\n");
                int target = s.nextInt();
                if (target==0) {
                    Scanner s1 = new Scanner(System.in);              	
                    System.out.print("输入不学习那个人姓名与电话:\n");  
                    String num3 = s1.nextLine();
                    out.write(num3);
                    out.write("\n");
                    out.flush(); 
                    System.out.print("还要继续输入吗?\n不了不了(0),还要!(1)\n");  
                    int target1 = s1.nextInt();
                    if (target1==1) {
                        Scanner s2 = new Scanner(System.in); 
                        System.out.print("输入不学习那个人姓名与电话:\n");  
                        String num4 = s2.nextLine();
                        out.write(num4);
                        out.write("\n");
                        out.flush(); 
                    }
                    System.out.print("\n最终数据表:\n");  
                    readFile();
                }
                out.close();
                System.out.print("谢谢使用!赶紧去学习,再见!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class Information{
	private String name;
	private String country;
	private int age;
	
	public Information(String name,String country,int age) {
		super();
		this.name = name;
		this.country = country;
		this.age = age;
	}
	public Information() {
		this.name = "JiMei University";
		this.country = "Fujian Province, China";
		this.age = 101;			
	}
	public String getInformation1() {
		return "Name:"+name+"\n"+"Country:"+country+"\n";
	}
	public String getInformation2() {
		return "Name:"+name+"\n"+"Country:"+country+"\n"+"Age:"+age+"\n";
	}
	
	public String toString() {
		return (this.name + ":" + this.age);
		
	}
	public boolean equals(Object obj) {
        System.out.print("\n比较两种信息最终结果为:");
        if(this == obj){
            return true;
        }
        if(obj == null){
            return false;
        }
		return false;
	}
}
public class Major extends Information{
	private String petname;
	private String class1;
	private int age;

public Major() {
	this.petname = "信息管理与信息系统";
	this.class1 = "1812";
	this.age = 5;
	}

public String getInformation2() {
	return "PetName"+petname+"\n"+"Class1:"+class1+"\n"+"Age:"+age+"\n";
	}
}
public class Other extends Information{
	private String petname;
	private String address;
	private int age;

public Other(String petname,String address,int age) {
	this.petname = petname;
	this.address = address;
	this.age = age;
	}

public String getInformation2() {
	return "PetName"+petname+"\n"+"Address:"+address+"\n"+"Age:"+age+"\n";
	}
}

四、设计参数

主要设计参数如图1所示

五.总结与心得

在JAVA的I/O流文件读取与继承关系的学习中,遇到了一些例如,文件打开错误或是读取乱码等的问题。在后续通过互联网工具与同学的交流帮助下解决了问题。跟深入了解了JAVA语言的程序设计思路与方式。