
正文
电话本的java程序代码 电话本的java程序代码怎么写
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java通讯录管理系统设计代码怎么编译
java编写这个通讯录管理系统
java编写这个通讯录管理系统_Java如何实现通讯录管理系统
咕噜噜在芬兰
原创
关注
3点赞·2305人阅读
Java如何实现通讯录管理系统
发布时间:2020-07-28 09:39:42
来源:亿速云
阅读:65
作者:Leah
这篇文章将为大家详细讲解有关Java如何实现通讯录管理系统,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
本文实例为大家分享了java实现通讯录管理系统的具体代码,供大家参考,具体内容如下
完成项目的流程:
1.根据需求,确定大体方向
2.功能模块分析
3.界面实现
4.功能模块设计
5.coding
6.代码测试
下面是源代码:import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.text.html.HTMLDocument.Iterator;
class Infro{
public String id;
public String name;
public String sex;
public String address;
public String e_mail;
public String phoneNumber;
static int index = 0;
static ArrayList list = new ArrayList();
static int len = list.size();
//构造函数
public Infro(String id,String name,String sex,String address,String e_mail,String phoneNumber){
this.id = id;
this.name = name;
this.sex = sex;
this.address = address;
this.e_mail = e_mail;
this.phoneNumber = phoneNumber;
}
public String toString(){
return "编号:"+id+" 姓名:"+name+" 性别:"+sex+" 通讯地址:"+address+" 邮箱地址:"+e_mail+" 电话:"+phoneNumber;
}
/**
* 添加功能
**/
public static void addFunction(){//添加功能
Infro infro = new Infro("","","","","","");
System.out.println("请输入添加的数据:");
Scanner in = new Scanner(System.in);
System.out.println("输入编号:");
infro.id = in.next();
System.out.println("输入姓名:");
infro.name = in.next();
System.out.println("输入性别:");
infro.sex = in.next();
System.out.println("输入通讯地址:");
infro.address = in.next();
System.
out.println("输入邮箱地址:");
infro.e_mail = in.next();
System.out.println("输入电话:");
infro.phoneNumber = in.next();
list.add(index,infro);
index++;
if(list.isEmpty()){
System.out.println("数据添加失败啦");
}else{
System.out.println("数据添加成功啦");
len++;//list集合长度加一
// System.out.println(list.get(0).toString());
}
}
// public static void deleteFunction(){//删除功能
// System.out.println("输入要删除的联系人的编号");
// Scanner in_2 = new Scanner(System.in);
// String d1 = in_2.nextLine();
// for(int a= 0; a
// if(d1.equals(list.get(a).id)){
// list.remove(list.get(a));
// len --;
// }
// }
// }
/**
* 删除功能
**/
public static void deleteFunction(){
System.out.println("输入要删除的联系人的编号");
Scanner in_2 = new Scanner(System.in);
String d1 = in_2.nextLine();
java.util.Iterator it = list.iterator();
while (it.hasNext()){
Infro infro = it.next();
if (infro.id.equals(d1)){
it.remove();
--index;//一定要加这个,否则当做了删除操作再做添加操作的时候会出现异常(类似于指针,栈)
System.out.println("删除完毕"+"此时通讯录记录条数为:" + --len);
}
}
}
/**
* 修改功能
**/
public static void reditFunction(){
System.out.println("输入要修改的通讯录的Id");
Scanner in_r = new Scanner(System.in);
String r1 = in_r.nextLine();
for(int a = 0; a len;a++){
if(r1.equals(list.get(a).id)){
System.out.println("输入修改后的姓名:");
String name_1 = in_r.next();
list.get(a).name = name_1;
System.out.println("输入修改后的性别:");
String sex_1 = in_r.next();
list.get(a).sex = sex_1;
System.out.println("输入修改后的通讯地址:");
String address_1 = in_r.next();
list.get(a).address = address_1;
System.out.println("输入修改后的邮箱地址:");
String e_mail_1 = in_r.next();
list.get(a).e_mail = e_mail_1;
System.out.println("输入修改后的电话:");
String phoneNumber_1 = in_r.next();
list.get(a).phoneNumber = phoneNumber_1;
System.out.println("数据修改完毕");
}
}
}
/**
* 查询功能
**/
public static void searchFunction() throws Exception{//查询功能
System.out.println("请输入要查询的姓名:");
Scanner in_1 = new Scanner(System.in);
String s1=in_1.nextLine();
for(int a= 0; a
if(s1.equals(list.get(a).name)){
System.out.println(list.get(a).toString());
}
}
}
/**
* 显示功能
**/
public static void showFunction(){
for(int i = 0 ;i
System.out.println(list.get(i).toString());
}
}
/**
* 保存功能
**/
public static void writeFunction() throws IOException{
FileWriter writer = new FileWriter("通讯录管理.txt");
for(int i = 0 ;i
String []strwriter = new String[len];
strwriter[i]=list.get(i).toString();
writer.write(strwriter[i]);
writer.write("\r\n");
System.out.println("成功写入一行数据到 通讯录管理.txt 中");
}
writer.close();//关闭写入流,释放资源
}
/**
* 读取功能
**/
public static void readFunction() throws IOException{
FileReader reader = new FileReader("通讯录管理.txt");
BufferedReader br = new BufferedReader(reader);
String str;
while((str = br.readLine()) != null){//每次读取一行文本,判断是否到达文件尾
System.out.println(str);
}
br.close();
}
}
public class Demo extends JFrame {
/**
* 界面设计
**/
public Demo(){
Container c = getContentPane(); //定义一个顶级容器c
JPanel jp = new JPanel(); //新建JPanel面板--jp
JButton button1 = new JButton("新建联系人");
JButton button2 = new JButton("删除联系人");
JButton button3 = new JButton("编辑联系人");
JButton button4 = new JButton("查找联系人");
JButton button5 = new JButton("显示所有联系人");
JButton button6 = new JButton("保存联系人到本地");
JButton button7 = new JButton("读取本地联系人");
jp.setLayout(new GridLayout(2,4,5,5));//新建网格布局管理器(行数,列数,组件间的水平垂直间距)
jp.add(button1);
jp.add(button2);
jp.add(button3);
jp.add(button4);
jp.add(button5);
jp.add(button6);
jp.add(button7);
c.add(jp);//将JPanel面板jp添加到顶级容器c中
setSize(600,500);
setTitle("*通 讯 录 管 理 系 统*");
setVisible(true);
setResizable(false);//窗体大小由程序员决定,用户不能自由改变大小
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
/**
*按键响应
*
**/
button1.addActionListener(new ActionListener(){//添加功能实现
public void actionPerformed(ActionEvent arg0){
Infro.addFunction();
}
});
button2.addActionListener(new ActionListener(){//删除功能实现
public void actionPerformed(ActionEvent arg0){
Infro.deleteFunction();
}
});
button3.addActionListener(new ActionListener(){//修改功能实现
public void actionPerformed(ActionEvent arg0){
Infro.reditFunction();
}
});
button4.addActionListener(new ActionListener(){//查询功能实现
public void actionPerformed(ActionEvent arg0){
try {
Infro.searchFunction();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button5.addActionListener(new ActionListener(){//显示功能实现
public void actionPerformed(ActionEvent arg0){
Infro.showFunction();
}
});
button6.addActionListener(new ActionListener(){//保存功能实现
public void actionPerformed(ActionEvent arg0){
try {
Infro.writeFunction();
} catch (IOException e) {
e.printStackTrace();
}
}
});
button7.addActionListener(new ActionListener(){//读取功能实现
public void actionPerformed(ActionEvent arg0){
try {
Infro.readFunction();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Demo();
Infro a = new Infro("", "", "", "", "", "");
}
}
关于Java如何实现通讯录管理系统就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
相关问答
Q1: java 电话本程序
只有最基本的功能,用户界面很粗糙,联系人明细也只有名字和一个电话,试着修改/增加一些功能以及配合数据库使用吧.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class PhoneBookDemo extends JFrame implements ActionListener,
PhoneBookDemoConstants {
private PhoneBook phoneBook;
JTextArea displayArea;
public PhoneBookDemo () {
phoneBook = new PhoneBookImpl();
displayArea = new JTextArea(20, 40);
displayArea.setEditable(false);
BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
JPanel buttonPanel = new JPanel();
JButton addButton = new JButton(ADD_BUTTON_DISPLAY);
JButton searchButton = new JButton(SEARCH_BUTTON_DISPLAY);
JButton updateButton = new JButton(UPDATE_BUTTON_DISPLAY);
JButton removeButton = new JButton(REMOVE_BUTTON_DISPLAY);
addButton.addActionListener(this);
searchButton.addActionListener(this);
updateButton.addActionListener(this);
removeButton.addActionListener(this);
buttonPanel.add(addButton);
buttonPanel.add(searchButton);
buttonPanel.add(updateButton);
buttonPanel.add(removeButton);
display();
add(displayArea);
add(buttonPanel);
setLayout(layout);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(ADD_BUTTON_DISPLAY)) {
addContact();
}
else if (e.getActionCommand().equals(SEARCH_BUTTON_DISPLAY)) {
searchContact();
}
else if (e.getActionCommand().equals(UPDATE_BUTTON_DISPLAY)) {
updatePhoneBook();
}
else if (e.getActionCommand().equals(REMOVE_BUTTON_DISPLAY)) {
removeContact();
}
}
private void addContact() {
String name = JOptionPane.showInputDialog(ASK_FOR_NAME);
if (name != null !name.trim().equals("")) {
String phone = JOptionPane.showInputDialog(ASK_FOR_PHONE);
if (phoneBook.add(name, phone)) {
JOptionPane.showMessageDialog(null, ADD_SUCCESSFUL);
}
else {
JOptionPane.showMessageDialog(null, ADD_FAILED_DUPLICATE);
}
display();
}
}
private void searchContact() {
int option = -1;
while (option == -1) {
option = JOptionPane.showOptionDialog(null, SEARCH_PROMPT, "",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, SEARCH_OPTIONS, SEARCH_OPTIONS[0]);
}
if (option == 0) {
String name = JOptionPane.showInputDialog(ASK_FOR_NAME);
Contact c = phoneBook.getByName(name);
if (c != null) {
JOptionPane.showMessageDialog(null, phoneBook.getByName(name));
}
else {
JOptionPane.showMessageDialog(null, SEARCH_NOT_FOUND);
}
}
else {
String phone = JOptionPane.showInputDialog(ASK_FOR_PHONE);
Contact c = phoneBook.getByPhone(phone);
if (c != null !c.getPhone().equals(ContactImpl.EMPTY_PHONE)) {
JOptionPane.showMessageDialog(null, c);
}
else {
JOptionPane.showMessageDialog(null, SEARCH_NOT_FOUND);
}
}
}
private void updatePhoneBook() {
phoneBook.update();
display();
}
private void removeContact() {
String name = JOptionPane.showInputDialog(ASK_FOR_NAME);
if (name != null !name.trim().equals("")) {
int option = JOptionPane.showConfirmDialog(null, REMOVE_CONFIRM
+ name + "?", "Confirm", JOptionPane.YES_NO_OPTION);
if (option == 0) {
if (!phoneBook.remove(name)) {
JOptionPane.showMessageDialog(null, SEARCH_NOT_FOUND);
}
else {
JOptionPane.showMessageDialog(null, REMOVE_SUCCESSFUL);
}
}
}
display();
}
private void display() {
displayArea.selectAll();
displayArea.replaceSelection("");
for (Contact c : phoneBook.getPhoneBook()) {
displayArea.append(c.toString() + "\n");
}
}
private static void createAndShowGUI() {
JFrame mainFrame = new PhoneBookDemo();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}
interface PhoneBookDemoConstants {
static final String ADD_BUTTON_DISPLAY = "Add";
static final String SEARCH_BUTTON_DISPLAY = "Search";
static final String UPDATE_BUTTON_DISPLAY = "Sort";
static final String REMOVE_BUTTON_DISPLAY = "Remove";
static final String ASK_FOR_NAME = "Please enter name of contact :";
static final String ASK_FOR_PHONE = "Please enter phone of contact :";
static final String ADD_SUCCESSFUL = "Contact has been added successfully.";
static final String ADD_FAILED_DUPLICATE = "Contact with same name has been dectected.";
static final String SEARCH_OPTIONS[] = {"By name", "By phone"};
static final String SEARCH_PROMPT = "Please Select a search method.";
static final String SEARCH_NOT_FOUND = "Contact is not found in your phone book.";
static final String REMOVE_CONFIRM = "Are you sure that you want to remove details of ";
static final String REMOVE_SUCCESSFUL = "Contact has been successfully removed.";
}
interface Contact {
public String getName();
public void setName(String name);
public String getPhone();
public void setPhone(String phone);
public String toString();
}
interface PhoneBook {
public ArrayListContact getPhoneBook();
public boolean add(String name);
public boolean add(String name, String phone);
public Contact getByName(String name);
public Contact getByPhone(String phone);
public void update();
public boolean remove(String name);
boolean isExist(String name);
}
class ContactImpl implements Contact {
static final String EMPTY_PHONE = "Empty";
private String name;
private String phone;
public ContactImpl (String name) {
this.name = name;
this.phone = null;
}
public ContactImpl (String name, String phone) {
this.name = name;
this.phone = phone;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getPhone() {
return (phone == null ? EMPTY_PHONE : phone);
}
@Override
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return getName() + "\t\t: " + getPhone();
}
}
class PhoneBookImpl implements PhoneBook {
ArrayListContact phoneBook;
public PhoneBookImpl () {
phoneBook = new ArrayListContact();
}
@Override
public ArrayListContact getPhoneBook() {
return phoneBook;
}
@Override
public boolean add(String name) {
if (isExist(name)) {
return false;
}
else {
phoneBook.add(new ContactImpl(name));
return true;
}
}
@Override
public boolean add(String name, String phone) {
if (isExist(name)) {
return false;
}
else {
if (phone == null || phone.equals("") || phone.trim().equals("")) {
add(name);
}
else {
phoneBook.add(new ContactImpl(name, phone));
}
return true;
}
}
@Override
public Contact getByName(String name) {
for (Contact c : phoneBook) {
if (c.getName().equals(name)) {
return c;
}
}
return null;
}
@Override
public Contact getByPhone(String phone) {
for (Contact c : phoneBook) {
if (c.getPhone().equals(phone)) {
return c;
}
}
return null;
}
@Override
public boolean remove(String name) {
for (Contact c : phoneBook) {
if (c.getName().equals(name)) {
phoneBook.remove(c);
return true;
}
}
return false;
}
@Override
public void update() {
for (int i = 0; i phoneBook.size() - 1; i++) {
for (int j = i + 1; j phoneBook.size(); j++) {
if (phoneBook.get(i).getName().toLowerCase().compareTo(
phoneBook.get(j).getName().toLowerCase()) 0) {
Contact c = phoneBook.get(i);
phoneBook.set(i, phoneBook.get(j));
phoneBook.set(j, c);
}
}
}
}
@Override
public boolean isExist(String name) {
for (Contact c : phoneBook) {
if (c.getName().equals(name)) {
return true;
}
}
return false;
}
}
Q2: 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);
}
}
Q3: 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;
}
}
Q4: Java编程
/*1、类与对象的基础题:
1)编程实现:以电话Phone为父类(例:电话有本机号码、打电话、接电话等属性和功能,
当然还有一些其它的特性), 移动电话Mobilephone和固定电话Fixedphone为两个子类,
并使移动电话实现接口:可移动Moveable。固定电话又有子类:无绳电话Cordlessphone。
设计并定义这四个类(Phone、Mobilephone、Fixedphone、Cordlessphone)和一个接口(Moveable),
明确它们的继承关系,定义子类时给出子类有别于父类的新特性。
*/
class Phone {//定义一个Phone类,其属性为电话号码,方法有打电话和接电话
private String phonenum;
public void callPhone() {}
//无参的构造方法
Phone() {}
//有参的构造方法 以后相似
Phone(String s) {
System.out.println("phonenum = " + s);
}
public void acceptPhone() {
System.out.println("父类方法");
}
}
//定义Mobilephone类,从Phone类继承,实现了Moveable接口
//实现接口要重写其中的全部方法,因为没有给出Moveable接口中的方法,所以就没写,即编译也不会成功,若想看到结果把下面的implements Moveable 注释掉
class MobilePhone extends Phone implements Moveable {
private String cellnum;
public void callPhone() {}//重写父类方法
public void setRing() {} //设置铃声
public void playGame() {} //玩游戏
MobilePhone(String s,String s1) {
super(s);
System.out.println("cellphone = " + s1);
}
}
//定义Fixedphone类,从Phone类继承
class FixedPhone extends Phone {
private String fixednum;
private String s;
FixedPhone() {}
FixedPhone(String s,String s2) {
super(s);
System.out.println("fixednum = " + s2);
}
public void acceptPhone() {
System.out.println("实现了多态性");
}//重写父类方法
public void selectNum() {
}
}
//定义Fixedphone子类
class CordlessPhone extends FixedPhone {
private char num;
public void setPassword() {} //设置密码
CordlessPhone() {
super();
}
CordlessPhone(String s2,String s3,char s4) {
super(s2,s3);
System.out.println("num = " + s4);
}
}
/*2)声明测试类:声明Phone类的数组(含5个元素),
生成五个对象存入数组:其中二个Phone类的对象、一个Mobilephone类的对象、一个Fixedphone类的对象和一个Cordlessphone类的对象,
打印输出每个对象的某个成员变量。将一个父类的引用指向一个子类对象,用这个塑型后的对象来调用某个方法实现多态性。 */
public class Test {
public static void main(String[] args) {
Phone[] p = new Phone[5];
p[0] = new Phone("123");
p[1] = new Phone("456");
p[2] = new MobilePhone("123456","138xxxxxxxxx");
p[3] = new FixedPhone("5861","5861xx");
p[4] = new CordlessPhone("5861xxx","12333",'5');
Phone p1 = new FixedPhone();//将一个父类引用指向子类对象
p1.acceptPhone();//调用方法 实现多态性
}
}
Q5: 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程序代码和电话本的java程序代码怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







