java语言面向对象实现atm机功能 卡 属性: 卡号 密码 所属人 余额 类型 所属银行... atm机 属性:所属银行 型号 单笔取款限额... 方法:验证密码 存款 取款 查询 转账 修改密码 运行 测试类
时间: 2024-02-15 20:57:22 浏览: 154
atm.zip_atm_atm java图形_site:www.pudn.com_每次取款
Card类:
public class Card {
private String cardNum;
private String password;
private String owner;
private double balance;
private String type;
private String bank;
public Card(String cardNum, String password, String owner, double balance, String type, String bank) {
this.cardNum = cardNum;
this.password = password;
this.owner = owner;
this.balance = balance;
this.type = type;
this.bank = bank;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getBank() {
return bank;
}
public void setBank(String bank) {
this.bank = bank;
}
}
ATM类:
import java.util.Scanner;
public class ATM {
private String bank;
private String model;
private double singleWithdrawLimit;
public ATM(String bank, String model, double singleWithdrawLimit) {
this.bank = bank;
this.model = model;
this.singleWithdrawLimit = singleWithdrawLimit;
}
public String getBank() {
return bank;
}
public void setBank(String bank) {
this.bank = bank;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getSingleWithdrawLimit() {
return singleWithdrawLimit;
}
public void setSingleWithdrawLimit(double singleWithdrawLimit) {
this.singleWithdrawLimit = singleWithdrawLimit;
}
// 验证密码
public boolean verifyPassword(Card card, String password) {
return card.getPassword().equals(password);
}
// 存款
public void deposit(Card card, double amount) {
card.setBalance(card.getBalance() + amount);
System.out.println("存款成功,当前余额为:" + card.getBalance());
}
// 取款
public void withdraw(Card card, double amount) {
if (amount > singleWithdrawLimit) {
System.out.println("超过单笔取款限额,无法取款");
} else if (amount > card.getBalance()) {
System.out.println("余额不足,无法取款");
} else {
card.setBalance(card.getBalance() - amount);
System.out.println("取款成功,当前余额为:" + card.getBalance());
}
}
// 查询
public void inquire(Card card) {
System.out.println("卡号:" + card.getCardNum());
System.out.println("所属人:" + card.getOwner());
System.out.println("余额:" + card.getBalance());
System.out.println("类型:" + card.getType());
System.out.println("所属银行:" + card.getBank());
}
// 转账
public void transfer(Card fromCard, Card toCard, double amount) {
if (amount > fromCard.getBalance()) {
System.out.println("余额不足,无法转账");
} else {
fromCard.setBalance(fromCard.getBalance() - amount);
toCard.setBalance(toCard.getBalance() + amount);
System.out.println("转账成功,当前余额为:" + fromCard.getBalance());
}
}
// 修改密码
public void changePassword(Card card, String newPassword) {
card.setPassword(newPassword);
System.out.println("密码修改成功");
}
// 运行
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入卡号:");
String cardNum = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
Card card = new Card(cardNum, password, "张三", 1000, "储蓄卡", bank);
while (true) {
System.out.println("请选择操作:1.存款 2.取款 3.查询 4.转账 5.修改密码 6.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入存款金额:");
double depositAmount = scanner.nextDouble();
deposit(card, depositAmount);
break;
case 2:
System.out.println("请输入取款金额:");
double withdrawAmount = scanner.nextDouble();
withdraw(card, withdrawAmount);
break;
case 3:
inquire(card);
break;
case 4:
System.out.println("请输入对方卡号:");
String toCardNum = scanner.next();
System.out.println("请输入转账金额:");
double transferAmount = scanner.nextDouble();
Card toCard = new Card(toCardNum, "123456", "李四", 500, "储蓄卡", bank);
transfer(card, toCard, transferAmount);
break;
case 5:
System.out.println("请输入新密码:");
String newPassword = scanner.next();
changePassword(card, newPassword);
break;
case 6:
System.out.println("退出系统");
return;
default:
System.out.println("请输入正确的操作编号");
break;
}
}
}
}
测试类:
public class Test {
public static void main(String[] args) {
ATM atm = new ATM("工商银行", "ATM001", 5000);
atm.run();
}
}
阅读全文