在今天第一次做了java的关于“ATM机系统”的程序代码设计,第一次做这种“系统”水平的java代码,确实遇到了很多问题,对这些问题做了改进。
首先是注释的问题。注释在一个代码中发挥着很重要的地位,它可以让自己和别人更明白自己的代码,可以在类的前面,或者要循环什么东西的时候加上注释,在一堆代码中可以很快了解到某一块的代码发挥的作用,不至于“迷失”在自己的代码中。在这次程序代码中我犯了这个错误,当学长检查自己的代码要求看到某一块时,我一时半会也没有找到对应的位置,浪费时间的同时还消耗着其他人的耐心。
首先是注释的问题。注释在一个代码中发挥着很重要的地位,它可以让自己和别人更明白自己的代码,可以在类的前面,或者要循环什么东西的时候加上注释,在一堆代码中可以很快了解到某一块的代码发挥的作用,不至于“迷失”在自己的代码中。在这次程序代码中我犯了这个错误,当学长检查自己的代码要求看到某一块时,我一时半会也没有找到对应的位置,浪费时间的同时还消耗着其他人的耐心。
不了解老师出题的本质上要考的东西。对于开学的这次考试,老师要求的是“增”“删”“减”“查”,在做题时我个人很容易忽略这些,做的时候仅仅是当成一道题,用不成熟的方法去思考,做的时候增加了一些难度。
多用快捷键。在写代码时很多东西都是用鼠标去点击的,这样就浪费了很多时间,在以后写更多的代码时,我们大多是双手放在键盘上,操纵着键位去完成很多东西,比如在运用get方法和set方法时,可以直接手写setAccountpassword和getAccountbalance等,更好的是直接alt加上s键,选中“getter和setter方法”,再次选中全部所写的private,局可以直接出来所有的get方法和set方法了,节省了绝大部分的时间。
在输出一些数量变化的数字时,我个人没有掌握这点,比如这次的“输入密码:输入3次后卡会被锁定”下次再输出时“3”会变成“2”,这数字不能直接去写出来,可以定义那个数字为count,定义int count = 3,进行一个while或者for循环,依次使得count递减,用:System.out.println(“输入密码:“输入”+count +次后卡会被锁定”),利用定义的count来代替会变动的数据。这个“ATM机系统”中有很多需要这样做的代码,由于不能掌握的原因,就只能放弃这相关的分数了。
对length的应用。在这次考试时,题目是固定的,题目里输入三次密码,如果不对的话,就会锁卡,循环时用了“I < 2”使得次数在3次以内,试想,如果不是3次,或者其他题目里更多的数字,那么还要依次改动循环里的东西,代码量很大的话,这将是很浪费时间的事。
对于文件的运用。在这次代码考试中,我没有想到会运用上文件的知识,在上学期学C语言时,学到了一些文件知识,在当时运用的并不多,个人也没有学的很深,加上半个学期没用用到,这次遇见时相当于“0知识”。这次对文件的创建、修改、输出大小做了一点学习。
public static void main(String[] args) {
File f=new File("E:\\wenjian\\WJ.txt");创建File对象
if (f.exists()) {//判断该路径下的文件是否存在
System.out.println("查找的文件存在!");
System.out.println("文件的修改时间为:"+new Date (f.lastModified()));//输出文件的修改日期
System.out.println("文件大小:"+f.length());//输出文件的大小
} else {
System.out.println("查找的文件不存在!");
这些只是我遇到的部分问题。通过这次考试,我意识到了自己还有很多的不足,在以后的学习中会全力补上不足。
package MTA;
import java.util.Date;
import java.text.SimpleDateFormat;
public class Account {
		String accountID;    //存储学生的用户账号(由八位数字组成)
		String accountName;   //存储账户的名称
		String operatorDate;   //存储操作的时间,由十位字符组成,显示格式为“2018-09-20”
		String accountPassword;  //储存用户密码,由六位数字组成
		int accountbalance;    //账户余e
		int amount;    //操作流水金额
		int operatortype;  //操作类型
	public void setAccountid(String ID) {
		this.accountID=ID;
	}
	public void setAccountname(String name) {
		this.accountName=name;
	}
	public void setOperatordate() {
		Date now = new Date();
	    SimpleDateFormat ft = new SimpleDateFormat ("yyyy.MM.dd");
		this.operatorDate = ft.format(now);
	}
	public void setAccountpassword(String pass) {
		this.accountPassword=pass;
	}
	public void setAccountbalance(int balance) {
		this.accountbalance=balance;
	}
	public void setAmount(int am) {
		this.amount=am;
	}
	public void setOperatortype(int type) {
		this.operatortype =type;
	}
	public String getAccountid(){
		return accountID;
	}
	public String getAccountname() {
		return accountName;
	}
	public String getOperatordate() {
		return operatorDate;
	}
	public String getAccountpassword() {
		return accountPassword;
	}
	public int getaccountbalance() {
		return accountbalance;
	}
	public int getAmount() {
		return amount;
	}
	public int getoperatortype() {
		return operatortype;
	}
}
package MTA;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class AccountManager {
	public static void main(String args[]) { 
	Scanner input=new Scanner(System.in);
	ArrayList<Account> A=new ArrayList<Account>();
int select1=1,flag1=0,flag2=0,flagq=0,flag4=0,k;
String q="q";
getInformation(A);
for(;;)
{
	do
	{
	face(1);//账户对比
	flag1=id(input.next(),A);
	if(flag1==-2) 
	{face(0,12,A);
	}
	if(flag1==-1) 
	{face(0,13,A);
	}
	}while(flag1<0);
	for(k=0;k<3;k++)
	{
		face(2);//密码对比
		flag2=login(flag1,input.next(),A);
		if(flag2<0) {
			face(flag1,14,A);
			}
		if(flag2>0) {break;}
	}
	if(flag2>0&&k<3)
	{
		do
		{
			face(flag1,3,A);//主界面
		try
			{
			select1=input.nextInt();
			if(select1<=0) throw new Exception ();
			}
			catch(Exception e)
			{
				face(flag1,15,A);
			}
			switch(select1)
			{
			case 1://存款
				{
					cunkuan(flag1,A);
					if(q.equals(input.next()))
					{
						q="";
						flagq=1;
					}
				}break;
			case 2://取款
				{
					flag4=qukuan(flag1,A);
					if(flag4==2)continue;
					if(flag4==1)flagq=1;
				}break;
			case 3://转账
				{
					zhuanzhang(flag1,A);
					if(q.equals(input.next()))
					{
						q="";
						flagq=1;
					}
				}break;
			case 4://修改密码
				{
					resetpassward(flag1, A);
					if(q.equals(input.next()))
					{
						q="";
						flagq=1;
					}
				}break;
			case 5://查看
				{
					face(flag1,10,A);
					if(q.equals(input.next()))
					{
						q="";
						flagq=1;
					}
				}break;
			}
		}while(flagq!=1);
	}
	else
	{
		face(flag1,11,A);
	}
}
    }
	public static void getInformation(ArrayList<Account> A)//获得信息
	{
		try
		{
		FileReader fr=new FileReader("AccountInformation");
		Scanner r=new Scanner(fr);
		while(r.hasNext())
		{
			Account a=new Account();
			a.setAccountid(r.next());
			a.setAccountpassword(r.next());
			a.setAccountname(r.next());
			a.setAccountbalance(r.nextInt());
			A.add(a);
		}
		fr.close();
		r.close();
		}
		catch(Exception e)
		{
			System.out.println("文件写入失败");
		}
	}
	public static void setInformation(ArrayList<Account> A)//写入信息
	{
		try
		{
			int i=0;
			FileWriter fw=new FileWriter("AccountInformation");
			PrintWriter pw=new PrintWriter(fw);
			for(i=0;i<A.size();i++)
			{
				pw.println(A.get(i).getAccountid()+" "+A.get(i).getAccountpassword()+" "+ A.get(i).getAccountname()+" "+A.get(i).getaccountbalance());
			}
			pw.flush();
			pw.close();
			fw.close();
		}
		catch(Exception e)
		{
			System.out.println("文件写入失败");
		}
	}
	public static String getList(int i,ArrayList<Account> A)      //读取流水
	{
		try
		{
			FileReader fr=new FileReader("AccountList");
			Scanner in=new Scanner(fr);
			String iid=A.get(i).getAccountid();
			String id=new String();
			String s=new String();
			String sum="";
			int z=1;
			while(in.hasNextLine())
			{
				id=in.next();
				if(iid.equals(id))
				{
					s=id+" "+in.nextLine()+"\n";
					sum=sum+"( "+z+" )\t"+s;
					z++;
				}
			}
			fr.close();
			in.close();
			return sum;
		}
		catch(Exception e)
		{
			System.out.println("文件读取失败");
			return "";
		}
	}
	public static void setList(int i,ArrayList<Account> A)     //写入流水
	{
		try
		{
			Account a=A.get(i);
			FileWriter fw=new FileWriter("AccountList",true);
			PrintWriter pw=new PrintWriter(fw);
			pw.println(a.getAccountid()+" "+a.getAccountname()+" "+ a.getOperatordate()+" "+a.getoperatortype()+" "+a.getAmount());
			pw.flush();
			pw.close();
			fw.close();
		}
		catch(Exception e)
		{
			System.out.println("文件写入失败");
		}
	}
	public static void zhuanzhang(int i,ArrayList<Account> A)
	{
		Account a=new Account();
		Account b=new Account();
		Scanner input=new Scanner(System.in);
		String z="",x="";
		int t=0,m=0,f1=0;
		while(f1==0)
		{
			face(i,6,A);
			z=input.next();
			if((t=id(z,A))>=0)//判断
			{
				try
				{
					face(i,7,A);
					m=input.nextInt();
					if(m<=0)throw new Exception();
				}
				catch(Exception e)
				{
					face(i,13,A);
					f1=1;
					continue;
				}
				if(f1==0)//输入正确 开始转账
				{
					a=A.get(i);
					b=A.get(t);
					a.setAmount(-m);
					b.setAmount(m);
					A.set(i, a);
					face(i,t,1,A);
					x=input.next();
					if(x.equals("Y")==true)
					{
						a.setOperatordate();
						a.setOperatortype(3);
						b.setOperatordate();
						b.setOperatortype(3);
						if(A.get(i).getaccountbalance()>=m)
						{
						a.setAccountbalance(a.getaccountbalance()-m);//取款
						A.set(i, a);
						setList(i, A);
						b.setAccountbalance(b.getaccountbalance()+m);//存款
						A.set(t, b);
						setList(t, A);
						setInformation(A);
						face(i,t,2,A);
						break;
						}
						else
						{
							face(i,19,A);
							break;
						}
					}
					else if(x.equals("N")==true)
					{
						break;
					}
					else
					{
						face(i,20,A);
					}
				}
			}
		}
	}
	public static void resetpassward(int i,ArrayList<Account> A)
	{
		Account a=new Account();
		Scanner input=new Scanner(System.in);
		String s1="",s2="",s3="";
		int f1=0,f2=0,t=0;
		face(i,8,A);
		s1=input.next();
		f1=login(i,s1,A);//密码正确
		if(f1==1)
		{
			s2=input.next();
			s3=input.next();
			if(s2.equals(s3))//相等
			{
				if(s2.length()!=6)//格式
				{
					f2= -1;
					face(i,16,A);
				}
				for(t=0;t<s2.length();t++)//格式
				{
					if(s2.charAt(t)<‘0‘||s2.charAt(t)>‘9‘)
					{
						f2= -1;
						face(i,16,A);
					}
				}
				if(f2==0)
				{
					a=A.get(i);
					a.setAmount(0);
					a.setOperatortype(4);
					a.setOperatordate();
					a.setAccountpassword(s2);
					A.set(i, a);
					setList(i, A);
					setInformation(A);
					face(i,9,A);
				}
			}
			else
			{
				face(i,17,A);
			}
		}
		else
		{
			face(i,18,A);
		}
	}
	public static int qukuan(int i,ArrayList<Account> A)
	{
		Account a=new Account();
		Scanner input=new Scanner(System.in);
		int f=0,m=0;
		face(i,5,A);
		do
		{
			try
			{
				f=input.nextInt();
				if(f<=0)throw new Exception();
			}
			catch(Exception e)
			{
				face(i,15,A);
				continue;
			}
			switch(f)
			{
			case 1:m=100;break;
			case 2:m=500;break;
			case 3:m=1000;break;
			case 4:m=1500;break;
			case 5:m=2000;break;
			case 6:m=5000;break;
			case 7:
			{
				try
				{
					m=input.nextInt();
					if(m<=0)throw new Exception();
				}
				catch(Exception e)
				{
					face(i,15,A);
					continue;
				}
			};break;
			case 8:return 1;
			case 9:return 2;
			}
		}while(f>8);
		if(A.get(i).getaccountbalance()>=m)
		{
		a=A.get(i);
		a.setAccountbalance(a.getaccountbalance()-m);
		a.setAmount(m*-1);
		a.setOperatortype(2);
		a.setOperatordate();
		A.set(i, a);
		setList(i, A);
		setInformation(A);
		face(i,22,A);
		}
		else
		{
			face(i,19,A);
		}
		return 0;
	}
	public static void cunkuan(int i,ArrayList<Account> A)
	{
		Account a=new Account();
		Scanner input=new Scanner(System.in);
		int m=0,f=0;
		face(i,4,A);
		try
		{
			m=input.nextInt();
			if(m<=0) throw new Exception();
		}
		catch(Exception e)
		{
			face(i,15,A);
			f= -1;
		}
		a=A.get(i);
		a.setAccountbalance(a.getaccountbalance()+m);
		a.setAmount(m);
		a.setOperatortype(1);
		a.setOperatordate();
		A.set(i, a);
		setList(i, A);
		setInformation(A);
		if(f==0)
		{
			face(i,21,A);
		}
	}
	public static int id(String accountID,ArrayList<Account> A)
	{
		int i=0,t=0;
		if(accountID.length()!=8)//格式
		{
			return -2;
		}
		for(t=0;t<accountID.length();t++)//格式
		{
			if(accountID.charAt(t)<‘0‘||accountID.charAt(t)>‘9‘)
			{
				return -2;
			}
		}
		for(i=0;i<A.size();i++)//查找 i 为账户序号
		{
			if(A.get(i).getAccountid().equals(accountID)==true)
			{
				return i;
			}
		}
		return -1;
	}
	public static int login(int i,String accountpassword,ArrayList<Account> A)
	{
		int t=0;
		if(accountpassword.length()!=6)//格式
		{
			return -1;
		}
		for(t=0;t<accountpassword.length();t++)//格式
		{
			if(accountpassword.charAt(t)<‘0‘||accountpassword.charAt(t)>‘9‘)
			{
				return -1;
			}
		}
		if(A.get(i).getAccountpassword().equals(accountpassword)==true)//正误
		{
			return 1;
		}
		else
		{
			return -1;
		}
	}
	public static void face(int select)
	{
		System.out.println("**********************************************");
		System.out.println("\t欢迎使用中国工商银行自助柜台系统");
		System.out.println("**********************************************");
		switch(select)
		{
		case 1://输入账号
		{
			System.out.println("\t\t请输入你的账号");
		}break;
		case 2://输入密码
		{
			System.out.println("\t\t请输入你的密码");
		}break;
		}
	}
	public static void face(int i,int select,ArrayList<Account> A)
	{
		System.out.println("**********************************************");
		System.out.println("        欢迎"+A.get(i).getAccountname()+"使用中国工商银行自助柜台系统");
		System.out.println("**********************************************");
		switch(select)
		{
		case 3://主界面
		{
			System.out.println("\t\t1、  存款");
			System.out.println("\t\t2、  取款");
			System.out.println("\t\t3、  转账汇款");
			System.out.println("\t\t4、  修改密码");
			System.out.println("\t\t5、  查询余额");
		}break;
		case 4://存款
		{
			System.out.println("\t\t请输入存款金额");
		}break;
		case 5://取款
		{
			System.out.println("\t"+A.get(i).getAccountname()+"账户当前账户每日可以支取2万元。");
			System.out.println("\t\t1、100元");
			System.out.println("\t\t2、500元");
			System.out.println("\t\t3、1000元");
			System.out.println("\t\t4、1500元");
			System.out.println("\t\t5、2000元");
			System.out.println("\t\t6、5000元");
			System.out.println("\t\t7、其他金额");
			System.out.println("\t\t8、退卡");
			System.out.println("\t\t9、返回");
		}break;
		case 6://输入转账账户
		{
			System.out.println("\t\t请输入转账账户:");
		}break;
		case 7://输入转账金额
		{
			System.out.println("\t\t请输入转账金额:");
		}break;
		case 8://修改密码
		{
			System.out.println("\t\t请输入当前密码:");
			System.out.println("\t\t请输入修改密码:");
			System.out.println("\t\t请输入确认密码:");
		}break;
		case 9://修改密码成功
		{
			System.out.println("\t\t当前账户修改密码成功");
		}break;
		case 10://显示余额和操作流程
		{
			System.out.println("\t\t当前账户余额为  "+A.get(i).getaccountbalance()+"  元。");
			System.out.println("\t\t当前账户清单信息为:");
			System.out.print(getList(i, A));
			Account a=A.get(i);
			a.setAmount(0);
			a.setOperatortype(5);
			a.setOperatordate();
			setList(i, A);
		}break;
		case 11:
		{
			System.out.println("\t该账号三次录入密码错误,该卡已被系统没收,请与工商银行及时联系。");
		}break;
		case 12:
		{
			System.out.println("\t\t该卡不是工商卡");
		}break;
		case 13:
		{
			System.out.println("\t\t该账号不存在");
		}break;
		case 14:
		{
			System.out.println("\t\t密码录入错误");
		}break;
		case 15:
		{
			System.out.println("\t\t请输入正整数");
		}break;
		case 16:
		{
			System.out.println("\t\t密码格式错误");
		}break;
		case 17:
		{
			System.out.println("\t修改密码与确认密码不一致");
		}break;
		case 18:
		{
			System.out.println("\t\t当前密码录入错误");
		}break;
		case 19:
		{
			System.out.println("\t\t账户余额不足");
		}break;
		case 20:
		{
			System.out.println("\t\t请输入\"Y\"或\"N\"");
		}break;
		case 21:
		{
			System.out.println("\t当前账户存款款"+A.get(i).getAmount()+"成功。");
			System.out.println("\t当前账户余额为  "+A.get(i).getaccountbalance()+"  元。");
		}break;
		case 22:
		{
			System.out.println("\t当前账户取款"+A.get(i).getAmount()+"成功。");
			System.out.println("\t当前账户余额为  "+A.get(i).getaccountbalance()+"  元。");
		}break;
		}
	}
	public static void face(int i,int t,int select,ArrayList<Account> A)
	{
		String s=A.get(t).getAccountname();
		String star=s.substring(1);
		star="*"+star;
		System.out.println("*********************************************");
		System.out.println("        欢迎"+A.get(i).getAccountname()+"使用中国工商银行自助柜台系统");
		System.out.println("**********************************************");
		switch(select)
		{
		case 1://确认
		{
			System.out.println("\t请确认是否向"+star+"转账"+A.get(i).getAmount()*-1+"元");
		}break;
		case 2:
		{
			System.out.println("\t当前账户成功向"+star+"转账"+A.get(i).getAmount()*-1+"元");
			System.out.println("\t当前账户余额为  "+A.get(i).getaccountbalance()+"  元。");
		}break;
		}
	}
}

原文:https://www.cnblogs.com/SirNie/p/13715342.html