我试图制作它,以便每个交易都有时间戳,但我遇到了日期对象的问题。每次程序到达构造函数时,我都会遇到语法错误。有什么想法吗?
非常感谢你!
DateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm");
Date dateobj = new Date();
但是现在我在MainMenu方法中调用后立即遇到一个空指针表达式。如何让它在所有方法中工作,如数组列表?
/**
* Bank Account
*
* @author: Seth Killian
* @version: 1.
* @citations: All okay. Adapted online tutorials in using the scanner tools to create a menu and list array.
*/
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
//import java.util.Calendar;
public class BankAccount
{
private double minbalance; // Minimum allowable account balance
private double maxdeposit; // Maximum allowable deposit
private double balance; // Account balance
private double rate; // Rate of annual interest in value of percent
private String AcctName; // Account name
private ArrayList<String> transactions;
public Date dateobj;
public DateFormat df;
/**
* Constructor for Bank Account Class with Starting Balance
* @param: Starting Balance
* @return: None
*/
public BankAccount(double startBalance)
{
DateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm");
Date dateobj = new Date();
transactions = new ArrayList<String>();
balance = startBalance;
transactions.add("+ $" + startBalance + " Initial Balance " + df.format(dateobj) + " Bal: $" + balance);
minbalance = 0.25;
maxdeposit = 5000.00;
rate = 6; // Six percent annual interest
AcctName = "Seth Killian";
}
/**
* Main Menu which allows the user to select from options
* @param: None
* @return: None
*/
public void MainMenu()
{
Scanner in = new Scanner (System.in);
switch (in.nextInt())
{
case 0:
Initialize();
case 1:
System.out.println("Balance: $" + balance + " as of " + df.format(dateobj));
SelectOption();
case 2: //Make a deposit
double amount;
System.out.print("Deposit Amount: $");
amount = in.nextDouble();
if (!(amount > 0) || (amount > maxdeposit))
{transactions.add("* Attempted Deposit: $" + amount + " " + df.format(dateobj));}
if (!(amount > 0))
{System.out.println("Error: Amount must be greater than zero");}
else if (amount > maxdeposit)
{System.out.println("Error: Maximum Deposit $" + maxdeposit + ".");}
else
{
balance += amount;
transactions.add("+ $" + amount + " Deposit " + df.format(dateobj) + " Bal: $" + balance);
System.out.println("Success: Deposited $" + amount + " Balance: $" + balance);
}
SelectOption();
case 3: //Make a withdrawl
System.out.print("Withdrawl Amount: $");
amount = in.nextDouble();
if (!(amount > 0) || (amount > balance) || (balance-amount < minbalance))
{transactions.add("* Attempted Withdrawl: $" + amount + " " + df.format(dateobj));}
if (!(amount > 0))
{System.out.println("Error: Amount must be greater than zero");}
else if (amount > balance)
{System.out.println("Error: Insufficent Funds");}
else if (balance-amount < minbalance)
{System.out.println("Error: Minimum Balance $" + minbalance + ".");}
else
{
balance -= amount;
transactions.add("- $" + amount + " Withdrawl " + df.format(dateobj) + " Bal: $" + balance);
System.out.println("Success: Withdrew $" + amount + " Balance: $" + balance);
}
SelectOption();
case 4: //Apply Annual Interest
balance = balance + (balance * (rate/100.0));
transactions.add("Interest ("+ rate + "%) "+ df.format(dateobj) + " Bal: $" + balance);
System.out.println("Success: " + rate + "% Interest rate applied. Balance: $" + balance);
SelectOption();
case 5: // Print Log
System.out.println();
System.out.println("Transaction Log for " + AcctName);
System.out.println("---------------------------------------- ");
for(int x = 0; x < transactions.size(); x++)
{System.out.println(transactions.get(x));}
System.out.println("---------------------------------------- ");
SelectOption();
case 6: // Exits application
System.exit(0);
default: // Unrecognized Selection
System.out.println ("Error: Selection Unrecognized");
SelectOption();
}
}
/**
* Sets the Main Menu up
* @param: None
* @return: None
*/
public void Initialize()
{
System.out.print('\u000C'); // Clears the terminal screen
System.out.println("Welcome " + AcctName + "!");
System.out.println("======================================== ");
System.out.println("1) Check Balance\n2) Make a Deposit\n3) Make a Withdrawl\n4) Add Annual Interest\n5) Transaction Logs");
System.out.println("\n0) Clear Screen\n6) Exit Application");
System.out.println("======================================== ");
SelectOption();
}
/**
* Allows the user to make a selection.
* @param: None
* @return: None
*/
public void SelectOption()
{
System.out.print ("Selection: ");
MainMenu();
}
}
答案 0 :(得分:0)
模式中的任何字符都不在[&#39; a&#39; ...&#39; z&#39;]和[&#39; A&#39; ..&#39范围内; Z&#39;]将被视为引用文本。例如,字符,如&#39;:&#39;,&#39;。&#39;,&#39; &#39;,&#39;#&#39;并且&#39; @&#39; 将出现在生成的时间文本中,即使它们未被单引号包含在内。
包含任何无效模式字母的模式将在格式化或解析过程中导致抛出异常。
因此,由于df = new SimpleDateFormat("MM/dd/yy HH:mm");
,您的代码行/
将引发异常。尝试将/
放在单引号中。
df = new SimpleDateFormat("MM'/'dd'/'yy HH:mm");
与@ DavidColer的回答状态一样,您永远不会初始化Date
对象。
经过测试,结果证明df = new SimpleDateFormat("MM/dd/yy HH:mm");
可以在没有单引号的情况下工作。我应该看一下Oracle的文档here。
您需要通过传递double来创建BankAccount
类的实例,如此BankAccount ba = new BankAccount(100.0);
或在类中添加默认构造函数。如下所示。
public BankAccount() {
this(0.25);
}
答案 1 :(得分:0)
您永远不会完全初始化日期。
DateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy HH:mm");
Date date = new Date();
System.out.println(simpleDateFormat.format(date));
我没有看到你在哪里使用重载的构造函数,甚至没有在代码中实例化你的bankaccount类的实例?如果你没有使用重载的构造函数,你应该创建一个默认的构造函数来实例化你的varibales。
public class BankAccount
{
final private double MINBALANCE; // Minimum allowable account balance
final private double MAXDEPOSIT; // Maximum allowable deposit
private double balance; // Account balance
private double rate; // Rate of annual interest in value of percent
private String AcctName; // Account name
private ArrayList<String> transactions;
public Date dateobj;
public DateFormat df;
public BankAccount()
{
MINBALANCE = 25.00; // Minimum allowable account balance this should be all caps for a constant
MAXDEPOSIT = 5000.00; // Maximum allowable deposit should also be a constant
balance = 0.0;
rate = 0.0;
AcctName = null;
transactions = new ArrayList<String>(10);
dateobj = new Date();
df = new SimpleDateFormat("MM/dd/yy HH:mm");
}
喜欢这样