import java.util.Arrays;
成都創(chuàng)新互聯(lián)專(zhuān)注于企業(yè)成都營(yíng)銷(xiāo)網(wǎng)站建設(shè)、網(wǎng)站重做改版、潮安網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、商城開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為潮安等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
public class AddressBook
{
private int totalContacts; // 現(xiàn)在通訊錄中的總數(shù)
private int maxContacts; // 通訊錄的最大值
private Contact[] contacts; // 通訊錄的array
/**
* Constructor. 設(shè)定這個(gè)通訊錄最初的大小為10人上限.
*/
public AddressBook()
{
totalContacts = 0;
maxContacts = 10;
contacts = new Contact[totalContacts];
}
/**
* 得到一旦笑個(gè)聯(lián)系通過(guò)通訊錄中的名字
*
* @param name
* 要去得到的聯(lián)系人名字
* @return 這個(gè)聯(lián)系人, 沒(méi)有的話(huà)為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;
}
/**
* 在這答森個(gè)contacts array的模舉含最后給這個(gè)通訊錄添加聯(lián)系人.
* 如果聯(lián)系人已經(jīng)存在且號(hào)碼參數(shù)不為0的話(huà),聯(lián)系人的號(hào)碼會(huì)更新或者地址參數(shù)不是null的話(huà),地址會(huì)更新。.
* 如果totalContacts達(dá)到maxContacts的值的話(huà),在新的聯(lián)系人被添加前,這個(gè)contacts
* array的size會(huì)乘以雙倍并且maxContacts也乘以雙倍。
*
* @param contact
* 要加到這個(gè)通訊錄的聯(lián)系人
* @return true 如果這個(gè)聯(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 如果這個(gè)聯(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中還有一個(gè)方法!
* 從通訊錄中移除一個(gè)指定的聯(lián)系人,之后把其他的入口左移,這樣在聯(lián)系人間就沒(méi)有空隙了,
* 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 如果這個(gè)聯(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);
}
}
具體方法如下:
1、定義封裝一條記錄的實(shí)體類(lèi)
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)前無(wú)記錄!");
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);
}
}
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("文件存悉謹(jǐn)儲(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("電話(huà):");
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] = "";
}
}