电话本管理系统

clipboard.png

clipboard.png

clipboard.png

import java.util.Scanner;

public class MyTel {

public static Scanner sc = new Scanner(System.in);
//电话本存储结构
public static String[][] myTels = new String[500][6];
public static int count = 0;   //记录当前电话本中的联系人个数
//显示主界面
public static void showMain(){
    System.out.println("-----------------电话本管理系统----------------");
    System.out.println("1.添加  2.删除  3.修改  4.查询所有  5.根据姓名查询  0.退出");
    System.out.println("-----------------电话本管理系统----------------");
    System.out.println("请选择业务:");
}
//根据索引值修改内容
public static void updateByIndex(int index){
    System.out.println("姓名:");
    myTels[index][0] = sc.next();
    System.out.println("性别:");
    myTels[index][1] = sc.next();
    System.out.println("年龄:");
    myTels[index][2]  = sc.next();
    System.out.println("电话:");
    myTels[index][3] = sc.next();
    System.out.println("QQ:");
    myTels[index][4] = sc.next();
    System.out.println("地址:");
    myTels[index][5] = sc.next();
    System.out.println("姓名:"+myTels[count][0]+",性别:"+myTels[index][1]+",年龄:"+myTels[index][2]+",电话:"+myTels[index][3]+",QQ:"+myTels[index][4]+",地址:"+myTels[index][5]);
}
//添加电话本
public static void myAdd(){
    System.out.println("------------------添加电话本-----------------");
    updateByIndex(count);
    count++;
    System.out.println("添加成功!");
}
//根据姓名查找联系人,返回查找到的索引
public static int findByName(String name){
    int index = -1;
    //循环遍历电话本
    for(int i=0;i<count;i++){
        if(myTels[i][0].equals(name)){
            index = i;
        }
    }
    return index;
}
//删除电话本
public static void myDelete(){
    System.out.println("-----------------删除电话本----------------");
    System.out.println("请输入要删除的联系人姓名:");
    String name = sc.next();
    //首先找到该联系人
    int tempIndex = findByName(name);
    for(int i=0;i<myTels.length;i++){
        if(myTels[i][0].equals(name)){
            tempIndex = i;
        }
    }
    //如果不存在该联系人则直接返回
    if(tempIndex == -1){
        System.out.println("该联系人不存在!");
        return;
    }
    //确认是否删除
    System.out.println("确认要删除吗?1(是)0(否)");
    int input = sc.nextInt();
    if(input == 1){
        //如果存在则直接删除(将后面的联系人替换前面的联系人)
        for(int i=tempIndex+1;i<count;i++){
            //前移
            myTels[i-1] = myTels[i];
        }
        //联系人个数减1
        count--;
        System.out.println("删除成功!");
    }
}
//修改
public static void myUpdate(){
    System.out.println("-----------------修改电话本----------------");
    System.out.println("请输入要修改的联系人姓名:");
    String tempname = sc.next();
    //根据姓名查找联系人
    int index = findByName(tempname);
    if(index == -1){
        System.out.println("无信息!");
        return;
    }
    //正常的修改逻辑
    System.out.println("姓名:"+myTels[index][0]+",性别:"+myTels[index][1]+",年龄:"+myTels[index][2]+",电话:"+myTels[index][3]+",QQ:"+myTels[index][4]+",地址:"+myTels[index][5]);
    System.out.println("求重新输入信息:");
    updateByIndex(index);
    System.out.println("修改成功!");
}
//打印所有电话本
public static void myshowAll(){
    for(String[] infos:myTels){
        for(String info:infos){
            System.out.println(info+",");
        }
        System.out.println();
    }
}
//根据姓名查询
public static void myFinfByName(){
    System.out.println("------------------根据姓名查询-----------------");
    System.out.println("请输入联系人姓名:");
    String name = sc.next();
    int index = findByName(name);
    // TODO 20190112添加“联系人是否存在”
    for(String info:myTels[index]){
        System.out.println(info+",");
    }
}

public static void main(String[] args) {
    while(true){
        //显示主界面
        showMain();
        //获取控制台输入
        int input = sc.nextInt();
        //根据输入进行相关业务处理
        switch(input){
        case 1:
            //添加
            myAdd();
            break;
        case 2:
            //删除
            myDelete();
            break;
        case 3:
            //修改
            myUpdate();
            break;
        case 4:
            //查询所有
            myshowAll();
            break;
        case 5:
            //根据姓名查询
            myFinfByName();
            break;
        case 0:
            //退出系统
            sc.close();
            System.exit(1);
            break;
        default:
            System.out.println("输入有误!");
            showMain();
            break;
        }
    }
}

}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
适用于C++初学者 /************************************************* Copyright (C), 2008- , mekinglong of cumtcs File name:面向对象8(面向对象8.cpp) Author: 计07-2 秦杰 Version: 1.00 Date: 08.11.12 Description:编写程序实现一个简单的电话记录簿,要求记录的个人信息包括: 姓名,单位,家庭电话,移动电话。具体功能如下: 1.创建信息链表并以磁盘文件保存。 2.读取磁盘文件并显示输出所有人的移动电话。 3.按姓名或单位查询家庭电话。 4.通过姓名和单位确定个人,修改其电话信息并存盘. Others: .... Function List: // 主要函数列表,每条记录应包括函数名及功能简要说明 1.main() 完成各种提示与主操作输入输出 History: <author> <time> <version > <desc> *****************************************************/ #include <iostream> #include <assert.h> #include <string> #include <cstring> #include <fstream> #include <iomanip> using namespace std; class telist; class Node { private: char name[30]; char workplace[50]; char hometel[50]; char mobiletel[50]; Node *next; public: friend class telist; friend void load(telist&); }; class telist { private: Node *head; Node *tail; public: telist() { head=tail=NULL; } telist(Node* h,Node *t):tail(t),head(h){} void add(); void del(); void show(); Node* search(); void change(); void store(); Node* find1(); Node* find2(); friend void load(telist&); }; void operate(string ,telist &); void load(telist &); int main() { system("cls"); cout<<"欢迎使用电话簿管理系统:\n"; telist tele; load(tele); string strord; do { cout<<"输入你的操作:\n"; cout<<"1.加入新的电话记录。\n"; cout<<"2.显示输出所有人的移动电话\n"; cout<<"3.按姓名或单位查询家庭电话\n"; cout<<"4.通过姓名和单位确定个人,修改其电话信息并存盘.\n"; cout<<"5.删除一个人信息并存盘。\n"; cout<<"0.退出程序。\n"; cin>>strord; operate(strord,tele); system("cls"); }while(strord!="0") ; system("pause"); system("cls"); return 0; } void operate(string str,telist &t) { if(str=="1") t.add(); else if(str=="2") t.show(); else if(str=="3") t.search(); else if(str=="4") t.change(); else if(str=="5") t.del(); else if(str=="0") t.store(); else cout<<"输入错误!\n"; system("pause"); } void load(telist &t) { ifstream infile("电话簿.dat",ios::in|ios::binary); if(infile == NULL) { cerr<<"打开电话簿失败!!\n"; t.head=t.tail=NULL; return; } if(!infile.eof()) { cout<<"没有数据\n"; t.head=t.tail=NULL; return ; } Node *temp=new Node; t.head=temp; Node *temp2; infile.read ((char*)temp,sizeof(Node)); while(!infile.eof()||temp->next==NULL) // { temp->next=new Node; temp2=temp; temp=temp->next; infile.read ((char*)temp,sizeof(*temp)); t.tail=temp; } temp->next=NULL; delete(temp); t.tail=temp2; t.tail->next=NULL; // delete temp; assert(t.tail->next==NULL); infile.close(); // delete infile; } void telist::add() { if(head==tail&&tail==NULL) { Node * newnode; newnode=head=tail=new Node; if(!newnode) { cout<<"警告:内存分配失败!\n"; return; }//if cout<<"姓名:\n"; cin>>newnode->name; cout<<"工作单位:\n"; cin>>newnode->workplace; cout<<"家庭电话:\n"; cin>>newnode->hometel; cout<<"移动电话:\n"; cin>>newnode->mobiletel; newnode->next=NULL; }//if else{ Node *temp; temp=new Node; tail->next = temp; if(!temp) { cout<<"警告:内存分配失败!\n"; return; } cout<<"姓名:\n"; cin>>temp->name; cout<<"工作单位:\n"; cin>>temp->workplace; cout<<"家庭电话:\n"; cin>>temp->hometel; cout<<"移动电话:\n"; cin>>temp->mobiletel; temp->next=NULL; tail=temp; }//else } void telist::del() { cout<<"先确定你要删除的人:\n"; Node *temp=search(); Node *p=head; if(temp==NULL) { cout<<"没找到要找的人!\n"; return ; } if(p==temp) { cout<<"确定删除?(y或n)"; char ord1; cin>>ord1; if(ord1=='N'||ord1=='n') return; if(ord1=='Y'||ord1=='y') { head=head->next; if(p==tail) tail=NULL;//wenti zaizheli } } else { for(;(p->next)!=temp;) { p=p->next; } cout<<"确定删除?(y或n)"; char ord; cin>>ord; if(ord=='N'||ord=='n')return; if(ord=='Y'||ord=='y')//if1 { if(temp==tail)//if2 { delete temp; tail=p; }//if2 else { p->next=temp->next; delete temp; }//else }//if1 }//else store(); }//del void telist::show() { Node *temp=head; for(;temp!=NULL;) { cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace; cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl; temp=temp->next; } return; } Node* telist::search() { // char sname[30]; // char sworkplace[50]; int ord; cout<<"按照什么查询?\n"; cout<<"1.名字。\n"; cout<<"2.工作单位。\n"; cin>>ord; for(;!cin;) { cout<<"error!"; cin>>ord; } switch(ord) { case 1:return(find1());break; case 2:return(find2());break; default:cout<<"警告:操作错误!\n";return 0; } } Node* telist::find1() { char sn[30]; cout<<"请输入姓名:\n"; cin>>sn; cout<<endl; Node *temp=head; for(;temp;temp=temp->next) { if(!strcmp(sn,temp->name)) { cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace; cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl; return temp; } if(temp->next==NULL) { cout<<"NO FIND!\n"; return NULL; } else return NULL; } } Node* telist::find2() { char sw[50]; cout<<"请输入工作单位:\n"; cin>>sw; cout<<endl; Node *temp=head; for(;temp;temp=temp->next) { if(!strcmp(sw,temp->workplace)) { cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace; cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl; return temp; break; } if(temp->next==NULL) { cout<<"NO FIND!\n"; return NULL; } else return NULL; } } void telist::store() { Node * temp=head; ofstream outfile("电话簿.dat",ios::out|ios::binary); if(!outfile) { cout<<"打开文件失败!\n"; return ; } for(;temp;) { outfile.write ((char *)temp,sizeof(*temp)); temp=temp->next ; } outfile.close(); } void telist::change() { Node *temp=search(); cout<<"请输入新的记录:\n"; cout<<"姓名:\n"; cin>>temp->name; cout<<"工作单位:\n"; cin>>temp->workplace; cout<<"家庭电话:\n"; cin>>temp->hometel; cout<<"移动电话:\n"; cin>>temp->mobiletel; store(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值