具有更多功能的地址簿程序 [英] Address book program with more functionality

查看:90
本文介绍了具有更多功能的地址簿程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一个小型通讯录程序,该程序允许用户:

I've done a small address book program that allows the user to:


  1. 添加联系人

  2. 搜索联系人

  3. 删除联系人

  4. 显示所有联系人

  1. add contact
  2. search contact
  3. delete contact
  4. display all contacts

输入一个选项后结束,我希望它继续运行直到用户说出例如5-退出

It ends after you enter one option, I want it to keep running until the user says eg 5- exit

我要写入数据的另一个问题并读取到data.dat文件

another problem I want the data to written and read to data.dat file

我是一个新手,可以告诉我如何将其拆分为单独的类并相互继承。

I'm completly new, can some tell me how to split up this into separate classes and inherit each other.

我的代码:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class AddressBookOperations
{


    public static void main(String[] args) throws IOException
    {
        String s = null;
        String s2 = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        // Console in = System.console();

        System.out.println(" Please select the required operations.\n"
                        + " 1- Add contact\t 2- search contact\t 3- delete contact\t 4- display all contacts\n");
        s2 = in.readLine();
        if (s2 != null && !(s2.equals("1") || s2.equals("2") || s2.equals("3") || s2.equals("4")))
        {
            System.out.println("Invalid Operation Selected\n");
            System.exit(0);
        }

        else
        {
            s = s2;
        }

        if (s != null)
        {
            String dataLine;
            String data;
            if (s.equals("1")) {
                System.out.println("Name: ");
                dataLine = in.readLine();
                data = dataLine;
                in = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("PhoneNumber: ");
                dataLine = in.readLine();
                data = data + ":" + dataLine;
                writeToFile("C:/AddressBook.bat", data, true, true);
            } else if (s.equals("2")) {
                System.out.println("Enter Name 0r PhoneNumber: ");
                dataLine = in.readLine();
                String result = readFromFile("C:/AddressBook.bat", dataLine);
                System.out.println("Search Results\n" + result);
            } else if (s.equals("3")) {
                System.out.println("Enter Name: ");
                dataLine = in.readLine();
                data = dataLine;
                System.out.println("PhoneNumber: ");
                dataLine = in.readLine();
                data = data + ":" + dataLine;
                deleteFromFile("C:/AddressBook.bat", data);
            } else if (s.equals("4")) {
                String result = readFromFile("C:/AddressBook.bat", null);
                System.out.println("Search Results\n" + result);
            }
        }

    }

    private static void deleteFromFile(String string, String dataLine) {
        String data = readFromFile(string, null);
        data = data.replaceAll(dataLine, "");
        writeToFile(string, data, false, false);
    }

    public static boolean writeToFile(String fileName, String dataLine,
            boolean isAppendMode, boolean isNewLine) {
        if (isNewLine) {
            dataLine = "\n" + dataLine;
        }

        try {
            File outFile = new File(fileName);
            DataOutputStream dos;
            if (isAppendMode) {
                dos = new DataOutputStream(new FileOutputStream(fileName, true));
            } else {
                dos = new DataOutputStream(new FileOutputStream(outFile));
            }

            dos.writeBytes(dataLine);
            dos.close();
        } catch (FileNotFoundException ex) {
            return (false);
        } catch (IOException ex) {
            return (false);
        }
        return (true);

    }

    /*
     * Reads data from a given file
     */
    public static String readFromFile(String fileName, String dataLine2) {
        String DataLine = "";
        String fileData = "";
        try {
            File inFile = new File(fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(inFile)));
            if (dataLine2 != null)
            {
                while ((DataLine = br.readLine()) != null)
                {
                    if (DataLine.contains(dataLine2)) {
                        fileData = DataLine;
                    }
                }
            }

            else
            {
                while ((DataLine = br.readLine()) != null)
                {
                    fileData = fileData + "\n" + DataLine;
                    //System.out.println(DataLine);
                }
            }
            br.close();
        }

        catch (FileNotFoundException ex)
        {
            return (null);
        } catch (IOException ex)
        {
            return (null);
        }
        return (fileData);

    }

    public static boolean isFileExists(String fileName) {
        File file = new File(fileName);
        return file.exists();
    }
}


推荐答案

您可以将您的逻辑包装在 while 循环中,该循环在给定的 boolean 为真时终止,因此您将继续返回每个操作执行后的开始。例如:

You can wrap your logic in a while loop which terminates when a given boolean is true, therefore you will keep going back to the start after each operation is performed. For example:

boolean isRunning = true;

while (isRunning) {
  //your code here

  if (s2.equals("5")) {
    isRunning = false;
  }
}

您还应该将所有逻辑移出 main()并进入其自己的单独函数,该函数从 main()调用。我也不确定为什么要写入.bat文件?如果要写入.dat文件,请将扩展名更改为.dat。

You should also move all of your logic out of main() and into its own seperate function that is called from main(). I'm also not sure why you are writing to a .bat file? Change the extension to .dat if you want to write to a .dat file.

这篇关于具有更多功能的地址簿程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