建一個電話薄類
創(chuàng)新互聯(lián)是一家專業(yè)提供蘿北企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、做網(wǎng)站、H5建站、小程序制作等業(yè)務(wù)。10年已為蘿北眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
public class PhoneBook{
String name;
String phoneno;
String email;
//再把這些設(shè)置上get set方法
}
存的話,把他們add進(jìn)一個List里
取的話,iterator遍歷List,一個一個查名字
==================================
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();// 添加一個就記錄一次文件
} 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;
}
}
具體方法如下:
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);
}
}