java基础练习题代码

Customer类的代码

package com.pro01.Customer;

public class Customer {
	//定义私有属性
	private String name;
	private char gender;
	private int age;
	private String phone;
	private String email;
	
	//进行封装设置get和set方法
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	//有参构造
	//初始化会有name,gender,age,phone,email属性赋值。
	public Customer(String name, char gender, int age, String phone, String email) {
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.phone = phone;
		this.email = email;
	}
	//无参构造
	public Customer() {
		
	}
	
	
	
}

Customer类设置为属性供CustomerList方法调用

CustomerList类的代码

package com.pro01.CustomerList;

import com.pro01.Customer.Customer;

public class CustomerList {

	private Customer[] customers;// 用来保存对象的数组
	private int total = 0;// 用来记录客户的个数

	 /**
	 * 传进一个有参构造来new一个Customer[]的对象,也就是当在CustomerView里面new
	 * CustomerList 通过CustomerList 的构造器new Customer[] 并传进去所存客户的人数
	 * @param 
	 * @return 
	 */   			  		
	public CustomerList(int totalCustomer) {
		customers = new Customer[totalCustomer];
	}
	/**
	 * 添加客户
	 * 写一个有返回一个布尔类型的方法,并带有一个形参Customer customer,这是当在
	 * CustomerView中new Customer的时候将当时的对象传给addCustomer方法中的形参
	 * 
	 * @param customer
	 * @return true 添加成功 ; false 添加失败
	 */
	public boolean addCustomer(Customer customer) {
		//如果所存客户中的数大于customers.length说明客户端所存客户已满则无法继续添加则返
		//一个false
		if (total > customers.length) {
			return false;
		}
		//如果第一个判断没进去说明客户端所存客户未满则可以继续添加客户信息
		//那么就将所传的Customer参数赋给customers[total],并且人数增加则total需自加1。
		//注:后++
		//添加成功后返回true 
		customers[total++] = customer;
		return true;
	}

	/**
	 * 修改客户
	 * 写一个有返回一个布尔类型的方法,并带有一个形参Customer cust和一个索引的index、
	 * 
	 * @param index
	 * @param customer
	 * @return true修改成功 ; false 修改失败
	 */

	public boolean repalceCustomer(int index, Customer cust) {
		//判断所传索引是否存在
		if (index < 0 || index >= total) {
			return false;
		}
		//如果存在则将新修改的参数cust赋给customers[index]返回true
		customers[index] = cust;
		return true;
	}

	/**
	 * 删除客户
	 *  写一个有返回一个布尔类型的方法,并带有一个索引的index、
	 * @param index
	 * @return true删除成功 ; false 删除失败
	 */

	public boolean deleteCustomer(int index) {
		//判断所传索引是否存在
		if (index < 0 || index >= total) {
			return false;
		}
		//如果存在则进行删除,删除则是用你所想删的位置的索引,用他的下一个索引的值替换掉
		//前一个索引的值然后让最后一个索引所在的位置设为null。而total为人数删除则需要--
		for (int i = index; i < total - 1; i++) {
			customers[i] = customers[i + 1];
		}
//		customers[total-1] = null;
//		total--;
		customers[--total] = null;
		return true;

	}

	/**
	 * 获取所有客户信息
	 * 
	 * @return
	 */

	public Customer[] getAllCustomers() {
		//创建了一个新的数组用遍历将已存的数组赋值过来
		Customer[] cust = new Customer[total];
		for (int i = 0; i < total; i++) {
			cust[i] = customers[i];
		}
		//则返回一个新的数组cust。
		return cust;
	}

	/**
	 * 获取索引位置上的客户
	 * 
	 * @param index
	 * @return 如果不符合就返回null
	 */
	public Customer getCustomer(int index) {
		//判断所传索引是否存在
		if (index < 0 || index >= total) {
			return null;
		}
		//如果存在就返回一个Customer的customers[index]里面的地址
		return customers[index];
	}
	/**
	 * 获取客户的数量
	 * @return
	 */
	public int getTotal() {
		return total;
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值