⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainlist.java~17~

📁 用JAVA编的一个通讯录的程序
💻 JAVA~17~
字号:
package phonebook;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;


public class MainList {
    public static void main(String[] args) {
        int i =1,j=1,k=0;
        String name;
        PhoneList list = new PhoneList();
        PersonInformation editPerson = new PersonInformation();
        DiskFile file = new DiskFile();
        file.readFromDisk("huhu",list);
        String choice = JOptionPane.showInputDialog("Enter your choice");
        while(!choice.equalsIgnoreCase("quit")){
            editPerson = new PersonInformation();
            if(choice.equalsIgnoreCase("add")){
                editPerson.inputInformation();
                editPerson.outputInformation();
                if(list.isEmpty())
                    list.add(editPerson);
                else
                    i = list.myAdd(editPerson);
            }
            if(choice.equalsIgnoreCase("del")){
                try{
                    list.remove(i);
                }
                catch(NoSuchElementException e){
                    System.out.println("The list is empty");
                }

            }
            if(choice.equalsIgnoreCase("edit")){
                editPerson.inputInformation();
                editPerson.outputInformation();
                try{
                    list.set(i,editPerson);
                }
                catch(NoSuchElementException e){
                    System.out.println("The list is empty");
                }
            }
            if(choice.equalsIgnoreCase("search")){
                name = JOptionPane.showInputDialog("Enter the name");
                for(j = 1,k=0;j <= list.size();j++){
                    if(((PersonInformation)list.get(j)).name.equalsIgnoreCase(name)){
                        i = j;
                        k=1;
                        break;
                    }
                }
                if(k == 1)
                    ((PersonInformation)list.get(i)).outputInformation();
                else
                    System.out.println("the person is not in the list");
            }
            if(choice.equalsIgnoreCase("save")){
                file.writeFromDisk("huhu",list);
            }
            choice = JOptionPane.showInputDialog("Enter your choice");

        }
    }

}

class PersonInformation{
    String name,telphone,mobile,email;
    public PersonInformation(){

    }
    public void inputInformation(){
        name = JOptionPane.showInputDialog("Enter the name");
        telphone = JOptionPane.showInputDialog("Enter the telphone number");
        mobile = JOptionPane.showInputDialog("Enter the mobilephone number");
        email = JOptionPane.showInputDialog("Enter the email");
    }
    public void outputInformation(){
        System.out.println("name: "+name+"\ntelphone num: "+telphone+"\nmobilephone num: "+mobile+"\nemail: "+email);
    }
}

class PhoneList extends LinkedList{
    public int myAdd( PersonInformation inform ){
        int i =1;
        while ((inform.name.compareToIgnoreCase(((PersonInformation)this.getFirst()).name) > 0 )||(i == this.size()+1))
            i++;
        System.out.println(i);
        if(i == 1)
            this.addFirst(inform);
        else if(i <= this.size())
            this.add( i, inform );
        else
            this.add(inform);
        return i;
    }

}

class DiskFile{
    public void readFromDisk(String fileName,PhoneList list){
        PersonInformation person = new PersonInformation();
        try{
            RandomAccessFile file = new RandomAccessFile(fileName, "r");
            String name = file.readLine();
            while(name != null)
            {
                person.name = name;
                person.telphone = file.readLine();
                person.mobile = file.readLine();
                person.email = file.readLine();
                list.add( person );
                name = file.readLine();
            }
        }
        catch(IOException e){}
    }
    public void writeFromDisk(String fileName,PhoneList list){
        int i=0;
        try{
            RandomAccessFile file = new RandomAccessFile(fileName, "rw");
            for(i=1;i <= list.size();i++){
                file.writeBytes(((PersonInformation)list.get(i)).name);
                file.writeBytes(((PersonInformation)list.get(i)).telphone);
                file.writeBytes(((PersonInformation)list.get(i)).mobile);
                file.writeBytes(((PersonInformation)list.get(i)).email);
            }

        }
        catch(IOException e){}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -