import?java.util.ArrayList;
創(chuàng)新互聯(lián)公司從2013年成立,先為徽縣等服務(wù)建站,徽縣等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為徽縣企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
import?java.util.List;
import?java.util.Scanner;
public?class?AddressList?{
static?Scanner?scanner?=?new?Scanner(System.in);
static?ListEntity?enlist?=?new?ArrayListEntity();
public?static?void?main(String[]?args)?{
int?input;
AddressList?addre?=?new?AddressList();
System.out.println("===============歡迎使用**通訊錄===============");
do{
System.out.println("1、插入聯(lián)系人");
System.out.println("2、刪除練習(xí)人");
System.out.println("3、修改聯(lián)系人");
System.out.println("4、查詢練習(xí)人");
System.out.println("5、查詢?nèi)柯?lián)系人");
System.out.println("0、退出系統(tǒng)");
System.out.println("請(qǐng)選擇:");
input?=?scanner.nextInt();
switch(input){
case?1:
Entity?e?=?new?Entity();
e.id?=?enlist.size();
System.out.println("請(qǐng)輸入聯(lián)系人姓名:");
e.name?=?scanner.next();
System.out.println("請(qǐng)輸入聯(lián)系方式:");
e.number?=?scanner.nextInt();
addre.add(e);
System.out.println("插入成功!");
break;
case?2:
System.out.println("請(qǐng)輸入要?jiǎng)h除聯(lián)系人編號(hào):");
int?num?=?scanner.nextInt();
if(addre.delete(num))
System.out.println("刪除成功!");
else
System.out.println("刪除失敗,請(qǐng)確認(rèn)信息是否正確!");
break;
case?3:
System.out.println("請(qǐng)輸入要修改人編號(hào):");
int?unum?=?scanner.nextInt();
addre.update(unum);
System.out.println("修改完成!");
break;
case?4:
System.out.println("請(qǐng)輸入要查詢?nèi)诵彰?);
String?name?=?scanner.next();
Entity?ent?=?addre.select(name);
if(ent!=null){
System.out.println(name+"的聯(lián)系方式為:"+ent.number);
}else{
System.out.println("查無(wú)此人!");
}
break;
case?5:
for(Entity?entit:enlist){
System.out.println(entit.name+"的聯(lián)系方式為:"+entit.number);
}
break;
}
}while(input!=0);
System.out.println("謝謝使用!");
}
/*
?*?添加聯(lián)系人
?*/
public?boolean?add(Entity?e){
enlist.add(e);
return?true;
}
/*
?*?刪除聯(lián)系人
?*/
public?boolean?delete(int?num){
if(numenlist.size())
return?false;
else
enlist.remove(num);
return?true;
}
/*
?*?修改聯(lián)系人
?*/
public?void?update(int?num){
if(numenlist.size())
System.out.println("查無(wú)此人!");
else{
Entity?e?=?new?Entity();
e.id?=?num;
System.out.println("請(qǐng)輸入聯(lián)系人姓名:");
e.name?=?scanner.next();
System.out.println("請(qǐng)輸入聯(lián)系方式:");
e.number?=?scanner.nextInt();
enlist.set(num,?e);
}
}
/*
?*?查詢指定聯(lián)系人電話
?*/
public?Entity?select(String?name){
Entity?en?=?null;
for(Entity?e?:?enlist){
if(e.name.equals(name))
en?=?e;
}
return?en;
}
}
class?Entity{
public?int?id;//編號(hào)
public?String?name;//姓名
public?int?number;//聯(lián)系電話
}
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] = "";
}
}
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("請(qǐng)輸入第" + (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("請(qǐng)輸入第" + (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("電話號(hào)碼輸入有誤,請(qǐng)重新輸入!");
continue;
} System.out.println("請(qǐng)輸入第" + (i + 1) + "位朋友的郵箱:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setEmail(str); System.out.println("請(qǐng)輸入第" + (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());
}
}
}