BloggerAds

2011年7月27日 星期三

電話簿(PhoneData)

也是知識+看到的


原PO


http://tw.knowledge.yahoo.com/question/question?qid=1611072503941


請寫一個程式功能如下:


1.可以輸入人名、性別、電話,然後記錄起來


2.可以查詢,用人名查詢,然後顯示出來


3.可以顯示所有的人名、性別、電話的資料


4.可以依據電話號碼排序,可以由大到小、小到大


5.可以刪除一筆資料


 


我自己是很雞婆 欄位多了性別 查詢則是每一種都寫出來...


phoneData1  


------------------------------------------------------------------


phoneData2  


--------------------------------------------------------------------


phoneData3  


-----------------------------------------------------------------------


phoneData4  


開始程式碼囉 有些System.out.println 太長被截掉 自己依樣畫葫蘆應該也能推出來


我加了三筆資料去test 不想要的話自己移除



phonedata.add(new phoneData("jay","男",25,"0985526148"));
phonedata.add(new phoneData("Mary","女",18,"0921525948"));
phonedata.add(new phoneData("Bob","男",45,"0945896141"));


============================================phoneData


public class phoneData implements Comparable{
protected String Name;//名字
protected String Gender;//性別
protected int Age;//年紀
protected String Phone;//電話號碼
protected static int compare;//1的話由大到小排列,2的話由小到大排列,3依年紀
phoneData(){

}
phoneData(String name,String gender,int age,String phone){
Name=name;
Gender=gender;
Age=age;
Phone=phone;
}

@Override
public int compareTo(Object o) {
phoneData p=(phoneData) o;
if(compare==1){//由大到小
if(Integer.parseInt(this.Phone)>Integer.parseInt(p.Phone)){
return -1;
}else if(Integer.parseInt(this.Phone)<Integer.parseInt(p.Phone)){
return 1;
}else{
return 0;
}
}else if(compare==2){//由小到大
if(Integer.parseInt(this.Phone)>Integer.parseInt(p.Phone)){
return 1;
}else if(Integer.parseInt(this.Phone)<Integer.parseInt(p.Phone)){
return -1;
}else{
return 0;
}
}else if(compare==3){//依年紀小到大
if(this.Age>p.Age){
return 1;
}else if(this.Age<p.Age){
return -1;
}else{
return 0;
}
}
return 0;
}

}

 


============================================menu


Created with colorer-take5 library. Type 'java'

import java.util.Scanner;


public class menu extends menuImplement{

private static int chooseitem;
public static void main(String[] args) {
phonedata.add(new phoneData("jay","男",25,"0985526148"));
phonedata.add(new phoneData("Mary","女",18,"0921525948"));
phonedata.add(new phoneData("Bob","男",45,"0945896141"));
while(1<2){
System.out.println("=============");
System.out.println("1.顯示全部電話資料\n"+"2.加入電話資料\n"+

                                           "3.資料查詢\n"+"4.刪除一筆電話資料\n"+"5.離開");
System.out.println("=============");
Scanner scan =new Scanner(System.in);
chooseitem=scan.nextInt();
try{

switch(chooseitem){
case 1://顯示全部電話資料
int choose;
System.out.println("依照電話號碼\n(大到小請按1)\n(小到大請按2)\n(依照年齡請按3):");
try{
choose=scan.nextInt();
if(choose==1){
sortMax();
}else if(choose==2){
sortMin();
}else if(choose==3){
sortAge();
}else{
System.out.println("請輸入1 OR 2 OR 3");
}
}catch(Exception e){
System.out.println("輸入有錯誤:"+e);
}
break;
case 2://加入
addData();
break;
case 3://查詢
searchData();
break;
case 4://刪除資料
deleteData();
break;
case 5://離開
break;
default :
System.out.println("~~請輸入1到5~~");
break;
}

}catch(Exception e){
System.out.println("有無法辨識的錯誤");
System.out.println(e);
}
if(chooseitem==5){
break;
}
}//while

}

}

============================================menuImplement


