zoukankan      html  css  js  c++  java
  • 对象数组实现添加和显示客户信息

    public class Customer {
    private String name;
    private int age;
    private boolean flag;//是否有会员卡
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public boolean isFlag() {
    return flag;
    }
    public void setFlag(boolean flag) {
    this.flag = flag;
    }
    }
    import java.util.Scanner;
    public class CustomerBiz {
    //定义一个对象数组
    private Customer[] cus = new Customer[30];

    public static void main(String[] args) {
    	// TODO Auto-generated method stub
    	Scanner sc = new Scanner(System.in);	
    	CustomerBiz biz = new CustomerBiz();
    	
    	for (int i = 0; i < 2; i++) {
    		Customer cus = new Customer();
    		System.out.print("请输入名称:");
    		//给属性赋值
    		cus.setName(sc.next());
    		System.out.print("年龄:");
    		cus.setAge(sc.nextInt());
    		//添加客户对象
    		biz.addCustomer(cus);
    	}
    	//展示
    	biz.showCustomers();
    }
    
    /**
     * 添加对象
     * @param customer
     */
    public void addCustomer(Customer customer){
    	for (int i = 0; i < cus.length; i++) {
    		if (cus[i]==null) {
    			cus[i]=customer;
    			break;
    		}
    	}
    }
    /**
     * 显示客户信息
     */
    public void showCustomers(){
    	System.out.println("客户信息:");
    	for (Customer customer : cus) {
    		if(customer != null){
    			System.out.println(customer.getName()+"	"+customer.getAge()+"	"+customer.isFlag());
    		}
    	}		
    }
    

    }

  • 相关阅读:
    FATFS 初学之 f_open/ f_close
    前端JQuery(二)
    前端JQuery(一)
    8.22MySQL(五)pymysql模块、sql注入问题
    8.21MySQL(四)基本查询语句及方法、连表、子查询
    8.20MySQL(三)外键
    8.19MySQL(二)
    8.16MySQL(一)
    8.15并发编程(四)
    8.14并发编程(三)
  • 原文地址:https://www.cnblogs.com/rainsnow/p/12188008.html
Copyright © 2011-2022 走看看