包
包就像文件夹,将java类区分管理
在不同的包中可以出现相同的类名
命名规则:
通常以域名反写为前缀
如:com.google.common
包名全部小写,不能以点开头
避免使用重复的包名
如:把自己项目报名写成com.google
导入包
如何使用另一个包中的类
使用完全限定名 如:
java.util.Scanner
在文件中导入
import java.util.Scanner;
再使用时就不需要加java.util.了;
import语句位于package语句下方
方案一:
功能:1:新增 2:删除 3:快速查找 4:修改 5:退出
运行时直接显示练习人,一目了然.代码中把不同的功能封装成各种方法,但在写代码是发现有很多代码都是重复的.总体来看,感觉代码不够精简,此方法,没有用到类_数组保存用户数据,而是把文件中数据通过中间变量进行修改或增减.
package com.phonebook.begin;
import java.io.IOException;
import com.phonebook.app.App;
public class Begin {
public static void main(String[] args) throws IOException {
App app = new App();
while(true){
int a = app.ui();
if (a == 1){
break;
}
}
}
}
package com.phonebook.app;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class App {
final String Path = "h:/lianxiren.txt";
final String Mark = "\t";
public int ui() throws IOException {
System.out.println(" 电话本系统1.0");
System.out.println(" 姓名 \t 电话 \t 邮箱");
System.out.println("==========================================");
try {
read();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("==========================================");
System.out.println("1:新增 2:删除 3:快速查找 4:修改 5:退出");
Scanner input = new Scanner(System.in);
String choose = input.next();
if (choose.matches("[a-zA-Z]")) {
System.out.println("输入错误!");
} else {
switch (choose) {
case "1":
add();
break;
case "2":
del();
break;
case "3":
find();
break;
case "4":
xiuGai();
break;
case "5":
System.out.println("再见!");
return 1;
}
}
return 0;
}
// 读取联系人
public void read() throws Exception {
File f = new File(Path);
if (!f.isFile()) {
f.createNewFile();
}
BufferedReader br = new BufferedReader(new FileReader(f));
int i = 1;
while (true) {
String str = br.readLine();
if (str != null && str.length() > 0) {
String[] strs = str.split(Mark);
System.out.println(i + ":" + strs[0] + Mark + strs[1] + Mark
+ strs[2]);
}
if (str == null) {
br.close();
return;
}
i++;
}
}
// 新增联系人
public void add() throws IOException {
File f = new File(Path);
FileWriter fw = new FileWriter(f, true);
BufferedWriter bw = new BufferedWriter(fw);
Scanner input = new Scanner(System.in);
System.out.println("请输入姓名: ");
String name = input.next();
System.out.println("请输入电话号码: ");
String num = input.next();
System.out.println("请输入邮箱: ");
String email = input.next();
String str = name + Mark + num + Mark + email;
bw.write("\n" + str);
bw.flush();
bw.close();
fw.close();
System.out.println("新增联系人完成!");
}
// 删除联系人
public void del() throws IOException {
Scanner input = new Scanner(System.in);
File f = new File(Path);
BufferedReader br = new BufferedReader(new FileReader(f));
String[] user = new String[200];
for (int i = 0; i < user.length; i++) {
String str = br.readLine();
if (str != null) {
user[i] = str;
}
}
System.out.print("请输入要删除的姓名: ");
String delName = input.next();
for (int i = 0; i < user.length; i++) {
if (user[i].contains(delName)) {
System.out.println(user[i]);
System.out.println("你确定要删除码?");
if (input.next().equals("y")) {
user[i] = null;
System.out.println("删除成功!");
i = user.length;
}
}
}
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
for (String s : user) {
if (s != null) {
bw.write(s + "\r\n");
}
}
bw.flush();
bw.close();
fw.close();
br.close();
}
// 快速查找联系人
public void find() throws IOException {
File f = new File(Path);
BufferedReader br = new BufferedReader(new FileReader(f));
Scanner input = new Scanner(System.in);
System.out.println("请输入要查找的关键字: ");
String str = input.next();
for (int i = 0; i < 500; i++) {
String s = br.readLine();
if (s != null && s.contains(str)) {
System.out.println(s);
}
}
br.close();
System.out.println("按y键继续..");
String wait = input.next();
}
// 修改联系人
public void xiuGai() throws IOException {
Scanner input = new Scanner(System.in);
File f = new File(Path);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String[] user = new String[500];
for (int i = 0; i < user.length; i++) {
String s = br.readLine();
if (s != null) {
user[i] = s;
}
}
System.out.print("请输入要修改的联系人: ");
String re = input.next();
boolean bool= true;
for (int i = 0; i < user.length; i++) {
if ( user[i] != null && user[i].contains(re)) {
System.out.print("请输入姓名: ");
String name = input.next();
System.out.print("请输入电话号码: ");
String num = input.next();
System.out.print("请输入邮箱: ");
String email = input.next();
user[i] = name + Mark + num + Mark + email;
bool = false;
break;
}
}
if (!bool){
System.out.println("修改完成!");
}else{
System.out.println("找不到该联系人!");
}
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < user.length; i++) {
if (user[i] != null) {
bw.write(user[i] + "\r\n");
}
}
bw.flush();
bw.close();
fw.close();
br.close();
fr.close();
}
}
-------
方案二:
功能: 1 新增 2:修改 3:查找 4:删除 5:显示全部 6:退出
代码: 代码复用性比方案一高.写代码时思路比较清晰,也比较简单.用到了自定义类_数组.为了防止提示空指针异常,中间有很多小细节要注意.数据的读取与保存也有很多地方要注意,非常容易出错
package com.phonebook.update;
import java.io.IOException;
public class KaiShi {
public static void main(String[] args) throws IOException {
App app = new App();
while (true) {
app.begin();
}
}
}
--
package com.phonebook.update;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import com.phonebook.user.User;
public class App {
final String Path = "h:/mingdan.txt";
final String Mark = "\t";
User[] user = new User[500];
public void begin() throws IOException {
read();
Scanner input = new Scanner(System.in);
while (true) {
System.out.println(" 电话簿1.2");
System.out.println("===================");
System.out.println(" 1:新增");
System.out.println(" 2:修改");
System.out.println(" 3:查找");
System.out.println(" 4:删除");
System.out.println(" 5:显示全部");
System.out.println(" 6:退出");
System.out.println("===================");
switch (input.next()) {
case "1":
boolean bool = false;
for (int i = 0; i < user.length; i++) {
if (user[i] == null) {
User us = new User();
System.out.print("请输入姓名: ");
us.setName(input.next());
System.out.print("请输入手机号: ");
us.setNum(input.next());
System.out.print("请输入邮箱: ");
us.seteMail(input.next());
user[i] = us;
bool = true;
break;
}
}
if (!bool) {
System.out.println("内存已满");
} else {
System.out.println("新增成功!");
}
break;
case "2":
System.out.println("请输入要修改的联系人的姓名: ");
String reName = input.next();
boolean bool1 = false;
for (int i = 0; i < user.length; i++) {
if (user[i] != null && user[i].getName().equals(reName)) {
System.out.print("请输入姓名: ");
user[i].setName(input.next());
System.out.print("请输入手机号: ");
user[i].setNum(input.next());
System.out.print("请输入邮箱: ");
user[i].seteMail(input.next());
bool1 = true;
break;
}
}
if (bool1) {
System.out.println("修改联系人完成!");
} else {
System.out.println("为找到该联系人!");
}
break;
case "3":
System.out.println("请输入要查找的关键字: ");
String find = input.next();
System.out.println("--------------------------------------");
for (int i = 0; i < user.length; i++) {
if (user[i] != null) {
String ff = user[i].getName() + user[i].getNum()
+ user[i].geteMail();
if (ff.contains(find)) {
System.out.println(user[i].getName() + Mark
+ user[i].getNum() + Mark
+ user[i].geteMail());
}
}
}
System.out.println("--------------------------------------");
System.out.println("按任意键继续...");
input.next();
break;
case "4":
System.out.println("请输入要删除的联系人的姓名: ");
String delName = input.next();
boolean bool2 = false;
for (int i = 0; i < user.length; i++) {
if (user[i] != null && user[i].getName().equals(delName)) {
user[i] = null;
bool2 = true;
break;
}
}
if (bool2) {
System.out.println("删除联系人成功!");
} else {
System.out.println("未找到该联系人!");
}
break;
case "5":
System.out.println("姓名 \t 手机号\t 邮箱");
System.out
.println("--------------------------------------------");
for (int i = 0; i < user.length; i++) {
if (user[i] != null) {
System.out.println(user[i].getName() + Mark
+ user[i].getNum() + Mark + user[i].geteMail());
}
}
System.out
.println("--------------------------------------------");
System.out.println("按任意键继续...");
input.next();
break;
case "6":
save();
System.out.println("再见!");
return;
}
}
}
// 读取本地数据
public void read() throws IOException {
File f = new File(Path);
if (!f.isFile()) {
f.createNewFile();
}
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
for (int i = 0; i < 500; i++) {
String str = br.readLine();
if (str != null && str.length() > 0) {
String[] strs = str.split(Mark);
User us = new User();
us.setName(strs[0]);
us.setNum(strs[1]);
us.seteMail(strs[2]);
user[i] = us;
}
}
}
// 把数据保存到本地
public void save() throws IOException {
File f = new File(Path);
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < user.length; i++) {
if (user[i] != null) {
String str = user[i].getName() + Mark + user[i].getNum() + Mark
+ user[i].geteMail();
bw.write(str + "\r\n");
}
}
bw.flush();
bw.close();
fw.close();
}
}
---
package com.phonebook.user;
public class User {
private String name;
private String num;
private String eMail;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String geteMail() {
return eMail;
}
public void seteMail(String eMail) {
this.eMail = eMail;
}
}