我目前正在开发一个小型ATM项目,该项目使用阵列记录总共3个帐户中的金额。一切都运作顺利,但我似乎无法读取和写入文件。任何意见都将非常感谢!
package atmfun;
import atmfun.ATMAccounts;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.StringTokenizer;
public class Account implements java.io.Serializable
{
double startingBalance = 100;
int firstdate = 0;
int seconddate = 0;
int datediff = 0;
Calendar cal1 = new GregorianCalendar();
Calendar cal2 = new GregorianCalendar();
boolean dateflag = false;
double rate = 0;
double ratetime = 0;
ATMAccounts[] ATMAccount = new ATMAccounts[3];
int i = 0;
private String path;
// cal1 = 0;
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
Account acc = new Account();
acc.ATMAccount[0] = new ATMAccounts();
acc.ATMAccount[1] = new ATMAccounts();
acc.ATMAccount[2] = new ATMAccounts();
do
{
System.out.println("Which account? 1, 2 or 3?");
System.out.println("Enter 4 to exit.");
System.out.print("Enter choice: ");
acc.i = sc.nextInt();
if (acc.i == 4)
{
System.out.println("Have a nice day!");
System.exit(1);
} else
{
acc.i--;
acc.menu();
}
} while(acc.i != 4);
}
public void menu() throws IOException
{
int input = 0;
do{
Scanner sc = new Scanner(System.in);
System.out.println("");
System.out.println("Please enter 1 for deposit, 2 for withdraw, 3 for check balance, 4 exit");
System.out.print("Enter choice: ");
try
{
input = sc.nextInt();
}
catch(Exception e)
{
System.out.println("You made a mistake, try again");
}
writeFile(); //IT BREAKS RIGHT HERE
readFile();
if (input == 1)
{
deposit();
writeFile();
readFile();
}
if(input == 2)
{
withdraw();
}
if(input == 3)
{
checkBalance();
}
if (input == 4);
{
System.out.println("Return to account menu.");
}
}while (input != 4);
}
public void deposit() throws IOException
{
getDate1();
//getInterest();
System.out.print("Deposit Amount: ");
Scanner AC = new Scanner(System.in);
double dep = AC.nextDouble();
ATMAccount[i].balance = ATMAccount[i].balance + dep;
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println("New Balance: " + nf.format(ATMAccount[i].balance + ratetime));
}
public void withdraw()
{
System.out.print("Withdraw: ");
Scanner WD = new Scanner(System.in);
double with = WD.nextDouble();
ATMAccount[i].balance = ATMAccount[i].balance - with;
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.print("New Balance: " + nf.format(ATMAccount[i].balance));
System.out.println("");
}
public void checkBalance() throws IOException
{
getDate2();
getInterest();
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.print("Amount: " + nf.format(ATMAccount[i].balance + ratetime));
System.out.println("");
}
public void getInterest()
{
int datediff = seconddate - firstdate;
rate = .05/365;
double ratetime = Math.pow(1+rate,datediff); // datediff*(1+rate);
ATMAccount[i].balance = ATMAccount[i].balance * ratetime;
firstdate = seconddate;
}
public void getDate1() throws IOException
{
System.out.print("Enter todays date(mm/dd/yyyy): ");
BufferedReader br;
br = new BufferedReader(new InputStreamReader(System.in));
String inputText = br.readLine();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
ParsePosition pos = new ParsePosition(0);
//Date date= new Date();
Date date = formatter.parse(inputText, pos);
cal1.setTime(date);
firstdate = cal1.get(Calendar.DAY_OF_YEAR);
dateflag = true;
}
public void getDate2() throws IOException
{
System.out.print("Enter todays date(mm/dd/yyyy): ");
BufferedReader br;
br = new BufferedReader(new InputStreamReader(System.in));
String inputText = br.readLine();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
ParsePosition pos = new ParsePosition(0);
Date date = formatter.parse(inputText, pos);
cal2.setTime(date);
seconddate = cal2.get(Calendar.DAY_OF_YEAR);
dateflag = true;
}
// public void readFile()
// {
// try
// {
// BufferedReader br = new BufferedReader(new FileReader("accountINFO.txt"));
//
// } catch (IOException e)
// {
// e.printStackTrace();
// }
// }
// public void writeFile()
// {
// try
// {
// File file = new File ("./cheekymonkey/file.out");
// FileOutputStream fos = new FileOutputStream("./cheekymonkey/file.out");
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// oos.writeObject(i);
// oos.flush();
// fos.close();
//
//
//
//
//
// } catch (IOException e)
// {
// e.printStackTrace();
// }
//
//
// }
public void writeFile ()
{
{
try {
File file = new File("accountINFO.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
//Or comment out above line and create for loop to loop through
//and write all accounts out.
String tempString = ATMAccount[i].getAcctNum();
double tempNumber = ATMAccount[i].getBalance();
String tempStringNum = Double.toString(tempNumber);
bw.write("|");
bw.write(tempStringNum);
bw.newLine();
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void readFile() throws IOException
{
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
try
{
BufferedReader br = new BufferedReader(new FileReader("accountINFO.txt"));
int counter = 0;
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
StringTokenizer st = new StringTokenizer(sCurrentLine, "|");
while(st.hasMoreTokens())
{
for(int i = 0; i < 2; i++)
{
if(i == 0)
{
String tempAcct = st.nextToken();
ATMAccount[counter].setAcctNum(tempAcct);
}
else
{
ATMAccount[counter].setBalance(Double.parseDouble(st.nextToken()));
}
}
}
counter++;
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
如果尚未初始化对象,则会发生NullPointerException。有八种原始类型不需要初始化,它具有默认值。 字节0, 短0, int 0, 长0L, 浮点数0.0f, 双0.0d, char'\ u0000', 布尔值假。