求大虾帮帮忙了!小弟谢谢了 把你设置好的用户名放入list1中,密码放入list2中,当需要从界面输入用户名时,就与list1中的内容比较,当需要从界面输入密码时,就与list2中的内容比较,两者都满足才能进去。不知这样满足你的要求不? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那怎么能保证账号和密码是匹配呢 是用ArrayList吗怎么能像数组那样当匹配的时候 能得到是哪个下标 从而来创银行卡的对象因为我下面还要用到卡类的方法谢谢 我还有银行卡的用户名 等等要每一类都建一个LIST吗 怎么才能把账号和密码匹配之后我能获取到这个账号的所有信息呢 再次感谢 还是用map好,key是用户名(唯一),value是密码这样就可以保证账号和密码是匹配的了。创银行卡时又新建一个map,key是卡号,value是用户名。 用hashmap 把key-value写入文件,比较时从文件取出就可以了;其实还是用数据库方便,学起来也蛮快的。 用Map.Key Value适合楼主这种情况 public List getInfo(int cardId,int password){ List list=new ArrayList(); Card card=new Card(); if(card.getId().equals(cardId)&&card.getPassword().equals(password)){ list.add(card.getMoney()); 。 } return list;}随便写了几句,不知是否适合你。 写了个小型的,仅供参考:import java.util.HashMap;import java.util.Map;public class ATM { private static AccountSet accounts = AccountSet.getInstance(); static { try { accounts.addAccount("30001", "123456", "张大", 500); accounts.addAccount("30002", "123457", "张二", 600); accounts.addAccount("30003", "123458", "张三", 700); accounts.addAccount("30004", "123459", "张四", 800); } catch (AccountException e) { e.printStackTrace(); } } public static void main(String[] args) throws AccountException { System.out.println("操作1,取款:"); operate1(); System.out.println("操作2,存款:"); operate2(); System.out.println("操作3,修改密码:"); operate3(); } private static void operate1() throws AccountException { AtmInput input = new AtmInput("30001", "123456", 30, AtmOperation.WITHDRAWAL); Account account = accounts.getAccount(input); int balance = input.operate(account); System.out.println(account); } private static void operate2() throws AccountException { AtmInput input = new AtmInput("30002", "123457", 44, AtmOperation.DEPOSIT); Account account = accounts.getAccount(input); int balance = input.operate(account); System.out.println(account); } private static void operate3() throws AccountException { AtmInput input = new AtmInput("30003", "123458", "456789"); Account account = accounts.getAccount(input); int result = input.operate(account); System.out.println(account); }}/** * ATM 输入 * @author frankiegao123 * 2010-1-4 下午10:02:53 */class AtmInput { private String accountNumber; // 账号 private String password; // 密码 private int operationMoney; // 操作金额 private AtmOperation operation; // 操作 private String newpassword; // 新密码 public AtmInput(String accountNumber, String password, int operationMoney, AtmOperation operation) { this.accountNumber = accountNumber; this.password = password; this.operationMoney = operationMoney; this.operation = operation; } public AtmInput(String accountNumber, String password, String newpassword) { this.accountNumber = accountNumber; this.password = password; this.newpassword = newpassword; this.operation = AtmOperation.CHANGE_PASSWORD; } public int operate(Account account) throws AccountException { return operation.operate(account, this); } public String getAccountNumber() { return accountNumber; } public String getPassword() { return password; } public int getOperationMoney() { return operationMoney; } public String getOperation() { return operation.name(); } public String getNewpassword() { return newpassword; }}enum AtmOperation { /** * 取款 */ WITHDRAWAL { public int operate(Account account, AtmInput input) throws AccountException { return account.debit(input.getOperationMoney()); } }, /** * 存款 */ DEPOSIT { public int operate(Account account, AtmInput input) throws AccountException { return account.credit(input.getOperationMoney()); } }, /** * 改密码 */ CHANGE_PASSWORD { public int operate(Account account, AtmInput input) throws AccountException { return account.changePassword(input.getPassword(), input.getNewpassword()); } } ; public abstract int operate(Account account, AtmInput input) throws AccountException;}/** * 账户集 * @author frankiegao123 * 2010-1-4 下午09:51:06 */class AccountSet { private static AccountSet instance = new AccountSet(); private Map<String, Account> accounts; private AccountSet() { accounts = new HashMap<String, Account>(); } public static AccountSet getInstance() { return instance; } /** * 添加一个账户 * @param account * @return * @throws AccountException */ public Account addAccount(Account account) throws AccountException { if(accounts.containsKey(account.getAccountNumber())) { throw new AccountException(account.getAccountNumber() + " 账号已经存在,不允许再添加"); } accounts.put(account.getAccountNumber(), account); return account; } public Account addAccount(String accountNumber, String password, String name, int balance) throws AccountException { return addAccount(new Account(accountNumber, password, name, balance)); } /** * 根据输入获得账户信息 * * @param input * @return * @throws AccountException */ public Account getAccount(AtmInput input) throws AccountException { Account account = getAccountFromSet(input.getAccountNumber()); if(account != null && account.checkPassword(input.getPassword())) { return account; } throw new AccountException("账号或密码错误"); } private Account getAccountFromSet(String accountNumber) { return accounts.get(accountNumber); }}/** * 账户异常 * @author frankiegao123 * 2010-1-4 下午09:52:21 */class AccountException extends Exception { private static final long serialVersionUID = 1L; public AccountException() { super(); } public AccountException(String message) { super(message); }}/** * 账户 */class Account { private String accountNumber; // 账号 private String password; // 预留密码 private String name; // 姓名 private int balance; // 余额 public Account(String accountNumber, String password, String name, int balance) { super(); this.accountNumber = accountNumber; this.password = password; this.name = name; this.balance = balance; } /** * 检查密码的有效性 * * @param inputPassword * @return */ public boolean checkPassword(String inputPassword) { return password.equals(inputPassword); } public String getAccountNumber() { return accountNumber; } public String getPassword() { return password; } public int changePassword(String oldPassword, String newPassword) throws AccountException { if(checkPassword(oldPassword)) { password = newPassword; return 0; } throw new AccountException("账号或密码错误,无法更改密码"); } public String getName() { return name; } public int getBalance() { return balance; } /** * 借 * @param money * @return */ public int credit(int money) { balance += money; return balance; } /** * 贷 * @param money * @return * @throws AccountException */ public int debit(int money) throws AccountException { if(balance >= money) { balance -= money; return balance; } throw new AccountException("交易失败,账户中余额不足"); } public String toString() { return "账号: " + accountNumber + ", 姓名: " + name + ", 密码: " + password + ", 余额: " + balance; }} 有问题请教 父类方法通过反射取属性,是子类还是父类的属性? 为什么在JSP中存取CLOB字段要用流方式处理 紧急求助!java如何通过socket发送16进制的数据包(在线等) 软件交易 提示Please specify a main Java File first,(请首先确认一个主Java文件),请问如何解决! 请教一个内部类的问题: new A().super() 请问Java 2D API Graphics这本书有没有买得啊. 这是什么错? 取字符串的子串到数组中? equals(),compareTo()方法的有关问题 java applet 资料
那怎么能保证账号和密码是匹配呢
是用ArrayList吗怎么能像数组那样当匹配的时候 能得到是哪个下标 从而来创银行卡的对象
因为我下面还要用到卡类的方法谢谢
我还有银行卡的用户名 等等要每一类都建一个LIST吗 怎么才能把账号和密码匹配之后我能获取到这个账号的所有信息呢 再次感谢
这样就可以保证账号和密码是匹配的了。
创银行卡时又新建一个map,key是卡号,value是用户名。
其实还是用数据库方便,学起来也蛮快的。
List list=new ArrayList();
Card card=new Card();
if(card.getId().equals(cardId)&&card.getPassword().equals(password)){
list.add(card.getMoney());
。
}
return list;
}
随便写了几句,不知是否适合你。
import java.util.Map;public class ATM {
private static AccountSet accounts = AccountSet.getInstance();
static {
try {
accounts.addAccount("30001", "123456", "张大", 500);
accounts.addAccount("30002", "123457", "张二", 600);
accounts.addAccount("30003", "123458", "张三", 700);
accounts.addAccount("30004", "123459", "张四", 800);
} catch (AccountException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws AccountException {
System.out.println("操作1,取款:");
operate1();
System.out.println("操作2,存款:");
operate2();
System.out.println("操作3,修改密码:");
operate3();
}
private static void operate1() throws AccountException {
AtmInput input = new AtmInput("30001", "123456", 30, AtmOperation.WITHDRAWAL);
Account account = accounts.getAccount(input);
int balance = input.operate(account);
System.out.println(account);
}
private static void operate2() throws AccountException {
AtmInput input = new AtmInput("30002", "123457", 44, AtmOperation.DEPOSIT);
Account account = accounts.getAccount(input);
int balance = input.operate(account);
System.out.println(account);
}
private static void operate3() throws AccountException {
AtmInput input = new AtmInput("30003", "123458", "456789");
Account account = accounts.getAccount(input);
int result = input.operate(account);
System.out.println(account);
}
}/**
* ATM 输入
* @author frankiegao123
* 2010-1-4 下午10:02:53
*/
class AtmInput {
private String accountNumber; // 账号
private String password; // 密码
private int operationMoney; // 操作金额
private AtmOperation operation; // 操作
private String newpassword; // 新密码
public AtmInput(String accountNumber, String password, int operationMoney, AtmOperation operation) {
this.accountNumber = accountNumber;
this.password = password;
this.operationMoney = operationMoney;
this.operation = operation;
}
public AtmInput(String accountNumber, String password, String newpassword) {
this.accountNumber = accountNumber;
this.password = password;
this.newpassword = newpassword;
this.operation = AtmOperation.CHANGE_PASSWORD;
}
public int operate(Account account) throws AccountException {
return operation.operate(account, this);
} public String getAccountNumber() {
return accountNumber;
}
public String getPassword() {
return password;
}
public int getOperationMoney() {
return operationMoney;
}
public String getOperation() {
return operation.name();
}
public String getNewpassword() {
return newpassword;
}
}enum AtmOperation {
/**
* 取款
*/
WITHDRAWAL {
public int operate(Account account, AtmInput input) throws AccountException {
return account.debit(input.getOperationMoney());
}
},
/**
* 存款
*/
DEPOSIT {
public int operate(Account account, AtmInput input) throws AccountException {
return account.credit(input.getOperationMoney());
}
},
/**
* 改密码
*/
CHANGE_PASSWORD {
public int operate(Account account, AtmInput input) throws AccountException {
return account.changePassword(input.getPassword(), input.getNewpassword());
}
}
;
public abstract int operate(Account account, AtmInput input) throws AccountException;
}/**
* 账户集
* @author frankiegao123
* 2010-1-4 下午09:51:06
*/
class AccountSet {
private static AccountSet instance = new AccountSet();
private Map<String, Account> accounts;
private AccountSet() {
accounts = new HashMap<String, Account>();
}
public static AccountSet getInstance() {
return instance;
}
/**
* 添加一个账户
* @param account
* @return
* @throws AccountException
*/
public Account addAccount(Account account) throws AccountException {
if(accounts.containsKey(account.getAccountNumber())) {
throw new AccountException(account.getAccountNumber() + " 账号已经存在,不允许再添加");
}
accounts.put(account.getAccountNumber(), account);
return account;
}
public Account addAccount(String accountNumber, String password, String name,
int balance) throws AccountException {
return addAccount(new Account(accountNumber, password, name, balance));
}
/**
* 根据输入获得账户信息
*
* @param input
* @return
* @throws AccountException
*/
public Account getAccount(AtmInput input) throws AccountException {
Account account = getAccountFromSet(input.getAccountNumber());
if(account != null && account.checkPassword(input.getPassword())) {
return account;
}
throw new AccountException("账号或密码错误");
}
private Account getAccountFromSet(String accountNumber) {
return accounts.get(accountNumber);
}
}/**
* 账户异常
* @author frankiegao123
* 2010-1-4 下午09:52:21
*/
class AccountException extends Exception { private static final long serialVersionUID = 1L; public AccountException() {
super();
}
public AccountException(String message) {
super(message);
}
}/**
* 账户
*/
class Account {
private String accountNumber; // 账号
private String password; // 预留密码
private String name; // 姓名
private int balance; // 余额
public Account(String accountNumber, String password, String name,
int balance) {
super();
this.accountNumber = accountNumber;
this.password = password;
this.name = name;
this.balance = balance;
}
/**
* 检查密码的有效性
*
* @param inputPassword
* @return
*/
public boolean checkPassword(String inputPassword) {
return password.equals(inputPassword);
} public String getAccountNumber() {
return accountNumber;
} public String getPassword() {
return password;
}
public int changePassword(String oldPassword, String newPassword) throws AccountException {
if(checkPassword(oldPassword)) {
password = newPassword;
return 0;
}
throw new AccountException("账号或密码错误,无法更改密码");
} public String getName() {
return name;
} public int getBalance() {
return balance;
}
/**
* 借
* @param money
* @return
*/
public int credit(int money) {
balance += money;
return balance;
}
/**
* 贷
* @param money
* @return
* @throws AccountException
*/
public int debit(int money) throws AccountException {
if(balance >= money) {
balance -= money;
return balance;
}
throw new AccountException("交易失败,账户中余额不足");
}
public String toString() {
return "账号: " + accountNumber + ", 姓名: " + name + ", 密码: " + password + ", 余额: " + balance;
}
}