atm机编程

import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;

import javax.swing.JOptionPane;

public class Atm {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,"欢迎来到**银行!");
boolean isFind=login();
if(isFind==false){
JOptionPane.showMessageDialog(null, "非法用户!");
System.exit(0);
}
while(true){
String s=JOptionPane.showInputDialog(null,"1.存款\n2.取款\n3.查询\n4.改密\n5.退出");
switch(s){
case "1":
savaMoney();
break;
case "2":
getMoney();
break;
case "3":
showMoney();
break;
case "4":
revirse();
break;
case "5":
System.exit(0);
break;
}
}

}
/**
* 登陆
* @return
*/
public static boolean login(){
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}
String name=pro.getProperty("userName");
String pwd=pro.getProperty("password");
for(int i=0;i<3;i++){
String n=JOptionPane.showInputDialog(null,"请输入用户名:");
String p=JOptionPane.showInputDialog(null,"请输入密码:");
if(n.equals(name)&&p.equals(pwd)){
return true;
}else {JOptionPane.showMessageDialog(null, "密码或者用户名错误");}
}
return false;
}
/**
* 存款
*/
public static void savaMoney(){
String m=JOptionPane.showInputDialog(null,"请输入存款金额");
int num=Integer.parseInt(m);
if(num%100!=0||num>10000){
JOptionPane.showMessageDialog(null, "输入不符合规定!");
}else {
int item=liveMoney();
num+=item;
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}
pro.setProperty("money",""+num);
try{
pro.store(new FileWriter("Pop.txt"),null);
}catch (Exception e){
System.out.println("文件没找到!");
}
}

}
/**
* 取款
*/
public static void getMoney(){
String s=JOptionPane.showInputDialog(null,"请输入要取款的金额");
int n=Integer.parseInt(s);
int item=liveMoney();
if(n%100!=0||n>item||n>5000){
JOptionPane.showMessageDialog(null, "输入有误!");
}else {
item-=n;
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}
pro.setProperty("money",""+item);
try{
pro.store(new FileWriter("Pop.txt"),null);
}catch (Exception e){
System.out.println("文件没找到!");
}
}

}
/**
* 查询
*/
public static void showMoney(){
int item=liveMoney();
JOptionPane.showMessageDialog(null, "您的余额为:"+item);
}
/**
* 改密
*/
public static void revirse(){
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}String p=pro.getProperty("password");
String s=JOptionPane.showInputDialog(null,"请输入旧密码:");
if(s.equals(p)){
String s1=JOptionPane.showInputDialog(null,"请输入新密码:");
String item=JOptionPane.showInputDialog(null,"请确认新密码:");
if(s1.equals(item)){

try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}
pro.setProperty("password",s1);
try{
pro.store(new FileWriter("Pop.txt"),null);
}catch (Exception e){
System.out.println("文件没找到!");
}
}else {
JOptionPane.showMessageDialog(null, "两次输入的密码不一致!");
}
}else {
JOptionPane.showMessageDialog(null, "旧密码输入不正确!");
}
}
/**
* 文件余额调用
* @return
*/
public static int liveMoney(){
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}String money=pro.getProperty("money");
int num=Integer.parseInt(money);
return num;
}

}

