java开学考试有感以及源码

时间:2023-12-26 13:11:43

一.感想

Java开学测试有感

九月二十号,王老师给我们上的第一节java课,测试。

说实话,不能说是十分有自信,但还好,直到看见了开学测试的题目,之前因为已经做过了王老师发的16级的题目,所以当时还是有些底气的。遇到题目的时候是的确蒙了。因为小学期已经做过学生的通讯录。所以16级的试题显得好做一些,考试前学委说可能老师会加上文件操作,所以自己也相应的添加了文件的读取和写入操作,但是王老师显然是*湖,觉得这些题目还不够有挑战性,换成了现在的ATM程序。

变量的定义,按照老师的说法就是送分,的确,这算是基本的操作了,不过令人尴尬的是,我在用eclipse的对成员变量进行构造set和get方法时也出现了问题。但是在后来的编程过程当中,这个问题也是无缘无故地消失了,我也是莫名其妙。

整个程序基本上可以说是建立在文件的基础上的,除了开头的操作,大多都要用到文件功能。

再说具体的功能,第一个账号的辨别,现在对我来说,这个功能的实现已经很简单了,但是当时确实花了不少的时间去解决这个问题,导致当时的进度一直无法推进,因为自己的文件功能还算比较陌生的,识别账号的关键就是读取文件之后对于没一行的数据进行遍历,对比,从而来达到这个功能。这个地方对比的时候,必须要注意不可以直接用==来对比,因为两者的地址不同,对比的结果肯定是不一样的,可以用equal方法来解决这个问题。

对于密码的功能的实现,我是在之前账号的基础上,对账号的下一行数据保存到对象的密码成员数据中,然后在输入密码时和密码进行对比。

目前程序已经完成,所有的功能大致已经都可以很好的实现,但是还有地方有欠缺,就是账号和密码这连个功能是分开实现的,没有连成一个整体,也就是说,账号功能无论是否完成都会进行密码阶段,我个人觉得这连个功能和之后的五个功能都要在一个大的while循环中完成就可以解决问题,或者是思考如何返回界面这个问题,当然我也觉得这个问题是本程序的难点之一,可惜自己现在还没有完成。

存款等功能是在while循环中做出来的,正整数可以用除余的方法来实现。下面的几个程序都能够很正常的实现。我觉得大多数同学也觉得这个地方比较简单,最重要问题的就是文件的读取和写入。

在修改密码的功能中就需要文件的写入,因为我在密码功能实现的时候是把账号的下一行数值当做密码赋值给成员数据的,所以在写入新密码的时候也需要将新密码写在账号的后一行,这样才能确保程序下一次运行时密码和账号匹配。所以我的解决方案是先将文件中的数据读到一个String数组当中,然后再次覆盖写入,在覆盖写入的时候对写入的进行辨别,如果是当前账号,就在账号后面先插入修改完成的密码,之后再将剩下的数据依次插入即可完成。

最后要求完成的功能就是对操作过程的记录,这个即使简单的对文件的写入,唯一的尴尬就是我没有办法实现排序,即不能1.2.这样。。。。

到这里,所有功能的讲解已全部结束,以上讲解了我的解决方案还有自己程序的不足之处。

很显然,自己的这次开学考试并不理想,从ATM这个程序已经看出了自己的不住,以及需要改进之处。文件的读写操作需要联系,自己还是太生疏,还有一些小的地方还需改进和完善。在软件工程专业,我还有很长的路要走,也感谢老师,让我明白自己的编程技术是多么的菜。

在查看别人的博客园资料时,看到别人引用了老子的话,觉得很中二,但是也感觉有那么点认同:道可道,非常道;名可名,非常名。每个人都需要领悟,不管是程序还是人生,我现在还是有些迷茫。对人生还是没有什么太大的认识,我希望未来我也可以领悟。。。。

二.源码

 //信1705-3  20173579  陈阳
