PhoneBook類:

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了澤州免費(fèi)建站歡迎大家使用!
PhoneBook
數(shù)據(jù)存儲(chǔ)Database類
Database
鍵盤輸入并轉(zhuǎn)換字符(KeyBoardInput)類:
KeyBoardInput類
存儲(chǔ)查找控制類(DoMain):
DoMain
測(cè)試類 Demo:
Demo24
測(cè)試結(jié)果:
1 設(shè)計(jì)功能 如 增加聯(lián)系人,刪除練習(xí)人,修改聯(lián)系人
2 設(shè)計(jì)界面 用swing畫個(gè)簡(jiǎn)單界面。
3 編寫功能代碼,實(shí)現(xiàn)功能。
4 做數(shù)據(jù)持久化,看是把信息存在數(shù)據(jù)庫,還是存在文件里。
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("文件存儲(chǔ)路徑:");
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("文件存儲(chǔ)路徑:");
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("你要?jiǎng)h除的姓名是:");
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] = "";
}
}
具體方法如下:
1、定義封裝一條記錄的實(shí)體類
2、根據(jù)實(shí)際系統(tǒng)容量,定義一個(gè)數(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("請(qǐng)輸入你得選擇:(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("請(qǐng)輸入手機(jī)號(hào):");
String cellphone = scn.nextLine();
System.out.print("請(qǐng)輸入姓名:");
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("請(qǐng)輸入要?jiǎng)h除的手機(jī)號(hào):");
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("請(qǐng)輸入要修改的手機(jī)號(hào):");
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("請(qǐng)輸入姓名:");
String name = scn.nextLine();
contacts[index].name = name;
}
}
} while (!cmd.equals("0"));
System.out.println("退出成功!");
scn.close();
System.exit(0);
}
}
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;//計(jì)數(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("沒有該信息!");
}
//按編號(hào)查找
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("容量已滿,不能再存儲(chǔ)了!");
}
else{
data[count++]=new PhoneData(name,phonenumber,ID);
}
}
//刪除指定編號(hào)
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("沒有該信息!");
}
//顯示所有號(hào)碼
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.按號(hào)碼查找");
System.out.println("4.添加通訊錄");
System.out.println("5.刪除通訊錄");
System.out.println("6.顯示所有號(hào)碼");
System.out.println("請(qǐng)輸入數(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("請(qǐng)輸入姓名");
String name=readerStream.readLine();
phonebook.searchByName(name);
}
break;
case 2:{
System.out.println("請(qǐng)輸入ID");
int ID=Integer.parseInt(readerStream.readLine());
phonebook.serchByPhoneid(ID);
}
break;
case 3:{
System.out.println("請(qǐng)輸入號(hào)碼");
String phonenumber=readerStream.readLine();
phonebook.searhByPhoneNum(phonenumber);
}
break;
case 4:{
System.out.println("請(qǐng)輸入信息");
System.out.println("請(qǐng)輸入ID");
int ID=Integer.parseInt(readerStream.readLine());
System.out.println("請(qǐng)輸入姓名");
String name=readerStream.readLine();
System.out.println("請(qǐng)輸入號(hào)碼");
String phonenumber=readerStream.readLine();
phonebook.addinfo(name, phonenumber, ID);
}
break;
case 5:{
System.out.println("請(qǐng)輸入ID");
int ID=Integer.parseInt(readerStream.readLine());
phonebook.deleteinfo(ID);
}
break;
case 6:
phonebook.disp();
break;
}
}
}
}
代碼基本如上,基本實(shí)現(xiàn)了功能,但還是有 點(diǎn)不足的。。
==================================
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();// 添加一個(gè)就記錄一次文件
} 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;
}
}
本文標(biāo)題:電話簿Java代碼結(jié)果,java電話簿程序?qū)嵺`報(bào)告
文章來源:http://chinadenli.net/article11/dsgshdd.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站排名、網(wǎng)站改版、做網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)