设计一个账户信息管理系统,使用文件实现账户数据的存储、查询等功能
【问题分析】(1)定义account类,包含成员变量id(账户ID)、amount(金额)和date(交易日期),以及如下方法:
setXXX()和getXXX()方法设置和获取成员变量的值。
无参构造方法创建一个具有默认值Account类对象,而有参构造方法是哦那个setXXX()方法初始化成员变量
read()方法读取账户信息,通过DataInputStream类的readLong()方法读取id和date,readDouble()方法读取amount,并使用setXXX()方法给成员变量赋值,返回Amount类对象
write()方法写账户信息,通过DataOutputStream类的writeLong()方法写入id和date,writeDouble()方法写入amount
readAmount()方法返回Account类对象数组,在其中首先使用无参构造方法创建Account类对象,然后该对象调用read()方法读取账户信息,并将返回的对象保存在对象数组中
writeAccount()方法返回写账户标记flag,将传入的Account类对象数组通过调用write()方法写入文件,如果写入失败,则flag为false
(2)定义AccountTest类,创建Account类的对象数组并初始化,然后分别调用writeAccount()和readAccount()方法向文件写入和从文件读取账户信息
package com.chinamobile.cmss.ms.TestMore;
import com.chinamobile.cmss.ms.dto.sourcing.innerpurchaseanalyze.dataDisplay.AmountTargetDTO;
import java.io.*;
import java.nio.file.FileAlreadyExistsException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
/*账户信息管理系统*/
public class Account {
private long id;//账户
private double amount;//金额
private Date date;//交易日期
public Account(){
new Account(01,0,new Date());
}
public Account(long id, double amount, Date date) {
this.id = id;
this.amount = amount;
this.date = date;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", amount=" + amount +
", date=" + date +
'}';
}
public void write(DataOutputStream out) throws IOException {
out.writeLong(this.getId());
out.writeDouble(this.getAmount());
out.writeLong(this.getDate().getTime());
}
public Account read(DataInputStream in) throws IOException{
this.setId(in.readLong());
this.setAmount(in.readDouble());
this.setDate(new Date(in.readLong()));
return this;
}
//定义readAccount()方法,实现读取账户信息,并返回账户对象数组
public static Account[] readAccount(File file) throws IOException{
FileInputStream in = null;
DataInputStream dataIn = null;
Account[] accounts = new Account[2];
try {
in= new FileInputStream(file);
}catch (IOException e){
System.out.println("文件不存在");
}
dataIn = new DataInputStream(in);
try {
//available()方法用来判断是否到达流的末尾
for (int i = 0; i < accounts.length && dataIn.available()>0; i++) {
Account temp = new Account();
accounts[i] = temp.read(dataIn);
}
}catch (IOException e){
e.printStackTrace();
}
dataIn.close();
in.close();
return accounts;
}
public static boolean writeAccount(Account[] accounts,File file) throws IOException{
boolean flag = false;
FileOutputStream out = null;
DataOutputStream dataOut = null;
try {
out = new FileOutputStream(file);
dataOut = new DataOutputStream(out);
for (int i = 0; i < accounts.length && accounts.length > 0; i++) {
accounts[i].write(dataOut);
}
}catch (IOException e){
System.out.println("写账户信息失败");
flag = false;
}finally {
if (dataOut != null){
dataOut.close();
}
}
return flag;
}
public static void main(String[] args) {
try {
//获取默认分隔的日期/时间格式器
//调用parse()方法将字符串转换为Date对象
DateFormat df = DateFormat.getDateInstance();
File file = new File("account.txt");
Account[] accounts = {
new Account(1,2000.5,df.parse("2021-01-25")),//这里如果写成2021年1月25日会报错,可以在前面的toString方法中进行设置
new Account(2,1000.5,df.parse("2021-02-25"))
};
System.out.println("正在保存数据中");
if (Account.writeAccount(accounts,file)){
System.out.println("数据保存成功");
}else {
System.out.println("数据保存失败");
}
accounts = Account.readAccount(file);
System.out.println("数据读取结束,正在回显中...");
if (accounts.length == 0){
System.out.println("文件中没有数据");
}else{
for (int i = 0; i < accounts.length; i++) {
System.out.println(accounts[i]);
}
System.out.println("数据显示结束");
}
} catch (IOException e) {
e.printStackTrace();
}catch (ParseException e){
e.printStackTrace();
}
}
}
正在保存数据中
数据保存成功
数据读取结束,正在回显中...
Account{id=1, amount=2000.5, date=Mon Jan 25 00:00:00 CST 2021}
Account{id=2, amount=1000.5, date=Thu Feb 25 00:00:00 CST 2021}
数据显示结束
解决:下面代码不能识别的问题
DateFormort df = Dateformort.getDateInstance(); Account[] accounts = { new Account(1,2000.5,df.parse("2021年01月25日")),//这里如果写成2021年1月25日会报错,可以在前面的toString方法中进行设置 new Account(2,1000.5,df.parse("2021年02月25日")) }
解决方法:将DateFormort df = Dateformort.getDateInstance()替换为:
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat用于一些特殊的时间格式;
解决:解决结果不是如下的方式:
正在保存数据中
数据保存成功
数据读取结束,正在回显中...
账户ID:1账户余额:2000.5¥日期:2021年01月25日
账户ID:2账户余额:1000.5¥日期:2021年02月25日
数据显示结束
解决方案:通过重写toString方法来改变:
@Override public String toString() { DateFormat df = new SimpleDateFormat("yyyy年MM月dd日"); return "账户ID:" + id + "账户余额:" + amount + "¥" + "日期:" + df.format(date); }
或者
public String toString() { DateFormat df = new SimpleDateFormat("yyyy年MM月dd日"); return "账户ID:" + getId() + "账户余额:" + getAmount() + "¥" + "日期:" + df.format(getDate()); }