电话簿Java代码结果,java电话簿程序实践报告

Java编程题(电话簿)?

PhoneBook类:

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了泽州免费建站欢迎大家使用!

PhoneBook

数据存储Database类

Database

键盘输入并转换字符(KeyBoardInput)类:

KeyBoardInput类

存储查找控制类(DoMain):

DoMain

测试类 Demo:

Demo24

测试结果:

求问用java写一个简单的电话簿代码。我是小白,求具体流程。做实验课呢

1 设计功能 如 增加联系人,删除练习人,修改联系人

2 设计界面 用swing画个简单界面。

3 编写功能代码,实现功能。

4 做数据持久化,看是把信息存在数据库,还是存在文件里。

java通讯录全部代码!

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class AddList {

private String filePath = "";

private String bakPath = "";

private String content = "";

Scanner sc = new Scanner(System.in);

public String readFile(){

content = "";

if (isNull(filePath)) {

System.out.println("文件存储路径:");

filePath = sc.nextLine();

}

File file = new File(filePath);

FileReader fr = null;

try {

if (file.exists()) {

fr = new FileReader(file);

char[] chars = new char[1024];

int n = 0;

while((n = fr.read(chars)) != -1){

String string = new String(chars, 0, n);

content = content + string;

}

} else {

System.out.println("文件不存在");

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fr != null) {

try {

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return content;

}

public void writeFile(String path){

File file = new File(path);

FileOutputStream fos = null;

mkDirs(path);

try {

fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

PrintWriter pw = new PrintWriter(bos, true);

pw.print(content);

pw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void writeFile(){

if (isNull(filePath)) {

System.out.println("文件存储路径:");

filePath = sc.nextLine();

}

File file = new File(filePath);

FileOutputStream fos = null;

mkDirs(filePath);

try {

fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

PrintWriter pw = new PrintWriter(bos, true);

pw.print(content);

pw.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void mkDirs(String filepath){

if (filepath.indexOf("\\") != -1) {

filepath = filepath.replaceAll("\\", "/");

}

int n = filepath.indexOf("//");

String path = filepath.substring(0, n) + "//";

filepath = filepath.substring(filepath.indexOf("//") + 1, filepath.length());

String[] files = filepath.split("/");

for (int i = 0; i files.length - 1; i++) {

path = path + files[i];

File file = new File(path);

if (!file.exists()) {

file.mkdir();

}

}

}

public void addImfor(){

System.out.println("--------增加记录---------");

String name = "";

String tel = "";

String email = "";

content = readFile();

while(true){

System.out.println("姓名:");

name = sc.next();

System.out.println("电话:");

tel = sc.next();

System.out.println("Email:");

email = sc.next();

content = content + name + "" + tel + "" + email +"==";

System.out.println("0、Exit 1、继续");

int i = sc.nextInt();

if (i == 0) {

break;

}

}

writeFile();

}

public void deleteImfor(){

System.out.println("---------删除记录---------");

String name = "";

String[] imfors = null;

content = readFile();

while(true){

System.out.println("你要删除的姓名是:");

name = sc.next();

if (content.indexOf(name) != -1) {

imfors = content.split("==");

for (int i = 0; i imfors.length; i++) {

if (imfors[i].indexOf(name) != -1) {

imfors[i] = "";

}

}

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编写一个电话本类PhoneBook,

public class PhoneData {

private String name;

private String phonenumber;

private int ID;

PhoneData(String name,String phonenumber,int ID){

this.setName(name);

this.setPhonenumber(phonenumber);

this.setID(ID);

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPhonenumber() {

return phonenumber;

}

public void setPhonenumber(String phonenumber) {

this.phonenumber = phonenumber;

}

public int getID() {

return ID;

}

public void setID(int iD) {

ID = iD;

}

}

import java.io.*;

public class PhoneBook {

int count=0;//计数器

String name;

String phonenumber;

int ID;

PhoneData[] data=new PhoneData[100];

//初始化

PhoneBook(){

this.name="张三";

this.phonenumber="12587968541";

this.ID=1;

data[0]=new PhoneData(name,phonenumber,ID);

data[1]=new PhoneData("li","245879",2);

count=2;

}

//按电话查找

public void searhByPhoneNum(String phonenumber){

for(int i=0;icount;i++)

if(phonenumber.equals(data[i].getPhonenumber())){

System.out.println(data[i].getName());

System.out.println(data[i].getID());

}

else

System.out.println("没有该信息!");

}

//按编号查找

public void serchByPhoneid(int ID){

for(int i=0;icount;i++)

if(ID==(data[i].getID())){

System.out.println(data[i].getName());

System.out.println(data[i].getPhonenumber());

}

else

System.out.println("没有该信息!");

}

//按姓名查找

public void searchByName(String name){

for(int i=0;icount;i++)

if(name.equals(data[i].getName())){

System.out.println(data[i].getPhonenumber());

System.out.println(data[i].getID());

}

else

System.out.println("没有该信息!");

}

//添加通讯录

public void addinfo(String name,String phonenumber,int ID){

if(count100){

System.out.println("容量已满,不能再存储了!");

}

else{

data[count++]=new PhoneData(name,phonenumber,ID);

}

}

//删除指定编号

public void deleteinfo(int id){

for(int i=0;icount;i++)

if(id==data[i].getID()){

System.out.println(data[i].getName());

System.out.println(data[i].getPhonenumber());

for(int j=i;jcount-1;j++){

data[j]=data[j+1];

}

count--;

System.out.println("已删除!");

}

else

System.out.println("没有该信息!");

}

//显示所有号码

public void disp(){

if(count==0)

System.out.println("没有信息!");

else

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

System.out.println(data[i].getName());

System.out.println(data[i].getPhonenumber());

System.out.println(data[i].getID());

}

}

//显示菜单

public static void dispMenu(){

System.out.println("1.按姓名查找");

System.out.println("2.按ID查找");

System.out.println("3.按号码查找");

System.out.println("4.添加通讯录");

System.out.println("5.删除通讯录");

System.out.println("6.显示所有号码");

System.out.println("请输入数字:");

}

public static void main(String []arg)throws IOException{

PhoneBook phonebook=new PhoneBook();

BufferedReader readerStream=new BufferedReader(new InputStreamReader(System.in));

while(true){

PhoneBook.dispMenu();

int operater=Integer.parseInt(readerStream.readLine());

switch(operater){

case 1:{

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

String name=readerStream.readLine();

phonebook.searchByName(name);

}

break;

case 2:{

System.out.println("请输入ID");

int ID=Integer.parseInt(readerStream.readLine());

phonebook.serchByPhoneid(ID);

}

break;

case 3:{

System.out.println("请输入号码");

String phonenumber=readerStream.readLine();

phonebook.searhByPhoneNum(phonenumber);

}

break;

case 4:{

System.out.println("请输入信息");

System.out.println("请输入ID");

int ID=Integer.parseInt(readerStream.readLine());

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

String name=readerStream.readLine();

System.out.println("请输入号码");

String phonenumber=readerStream.readLine();

phonebook.addinfo(name, phonenumber, ID);

}

break;

case 5:{

System.out.println("请输入ID");

int ID=Integer.parseInt(readerStream.readLine());

phonebook.deleteinfo(ID);

}

break;

case 6:

phonebook.disp();

break;

}

}

}

}

代码基本如上,基本实现了功能,但还是有 点不足的。。

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电话簿程序实践报告
当前路径:http://lszwz.com/article/dsgshdd.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款