package in;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Demo extends JFrame {
/*
* 界面设计
* */
public Demo(){
Container c = getContentPane(); //定义一个顶级容器c
setAlwaysOnTop(true); //窗体置顶
JPanel jp = new JPanel(); //新建JPanel面板--jp
JButton button1 = new JButton("新建联系人");
JButton button2 = new JButton("删除联系人");
JButton button3 = new JButton("编辑联系人");
JButton button4 = new JButton("查找联系人");
JButton button5 = new JButton("显示所有联系人");
JButton button6 = new JButton("保存联系人到本地");
JButton button7 = new JButton("读取本地联系人");
jp.setLayout(new GridLayout(3,4,5,5));//新建网格布局管理器(行数,列数,组件间的水平垂直间距)
jp.add(button1);
jp.add(button2);
jp.add(button3);
jp.add(button4);
jp.add(button5);
jp.add(button6);
jp.add(button7);
c.add(jp);//将JPanel面板jp添加到顶级容器c中
setSize(600,150);
setTitle("*通 讯 录 管 理 系 统*");
setVisible(true);
setResizable(false);//窗体大小由程序员决定,用户不能自由改变大小
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
/*
*按键响应
*
* */
button1.addActionListener(new ActionListener(){//添加功能实现
public void actionPerformed(ActionEvent arg0){
Infro.addFunction();
}
});
button2.addActionListener(new ActionListener(){//删除功能实现
public void actionPerformed(ActionEvent arg0){
Infro.deleteFunction();
}
});
button3.addActionListener(new ActionListener(){//修改功能实现
public void actionPerformed(ActionEvent arg0){
Infro.reditFunction();
}
});
button4.addActionListener(new ActionListener(){//查询功能实现
public void actionPerformed(ActionEvent arg0){
try {
Infro.searchFunction();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button5.addActionListener(new ActionListener(){//显示功能实现
public void actionPerformed(ActionEvent arg0){
Infro.showFunction();
}
});
button6.addActionListener(new ActionListener(){//保存功能实现
public void actionPerformed(ActionEvent arg0){
try {
Infro.writeFunction();
} catch (IOException e) {
e.printStackTrace();
}
}
});
button7.addActionListener(new ActionListener(){//读取功能实现
public void actionPerformed(ActionEvent arg0){
try {
Infro.readFunction();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Demo();
Infro a = new Infro("", "", "", "", "", "");
}
}
package in;
import java.applet.Applet;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JPanel;
class Infro {
public String Name;
public String Sex;
public String Unit;
public String Homephone;
public String Telephone;
public String E_mail;
static int index = 0;
static ArrayList<Infro> list = new ArrayList();
static int len = list.size();
//构造函数
public Infro(String Name,String Sex,String Unit,String Homephone,String Telephone,String E_mail){
this.Name = Name;
this.Sex = Sex;
this.Unit = Unit;
this.Homephone = Homephone;
this.Telephone = Telephone;
this.E_mail = E_mail;
}
public String toString(){
return "姓名:"+Name+"\t性别:"+Sex+"\t单位:"+Unit+"\t住宅电话:"+Homephone
+"\t移动电话:"+Telephone+"\tE_mail:"+E_mail;
}
/*
* 添加功能
* */
public static void addFunction(){//添加功能
Infro infro = new Infro("","","","","","");
System.out.println("请输入添加的数据:");
Scanner in = new Scanner(System.in);
System.out.println("输入姓名:");
infro.Name = in.next();
System.out.println("输入性别:");
infro.Sex = in.next();
System.out.println("输入单位:");
infro.Unit = in.next();
System.out.println("输入住宅电话:");
infro.Homephone = in.next();
System.
out.println("输入移动电话:");
infro.Telephone = in.next();
System.out.println("输入E_mail:");
infro.E_mail = in.next();
list.add(index,infro);
index++;
if(list.isEmpty()){
System.out.println("数据添加失败啦");
}else{
System.out.println("数据添加成功啦");
len++;//list集合长度加一
// System.out.println(list.get(0).toString());
}
System.out.println("是否继续添加?1.是 2.否");
Scanner e=new Scanner(System.in);
int e1=e.nextInt();
if(e1==1){
Infro.addFunction();
}else{
return;
}
}
/*
* 删除功能
* */
public static void deleteFunction(){
System.out.println("输入要删除的联系人的姓名");
Scanner in_2 = new Scanner(System.in);
String d1 = in_2.nextLine();
java.util.Iterator<Infro> it = list.iterator();
while (it.hasNext()){
Infro infro = it.next();
if (infro.Name.equals(d1)){
it.remove();
--index;//一定要加这个,否则当做了删除操作再做添加操作的时候会出现异常(类似于指针,栈)
System.out.println("删除完毕"+"此时通讯录记录条数为:" + --len);
}
}
System.out.println("是否继续删除?1.是 2.否");
Scanner e=new Scanner(System.in);
int e1=e.nextInt();
if(e1==1){
Infro.deleteFunction();
}else{
return;
}
}
/*
* 修改功能
* */
public static void reditFunction(){
System.out.println("输入要修改的通讯录的姓名");
Scanner in_r = new Scanner(System.in);
String r1 = in_r.nextLine();
for(int a = 0; a < len;a++){
if(r1.equals(list.get(a).Name)){
System.out.println("输入修改后的性别:");
String Sex_1 = in_r.next();
list.get(a).Sex = Sex_1;
System.out.println("输入修改后的单位:");
String Unit_1 = in_r.next();
list.get(a).Unit = Unit_1;
System.out.println("输入修改后的住宅电话:");
String Homephone_1 = in_r.next();
list.get(a).Homephone = Homephone_1;
System.out.println("输入修改后的移动电话:");
String Telephone_1 = in_r.next();
list.get(a).Telephone = Telephone_1;
System.out.println("输入修改后的E_mail:");
String E_mail_1 = in_r.next();
list.get(a).E_mail = E_mail_1;
System.out.println("数据修改完毕");
}
}
System.out.println("是否继续修改?1.是 2.否");
Scanner e=new Scanner(System.in);
int e1=e.nextInt();
if(e1==1){
Infro.reditFunction();
}else{
return;
}
}
/*
* 查询功能
* */
public static void searchFunction() throws Exception{//查询功能
System.out.println("请选择查询方式: ");
System.out.println("1.姓名 2.性别 3.单位 4.住宅电话 5.移动电话 6.E_mail ");
Scanner in_0=new Scanner(System.in);
int s0=in_0.nextInt();
if(s0==1){
System.out.println("请输入要查询的姓名:");
Scanner in_1 = new Scanner(System.in);
String s1=in_1.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s1.equals(list.get(a).Name)){
System.out.println(list.get(a).toString());
}
}
}
else if(s0==2){
System.out.println("请输入要查询的性别:");
Scanner in_2 = new Scanner(System.in);
String s2=in_2.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s2.equals(list.get(a).Sex)){
System.out.println(list.get(a).toString());
}
}
}
else if(s0==3){
System.out.println("请输入要查询的单位:");
Scanner in_3 = new Scanner(System.in);
String s3=in_3.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s3.equals(list.get(a).Unit)){
System.out.println(list.get(a).toString());
}
}
}
if(s0==4){
System.out.println("请输入要查询的住宅电话:");
Scanner in_4 = new Scanner(System.in);
String s4=in_4.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s4.equals(list.get(a).Homephone)){
System.out.println(list.get(a).toString());
}
}
}
else if(s0==5){
System.out.println("请输入要查询的移动电话:");
Scanner in_5 = new Scanner(System.in);
String s5=in_5.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s5.equals(list.get(a).Telephone)){
System.out.println(list.get(a).toString());
}
}
}
else if(s0==6){
System.out.println("请输入要查询的E_mail:");
Scanner in_6 = new Scanner(System.in);
String s6=in_6.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s6.equals(list.get(a).E_mail)){
System.out.println(list.get(a).toString());
}
}
}
}
/*
* 显示功能
* */
public static void showFunction(){
for(int i = 0 ;i<len;i++){
System.out.println(list.get(i).toString());
}
}
/*
* 保存功能
* */
public static void writeFunction() throws IOException{
FileWriter writer = new FileWriter("通讯录管理.txt");
for(int i = 0 ;i<len;i++){
String []strwriter = new String[len];
strwriter[i]=list.get(i).toString();
writer.write(strwriter[i]);
writer.write("\r\n");
System.out.println("成功写入一行数据到 通讯录管理.txt 中");
}
writer.close();//关闭写入流,释放资源
}
/*
* 读取功能
* */
public static void readFunction() throws IOException{
FileReader reader = new FileReader("通讯录管理.txt");
BufferedReader br = new BufferedReader(reader);
String str;
while((str = br.readLine()) != null){//每次读取一行文本,判断是否到达文件尾
System.out.println(str);
}
br.close();
}
}
课程设计中遇到了问题… 在运行时输入,可以查询,编辑等操作。可是保存到本地后退出运行,第二次再运行时读取本地联系人,就没办法执行查询操作了。我改了好久还是不行,请各位哥哥姐姐帮忙看一下,拜托~因为还没有学数据库,只能用文件了。
超过20个分屏显示就不要了,因为貌似java没有分屏显示……其他的功能,按姓名排序也希望你们能给些思路,谢谢谢谢
这是题目:
3. 以本班同学的具体数据为背景,设计一个本班同学通讯录(3人)
通讯录要求存储姓名、性别、单位、住宅电话、移动电话、E-mail地址等内容。系统功能要求如下:
(1)通讯录记录按姓名排序存放,显示时每屏不超过20个记录,超过时分屏显示。
(2)增加某人的通讯录。
(3)修改某人的通讯录。
(4)删除某人的通讯录。
(5)按多种方式查询符合条件的信息。
你重新打开新的程序 必须先执行加载功能 把文件里的数据加载到list中
因为你的查询代码遍历的是list 如果你不执行加载 遍历一个空的list当然就没有数据了。
并且 我发现 你再次打开一个 重新新增会把原来的文件覆盖掉。所以我觉得必须写一个static静态块,加载文件中内容对list进行初始化
//Infro
package in;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Infro {
public String Name;
public String Sex;
public String Unit;
public String Homephone;
public String Telephone;
public String E_mail;
static int index = 0;
static ArrayList<Infro> list = new ArrayList<>();
static int len;
//TODO 分页大小
private static final int limit = 5;
//TODO
static {
try {
initList();
} catch (IOException e) {
System.out.println("读取文件失败!");
e.printStackTrace();
}
}
//TODO 初始化list
private static void initList() throws IOException {
FileReader reader = new FileReader("通讯录管理.txt");
BufferedReader br = new BufferedReader(reader);
String str;
while((str = br.readLine()) != null){//每次读取一行文本,判断是否到达文件尾
String[] splitStr = str.split(" ");
Infro info = new Infro(splitStr[0],
splitStr[1], splitStr[2], splitStr[3],
splitStr[4], splitStr[5]);
list.add(info);
}
len = list.size();
br.close();
}
//构造函数
public Infro(String Name,String Sex,String Unit,String Homephone,String Telephone,String E_mail){
this.Name = Name;
this.Sex = Sex;
this.Unit = Unit;
this.Homephone = Homephone;
this.Telephone = Telephone;
this.E_mail = E_mail;
}
public String toString(){
return "姓名:"+Name+"\t性别:"+Sex+"\t单位:"+Unit+"\t住宅电话:"+Homephone
+"\t移动电话:"+Telephone+"\tE_mail:"+E_mail;
}
//TODO 保存到本地格式 方便读取 split
public String saveFormat(){
return Name+" "+Sex+" "+Unit+" "+Homephone
+" "+Telephone+" "+E_mail;
}
/*
* 添加功能
* */
public static void addFunction(){//添加功能
Infro infro = new Infro("","","","","","");
System.out.println("请输入添加的数据:");
Scanner in = new Scanner(System.in);
System.out.println("输入姓名:");
infro.Name = in.next();
System.out.println("输入性别:");
infro.Sex = in.next();
System.out.println("输入单位:");
infro.Unit = in.next();
System.out.println("输入住宅电话:");
infro.Homephone = in.next();
System.
out.println("输入移动电话:");
infro.Telephone = in.next();
System.out.println("输入E_mail:");
infro.E_mail = in.next();
list.add(index,infro);
index++;
if(list.isEmpty()){
System.out.println("数据添加失败啦");
}else{
System.out.println("数据添加成功啦");
len++;//list集合长度加一
// System.out.println(list.get(0).toString());
}
System.out.println("是否继续添加?1.是 2.否");
Scanner e=new Scanner(System.in);
int e1=e.nextInt();
if(e1==1){
Infro.addFunction();
}else{
return;
}
}
/*
* 删除功能
* */
public static void deleteFunction(){
System.out.println("输入要删除的联系人的姓名");
Scanner in_2 = new Scanner(System.in);
String d1 = in_2.nextLine();
java.util.Iterator<Infro> it = list.iterator();
while (it.hasNext()){
Infro infro = it.next();
if (infro.Name.equals(d1)){
it.remove();
--index;//一定要加这个,否则当做了删除操作再做添加操作的时候会出现异常(类似于指针,栈)
System.out.println("删除完毕"+"此时通讯录记录条数为:" + --len);
}
}
System.out.println("是否继续删除?1.是 2.否");
Scanner e=new Scanner(System.in);
int e1=e.nextInt();
if(e1==1){
Infro.deleteFunction();
}else{
return;
}
}
/*
* 修改功能
* */
public static void reditFunction(){
System.out.println("输入要修改的通讯录的姓名");
Scanner in_r = new Scanner(System.in);
String r1 = in_r.nextLine();
for(int a = 0; a < len;a++){
if(r1.equals(list.get(a).Name)){
System.out.println("输入修改后的性别:");
String Sex_1 = in_r.next();
list.get(a).Sex = Sex_1;
System.out.println("输入修改后的单位:");
String Unit_1 = in_r.next();
list.get(a).Unit = Unit_1;
System.out.println("输入修改后的住宅电话:");
String Homephone_1 = in_r.next();
list.get(a).Homephone = Homephone_1;
System.out.println("输入修改后的移动电话:");
String Telephone_1 = in_r.next();
list.get(a).Telephone = Telephone_1;
System.out.println("输入修改后的E_mail:");
String E_mail_1 = in_r.next();
list.get(a).E_mail = E_mail_1;
System.out.println("数据修改完毕");
}
}
System.out.println("是否继续修改?1.是 2.否");
Scanner e=new Scanner(System.in);
int e1=e.nextInt();
if(e1==1){
Infro.reditFunction();
}else{
return;
}
}
/*
* 查询功能
* */
public static void searchFunction() throws Exception{//查询功能
System.out.println("请选择查询方式: ");
System.out.println("1.姓名 2.性别 3.单位 4.住宅电话 5.移动电话 6.E_mail ");
Scanner in_0=new Scanner(System.in);
int s0=in_0.nextInt();
if(s0==1){
System.out.println("请输入要查询的姓名:");
Scanner in_1 = new Scanner(System.in);
String s1=in_1.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s1.equals(list.get(a).Name)){
System.out.println(list.get(a).toString());
}
}
}
else if(s0==2){
System.out.println("请输入要查询的性别:");
Scanner in_2 = new Scanner(System.in);
String s2=in_2.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s2.equals(list.get(a).Sex)){
System.out.println(list.get(a).toString());
}
}
}
else if(s0==3){
System.out.println("请输入要查询的单位:");
Scanner in_3 = new Scanner(System.in);
String s3=in_3.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s3.equals(list.get(a).Unit)){
System.out.println(list.get(a).toString());
}
}
}
if(s0==4){
System.out.println("请输入要查询的住宅电话:");
Scanner in_4 = new Scanner(System.in);
String s4=in_4.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s4.equals(list.get(a).Homephone)){
System.out.println(list.get(a).toString());
}
}
}
else if(s0==5){
System.out.println("请输入要查询的移动电话:");
Scanner in_5 = new Scanner(System.in);
String s5=in_5.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s5.equals(list.get(a).Telephone)){
System.out.println(list.get(a).toString());
}
}
}
else if(s0==6){
System.out.println("请输入要查询的E_mail:");
Scanner in_6 = new Scanner(System.in);
String s6=in_6.nextLine();
for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
if(s6.equals(list.get(a).E_mail)){
System.out.println(list.get(a).toString());
}
}
}
}
/*
TODO * 显示功能
* */
public static void showFunction(int page) {
if (page <=0 || page > len/limit + 1) {
System.out.println("页数越界啦");
return;
}
for (int i = (page-1) * limit; i<len && i < page * limit; i++) {
System.out.println(list.get(i).toString());
}
System.out.println("第"+page+"/"+(len/limit + 1)+"页");
System.out.println("1:下一页 2:上一页 0:退出");
int e1 = new Scanner(System.in).nextInt();
if (e1==1) {
Infro.showFunction(page + 1);
} else if (e1 == 2){
Infro.showFunction(page - 1);
}
}
/*
* TODO 保存功能(保存不用toString)
* */
public static void writeFunction() throws IOException{
FileWriter writer = new FileWriter("通讯录管理.txt");
for(int i = 0 ;i<len;i++){
String []strwriter = new String[len];
strwriter[i] = list.get(i).saveFormat();
writer.write(strwriter[i]);
writer.write("\n");
}
System.out.println("成功写入" + len + "行数据到 通讯录管理.txt 中");
writer.close();//关闭写入流,释放资源
}
/*
* TODO 读取功能 不再读取文件 而是遍历list
* */
public static void readFunction() throws IOException{
for (int i=0; i<list.size(); i++) {
Infro info = list.get(i);
System.out.println(info.toString());
}
}
}
另外很想吐槽你这样 窗体做一半 控制台做一半的行为。还不如全做成窗体来得好。
分屏显示 不就是传入 页数 和 页行数, 比如传入 1 20 就是查询 前 20条记录。你再弄一个 下一页上一页的选项,再传入 2 20 3 20 就可以达到这样的效果了。按姓名排序也简单的,你把所有的数据保存到list中后,用Collections.sort(List list,Comparator c) 写匿名接口,重写compare方法就可以排序了
//Demo
package in;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Demo extends JFrame {
/*
* 界面设计
* */
public Demo(){
Container c = getContentPane(); //定义一个顶级容器c
setAlwaysOnTop(true); //窗体置顶
JPanel jp = new JPanel(); //新建JPanel面板--jp
JButton button1 = new JButton("新建联系人");
JButton button2 = new JButton("删除联系人");
JButton button3 = new JButton("编辑联系人");
JButton button4 = new JButton("查找联系人");
JButton button5 = new JButton("显示所有联系人");
JButton button6 = new JButton("保存联系人到本地");
JButton button7 = new JButton("读取本地联系人");
jp.setLayout(new GridLayout(3,4,5,5));//新建网格布局管理器(行数,列数,组件间的水平垂直间距)
jp.add(button1);
jp.add(button2);
jp.add(button3);
jp.add(button4);
jp.add(button5);
jp.add(button6);
jp.add(button7);
c.add(jp);//将JPanel面板jp添加到顶级容器c中
setSize(600,150);
setTitle("*通 讯 录 管 理 系 统*");
setVisible(true);
setResizable(false);//窗体大小由程序员决定,用户不能自由改变大小
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
/*
*按键响应
*
* */
button1.addActionListener(new ActionListener(){//添加功能实现
public void actionPerformed(ActionEvent arg0){
Infro.addFunction();
}
});
button2.addActionListener(new ActionListener(){//删除功能实现
public void actionPerformed(ActionEvent arg0){
Infro.deleteFunction();
}
});
button3.addActionListener(new ActionListener(){//修改功能实现
public void actionPerformed(ActionEvent arg0){
Infro.reditFunction();
}
});
button4.addActionListener(new ActionListener(){//查询功能实现
public void actionPerformed(ActionEvent arg0){
try {
Infro.searchFunction();
} catch (Exception e) {
e.printStackTrace();
}
}
});
button5.addActionListener(new ActionListener(){//显示功能实现
public void actionPerformed(ActionEvent arg0){
Infro.showFunction(1);//TODO 默认显示第一页
}
});
button6.addActionListener(new ActionListener(){//保存功能实现
public void actionPerformed(ActionEvent arg0){
try {
Infro.writeFunction();
} catch (IOException e) {
e.printStackTrace();
}
}
});
button7.addActionListener(new ActionListener(){//读取功能实现
public void actionPerformed(ActionEvent arg0){
try {
Infro.readFunction();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public static void main(String[] args) {
new Demo();
Infro a = new Infro("", "", "", "", "", "");
}
}