银行卡类
public class UserCard {
private String cardNo;
private String password;
private String name;
private double money;
public UserCard(String cardNo,String password,String name)
{
this.cardNo=cardNo;
this.password=password;
this.name=name;
}
public double queryBalance()//查询余额
{
return money;
}
public double deposit(double money)//存钱
{
return this.money+=money;
}
public double withdraw(double money)//取钱
{
if(money>this.money)
{
return 0;
}
return this.money-=money;
}
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类
import java.util.Scanner;
/**
* Created by ttc on 17-12-29.
*/
public class ATM {
Scanner n=new Scanner(System.in);
private UserCard[] liberary=new UserCard[50];
private int index;
private UserCard currentCard;
public void atmSystem()
{
System.out.println("欢迎来到ATM自动存取款系统");
System.out.println("请选择你要使用的业务");
System.out.println("--------------------");
System.out.println("1.>取款.");
System.out.println("2.>存款.");
System.out.println("3.>查询信息.");
System.out.println("4.>密码设置.");
System.out.println("5.>退出系统.");
int input=n.nextInt();
if(input==1)
{
System.out.println("请输入您要提取的现金:");
double withdraw=n.nextDouble();
double result=currentCard.withdraw(withdraw);
if(result==0)
{
System.out.println("余额不足");
}
else
{
System.out.println("提款成功!");
System.out.println("您当前余额为:"+currentCard.getMoney()+"元。");
}
atmSystem();
}
if(input==2)
{
System.out.println("请输入您要储存的现金:");
double deposit=n.nextDouble();
currentCard.deposit(deposit);
System.out.println("存款成功!");
System.out.println("您当前余额为:"+currentCard.getMoney()+"元。");
atmSystem();
}
if(input==3)
{
System.out.println("姓名:"+currentCard.getName());
System.out.println("卡号:"+currentCard.getCardNo());
System.out.println("余额:"+currentCard.getMoney());
atmSystem();
}
if(input==4)
{
System.out.println("请输入旧密码:");
String oldPassword=n.next();
if(oldPassword.equals(currentCard.getPassword()))
{
System.out.println("请输入新密码:");
String newPassword=n.next();
System.out.println("请再次输入新密码:");
String newPassword2=n.next();
if(newPassword.equals(newPassword2))
{
System.out.println("密码修改成功!");
currentCard.setPassword(newPassword);
atmSystem();
}
else
{
System.out.println("输入错误,请重新输入!");
atmSystem();
}
}
else
{
System.out.println("输入错误!");
atmSystem();
}
}
if(input==5)
{
System.out.println("退出程序!");
}
}
public void addUserCard(UserCard userCard)
{
if(index<liberary.length)
{
liberary[index]=userCard;
index++;
}
}
public void login()
{
System.out.println("请输入您的卡号:");
String inputID=n.next();
System.out.println("请输入您的密码:");
String password=n.next();
currentCard=checkCard(inputID,password);
if(currentCard!=null)
{
System.out.println("登陆成功!");
atmSystem();
}
else
{
System.out.println("账号或密码错误!");
}
}
public UserCard checkCard(String strID,String password)
{
for(int i=0;i<index;i++)
{
if(strID.equals(liberary[i].getCardNo())&& password.equals(liberary[i].getPassword()))
{
return liberary[i];
}
}
return null;
}
}
Test类
public class ATMtest {
public static void main(String[] args) {
UserCard user1=new UserCard("001","000000","小李");
user1.setMoney(2000);
ATM atm=new ATM();
atm.addUserCard(user1);
atm.login();
}
}
网友评论