Created with colorer-take5 library. Type 'java'

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class menuImplement extends phoneData {
protected static ArrayList<phoneData> phonedata=new ArrayList<phoneData>();
protected static void sort(){
for(int i=0;i<phonedata.size();i++){
System.out.println("姓名:"+phonedata.get(i).Name+" 性別:"+phonedata.get(i).Gender+
" 年齡:"+phonedata.get(i).Age+" 電話號碼:"+phonedata.get(i).Phone);
}

}
protected static void sortMax(){//顯示電話資料大到小
compare=1;
Collections.sort(phonedata);
sort();
}
protected static void sortMin(){//顯示電話資料小到大
compare=2;
Collections.sort(phonedata);
sort();
}
protected static void sortAge(){//依照年齡小到大顯示電話資料
compare=3;
Collections.sort(phonedata);
sort();
}

protected static void addData(){
Scanner scan =new Scanner(System.in);
String Name;//名字
int temp;//暫時存放性別
String Gender;//性別
int Age;//年紀
String Phone;//電話號碼
try{
System.out.println("請輸入姓名:");
Name=scan.next();
System.out.println("請輸入性別(男輸入1)(女輸入2)(人妖輸入其他):");
temp=scan.nextInt();
if(temp==1){
Gender="男";
}else if(temp==2){
Gender="女";
}else{
Gender="人妖";
}
System.out.println("請輸入年紀:");
Age=scan.nextInt();
System.out.println("請輸入電話號碼:");
Phone=scan.next();
phonedata.add(new phoneData(Name,Gender,Age,Phone));
System.out.println("寫入成功");
}catch(Exception e){
System.out.println("不明錯誤 請檢查輸入格式 錯誤資訊:"+e);
}

}

protected static void searchData(){
Scanner scan =new Scanner(System.in);
int search;
boolean find=false;//確定有沒有找到
String name="";
int gender;
int age=0;
String phone="0000-000000";
System.out.println("請輸入搜尋條件\n(1.姓名 2.性別 3.年紀 4.電話號碼):");
try{
search=scan.nextInt();
switch(search){
case 1://姓名
System.out.println("請輸入姓名:");
name=scan.next();
for(int i=0;i<phonedata.size();i++){
if(phonedata.get(i).Name.equals(name)){
System.out.println("姓名:"+phonedata.get(i).Name+" 性別:"+phonedata.get(i).Gender+
" 年齡:"+phonedata.get(i).Age+" 電話號碼:"+phonedata.get(i).Phone);
find=true;
}
}
if(find==false){
System.out.println("未找到 "+name);
}
break;
case 2://性別
System.out.println("請輸入性別(男輸入1)(女輸入2)(人妖輸入其他):");
gender=scan.nextInt();
if(gender==1){
for(int i=0;i<phonedata.size();i++){
if(phonedata.get(i).Gender.equals("男")){
System.out.println("姓名:"+phonedata.get(i).Name+" 性別:"+phonedata.get(i).Gender+
" 年齡:"+phonedata.get(i).Age+" 電話號碼:"+phonedata.get(i).Phone);
find=true;
}
}
}else if(gender==2){
for(int i=0;i<phonedata.size();i++){
if(phonedata.get(i).Gender.equals("女")){
System.out.println("姓名:"+phonedata.get(i).Name+" 性別:"+phonedata.get(i).Gender+
" 年齡:"+phonedata.get(i).Age+" 電話號碼:"+phonedata.get(i).Phone);
find=true;
}
}
}else{
for(int i=0;i<phonedata.size();i++){
if(phonedata.get(i).Gender.equals("人妖")){
System.out.println("姓名:"+phonedata.get(i).Name+" 性別:"+phonedata.get(i).Gender+
" 年齡:"+phonedata.get(i).Age+" 電話號碼:"+phonedata.get(i).Phone);
find=true;
}
}
}
break;
case 3://依照年紀
System.out.println("請輸入年齡:");
age=scan.nextInt();
for(int i=0;i<phonedata.size();i++){
if(phonedata.get(i).Age==age){
System.out.println("姓名:"+phonedata.get(i).Name+" 性別:"+phonedata.get(i).Gender+
" 年齡:"+phonedata.get(i).Age+" 電話號碼:"+phonedata.get(i).Phone);
find=true;
}
}
if(find==false){
System.out.println("未找到 "+age+"歲的人");
}
break;
case 4://依照電話號碼
System.out.println("請輸入電話號碼:");
phone=scan.next();
for(int i=0;i<phonedata.size();i++){
if(phonedata.get(i).Phone.equals(phone)){
System.out.println("姓名:"+phonedata.get(i).Name+" 性別:"+phonedata.get(i).Gender+
" 年齡:"+phonedata.get(i).Age+" 電話號碼:"+phonedata.get(i).Phone);
find=true;
}
}
if(find==false){
System.out.println("未找到電話號碼為 "+phone+" 的人");
}
break;
}

}catch(Exception e){
System.out.println("不明錯誤 請檢查輸入格式 錯誤資訊:"+e);
}
find=false;//reset
}

protected static void deleteData(){//刪除資料
Scanner scan =new Scanner(System.in);
String name;
boolean find=false;
System.out.println("請輸入要刪除的資料姓名(如果不清楚可先用查詢功能查詢):");
name=scan.next();
for(int i=0;i<phonedata.size();i++){
if(phonedata.get(i).Name.equals(name)){
phonedata.remove(i);
find=true;
System.out.println("刪除成功!");
}
}
if(find==false){
System.out.println("找不到此名字,請檢查有無輸入錯誤或新增資料");
}
find=false;//reset
}


}