IOperatorMobile.java
package com.geminno.mobile;
public interface IOperatorMobile {
public void addMobile();
public Mobile[] showMobile();
public Mobile[] showMobileByName(); 1.想問下這個是啥意思?
public void deleteByName();
public void updateByName();
}
Mobile.java
package com.geminno.mobile;
public class Mobile {
private int id;
private String code;
private String name;
private String tel;
private String address;
public Mobile(String code, String name, String tel, String address) {
super();
this.code = code;
this.name = name;
this.tel = tel;
this.address = address;
}
public Mobile(int id, String code, String name, String tel, String address) {
super();
this.id = id;
this.code = code;
this.name = name;
this.tel = tel;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Mobile() {
super();
// TODO Auto-generated constructor stub
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode()); 2.這個三元運算子又是啥意思,prime為啥初始值賦31
result = prime * result + ((code == null) ? 0 : code.hashCode()); result為啥賦值1?
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((tel == null) ? 0 : tel.hashCode());
return result;
}
@Override
public boolean equals(Object obj) { 3.這里這個Object obj啥意思 我覺得這應該是一個類把。。。
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass()) 4.方法還能不等于方法?
return false;
Mobile other = (Mobile) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (code == null) {
if (other.code != null)
return false;
} else if (!code.equals(other.code))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (tel == null) {
if (other.tel != null)
return false;
} else if (!tel.equals(other.tel))
return false;
return true;
}
}
MobileMain.java
package com.geminno.mobile;
public class MobileMain {
public MobileMain() {
System.out.println(
"-------------------------------------------------------------歡迎使用手機通訊錄--------------------------------------------------------");
System.out.println("\t1 查看全部");
System.out.println("\t2 新增人員");
System.out.println("\t3 修改資訊");
System.out.println("\t4 洗掉人員");
System.out.println("\t5 查找人員");
System.out.println("\t6 退出");
System.out.print("請選擇[1-6]:");
}
}
OperatorMobileImpl.java
package com.geminno.mobile;
import java.util.Scanner;
public class OperatorMobileImpl implements IOperatorMobile {
Mobile[] mobiles;
int index; 5.這個index沒有賦值吧。。。也沒加static為啥不報錯呢?初始值?
int id=1;
public OperatorMobileImpl(){
mobiles=new Mobile[5];
}
@Override
public void addMobile() {
System.out.println("增加通訊錄");
System.out.println("請輸入姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("請輸入手機號碼:");
String tel = sc.nextLine();
System.out.println("請輸入qq號碼:");
String qq = sc.nextLine();
System.out.println("請輸入地址:");
String address = sc.nextLine();
Mobile person0 = new Mobile(id,qq, name, tel, address);
if (index < mobiles.length) {
mobiles[index] = person0;
index++;
} else {
Mobile newperson[] = new Mobile[mobiles.length * 2];
for (int i = 0; i < index; i++) {
newperson[i] = mobiles[i];
}
newperson[index] = person0;
mobiles = newperson;
index++;
}
id++;
System.out.println("通訊錄增加成功。。。。。。");
System.out.println("按任意鍵繼續");
}
@Override
public Mobile[] showMobile() {
if(index==0){
return null;
}else{
System.out.println(
"-------------------------------------------------------------------------------------------------------------------------");
System.out.println(
"編號" + "\t\t\t" + " 手機 " + "\t\t\t " + "\t號碼姓名" + "\t\t\t " + "\tQQ號碼" + "\t\t\t\t" + "地址");
System.out.println(
"--------------------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < index; i++) {
System.out.printf("%-30s", mobiles[i].getId());
System.out.printf("%-30s", mobiles[i].getTel());
System.out.printf("%-30s", mobiles[i].getName());
System.out.printf("%-30s", mobiles[i].getCode());
System.out.printf("%-30s", mobiles[i].getAddress());
System.out.println(" ");
}
return null;}
}
@Override
public Mobile[] showMobileByName() {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入姓名:");
String t = sc.nextLine();
for (int i = 0; i < index; i++) {
if (mobiles[i].getName().equals(t)) {
System.out.println("該手機號碼對應的資訊是");
System.out.println("姓名:" + mobiles[i].getName());
System.out.println("qq號:" + mobiles[i].getCode());
System.out.println("地址:" + mobiles[i].getAddress());
}
}
System.out.println("按任意鍵繼續。。。");
return null;
}
@Override
public void deleteByName() {
int delete = 0;
Scanner sc = new Scanner(System.in);
System.out.println("請輸入姓名:");
String tel = sc.nextLine();
for (int i = 0; i < index; i++) {
if (mobiles[i].getName().equals(tel)) {
System.out.println("該姓名對應的資訊是");
System.out.println("姓名:" + mobiles[i].getName());
System.out.println("qq號:" + mobiles[i].getCode());
System.out.println("地址:" + mobiles[i].getAddress());
delete = i;
}
}
System.out.println("確認洗掉么?(Y/N)");
String yn = sc.nextLine();
if (yn.equals("Y")) {
if (index == 5) {
index--;
} else {
for (int j = delete; j < index; j++) {
mobiles[j] = mobiles[j + 1];
}
index--;
}
}
}
@Override
public void updateByName() {
int xiugai = 0;
Scanner sc = new Scanner(System.in);
System.out.println("請輸入要修改的姓名:");
String t = sc.nextLine();
for (int i = 0; i < index; i++) {
if (mobiles[i].getName().equals(t)) {
System.out.println("該姓名對應的資訊是");
System.out.println("姓名:" + mobiles[i].getName());
System.out.println("qq號:" + mobiles[i].getCode());
System.out.println("地址:" + mobiles[i].getAddress());
xiugai = i;
}
}
System.out.println("請輸入姓名:");
String name = sc.nextLine();
System.out.println("請輸入手機號碼:");
String tel = sc.nextLine();
System.out.println("請輸入qq號碼:");
String qq = sc.nextLine();
System.out.println("請輸入地址:");
String address = sc.nextLine();
mobiles[xiugai].setName(name);
mobiles[xiugai].setCode(qq);;
mobiles[xiugai].setAddress(address);
mobiles[xiugai].setTel(tel);
System.out.println("修改成功!!!按任意鍵繼續。。。");
}
}
Test.java
package com.geminno.mobile;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
OperatorMobileImpl om=new OperatorMobileImpl();
while (true) {
MobileMain mobilemain=new MobileMain();
Scanner keyin = new Scanner(System.in);
String select = keyin.nextLine();
switch (select) {
case "1":
om.showMobile(); 6.最后,感覺不大懂,希望有老哥能給弟弟講講,快哭了
break;
case "2":
om.addMobile();
break;
case "3":
om.updateByName();
break;
case "4":
om.deleteByName();
break;
case "5":
om.showMobileByName();
case "6":
System.exit(0);
break;
default:
break;
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/9682.html
標籤:Eclipse
