Java感受

           今天是第一次真正意义上的写java 虽然假期在家里简单的接触了一点而且也准备了一下建民老师给的试卷,但是这个临阵变卦就很难受 用老师的话来说就是给我们一个下马威 杀杀我们,毫无疑问老师成功了,从一开始的毫无头绪 只能一点一点的摸索下去 犹如盲人摸象一般 在网上搜点教程慢慢的改造。      

    首先,要定义一个Account类,有七个私有变量,给每个人变量定义get和set函数。并要求实现相关的功能,要求通过文本文件进行用户信息的录入,并可有通过程序在文本文件里修改用户的信息。

在测试之前我对JAVA进行了简单的学习,但是没有去学习文件的操作,关于文件什么都不会(之前学C,C++的时候也没有学好文件操作)。因此在这次测试的过程中,我暂时没有实现文件的读入,而是实例化五个Account对象后,要求操作人员先输入五个用户信息后,再进行相关操作的选择。

为了方便实现操作过程中界面的跳转,我尽可能的写出了很多的函数,将各个操作独立的写出来,这样一来当程序判断输入的账号或者密码不正确的时候,可以很方便的跳转到相应的输入界面中再次进行输入,直到输入正确的字符。当然这都是很简单的用老师的话来说就是白给的分 其实说的也没错这个也没啥难的,但是后面的具体功能实现以及各部分之间的连接是我现在的水平所搞不懂的。测试结束之后,我对文件的简单读取和写入的操作进行了学习,以满足测试题目的要求,具体学会了一下简单操作。使用FileReader和BufferedReader和readline实现了从文本文件里面一行一行的读入信息。和我之前的程序相比,仍是先实例化五个对象,不过这次不需要操作者在程序开始后输入信息,而是程序从文本文件里面逐行读取信息并存储到对象相应的数据项里面。而当选择一些会修改到用户信息的操作时(如存取款,修改密码),我使用FileWriter和BufferedWriter和newLine将更新后的信息又按行写入了文件中(采用覆盖写入的方式),汇款的文本文件采用文件末尾追加的方式写入。

      所以还是需要继续努力努力达到毕业应有的水平。共勉吧。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Account {
private String accountID;
private String accountname;
private String operatedate;
private int operatetype;
private String accountpassword;
private int accountbalance;
private int amount;
public void setID(String ID) {
this.accountID=ID;
}
public String getID() {
return this.accountID;
}
public void setname(String name) {
this.accountname=name;
}
public String getname() {
return this.accountname;
}
public void setdate(String date) {
this.operatedate=date;
}
public String getdate() {
return this.operatedate;
}
public void settype(int type) {
this.operatetype=type;
}
public int gettype() {
return this.operatetype;
}
public void setpassword(String word) {
this.accountpassword=word;
}
public String getpassword() {
return this.accountpassword;
}
public void setbalance(int balance) {
this.accountbalance=balance;
}
public int getbalance() {
return this.accountbalance;
}
public void setamount(int amount) {
this.amount=amount;
}
public int getamount() {
return this.amount;
}
Account(String accountID,String accountname,String accountpassword,int accountbalance){
this.accountID=accountID;
this.accountname=accountname;
this.accountpassword=accountpassword;
this.accountbalance=accountbalance;
}
Account(){
this.accountID="null";
this.accountname="null";
this.accountpassword="null";
this.accountbalance=0;
}
public static void zhanghaoshurujiemian(Account[] a) {
boolean bool1=false;
int hao=10;
Scanner scan=new Scanner(System.in);
System.out.println("**********************************");
System.out.println(" 欢迎使用中国工商银行自动柜员系统");
System.out.println("**********************************");
System.out.println(" 请输入您的账号:");
String zhanghao=scan.next();
for(int i=0;i<5;i++) {
if(a[i]!=null&&a[i].accountID.equals(zhanghao)) {
hao=i;
bool1=true;
break;
}
}
if(bool1) {
mimashuru(a,hao);
}
else {
System.out.println("未录入此账号,请重新输入。");
zhanghaoshurujiemian(a);
}
}
public static void gongnengjiemian()
{
System.out.println("**********************************");
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(" 6、退出");
System.out.println("**********************************");
}public static void cun(Account [] a,int i) {
int nmoney;
int omoney=a[i].getbalance();
Scanner scan=new Scanner(System.in);
System.out.println("请输入存入的款数:");
int cmoney=scan.nextInt();
nmoney=a[i].getbalance()+cmoney;
a[i].setbalance(nmoney);
System.out.println("该账户的存款变化为:");
System.out.println(cmoney+"+"+omoney+"="+a[i].getbalance());
File file=new File("accountlist.txt");
try {
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
for(int j=0;j<5;j++) {
if(a[j].getname().equals("null")) {
break;
}
bw.write(a[j].getname());
bw.newLine();
bw.write(a[j].getID());
bw.newLine();
bw.write(a[j].getpassword());
bw.newLine();
bw.write(String.valueOf(a[j].getbalance()));
bw.newLine();
}
bw.close();
fw.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void zhuan(Account [] a,int i) {
Scanner scan=new Scanner(System.in);
int zhuanru=6;
System.out.println("请输入转入用户的账号:");
String zhang=scan.nextLine();
for(int j=0;j<5;j++) {
if(a[j].accountID.equals(zhang)) {
zhuanru=j;
break;
}
}
System.out.println("请输入转入的金额:");
int zmoney=scan.nextInt();
int imoney=a[i].getbalance();
int jmoney=a[zhuanru].getbalance();
a[i].setbalance(imoney-zmoney);
a[zhuanru].setbalance(jmoney+zmoney);
System.out.println("汇款已完成");
File file=new File("accountlist.txt");
try {
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
for(int j=0;j<5;j++) {
if(a[j].getname().equals("null")) {
break;
}
bw.write(a[j].getname());
bw.newLine();
bw.write(a[j].getID());
bw.newLine();
bw.write(a[j].getpassword());
bw.newLine();
bw.write(String.valueOf(a[j].getbalance()));
bw.newLine();
}
bw.close();
fw.close();
}
catch(IOException e) {
e.printStackTrace();
}
FileWriter fw1 = null;
try {
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f=new File("accountinformation.txt");
fw1 = new FileWriter(f, true);
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fw1);
pw.println(a[i].getname()+"转"+zmoney+"元钱给"+a[zhuanru].getname());
pw.flush();
pw.println();
try {
fw1.flush();
pw.close();
fw1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void qu(Account [] a,int i) {
int nmoney;
int omoney=a[i].getbalance();
Scanner scan=new Scanner(System.in);
System.out.println("请输入取出的款数:");
int qmoney=scan.nextInt();
nmoney=omoney-qmoney;
a[i].setbalance(nmoney);
System.out.println("该账户的存款变化为:");
System.out.println(omoney+"-"+qmoney+"="+a[i].getbalance());
File file=new File("accountlist.txt");
try {
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
for(int j=0;j<5;j++) {
if(a[j].getname().equals("null")) {
break;
}
bw.write(a[j].getname());
bw.newLine();
bw.write(a[j].getID());
bw.newLine();
bw.write(a[j].getpassword());
bw.newLine();
bw.write(String.valueOf(a[j].getbalance()));
bw.newLine();
}
bw.close();
fw.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void yue(Account [] a,int i) {
System.out.println("该账户的余额为:");
System.out.println(a[i].accountbalance);
}
public static void xiugai(Account[] a,int i) {
System.out.println("请输入新的密码:");
Scanner scan=new Scanner(System.in);
String newword=scan.nextLine();
a[i].setpassword(newword);
System.out.println("密码修改完成!");
File file=new File("accountlist.txt");
try {
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
for(int j=0;j<5;j++) {
if(a[j].getname().equals("null")) {
break;
}
bw.write(a[j].getname());
bw.newLine();
bw.write(a[j].getID());
bw.newLine();
bw.write(a[j].getpassword());
bw.newLine();
bw.write(String.valueOf(a[j].getbalance()));
bw.newLine();
}
bw.close();
fw.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void gongneng(Account [] a,int i) {
boolean bool3=true;
Scanner scan=new Scanner(System.in);
int choice;
do
{
gongnengjiemian();
System.out.println("请输入您的选择:");
choice=scan.nextInt();
switch(choice) {
case 1:
cun(a,i);
break;
case 2:
qu(a,i);
break;
case 3:
zhuan(a,i);
break;
case 4:
xiugai(a,i);
break;
case 5:
yue(a,i);
break;
case 6:
System.out.println("感谢您的使用!!!");
bool3=false;
break;
}
}
while(bool3);
}
public static void mimashuru(Account [] a,int i) {
Scanner scan=new Scanner(System.in);
System.out.println("**********************************");
System.out.println(" 欢迎使用中国工商银行自动柜员系统");
System.out.println("**********************************");
System.out.println(" 请输入您的密码:");
String mima=scan.nextLine();
if(a[i].accountpassword.equals(mima)) {
gongneng(a,i);
}
else {
System.out.println("密码输入有误,请重新输入。");
mimashuru(a,i);
}
}
public static void jinxing() {
Scanner scan=new Scanner(System.in);
Account []a=new Account[5];
a[0]=new Account();
a[1]=new Account();
a[2]=new Account();
a[3]=new Account();
a[4]=new Account();
try {
FileReader file = new FileReader("accountlist.txt");
BufferedReader br = new BufferedReader(file);
int i=0;
String str = null;
while(true) {
str = br.readLine();
if(str == null) {
break;
}
a[i].setname(str);
str = br.readLine();
a[i].setID(str);;
str = br.readLine();
a[i].setpassword(str);
str = br.readLine();
a[i].setbalance(Integer.parseInt(str));
++i;
}
br.close();
file.close();
} catch (FileNotFoundException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
}
zhanghaoshurujiemian(a);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
jinxing();
}
}

posted on 2018-09-24 09:49  哈弗h6  阅读(102)  评论(0编辑  收藏  举报

导航