具體方法如下:
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了水磨溝免費建站歡迎大家使用!
1、定義封裝一條記錄的實體類
2、根據(jù)實際系統(tǒng)容量,定義一個數(shù)組
3、完成系統(tǒng)中顯示全部記錄的邏輯
4、完成系統(tǒng)中添加一條記錄的邏輯
5、完成系統(tǒng)中刪除一條記錄的邏輯
6、完成系統(tǒng)中修改一條記錄的邏輯
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("系統(tǒng)當(dāng)前無記錄!");
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("請輸入手機(jī)號:");
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("請輸入要刪除的手機(jī)號:");
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("請輸入要修改的手機(jī)號:");
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);
}
}
import java.util.Arrays;
public class AddressBook
{
private int totalContacts; // 現(xiàn)在通訊錄中的總數(shù)
private int maxContacts; // 通訊錄的最大值
private Contact[] contacts; // 通訊錄的array
/**
* Constructor. 設(shè)定這個通訊錄最初的大小為10人上限.
*/
public AddressBook()
{
totalContacts = 0;
maxContacts = 10;
contacts = new Contact[totalContacts];
}
/**
* 得到一個聯(lián)系通過通訊錄中的名字
*
* @param name
* 要去得到的聯(lián)系人名字
* @return 這個聯(lián)系人, 沒有的話為null
*/
public Contact getContactByName(String name)
{
for(int i = 0; i totalContacts; i++)
{
Contact c = contacts[i];
if(name.equals(c.getName()))
{
return c;
}
}
return null;
}
/**
* 在這個contacts array的最后給這個通訊錄添加聯(lián)系人.
* 如果聯(lián)系人已經(jīng)存在且號碼參數(shù)不為0的話,聯(lián)系人的號碼會更新或者地址參數(shù)不是null的話,地址會更新。.
* 如果totalContacts達(dá)到maxContacts的值的話,在新的聯(lián)系人被添加前,這個contacts
* array的size會乘以雙倍并且maxContacts也乘以雙倍。
*
* @param contact
* 要加到這個通訊錄的聯(lián)系人
* @return true 如果這個聯(lián)系人添加成功, 否則 false;
*/
public boolean addContact(Contact contact)
{
for(int i = 0; i contacts.length; i++)
{
Contact ct = contacts[i];
if(ct.equals(contact))
{
if(ct.getNumber() != 0)
{
ct.setNumber(contact.getNumber());
}
if(null != ct.getAddress())
{
ct.setAddress(contact.getAddress());
}
return true;
}
}
if(totalContacts == maxContacts)
{
totalContacts *= 2;
maxContacts *= 2;
}
Contact[] cs = new Contact[totalContacts + 1];
System.arraycopy(contacts, 0, cs, 0, totalContacts);
cs[cs.length - 1] = contact;
contacts = cs;
totalContacts++;
return true;
}
/**
* 檢查是否聯(lián)系人已經(jīng)存在
*
* @param contact
* 要去檢查的contact
* @return true 如果這個聯(lián)系人被找到,否則false
*/
public boolean contains(Contact contact)
{
for(int i = 0; i totalContacts; i++)
{
if(contacts[i].equals(contact))
{
return true;
}
}
return false;
}
public void printAddressBook()
{
for(int i = 0; i totalContacts; i++)
{
System.out.println(contacts[i]);
}
}
/**
* 在AddressBook中還有一個方法!
* 從通訊錄中移除一個指定的聯(lián)系人,之后把其他的入口左移,這樣在聯(lián)系人間就沒有空隙了,
* For example:
* before - {C1, C2, C3, C4, C5, C6, C7, C8, C9, null} remove C6
* after - {C1, C2, C3, C4, C5, C7, C8, C9, null, null}
*
* @param contact
* 要移除的聯(lián)系人,
* @return true 如果這個聯(lián)系人被成功移除了,否則false;
*/
public boolean removeContact(Contact contact)
{
if(totalContacts == 0)
{
return false;
}
for(int i = 0; i totalContacts; i++)
{
Contact ct = contacts[i];
if(ct.equals(contact))
{
Contact[] cs = new Contact[totalContacts];
System.arraycopy(contacts, 0, cs, 0, i);
System.arraycopy(contacts, i + 1, cs, i, cs.length - i - 1);
contacts = cs;
totalContacts--;
return true;
}
}
return false;
}
@Override
public String toString()
{
return String.format("AddressBook [totalContacts=%s, maxContacts=%s, contacts=%s]", totalContacts, maxContacts,
Arrays.toString(contacts));
}
}
//--------------------------------------------------
public class Contact
{
private String name;
private long number;
private String address;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public long getNumber()
{
return number;
}
public void setNumber(long number)
{
this.number = number;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Override
public boolean equals(Object obj)
{
if(!(obj instanceof Contact))
{
return false;
}
Contact contact = (Contact) obj;
if(this.getName().equals(contact.getName())
this.getAddress().equals(contact.getAddress())
this.getNumber() == contact.getNumber())
{
return true;
}
return false;
}
@Override
public String toString()
{
return String.format("Contact [name=%s, number=%s, address=%s]", name, number, address);
}
}
//---------------------------------------------
public class Test
{
public static void main(String[] args)
{
AddressBook ab = new AddressBook();
for(int i = 0; i 10; i++)
{
Contact contact = new Contact();
int x = i + 1;
contact.setName("C" + x);
contact.setAddress("china");
contact.setNumber(x);
ab.addContact(contact);
}
System.out.println(ab);
Contact c = new Contact();
c.setName("C6");
c.setAddress("china");
c.setNumber(6);
ab.removeContact(c);
System.out.println(ab);
}
}
java編寫這個通訊錄管理系統(tǒng)
java編寫這個通訊錄管理系統(tǒng)_Java如何實現(xiàn)通訊錄管理系統(tǒng)
咕嚕嚕在芬蘭
原創(chuàng)
關(guān)注
3點贊·2305人閱讀
Java如何實現(xiàn)通訊錄管理系統(tǒng)
發(fā)布時間:2020-07-28 09:39:42
來源:億速云
閱讀:65
作者:Leah
這篇文章將為大家詳細(xì)講解有關(guān)Java如何實現(xiàn)通訊錄管理系統(tǒng),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
本文實例為大家分享了java實現(xiàn)通訊錄管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
完成項目的流程:
1.根據(jù)需求,確定大體方向
2.功能模塊分析
3.界面實現(xiàn)
4.功能模塊設(shè)計
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();
//構(gòu)造函數(shù)
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("請輸入添加的數(shù)據(jù):");
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("數(shù)據(jù)添加失敗啦");
}else{
System.out.println("數(shù)據(jù)添加成功啦");
len++;//list集合長度加一
// System.out.println(list.get(0).toString());
}
}
// public static void deleteFunction(){//刪除功能
// System.out.println("輸入要刪除的聯(lián)系人的編號");
// 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("輸入要刪除的聯(lián)系人的編號");
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;//一定要加這個,否則當(dāng)做了刪除操作再做添加操作的時候會出現(xiàn)異常(類似于指針,棧)
System.out.println("刪除完畢"+"此時通訊錄記錄條數(shù)為:" + --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("數(shù)據(jù)修改完畢");
}
}
}
/**
* 查詢功能
**/
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("成功寫入一行數(shù)據(jù)到 通訊錄管理.txt 中");
}
writer.close();//關(guān)閉寫入流,釋放資源
}
/**
* 讀取功能
**/
public static void readFunction() throws IOException{
FileReader reader = new FileReader("通訊錄管理.txt");
BufferedReader br = new BufferedReader(reader);
String str;
while((str = br.readLine()) != null){//每次讀取一行文本,判斷是否到達(dá)文件尾
System.out.println(str);
}
br.close();
}
}
public class Demo extends JFrame {
/**
* 界面設(shè)計
**/
public Demo(){
Container c = getContentPane(); //定義一個頂級容器c
JPanel jp = new JPanel(); //新建JPanel面板--jp
JButton button1 = new JButton("新建聯(lián)系人");
JButton button2 = new JButton("刪除聯(lián)系人");
JButton button3 = new JButton("編輯聯(lián)系人");
JButton button4 = new JButton("查找聯(lián)系人");
JButton button5 = new JButton("顯示所有聯(lián)系人");
JButton button6 = new JButton("保存聯(lián)系人到本地");
JButton button7 = new JButton("讀取本地聯(lián)系人");
jp.setLayout(new GridLayout(2,4,5,5));//新建網(wǎng)格布局管理器(行數(shù),列數(shù),組件間的水平垂直間距)
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("*通 訊 錄 管 理 系 統(tǒng)*");
setVisible(true);
setResizable(false);//窗體大小由程序員決定,用戶不能自由改變大小
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
/**
*按鍵響應(yīng)
*
**/
button1.addActionListener(new ActionListener(){//添加功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
Infro.addFunction();
}
});
button2.addActionListener(new ActionListener(){//刪除功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
Infro.deleteFunction();
}
});
button3.addActionListener(new ActionListener(){//修改功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
Infro.reditFunction();
}
});
button4.addActionListener(new ActionListener(){//查詢功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
try {
Infro.searchFunction();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button5.addActionListener(new ActionListener(){//顯示功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
Infro.showFunction();
}
});
button6.addActionListener(new ActionListener(){//保存功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
try {
Infro.writeFunction();
} catch (IOException e) {
e.printStackTrace();
}
}
});
button7.addActionListener(new ActionListener(){//讀取功能實現(xiàn)
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("", "", "", "", "", "");
}
}
關(guān)于Java如何實現(xiàn)通訊錄管理系統(tǒng)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
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("http://");
String path = filepath.substring(0, n) + "http://";
filepath = filepath.substring(filepath.indexOf("http://") + 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、繼續(xù)");
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] = "";
}
}
Friend類:public class Friend {
/*
* 姓名
*/
private String name;
/*
* 電話
*/
private String telephone;
/*
* 郵箱
*/
private String email;
/*
* 公司
*/
private String company; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} public String toString() {
StringBuffer str = new StringBuffer(); str.append("姓名:" + name).append("\n");
str.append("電話:" + telephone).append("\n");
str.append("郵箱:" + email).append("\n");
str.append("公司:" + company).append("\n");
str.append("-----------------------------------------\n");
return str.toString();
}
}AddFriend類:public class AddFriend { /**
* 主方法 程序的入口
*/
public static void main(String[] args) {
ListFriend friendList = new ArrayListFriend();
char isGo = 'Y';
int i = 0;
do {
Friend friend = new Friend();
System.out.println("請輸入第" + (i + 1) + "位朋友的姓名:");
InputStreamReader reader = new InputStreamReader(System.in);
String str = "";
try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setName(str); System.out.println("請輸入第" + (i + 1) + "位朋友的電話:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (str.matches("\\d*") str.length() == 11) {// 判斷用戶輸入的電話是否符合標(biāo)準(zhǔn)
friend.setTelephone(str);
} else {
System.out.println("電話號碼輸入有誤,請重新輸入!");
continue;
} System.out.println("請輸入第" + (i + 1) + "位朋友的郵箱:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setEmail(str); System.out.println("請輸入第" + (i + 1) + "位朋友的公司:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setCompany(str); friendList.add(friend); i++; System.out.println("是否繼續(xù)添加?(Y/N):");
String go = "";
try {
go = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
isGo = go.charAt(0);
} while (isGo == 'Y' || isGo == 'y'); for (int j = 0; j friendList.size(); j++) {
System.out.println(friendList.get(j).toString());
}
}
}