package atm0;
import java.io.IOException;
import java.util.*;
public class mynode { public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Scanner in =new Scanner(System.in);
jiemian dang0=new jiemian();
int count=0;
atmbianliang dang1=new atmbianliang();
atmbianliang dang2=new atmbianliang();
while(true)
{
dang0.jiemiankaishi();
String Id=in.nextLine();
dang1.setAccountID(Id);
dang1.bianbie();
if(dang1.bianbie()==true)
{
dang1.bianbie2();
break;
}
count++;
if(count>2)
{
System.out.println("该账号三次录入错误,该卡已被系统没收,请与工行及时联系处理");
break;
}
}
while(true)
{
dang0.shumima();
String shupassword=in.nextLine();
if(dang1.bianbiemima1(shupassword)==true)
{
String password=dang1.getAccountpassword();
if((password.equals(shupassword)))
break;
}
System.out.println("密码输入错误,请重新输入");
count++;
if(count>2)
{
System.out.println("该账号三次录入密码错误,该卡已被系统没收,请与工行及时联系处理");
break;
}
}
String xuanze1="1";
String xuanze2="2";
String xuanze3="3";
String xuanze4="4";
String xuanze5="5";
String xuanze6="q";
String xuanze; while(true)
{
dang1.shuzijiemian();
xuanze=in.nextLine();
if((xuanze.equals(xuanze1)))
{
dang1.cunkuan();
int cunkuanshuzhi=in.nextInt();
if(cunkuanshuzhi%100!=0)
{
System.out.println("输入金额有误,返回操作界面");
}
else
{
dang1.cunkuanshezhi(cunkuanshuzhi);
dang1.cunkuanchengongjiemian();
}
}
else if((xuanze.equals(xuanze2)))
{
dang1.qukuanjiemian();
int qukuanxuanxiang=in.nextInt();
switch(qukuanxuanxiang)
{
case 1:
{
dang1.bianbieluru(100);
}break;
case 2:
{
dang1.bianbieluru(500);
}break;
case 3:
{
dang1.bianbieluru(1000);
}break;
case 4:
{
dang1.bianbieluru(1500);
}break;
case 5:
{
dang1.bianbieluru(2000);
}break;
case 6:
{
dang1.bianbieluru(5000);
}break;
case 7:
{
System.out.print("请输入其他金额:");
int luruyue=in.nextInt();
dang1.bianbieluru(luruyue);
}break;
case 8:
{
System.out.println("感谢使用!");
System.exit(0);
}break;
case 9:
{
break;
}
}
}
else if((xuanze.equals(xuanze3)))
{
dang1.zhuanzhangjiemian();
String zhuanzhangzhanghu=in.nextLine();
dang1.zhuanzhangzhanghupanding(zhuanzhangzhanghu);
dang2.bianbie();
if(dang2.bianbie()==true)
{
dang2.bianbie2();
dang1.zhuanzhangjinejiemian();
int zhuanzhangjine=in.nextInt();
if(dang1.zhuanzhangbianbie(zhuanzhangjine, zhuanzhangzhanghu)==true)
dang2.zhnaghu2zenyue(zhuanzhangjine);
}
}
else if((xuanze.equals(xuanze4)))
{
dang1.xiugaimimajiemian();
String shurumima=in.nextLine();
dang1.bianbiemima(shurumima);
dang1.mimacunruwenjian();
dang1.mimachongxinxieru(); }
else if((xuanze.equals(xuanze5)))
{
dang1.chaxungongneng();
}
else if((xuanze.equals(xuanze6)))
{
}
} }
} //信1705-3 20173579 陈阳
package atm0;
import java.io.IOException;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.Reader;
import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
public class atmbianliang {
Scanner in =new Scanner(System.in);
private static String accountID;
private String accountname;
private String operatedate;
private int operatetype;
private static String accountpassword;
public String getAccountname() {
return accountname;
}
private int accountbalance;
private int amount;
static String[] content2=new String[6];
public atmbianliang()
{
accountID=null;
accountname=null;
operatedate=null;
operatetype=0;
accountpassword=null;
accountbalance=0;
amount=0;
}
public void yue()
{
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 当前账户存款操作成功。 ");
System.out.print(" 当前账户余额为:");
System.out.print(accountbalance);
System.out.print("元");
System.out.println("***************************************************************"); }
public static String getAccountID() {
return accountID;
}
public void setAccountID(String accountID) {
this.accountID = accountID;
} public String getOperatedate() {
return operatedate;
}
public void setOperatedate(String operatedate) {
this.operatedate = operatedate;
}
public int getOperatetype() {
return operatetype;
}
public void setOperatetype(int operatetype) {
this.operatetype = operatetype;
}
public String getAccountpassword() {
return accountpassword;
}
public void setAccountpassword(String accountpassword) {
this.accountpassword = accountpassword;
}
public int getAccountbalance() {
return accountbalance;
}
public void setAccountbalance(int accountbalance) {
this.accountbalance = accountbalance;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public boolean bianbie()throws IOException //辨别账户位数
{
if(accountID.length()!=8)
{
System.out.println("该卡不是工行卡");
return false;
}
else
return true;
}
public boolean bianbiemima1(String ps)throws IOException //辨别密码位数
{
if(ps.length()!=6)
{
System.out.println("密码格式错误");
return false;
}
else
return true;
}
public void bianbie2()throws Exception //辨别账户信息
{
String fileContent = readFileContent("D:/accountinformation.txt");
System.out.println(fileContent);
}
private static String readFileContent(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = "";
StringBuilder sb = new StringBuilder();
String Idjia1=atmbianliang.getAccountID();
while(content != null){
content = bf.readLine();
if((Idjia1.equals(content)))
{
System.out.println("账户正确");
String zhanghumima=bf.readLine();
accountpassword=zhanghumima;
}
if(content == null){
break;
}
// sb.append(content.trim()); } bf.close();
System.out.println("识别账户结束");
return sb.toString();
}
public void bianbie001()throws Exception //辨别转账账户
{
String fileContent2 = readFileContent2("D:/accountinformation.txt");
System.out.println(fileContent2);
}
private static String readFileContent2(String fileName) throws IOException { File file = new File(fileName); BufferedReader bf = new BufferedReader(new FileReader(file)); String content = "";
StringBuilder sb = new StringBuilder();
String Idjia1=atmbianliang.getAccountID();
while(content != null){
content = bf.readLine();
if((Idjia1.equals(content)))
{
System.out.println("账户正确");
String zhanghumima=bf.readLine();
accountpassword=zhanghumima;
}
if(content == null){
break;
}
// sb.append(content.trim()); } bf.close();
System.out.println("识别账户结束");
return sb.toString();
} public void cunkuanshezhi(int a) throws FileNotFoundException
{
accountbalance=accountbalance+a;
cunkuanjilu(a);
}
public void zhuanzhangzhanghupanding(String c)
{
if((c.equals(accountID)))
{
System.out.println("账户不能是自己");
}
else
return;
}
public void bianbieluru(int b)throws FileNotFoundException
{
if(accountbalance>=b)
{
accountbalance=accountbalance-b;
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 当前账户取款"+b+"元操作成功 ");
System.out.println(" 当前账户余额为:"+accountbalance+"元 ");
System.out.println("***************************************************************");
qukuanjilu(b);
} else
System.out.println("账户余额不足");
}
public void qukuanjilu(int a)throws FileNotFoundException //取款记录写入
{
String filePath = "D:/accountlist.txt";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
try
{
File file = new File(filePath);
PrintStream qk = new PrintStream(new FileOutputStream(file));
qk.append("操作日期"+df.format(new Date())+"取款 "+a+"元");
}catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
public boolean zhuanzhangbianbie(int a,String b)throws FileNotFoundException
{
if(accountbalance>=a)
{
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 请确认是否向"+b+"转账"+a+"元 (Y确认转账,N不转账) ");
String zhuanzhangqueren=in.nextLine();
if((zhuanzhangqueren.equals("Y")))
{
accountbalance=accountbalance-a;
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 当前账户向"+b+"成功转账"+a+"元");
System.out.println(" 当前账户余额为"+accountbalance+"元");
System.out.println("***************************************************************");
zhuanzhangjilu(a);
return true;
}
else
{
return false;
} }
return false;
}
public void zhuanzhangjilu(int a)throws FileNotFoundException
{
String filePath = "D:/accountlist.txt";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
try
{
File file = new File(filePath);
PrintStream zz = new PrintStream(new FileOutputStream(file));
zz.append("操作日期"+df.format(new Date())+"转账 "+a+"元");
}catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
public void zhnaghu2zenyue(int zz2)
{
accountbalance=accountbalance+zz2;
}
public void bianbiedangqianmima()
{
String dangqianmima=in.nextLine();
if((dangqianmima.equals(accountpassword)))
return;
else
System.out.println("当前密码录入错误");
}
public void bianbiemima(String a)
{
if((a.equals(accountpassword)))
{
String xiugaimima=in.nextLine();
String querenmima=in.nextLine();
if((xiugaimima.equals(querenmima)))
{
accountpassword=querenmima;
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 当前账户密码修改成功 ");
System.out.println("***************************************************************"); }
else
{
System.out.println(" 修改密码与确认密码不一致 ");
return;
}
}
else
{
System.out.println(" 当前密码录入错误 ");
return;
}
}
public void xiugaimimaluru()throws FileNotFoundException
{
String filePath = "D:/accountlist.txt";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
try
{
File file = new File(filePath);
PrintStream xmm = new PrintStream(new FileOutputStream(file));
xmm.append("操作日期"+df.format(new Date())+" 修改密码");
}catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
public void mimacunruwenjian()throws Exception //将文件中的数据写出
{
String fileContent3 = readFileContent3("D:/accountinformation.txt");
System.out.println(fileContent3);
}
private static String readFileContent3(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader bf = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
int zzi=0;
while(zzi<6) {
content2[zzi] = bf.readLine();
zzi++; }
bf.close();
return sb.toString();
}
public void mimachongxinxieru() throws FileNotFoundException //再将密码写入文件
{
int i=0;
String filePath = "D:/accountinformation.txt";
try
{
File file = new File(filePath);
PrintStream ps = new PrintStream(new FileOutputStream(file));
while(i<6)
{ ps.println(content2[i]);
if((content2[i].equals(accountID)))
ps.println(accountpassword);
i++;
} }catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
public void cunkuanjilu(int a)throws FileNotFoundException //存款记录记录
{
String filePath = "D:/accountlist.txt";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 try
{
File file = new File(filePath);
PrintStream ck = new PrintStream(new FileOutputStream(file));
ck.append("操作日期"+df.format(new Date())+"存款 "+a+"元");
}catch(FileNotFoundException e)
{
e.printStackTrace();
} } public void cunkuan() //存款界面
{
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 请输入存款金额; ");
System.out.println("***************************************************************");
}
public void shuzijiemian()
{
System.out.println("***************************************************************");
System.out.print(" 欢迎");
System.out.print(accountID);
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.查询余额 ");
System.out.println("***************************************************************");
}
public void cunkuanchengongjiemian()
{
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 当前账户存款操作成功 ");
System.out.println(" 当前账户余额为:"+accountbalance+"元 ");
System.out.println("***************************************************************");
}
public void qukuanjiemian()
{
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 当前账户每日可以支取2万元 ");
System.out.println(" 1、100元 ");
System.out.println(" 2、500元 ");
System.out.println(" 3、1000元 ");
System.out.println(" 4、1500元 ");
System.out.println(" 5、2000元 ");
System.out.println(" 6、5000元 ");
System.out.println(" 7、其他金额 ");
System.out.println(" 8、退卡 ");
System.out.println(" 9、返回 ");
System.out.println("***************************************************************");
}
public void zhuanzhangjiemian()
{
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 请输入转账账户 ");
System.out.println("***************************************************************");
}
public void zhuanzhangjinejiemian()
{
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 请输入转账金额 ");
System.out.println("***************************************************************");
}
public void xiugaimimajiemian()
{
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 请输入当前密码: ");
System.out.println(" 请输入修改密码: ");
System.out.println(" 请输入确认密码: ");
System.out.println("***************************************************************");
}
public void chaxungongneng()
{
System.out.println("***************************************************************");
System.out.println(" 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 当前账户余额为:"+accountbalance+"元 ");
System.out.println(" 账户清单信息为: ");
System.out.println("***************************************************************"); }
} package atm0;
public class jiemian {
public void jiemiankaishi()
{
System.out.println("***************************************************************");
System.out.println(" 欢迎使用中国工商银行自动柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 请输入您的账号: ");
System.out.println("***************************************************************");
}
public void shumima()
{
System.out.println("***************************************************************");
System.out.println(" 欢迎使用中国工商银行自动柜员系统 ");
System.out.println("***************************************************************");
System.out.println(" 请输入您的密码: ");
System.out.println("***************************************************************");
}
}