赞
踩
现在一个银行管理系统中有普通用户(id,姓名,手机号,密码,年龄,visible),vip用户(id,姓名,手机号,密码,年龄,visible,vipId,vip称号),管理员(id,姓名,手机号,密码)三中对象。可以看出,实体类若单独写三个无关联的类,很明显,三个类会在重复字段上耗费代码。又学习了继承,很明显我们可以构建一个基类User,它拥有这三类实体类的公共字段与方法,然后我们在写三个具体实体类BaseUser,VipUser,Manager分别继承User。这样他们就拥有了共同字段(id,姓名,手机号,密码),同时再自己添加自己的独有字段。构建完成后,我们会发现,我们的代码中不会再用到User的实例,只会用到它派生的子类。User在该系统中应该只起到了模板作用,他不需要被实例化,怎样做呢?
一个类中的所包含的信息不够描述这个类时,这个类就是抽象类,由abstract修饰
public abstract class User { private Integer id; private String name; private String phone; private String pwd; public User(String name, String phone, String pwd) { super(); this.name = name; this.phone = phone; this.pwd = pwd; } public User() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
抽象类的实例化只能依靠子类
User user = new User();这种写法是不能通过编译的
除了抽象类不能实例化意外,抽象类与普通类唯一的区别就是,抽象类中含有抽象方法。而继承抽象类的子类必须为实现对抽象方法的重写(除非继承承抽象类的子类也是抽象类)
BaseUser继承了User,并在此基础上增加了age与是否能被查看字段visible
public class BaseUser extends User{ private Integer age; private Boolean visible; public BaseUser(String name, String phone, String pwd,Integer age,Boolean visible) { super(name, phone, pwd); this.age = age; this.visible = visible; } public BaseUser() { super(); } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getVisible() { return visible; } public void setVisible(Boolean visible) { this.visible = visible; } @Override public String toString() { return "BaseUser 账号:"+getId() + ", 姓名" + getName()+ ", 电话" + getPhone()+ ", 年龄" +age + ", 是否可见:" + visible; } }
因为没有使用集合,用户数组的长度进行了限制,如果想对用户数组长度作出修改可以看一下数组的扩容与缩容
具体结构
dao层
ManagerDao
public class ManagerDao { private static int mid = 2001; private Manager [] managers; public ManagerDao() { //系统仅支持三个管理员 managers = new Manager[3]; } public Boolean addManager(Manager man) { for(int i=0;i<managers.length;i++) { if(managers[i] == null) { managers[i] = man; managers[i].setId(mid++); return true; } } return false; } /** * * 传入manager与内置的manager进行比较,账号密码正确则返回该manager,反之返回null * */ public Manager loginCheck(Manager man) { for(int i=0;i<managers.length;i++) { if(managers[i]!=null) { if(managers[i].getId().equals(man.getId()) &&managers[i].getPwd().equals(man.getPwd())) { return managers[i]; } } } return null; } public Manager findManagerById(Integer id) { for(int i=0;i<managers.length;i++) { if(managers[i] != null && managers[i].getId().equals(id)) { return managers[i]; } } return null; } public void findAllManager() { for (Manager manager : managers) { if(manager!=null) { System.out.println(manager); } } } }
UserManager
public class UserManager { private BaseUser[] users; public UserManager() { users = new BaseUser[10]; } public BaseUser findById(Integer id) { for (BaseUser user : users) { if (user != null) { if (id.equals(user.getId()) && user.getVisible()) { return user; } } } return null; } public void add(BaseUser user) { boolean isNull = true; System.out.println("添加"); for (int i = 0; i < users.length; i++) { if (users[i] == null) { users[i] = user; users[i].setId(i + 1001); isNull = false; break; } } if (isNull) { System.out.println("内存已满,请求扩容"); } } public void findAll() { for (BaseUser user : users) { if (user != null && user.getVisible() == true) { System.out.println(user.toString()); } } } public void delUser(Integer userid) { for(int i=0;i<users.length;i++) { if(users[i]!=null && users[i].getId().equals(userid)) { //System.out.println(users[i].toString()); users[i].setVisible(false); } } } public void updateUser(BaseUser user) { for (int i = 0; i < users.length; i++) { if (users[i] != null && users[i].getId().equals(user.getId())) { users[i] = user; } } } }
实体类(User BaseUser如上)
Manager
public class Manager extends User{ public Manager(String name, String phone, String pwd) { super(name,phone,pwd); } public Manager() { } public Manager(Integer id,String pwd) { this.setId(id); this.setPwd(pwd); } @Override public String toString() { return "Manager 账号:" + getId() + ", 姓名:" + getName() + ", 密码:" + getPwd() + "]"; } }
VipUser
public class VipUser extends BaseUser { private Long vipId; private String vipRank; public Long getVipId() { return vipId; } public void setVipId(Long vipId) { this.vipId = vipId; } public String getVipRank() { return vipRank; } public void setVipRank(String vipRank) { this.vipRank = vipRank; } public VipUser() { super(); } public VipUser(String name,String phone,String pwd,Integer age,Boolean visible,String vipRank) { super(name,phone,pwd,age,visible); this.vipId = System.currentTimeMillis(); this.vipRank = vipRank; } @Override public String toString() { return "BaseUser 账号:"+getId() + ", 姓名" + getName()+ ", 电话" + getPhone()+ ", 年龄" +getAge() + ", 是否可见:" + getVisible()+",vip账号"+vipId+",vip称号"+vipRank; } }
View层
主函数主页
public class Client { /** * @param args */ public static void main(String[] args) { // UserManager manager = new UserManager(); ManagerDao managerDao = new ManagerDao(); Scanner scanner = new Scanner(System.in); //登录前,先构建一个管理员 账号 2001 Manager manager1 = new Manager("张三","1008611","123456"); managerDao.addManager(manager1); while(true) { System.out.println("您好!欢迎使用XXX银行CRM系统:"); Boolean loginFlag = ManagerLoginView.openView(managerDao); if(!loginFlag) { System.out.println("管理员账号或密码有误,请重新登录!"); continue; } function:while(true) { System.out.println("请选择功能: 1. 添加 2.根据编号查询 3.查询全部 4.根据编号删除 5.根据编号修改 6.退出系统"); String choose = scanner.next(); switch (choose) { case "1": AddUserView.addUserViewMethod(manager); break; case "2": FindUserByIdView.openView(manager); break; case "3": FindAllView.openView(manager); break; case "4": DelUserView.openView(manager); break; case "5": UpdateView.openView(manager); break; case "6": break function; default: break; } } } } }
添加用户视图
public class AddUserView { private static Scanner scanner = new Scanner(System.in); public static void addUserViewMethod(UserManager manager) { // TODO Auto-generated method stub BaseUser newUser; System.out.println("新用户是否为Vip用户(Y/N)"); String choose = scanner.next(); System.out.println("请输入新用户姓名"); String newUserName = scanner.next(); System.out.println("请输入新用户电话"); String newUserPhe = scanner.next(); System.out.println("请输入新用户密码"); String newUserPwd = scanner.next(); System.out.println("请输入新用户年龄"); int newUserAge = scanner.nextInt(); if("Y".equalsIgnoreCase(choose)) { System.out.println("请输入vip层次"); String vipRank = scanner.next(); newUser = new VipUser(newUserName,newUserPhe,newUserPwd,newUserAge,true,vipRank); }else { newUser = new BaseUser(newUserName,newUserPhe,newUserPwd,newUserAge,true); } manager.add(newUser); System.out.println("新用户添加成功,创建账号:"+newUser.getId()); } }
管理员登录视图
public class ManagerLoginView { private static Scanner scanner = new Scanner(System.in); //管理员登录视图,登录成功返回true,登陆失败返回false public static Boolean openView(ManagerDao managerDao) { System.out.println("请输入管理员账号:"); Integer id = scanner.nextInt(); System.out.println("请输入管理员密码:"); String pwd = scanner.next(); Manager managerLogin = new Manager(id,pwd); //检验登录 Manager flag = managerDao.loginCheck(managerLogin); if(flag !=null) { System.out.println("登陆成功! 欢迎您,管理员:"+flag.getName()); return true; } return false; } }
唉,好难粘额,剩下的不粘了,大家都能写出来,我就不献丑了。抽象类改造用户管理系统,完成。
学习,我们一直在路上啊!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。