标签:面向对象 class ges 添加用户 sys 证件 成功 整型 proc
答案:
1、地址类
class Address{
private String country;//国家
private String province;//省份
private String street;//街道
private String id;//门排号
public Address(){}
public Address(String country,String province,String street,String id){
this.country = country;
this.province = province;
this.street = street;
this.id = id;
}
public void setCountry(String country){
this.country = country;
}
public String getCountry(){
return this.country;
}
public void setProvince(String province){
this.province = province;
}
public String getProvince(){
return this.province;
}
public void setStreet(String street){
this.street = street;
}
public String getStreet(){
return this.street;
}
public void setId(String id){
this.id = id;
}
public String getId(){
return this.id;
}
}
2、用户类
class User{
private int code;//账号
private String name;//姓名
private int password;//密码
private Address address;//地址
private double money;//存款余额
private Date date;//注册时间
private String openBankName;//开户行
public User(){}
public User(int code,String name,int password,Address address,double money,Date date,String openBankName){
this.code = code;
this.name = name;
this.password = password;
this.address = address;
this.money = money;
this.date = date;
this.openBankName = openBankName;
}
public void setCode(int code){
this.code = code;
}
public int getCode(){
return this.code;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setPassword(int password){
this.password = password;
}
public int getPassword(){
return this.password;
}
public void setAddress(Address address){
this.address = address;
}
public Address getAddress(){
return this.address;
}
public void setMoney(double money){
this.money = money;
}
public double getMoney(){
return this.money;
}
public void setDate(Date date){
this.date = date;
}
public Date getDate(){
return this.date;
}
public void setOpenBankName(String openBankName){
this.openBankName = openBankName;
}
public String getOpenBankName(){
return this.openBankName;
}
}
3、银行类
class Bank{
private String bankName ;
static User[] users = new User[10];
public Bank(){}
public Bank(String bankName){
this.bankName = bankName;
}
public void setBankName(String bankName){
this.bankName = bankName;
}
public String getBankName(){
return this.bankName;
}
/**
检查用户是否存在
*/
public boolean checkUser(int code){
for (User u : users){
if (u != null){
if (u.getCode() == code){
return true;
}
}
}
return false;
}
/**
检查用户是否已满
*/
public boolean isFullUser(){
for (User user : users){
if (user == null){
return false;
}
}
return true;
}
/**
检查用户和密码是否正确
*/
public boolean checkUserAndPassword(int userCode, int password){
if (checkUser(userCode)){
for (User u : users){
if (u.getCode() == userCode){
if (u.getPassword() == password){
return true;
}
}
}
}
return false;
}
/**
检查钱是否够
*/
public boolean checkMoney(int userCode,double money){
for (User u : users){
if (u.getCode() == userCode){
if (u.getMoney() >= money){
return true;
}else{
break;
}
}
}
return false;
}
/**
添加用户的方法
*/
public int addUser(User user){
if (checkUser(user.getCode())){
return 2;
}else if (isFullUser()){
return 3;
}else{
for (int i = 0;i < users.length ; i++){
if (users[i] == null){
users[i] = user;
return 1;
// break;
}
}
}
return 1;
}
/**
存钱方法
*/
public boolean saveMoney(int userCode,double money){
if (!checkUser(userCode)){
return false;
}else{
for (User u : users){
if (u.getCode() == userCode){
u.setMoney(u.getMoney() + money);
}
}
}
return true;
}
/**
取钱方法
*/
public int takeMoney(int userCode,int password,double takeMoney){
if (!checkUser(userCode)){
return 1;
}else if (!checkUserAndPassword(userCode,password)){
return 2;
}else if (!checkMoney(userCode,takeMoney)){
return 3;
}else{
for (User u : users){
if (u.getCode() == userCode){
u.setMoney(u.getMoney() - takeMoney);
break;
}
}
}
return 0;
}
/**
转账方法
*/
public int transferMoney(int outCode,int inCode,int outpassword,double outMoney){
if (!checkUser(outCode) && checkUser(inCode)){
return 1;
}else if (!checkUserAndPassword(outCode,outpassword)){
return 2;
}else if (!checkMoney(outCode,outMoney)){
return 3;
}else{
saveMoney(inCode,outMoney);
takeMoney(outCode,outpassword,outMoney);
}
return 0;
}
/**
查询账户
*/
public void selectUser(int userCode,int password){
if (!checkUser(userCode)){
System.out.println("该用户不存在!");
}else if (!checkUserAndPassword(userCode,password)){
System.out.println("密码错误!");
}else{
User u = null;
Address address = null;
for (User user : users){
if (user.getCode() == userCode){
u = user;
address = u.getAddress();
break;
}
}
System.out.println("当前账户为:\t" + u.getName() + "\n账号为:" + userCode
+ "\n密码:" + password + "\n余额:" + u.getMoney()
+ "\n用户当前居住地址:\t" + address.getCountry() + address.getProvince() + "市"
+ address.getStreet() + address.getId() + "号!");
}
}
}
4、页面模拟类
class Page{
public static void welcomePage(){
System.out.println("**");
System.out.println(" 中国工商银行 ");
System.out.println(" 账户管理系统 ");
System.out.println(" V1.0 ");
System.out.println("**");
System.out.println(" 1.开户 ");
System.out.println(" 2.存钱 ");
System.out.println(" 3.取钱 ");
System.out.println(" 4.转账 ");
System.out.println(" 5.查询 ");
System.out.println(" 6.bye! ");
System.out.println("**");
}
}
5、具体的测试类
import java.util.Date;
import java.util.Scanner;
public class Demo{
private static Scanner sc = new Scanner(System.in);
private static Bank bank = new Bank("中国工商银行沙河支行");
public static void main(String[] args){
Page.welcomePage();
System.out.print("请输入业务编号:");
int num = sc.nextInt();
if (num == 1){
System.out.print("请输入账户号:");
int code = sc.nextInt();
System.out.print("\n请输入用户名:");
String name = sc.next();
System.out.print("\n请输入密码:");
int password = sc.nextInt();
System.out.print("\n请输入输入居住的地址,首先输入国家:");
String country = sc.next();
System.out.print("\n请输入省份:");
String province = sc.next();
System.out.print("\n请输入街道:");
String street = sc.next();
System.out.print("\n请输入门牌号:");
String id = sc.next();
System.out.print("\n请输入您的账户的初始余额:");
double money = sc.nextDouble();
Address address = new Address(country,province,street,id);
User user = new User(code,name,password,address,money,new Date(),bank.getBankName());
int n = bank.addUser(user);
if (n == 2){
System.out.println("对不起,您输入的账户已存在,无法开户,请带好证件到营业厅窗口办理!");
}else if (n == 3){
System.out.println("对不起,当前银行的用户数已满,请到其他银行办理业务!");
}else if (n == 1){
System.out.println("恭喜你,开户成功下面是您的开户信息:");
bank.selectUser(code,password);
}
}else if (num == 2){
}else if (num == 3){
}else if (num == 4){
}else if (num == 5){
}else if (num == 6){
System.out.println("Bye~~");
System.exit(0);
}
}
}
标签:面向对象 class ges 添加用户 sys 证件 成功 整型 proc
原文地址:http://blog.51cto.com/lyinginsun/2154804