| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 558 人关注过本帖
标题:有关ATM问题
取消只看楼主 加入收藏
xy2bl
Rank: 1
等 级:新手上路
帖 子:61
专家分:2
注 册:2010-12-2
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:2 
有关ATM问题

public class ATM
{

    private Account acc;

    private File dataFile;
    private FileWriter fw;
    private BufferedWriter bw;

    private String filePath = "./data.txt";

    public ATM()
    {
        this.acc = new Account();
        try
        {
            this.dataFile = new File(this.filePath);
            if (!this.dataFile.exists())
            {
                this.dataFile.createNewFile();
            }
            this.fw = new FileWriter(this.filePath);
            this.bw = new BufferedWriter(this.fw);
        }
        catch (IOException io)
        {
            System.err.println("Cannot open file");
            io.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        new ATM().interact();
    }

    public void interact() {
  
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Account #: ");
  String temp = br.readLine();
  System.out.println("Password: ");
  String temp2 = br.readLine();
  if (!this.acc.isValid(Long.parseLong(temp.trim()), temp2.trim())) {
   System.err.println("Wrong password");
   return;
  }
  System.out.println("1. Account Inquery.");
  System.out.println("2. Withdraw");
  System.out.println("3. Deposit.");
  System.out.println("4. Change Password.");
  System.out.println("5. Export to File.");
  System.out.println("0. Exit.");
  int c = 100;
  while (c != 0) {
   String str = br.readLine();
   try {
    int c = Integer.parseInt(str.trim());
   } catch (NumberFormatException nfe) {
    System.err.println("Invalid choice");
    continue;
   }
   switch (c) {
   case 0:
    System.out.println("Thank you");
    break;
   case 1:
    System.out.println("Balance: " + this.acc.balanceInquery());
    break;
   case 2:
    System.out.println("How much? ");
    String temp = br.readLine();
    try {
     long ammount = Long.parseLong(temp.trim());
     this.acc.withdraw(ammount);
     break;
    } catch (NumberFormatException nfe) {
     System.err.println("Invalid amount");
     continue;
    }
   case 3:
    System.out.println("How much? ");
    String temp = br.readLine();
    try {
     long ammount = Long.parseLong(temp.trim());
     this.acc.deposit(ammount);
     break;
    } catch (NumberFormatException nfe) {
     System.err.println("Invalid amount");
     continue;
    }
   case 4:
    System.out.println("Old password: ");
    String temp = br.readLine();
    System.out.println("New password: ");
    String temp2 = br.readLine();
    this.acc.changePassword(temp, temp2);
    break;
   case 5:
    this.bw.write(this.acc.toString());
    break;
   default:
    break;
   }
  }
 }


}

class Account
{

    private long accNo = 123456;
    private String pass = "123456";
    private long balance = 10000;

    public Account()
    {

    }

    public boolean isValid(long accNo, String pass)
    {
        return (this.accNo == accNo) && (pass.equals(this.pass));
    }

    public void changePassword(String oldPass, String password)
    {
        if (!oldPass.equals(this.pass))
        {
            System.err.println("Wrong password.");
            return;
        }
        if (password.length < 6)
        {
            System.err.println("Password too short");
            return;
        }
        if (password.equals(this.pass))
        {
            System.err.println("Password cannot be the same.");
            return;
        }
        this.pass = password;
    }

    public long balanceInquery()
    {
        return this.balance;
    }

    public void withdraw(long amount)
    {
        if (amount > 5000 || amount < 0)
        {
            System.err.println("Withdraw limit: $0-$5000");
            return;
        }
        if ((amount % 100) != 0)
        {
            System.err.println("The amount has to be a product of 100");
            return;
        }
        long newBalance = this.balance - amount;
        if (newBalance < 0)
        {
            System.err.println("Not enough money in the account");
            return;
        }
        this.balance = newBalance;
    }

    public void deposit(long amount)
    {
        if (amount < 0)
        {
            System.err.println("Cannot deposit negative amount");
            return;
        }
        this.balance += amount;
    }

    public String toString()
    {
        return ("Account #: " + this.accNo + "\n" + "Password: " + this.pass + "\n" + "Balance: " + this.balance);
    }
}
哪位高手能帮我改改这个有错误的程序
哪位高手能帮我改改这个有错误的程序
搜索更多相关主题的帖子: private 
2011-06-07 20:25
xy2bl
Rank: 1
等 级:新手上路
帖 子:61
专家分:2
注 册:2010-12-2
收藏
得分:0 
  谢谢哈      但是要使用图形用户界面
2011-06-08 00:42
xy2bl
Rank: 1
等 级:新手上路
帖 子:61
专家分:2
注 册:2010-12-2
收藏
得分:0 
当输入给定的卡号和密码系统能登录ATM柜员机系统
2011-06-08 00:44
快速回复:有关ATM问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037448 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved