User类:
package baidu.com;
public class User {
public String uname;
private String upass;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
public User(String uname, String upass) {
super();
this.uname = uname;
this.upass = upass;
}
@Override
public String toString() {
return "User [uname=" + uname + ", upass=" + upass + "]";
}
}
Manager类:
package sina.com;
import java.util.Scanner;
import baidu.com.User;
public class Manager {
private User user;
public Manager(User user) {
super();
this.user = user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void changepass(){
Scanner cin = new Scanner(System.in);
System.out.println("请输入原密码");
if(cin.next().equals(user.getUpass())){
System.out.println("原密码输入正确");
while(true){
System.out.println("请输入新密码:");
String temp = cin.next();
System.out.println("请再次输入新密码:");
if(cin.next().equals(temp)){
this.user.setUpass(temp);
break;
}
else{
System.out.println("两次密码不一致");
}
}
}
else{
System.out.println("原密码输入错误");
}
}
}
主类:
package com.main;
import java.util.Scanner;
import baidu.com.User;
import sina.com.Manager;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in);
System.out.println("请输入用户名:");
String uname = cin.next();
System.out.println("请输入密码:");
String upass = cin.next();
Manager manager = new Manager(new User(uname, upass));
System.out.println("初始用户信息为:"+manager.getUser().toString());
manager.changepass();
System.out.println("结果用户信息为:"+manager.getUser().toString());
}
}