转载于:https://www.cnblogs.com/YGZ-321/p/6754795.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 账户类(满分50分) 版本1:满分10 分 设计Account1 类,包含: ■ 一个名为 id 的int 类型的私有数据域(默认值为 0),长度为 6 位。 ■ 一个名为 balance的double 类型的私有数据域(默认值为 0)。 ■ 一个名为 annualInterestRate 的double 类型的私有数据域存储当前利率(默认值为 0)。 假设所有的账户都有相同的利率。 ■ 一个名为 dateCreated 的Date 类型的私有数据域存储账户的开户日期。 ■ 一个能创建默认账户的无参构造方法。 ■ 一个能创建带特定 id 和初始余额的构造方法,初始余额不能为负数。 ■ id 、balance和annualInterestRate 的访问器和修改器。 ■ dateCreated 的访问器。 ■ 一个名为 getMonthlyInterestRate 的方法返回月利率。 ■ 一个名为 withDraw 的方法从账户提取特定金额。 ■ 一个名为 deposit 的方法向账户存人特定金额。 ■ double 类型的数据域保留 2 位小数。 ■ 成员方法和数据域应进行基本的合理性检查。 设计测试类 ATMMachine1: ■ 创建一个有 100 个账户的数组,其 id 为0,1,2,...99, 并初始化收支为 1000 美元。 ■ 主菜单如下(可参考教材中文版 P296或英文版 P367): Main menu 1: check balance 2: withdraw 3: deposit 版本2:满分20 分 扩展Account1 类为Account2 类: ■ Account2 类继承 Account1 类。 ■ 为Account2 类新增一个名为 password 的String 类型的私有数据域存储账号密码。 password 只能为字母或数字,长度不能小于 6 且不能大于 10。密码显示时为*******。 ■ 为Account2 类新增一个名为 name的String 类型的私有数据域存储客户名字。 ■ 为Account2 类新增一个名为 transactions 的ArrayList 类型的新数据域,其为客户存 储交易记录。这要求新建一个名为 Transaction 的类,类的定义请参照教材中文版 P327或英 文版P404。每笔交易都是 Transaction 类的一个实例。 ■ 新增一个带初始余额的构造方法,其 id 随产生,但不能与当前系统的 id 重复。 若初始余额的参数为负数,则抛出一个自定义异常并在当前构造方法中进行处理。 ■ 重写方法 withDraw ,要求支取的金额为 100 的整数倍,并且当日支取金额不能超过 5000,支取金额不允许透支。每进行一次操作应向 transactions 数组线性表添加一笔交易。 ■ 重写方法 deposit ,要求每进行一次操作应向 transactions 数组线性表添加一笔交易。 ■ 新增一个方法 changePassword ,只有旧密码正确,新密码符合要求,且两次输入相 同的情况下才可以成功修改密码 设计测试类 ATMMachine2,其主菜单如下(可参考教材中文版 P296或英文版 P367): Main menu 0:create a account 1: check balance 2: withdraw 3: deposit 4:details of the transaction 5: change password 6:exit ■ 若用户选择新建一个账号,则应提示用户输入账号 password 、balance 和 annualInterestRate ,其中 id 随产生。新产生的账户应序列化到名为 accounts.dat 的文件中。 所有账户只能通过这种方式产生。 ■ 所有用户操作结果应同步到 accounts.dat 文件中相应账户中。 ■ 所有用户操作应有友好、简介的提示语。 版本3:满分20 分 请参照银行的 AT M界面,在 Account2 类的基础上开发一个 GUI 界面的AT M系统。 要求界面应模拟小键盘,并且账户信息读、写于文件 accounts.dat 。
好的,下面是一个简单的Java GUI程序,实现了ATM界面: ```java import javax.swing.*; import java.awt.*; public class ATMGUI extends JFrame{ private JLabel welcomeLabel; private JLabel idLabel; private JLabel passwordLabel; private JTextField idField; private JPasswordField passwordField; private JButton loginButton; private JButton cancelButton; public ATMGUI() { init(); this.setTitle("ATM界面"); this.setSize(400, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void init() { // 欢迎语 welcomeLabel = new JLabel("欢迎使用ATM"); welcomeLabel.setFont(new Font("微软雅黑", Font.BOLD, 24)); welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER); // 用户名和密码输入框 idLabel = new JLabel("账号:"); passwordLabel = new JLabel("密码:"); idField = new JTextField(20); passwordField = new JPasswordField(20); // 登录和取消按钮 loginButton = new JButton("登录"); cancelButton = new JButton("取消"); // 设置布局 this.setLayout(new GridLayout(4, 2, 10, 10)); this.add(welcomeLabel); this.add(new JLabel("")); this.add(idLabel); this.add(idField); this.add(passwordLabel); this.add(passwordField); this.add(loginButton); this.add(cancelButton); } public static void main(String[] args) { new ATMGUI(); } } ``` 这个程序会生成一个ATM的登录界面,包含一个欢迎语、一个账号输入框、一个密码输入框以及一个登录按钮和一个取消按钮。你可以在代码中进一步完善登录逻辑和其他功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值