网站建设资讯

NEWS

网站建设资讯

电话本java程序代码,java打电话代码

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();//调用方法 实现多态性

}

}

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

}

}

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

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

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

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

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

java电子电话本小程序怎么写???急救!!!!

既然是小程序嘛~~~~简简单单啦,O(∩_∩)O哈哈~

package testPackage;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.HashMap;

import java.util.Map;

public class Main {

/**

* @param args

*/

public static void main(String[] args)

{

MapString, String telBook = new HashMapString, String();

String name = "";

String tel = "";

String input = "";

for(;;)

{

try {

System.out.println("选择命令:1、输入电话记录;2、查询电话薄记录;3、退出");

input = (new BufferedReader(new InputStreamReader(System.in))).readLine();

} catch (IOException e) {

e.printStackTrace();

}

try {

switch(Integer.parseInt(input))

{

case 1:

try {

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

input = (new BufferedReader(new InputStreamReader(System.in))).readLine();

name = input;

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

input = (new BufferedReader(new InputStreamReader(System.in))).readLine();

tel = input;

} catch (IOException e) {

e.printStackTrace();

};

try {

telBook.put(name, tel);

} catch (RuntimeException e) {

e.printStackTrace();

};

break;

case 2:

try {

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

input = (new BufferedReader(new InputStreamReader(System.in))).readLine();

System.out.println(input + "的电话号码是:" + (String)(telBook.get(input)));

} catch (IOException e) {

e.printStackTrace();

};

break;

case 3:

System.out.print("谢谢使用!");

System.exit(0);

break;

default: break;

}

} catch (Exception e) {

System.out.println("请输入正确的选择项!");

}

}

}

}

无聊之作……


分享标题:电话本java程序代码,java打电话代码
网站URL:http://njwzjz.com/article/dsgsesj.html