/*1、類與對象的基礎(chǔ)題:

專注于為中小企業(yè)提供網(wǎng)站設(shè)計制作、做網(wǎng)站服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)涇縣免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
1)編程實現(xiàn):以電話Phone為父類(例:電話有本機號碼、打電話、接電話等屬性和功能,
當然還有一些其它的特性), 移動電話Mobilephone和固定電話Fixedphone為兩個子類,
并使移動電話實現(xiàn)接口:可移動Moveable。固定電話又有子類:無繩電話Cordlessphone。
設(shè)計并定義這四個類(Phone、Mobilephone、Fixedphone、Cordlessphone)和一個接口(Moveable),
明確它們的繼承關(guān)系,定義子類時給出子類有別于父類的新特性。
*/
class Phone {//定義一個Phone類,其屬性為電話號碼,方法有打電話和接電話
private String phonenum;
public void callPhone() {}
//無參的構(gòu)造方法
Phone() {}
//有參的構(gòu)造方法 以后相似
Phone(String s) {
System.out.println("phonenum = " + s);
}
public void acceptPhone() {
System.out.println("父類方法");
}
}
//定義Mobilephone類,從Phone類繼承,實現(xiàn)了Moveable接口
//實現(xiàn)接口要重寫其中的全部方法,因為沒有給出Moveable接口中的方法,所以就沒寫,即編譯也不會成功,若想看到結(jié)果把下面的implements Moveable 注釋掉
class MobilePhone extends Phone implements Moveable {
private String cellnum;
public void callPhone() {}//重寫父類方法
public void setRing() {} //設(shè)置鈴聲
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("實現(xiàn)了多態(tài)性");
}//重寫父類方法
public void selectNum() {
}
}
//定義Fixedphone子類
class CordlessPhone extends FixedPhone {
private char num;
public void setPassword() {} //設(shè)置密碼
CordlessPhone() {
super();
}
CordlessPhone(String s2,String s3,char s4) {
super(s2,s3);
System.out.println("num = " + s4);
}
}
/*2)聲明測試類:聲明Phone類的數(shù)組(含5個元素),
生成五個對象存入數(shù)組:其中二個Phone類的對象、一個Mobilephone類的對象、一個Fixedphone類的對象和一個Cordlessphone類的對象,
打印輸出每個對象的某個成員變量。將一個父類的引用指向一個子類對象,用這個塑型后的對象來調(diào)用某個方法實現(xiàn)多態(tài)性。 */
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();//調(diào)用方法 實現(xiàn)多態(tài)性
}
}
==================================
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;
// 用來記錄信息的數(shù)組
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);// 名字轉(zhuǎn)換
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;
}
}
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;//計數(shù)器
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("請輸入數(shù)字:");
}
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;
}
}
}
}
代碼基本如上,基本實現(xiàn)了功能,但還是有 點不足的。。
只有最基本的功能,用戶界面很粗糙,聯(lián)系人明細也只有名字和一個電話,試著修改/增加一些功能以及配合數(shù)據(jù)庫使用吧.
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;
}
}
1 設(shè)計功能 如 增加聯(lián)系人,刪除練習人,修改聯(lián)系人
2 設(shè)計界面 用swing畫個簡單界面。
3 編寫功能代碼,實現(xiàn)功能。
4 做數(shù)據(jù)持久化,看是把信息存在數(shù)據(jù)庫,還是存在文件里。
既然是小程序嘛~~~~簡簡單單啦,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打電話代碼
網(wǎng)頁路徑:http://chinadenli.net/article37/dsgsesj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、微信小程序、營銷型網(wǎng)站建設(shè)、微信公眾號、云服務器、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)