ueser
public class UserCard {
private String cardNo;//卡號
private String password;//密碼
private String name;//戶名
private double money;//賬戶余額
public String toString()
{
return "卡號是:"+cardNo+"戶名是:"+name + "余額是:"+money;
}
//存款
public double deposit(double money)
{
this.money += money;
return this.money;
}
//取款
public double withdraw(double money)
{
//判斷余額
if(money > this.money)
{
return -1;
}
this.money -= money;
return this.money;
}
//構(gòu)造方法
public UserCard(String cardNo, String password,String name, double money)
{
this.money = money;
this.cardNo = cardNo;
this.name = name;
this.password = password;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getPassword() {
return password;
}
//修改密碼
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
ATM
public class ATM {
//當(dāng)前插入的卡
private UserCard currentUserCard = null;
private int index = 0;//數(shù)組的索引,下一個應(yīng)該放到數(shù)組中的位置
//ATM機(jī)可以識別的卡的集合
private UserCard[] userCards = new UserCard[100];
//向ATM中添加可以識別的卡片信息(讓ATM機(jī)認(rèn)這張卡)
public void addUserCard(UserCard userCard)
{
if(index < userCards.length)
{
userCards[index] = userCard;
index++;
}
}
//輸出atm首頁界面
public void print()
{
Scanner scanner = new Scanner(System.in);
while (true)
{
System.out.println("----------------------");
System.out.println("歡迎使用睿道java25出品的模擬ATM程序.");
System.out.println("----------------------");
System.out.println(" 1.>取款.");
System.out.println(" 2.>存款.");
System.out.println("3.>查詢信息.");
System.out.println("4.>退出系統(tǒng).");
int command = scanner.nextInt();
switch (command)
{
case 1:
{
System.out.println("請輸入取款金額");
double value = scanner.nextDouble();
double result = currentUserCard.withdraw(value);
if(result == -1)
{
System.out.println("余額不足");
}
else
{
System.out.println("取款成功");
System.out.println("卡內(nèi)余額為" + result);
}
break;
}
case 2:
{
System.out.println("請輸入存款金額");
double value = scanner.nextDouble();
double result = currentUserCard.deposit(value);
System.out.println("存款成功");
System.out.println("卡內(nèi)余額為" + result);
break;
}
case 3:
{
System.out.println(currentUserCard);
break;
}
case 4:
{
System.out.println("程序退出");
return;
}
default:
{
break;
}
}
}
}
//插卡
public void inputCard()
{
System.out.println("請輸入您的卡號:");
Scanner scanner = new Scanner(System.in);
String strNo = scanner.next();
System.out.println("請輸入您的密碼:");
String password = scanner.next();
currentUserCard = checkCard(strNo,password);//檢查卡
if(currentUserCard != null)
{
print();
}
else
{
System.out.println("賬號或密碼錯誤");
}
}
//檢查卡是否合法,如果用戶名密碼對,返回那張有效的卡
public UserCard checkCard(String strNo, String password)
{
//遍歷有效的卡的數(shù)組,檢查是否有和傳入用戶名,密碼一致的卡存在
for(int i = 0; i < index; i++)
{
if(strNo.equals(userCards[i].getCardNo()) && password.equals(userCards[i].getPassword()))
{
return userCards[i];
}
}
return null;
}
}