java语言电话簿代码 java电话簿排序

用面向对象方法(java)设计并实现一个简单电话簿?!急!

建一个电话薄类

创新互联是一家专业提供萝北企业网站建设,专注与成都网站制作、做网站、H5建站、小程序制作等业务。10年已为萝北众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

public class PhoneBook{

String name;

String phoneno;

String email;

//再把这些设置上get set方法

}

存的话,把他们add进一个List里

取的话,iterator遍历List,一个一个查名字

java的电话簿程序

==================================

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class PhoneBook {

// 代表有多少条记录

private int size = 0;

// 用来记录信息的数组

private Phone[] phones = new Phone[100];

private String filename = "phonebook.txt";

public PhoneBook() {

try {

read();

} catch (IOException e) {

}

}

private void read() throws IOException {

File f = new File(filename);

if (!f.exists()) {

return;

}

BufferedReader br = new BufferedReader(new FileReader(filename));

String line = null;

while ((line = br.readLine()) != null) {

if (line.trim().length() 0) {

Phone phone = new Phone(line);

addPhone(phone);

}

}

br.close();

}

public void store() throws IOException {

File f = new File(filename);

BufferedWriter bw = new BufferedWriter(new FileWriter(filename));

for (Phone phone : phones) {

if (phone == null) {

continue;

}

String str = phone.name + "::" + phone.number + "::" + phone.notes;

bw.write(str + "\r\n");

}

bw.close();

}

public void addPhone(Phone phone) {

phones[size++] = phone;

}

public Phone getPhone(String name) {

for (Phone phone : phones) {

if (phone == null) {

continue;

}

if (phone.name.equalsIgnoreCase(name)) {

return phone;

}

}

return null;

}

public Phone[] getPhones() {

return phones;

}

}

class Phone {

String name, number, notes;

public Phone() {

}

public Phone(String line) {

String[] strs = line.split("::");

name = strs[0];

number = strs[1];

notes = strs[2];

}

public String toString() {

return String.format("-- %s\r\n-- %s\r\n-- %s", name, number, notes);

}

}

=================================================

import java.io.IOException;

import java.util.Scanner;

public class MainClass {

private static PhoneBook book = new PhoneBook();

public static void main(String[] args) {

getCommand();

}

public static void getCommand() {

String cmd = getString("Command: ");

if (cmd.startsWith("e ")) {

add(cmd.substring(cmd.indexOf(' ') + 1));

try {

book.store();// 添加一个就记录一次文件

} catch (IOException e) {

e.printStackTrace();

}

} else if (cmd.startsWith("f ")) {

find(cmd.substring(cmd.indexOf(' ') + 1));

} else if (cmd.equals("l")) {

list();

} else if (cmd.startsWith("q")) {

quit();

} else {

System.out.println("unknown command!");

}

getCommand();

}

public static void add(String name) {

Phone phone = new Phone();

phone.name = convert(name);// 名字转换

phone.number = getString("Enter number: ");

phone.notes = getString("Enter notes: ");

book.addPhone(phone);

}

public static void find(String name) {

Phone phone = book.getPhone(name);

if (phone != null) {

System.out.println(phone);

} else {

System.out.println("** No entry with code " + name);

}

}

public static void list() {

for (Phone phone : book.getPhones()) {

if (phone != null) {

System.out.println(phone);

}

}

}

public static void quit() {

try {

book.store();

} catch (IOException e) {

}

System.exit(0);

}

public static String getString(String tip) {

System.out.print(tip);

Scanner sc = new Scanner(System.in);

return sc.nextLine();

}

private static String convert(String name) {

if (name != null name.length() 0) {

return name.substring(0, 1).toUpperCase()

+ name.substring(1).toLowerCase();

}

return null;

}

}

JAVA通讯录 求一个JAVA编写的通讯录,基本的就可以。

具体方法如下:

1、定义封装一条记录的实体类

2、根据实际系统容量,定义一个数组

3、完成系统中显示全部记录的逻辑

4、完成系统中添加一条记录的逻辑

5、完成系统中删除一条记录的逻辑

6、完成系统中修改一条记录的逻辑

7、全部代码:

import java.util.Scanner;

class Contact {

String cellPhone;

String name;

}

public class Main {

private static void menu () {

System.out.println("************** 菜单 ******"

+ "************");

System.out.println(" 1.显示全部通讯录");

System.out.println(" 2.增加一条记录");

System.out.println(" 3.删除一条记录");

System.out.println(" 4.修改一条记录");

System.out.println(" 0.退出");

}

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);

Contact[] contacts = new Contact[200];

int size = 0;

String cmd = "";

do {

menu();

System.out.print("请输入你得选择:(0-4)");

cmd = scn.nextLine();

if (cmd.equals("1")) {

if (size == 0)

System.out.println("系统当前无记录!");

else

for (int i = 0; i size; i++) {

System.out.println(contacts[i].name + ":"

+ contacts[i].cellPhone);

}

} else if (cmd.equals("2")) {

System.out.print("请输入手机号:");

String cellphone = scn.nextLine();

System.out.print("请输入姓名:");

String name = scn.nextLine();

Contact contact = new Contact();

contact.cellPhone = cellphone;

contact.name = name;

if (size contacts.length) {

contacts[size++] = contact;

System.out.println("添加成功!");

} else {

System.out.println("你最多只能添加" +

contacts.length + "条记录");

}

} else if (cmd.equals("3")) {

System.out.print("请输入要删除的手机号:");

String cellphone = scn.nextLine();

int index = -1;

for (int i = 0; i size i contacts.length;

i++) {

if (contacts[i].cellPhone.equals(cellphone)) {

index = i;

break;

}

}

if (index == -1) {

System.out.println("该记录不存在!");

} else {

for (int i = index; i size; i++) {

contacts[index] = contacts[index + 1];

}

contacts[size - 1] = null;

size--;

System.out.println("删除成功!");

}

} else if (cmd.equals("4")) {

System.out.print("请输入要修改的手机号:");

String cellphone = scn.nextLine();

int index = -1;

for (int i = 0; i size i contacts.length;

i++) {

if (contacts[i].cellPhone.equals(cellphone)) {

index = i;

break;

}

}

if (index == -1) {

System.out.println("该记录不存在!");

} else {

System.out.print("请输入姓名:");

String name = scn.nextLine();

contacts[index].name = name;

}

}

} while (!cmd.equals("0"));

System.out.println("退出成功!");

scn.close();

System.exit(0);

}

}


当前名称:java语言电话簿代码 java电话簿排序
转载来源:http://scyanting.com/article/hheoph.html