菜鸟的处子秀

我的java基础很差,但一开始做的那些编程题感觉很简单,直到遇上这次作业——完成电话本功能

通过这次作业,真正体会到java面向对象的含义:把一切事物看做对象,根据对象的属性设计实体类,根据对象的行为去设计方法类。(以上观点纯属个人理解)

<span style="font-size:18px;">
public class PhoneBook {

	public static void main(String[] args) {
		Show show=new Show();

	}

}
</span>
<span style="font-size:18px;">
public class User {         //实体类
	private String name;
	private String sex;
	private int age;
	private int number;
	private int qq;
	private String address;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public int <span style="font-family: Arial, Helvetica, sans-serif;">getQq() {</span></span><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;">                       return qq;</span></span>
}public void setQq(int qq) {this.qq = qq;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String toString() {super.toString();return " 名字:" + name + ", 性别:" + sex + ", 年龄:" + age+ ", 电话:" + number + ", qq:" + qq + ", 地址:" + address;}}

 
<span style="font-size:18px;">package phoneBook;

import java.util.Scanner;

public class Show {
	
	User[] arr=new User[100];
	static int count=0;     //用户计数器
	Scanner sc=new Scanner(System.in);
	public Show(){
		choice();
	}
	
	public void choice(){
		
		System.out.println("-------------------电话本系统------------------------");
		System.out.println("1.添加"+" "+"2.删除"+" "+"3.修改"+" "+"4.查询所有号码"+" "+"5.根据姓名查询"+" "+"0.退出");
		System.out.println("-------------------电话本系统------------------------");
		
		int cz=sc.nextInt();
		switch(cz){
		case 1:
			add();
			System.out.println("添加成功");
			choice();
			break;
		case 2:
			delete();
			choice();
			break;
		case 3:
			change();
			choice();
			break;
		case 4:
			select();
			choice();
			break;
		case 5:
			selectByName();
			choice();
			break;
		case 0:
			break;
			default:
				System.out.println("操作有误!请重新输入");
				choice();
				break;
				
			
		
		}
	}
	
	public void add(){        //添加
		System.out.print("姓名:");
		String name=sc.next();
		System.out.print("性别:");
		String sex=sc.next();
		System.out.print("年龄:");
		int age=sc.nextInt();
		System.out.print("电话:");
		int number=sc.nextInt();
		System.out.print("QQ:");
		int qq=sc.nextInt();
		System.out.print("地址:");
		String address=sc.next();
		User user=new User();
		user.setName(name);
		user.setSex(sex);
		user.setAge(age);
		user.setNumber(number);
		user.setQq(qq);
		user.setAddress(address);
		
		arr[count]=user;
		System.out.println(arr[count].toString());
		count++;         //每天加一个用户,计数器加1
	}
	
	public void delete(){    //删除
		
		if(count>0){
			System.out.print("请输入删除的姓名:");
			String mz=sc.next();
			int a=0;
			for(int i=0;i<count;i++){
				if(arr[i].getName().equals(mz)){   //查找匹配的用户并输出
					System.out.println(arr[i].toString());
				}else{
					a++;
				}
			}
				if(a==count){    //表示没有找到名字相同的用户
					System.out.println("电话本中没有该联系人,请重新输入!");
					choice();
				}else{
					System.out.println("确认删除?  1:删除  0:取消");
					int qr=sc.nextInt();
					switch(qr){
					case 1:
						for(int j=0;j<count;j++){
							if(arr[j].getName().equals(mz)){
								for (int t = j; t < count; t++){
									// 所有数组往前移位
									arr[t] = arr[t + 1];
								}
								count--;	
							}
							
						}
						System.out.println("删除成功");
					case 0:
						choice();
						break;
					default :
							System.out.println("输入有误,请重新输入!");
							delete();
							break;
							
					}	
					}
			
			
		}else{
			System.out.println("电话本中无信息");
			choice();
		}
	

    }
	public void change(){            //修改
		final int b=count;
		if(count>0){
			System.out.print("请输入修改的姓名:");
			String mz=sc.next();
			int a=0;
			for(int i=0;i<count;i++){
				if(arr[i].getName().equals(mz)){         //查找匹配的用户并输出
					System.out.println(arr[i].toString());
					for (int t = i; t < count; t++){
						// 所有数组往前移位
						arr[t] = arr[t + 1];
					}
					count--;
					
				}else{
					a++;
				}
			}
			if(a==b){
				System.out.println("电话本中没有该联系人,请重新输入!");
				choice();
			}else{            //删除匹配用户,重新添加
				for(int i=0;i<count;i++){
					if(arr[i].getName().equals(mz)){         
						
						for (int t = i; t < count; t++){
							// 所有数组往前移位
							arr[t] = arr[t + 1];
						}
						count--;
						
					}
				}
				System.out.println("请重新输入");
				add();
				System.out.println("修改成功");
			}
								
		}else{
				System.out.println("电话本中无信息");
				choice();
		}
		
	}
	
	public void select(){        //查询所有信息
		if(count>0){
			for(int i=0;i<count;i++){
				System.out.println(arr[i].toString());
			}
		}else{
			System.out.println("电话本中无信息");
		}
	}
	
	public void selectByName(){           //根据名字查询
		int a=0;
		if(count>0){
			System.out.print("请输入查询的姓名:");
			String mz=sc.next();
			for(int i=0;i<count;i++){
				if(arr[i].getName().equals(mz)){
					System.out.println(arr[i].toString());
				}else{
					a++;
				}
				if(a==count){
					System.out.println("电话本中没有该联系人,请重新输入!");
					selectByName();
				}
				
			}
		}else{
			System.out.println("电话本中无信息");
		}
	}

}



</span><span style="font-size:32px;color:#3366ff;"><strong>如有错误,欢迎指正</strong></span><span style="color:#ff0000;"><span style="font-size:32px;"> </span></span><pre name="code" class="java" style="font-size:18px; color: rgb(255, 0, 0);"><img alt="微笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif" />

 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值