编辑代码

class Main {
	public static void main(String[] args) {
        //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
		System.out.println("Hello world!   - java.jsrun.net ");
        Account account = new Account();
        account.setName("jack");
        account.setMoney(30);
        account.setPsd("123456");

        account.showInfo();
	}
 
}
/*
创建程序,在其中定义两个类:Account和AccountTest类
体会Java的封装性。
Account类要求具有属性:
姓名(长度为2位3位或4位)、余额(必须>20)、密码(必须是六位),
如果不满足,则给出提示信息,并给默认值(程序员自己定)
通过setXxx的方法给Account的属性赋值。
在AccountTest中测试
*/

class Account{
    //为了封装,将3个属性设置为private
   private String name;
   private double money;
   private String psd;

   //提供两个构造器
   public Account(){

   }
   
   // 注意:“封装的入门”案例通过创建对象设置私有属性的值
   // 本练习通过构造器调用本类方法设置私有属性的值
   public Account(String name, double money,String psd){
       this.setName(name);
       this.setMoney(money);
       this.setPsd(psd);
   }


     public void setName(String name){
        if( name.length()>=2 && name.length()<=4 ){ //判断名字:如果是合理范围
            this.name=name;
        } else {
            System.out.println("名字长度不对,需要2-4个字符之间,给一个默认名字大聪明");
            this.name= "小明";
        }
        
    }

    public String getName(){
        return name;
    }

    public void setMoney(double money){ 
        if(money>20) { // 判断余额:如果是合理范围    
        this.money=money;
        } else {
            System.out.println("余额必须大于20,默认为0");
           // this.money=;
        }
    }

    public double getMoney(){
        // 这里可以增加当前对象的权限判断,有权限则可以查看工资
        return money;
    }

    public void setPsd(String psd){
        if( psd.length()==6 ){ //判断名字:如果是合理范围
            this.psd = psd;
        } else {
            System.out.println("密码长度必须6位,给一个默认密码000000");
            this.psd= "000000";
        }
        
    }

    public String getPsd(){
        return psd;
    }

    //显示账号信息
    public void showInfo(){
        System.out.println("账号信息:姓名:"+ name +"\t余额:"+ money +"\t密码:"+ psd);
    }

}