所以在我的程序中,我试图在项目中创建一个基本的.txt文件来编写/读取单个客户对象。这是我读取和写入文件的代码。该程序运行时没有编译错误和权限而没有抛出异常但是当我尝试读取文件时,程序抛出"没有这样的文件或目录,null"这里要求的是程序的所有代码:你得到菜单输入1来添加客户(这似乎工作或我认为)然后输入2加载并显示不起作用的客户数据。
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Bank {
public static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
do {
try {
System.out.println("-----------------------------------");
System.out.println(" Main Menu");
System.out.println("-----------------------------------");
System.out.println("");
System.out.println(" Item Description");
System.out.println(" ---- --------------------------");
System.out.println(" 1 Creat a new customer");
System.out.println(" 2 View an existing customer");
System.out.println(" 3 Exit the program");
System.out.println("");
System.out.println(" Please enter your choice: ");
int select = Integer.valueOf(in.nextLine());
switch ( select ) {
case 1:
createCustomer();
break;
case 2:
viewCustomer();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("\nInvalid selection.");
break;
}
}
catch ( Exception e) {
System.out.println(e.getMessage());
}
} while( true );
}
//----------------------------Main Methods-------------------------------------------\\
public static void createCustomer() {
try {
String select;
System.out.println("");
System.out.println("-----------------------------------");
System.out.println(" Create Customer");
System.out.println("-----------------------------------");
System.out.print("Customer name: ");
String customerName = in.nextLine();
System.out.print("Customer number: ");
String customerNum = in.nextLine();
Customer newCustomer = new Customer( customerName, customerNum);
System.out.print("Checking account? (y/n): ");
select = in.nextLine();
if ( select.equalsIgnoreCase("y") ) {
System.out.print("\tBalance: ");
double intBalance = Double.valueOf(in.nextLine());
Checking newChecking = new Checking("001", intBalance);
newCustomer.addAccount(newChecking);
}
System.out.print("Saving account? (y/n): ");
select = in.nextLine();
if ( select.equalsIgnoreCase("y") ) {
System.out.print("\t APR: ");
double apr = Double.valueOf(in.nextLine());
System.out.print("\tBalance: ");
double intBalance = Double.valueOf(in.nextLine());
Savings newSavings = new Savings("002", intBalance);
Savings.setAnnualPercentageRate(apr);
newCustomer.addAccount(newSavings);
}
System.out.print("Money Market account? (y/n): ");
select = in.nextLine();
if ( select.equalsIgnoreCase("y") ) {
System.out.print("\t APR: ");
double apr = Double.valueOf(in.nextLine());
MoneyMarket.setAnnualPercentageRate(apr);
System.out.print("Minimum Balance: 1200.00");
System.out.print("\tBalance: ");
double intBalance = Double.valueOf(in.nextLine());
MoneyMarket newMoneyMarket = new MoneyMarket("003", intBalance);
newCustomer.addAccount(newMoneyMarket);
}
Customer.saveCustomer(newCustomer);
System.out.println(newCustomer.getCustomerDetails());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
public static void viewCustomer() {
System.out.println("");
System.out.println("-----------------------------------");
System.out.println(" View Customer");
System.out.println("-----------------------------------");
System.out.println(" \nEnter Customer number: ");
String customerNum = in.nextLine();
Customer newCustomer = Customer.loadCustomer(customerNum);
System.out.println(newCustomer.getCustomerDetails());
}
}
//=====================================================================================\\
interface ITaxable {
double getTaxesDue();
}
//=====================================================================================\\
abstract class Account {
private String accountNum;
private double balance;
public Account(String accountNum, double amount) throws Exception {
deposit(amount);
setAccountNum(accountNum);
}
public void setAccountNum(String accountNum) {
this.accountNum = accountNum;
}
public String getAccountNum() {
return accountNum;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) throws Exception {
if ( amount < 0){
throw new Exception("Must enter a deposit greater than 0.");
}
else {
balance += amount;
}
}
public void withdraw(double amount) throws Exception {
if ( amount > getBalance() ){
throw new Exception("Amount exceeds balance.");
}
else {
balance -= amount;
}
}
public String toString() {
return "Account Number: " + accountNum + ", Balance: " + balance;
}
}
//=====================================================================================\\
class Checking extends Account {
public Checking(String accountNum, double amount) throws Exception {
super(accountNum, amount);
}
public void cashCheck(int checkNum, int amount) throws Exception {
this.withdraw(amount);
}
public String toString(){
return "CHK" +getAccountNum()+ " " +getBalance();
}
}
//=====================================================================================\\
class Savings extends Account implements ITaxable {
static double annualPercentageRate;
public Savings( String accountNum, double amount ) throws Exception {
super(accountNum, amount);
}
public static void setAnnualPercentageRate(double annualPercentageRate) throws Exception {
if ( annualPercentageRate > 0 ) {
Savings.annualPercentageRate = annualPercentageRate;
}
else {
throw new Exception("APR rate must be greater then 0.");
}
}
public static double getAnnualPercentageRate() {
return annualPercentageRate;
}
public void addInterest() throws Exception {
int numberOfDays = 30;
double interest = getBalance() * numberOfDays * (getAnnualPercentageRate() / 100 / 365);
deposit(interest);
}
@Override public double getTaxesDue(){ return 0;}
public String toString(){
return "SAV" +getAccountNum()+ " " +getBalance();
}
}
//=====================================================================================\\
class MoneyMarket extends Savings{
static double miniumBalance = 1200;
public MoneyMarket( String accountNum, double balance ) throws Exception {
super(accountNum, balance);
if ( balance < miniumBalance ) {
throw new Exception("Minimum balance not met.");
}
}
public static void setMiniumBalance( double amount) throws Exception {
if ( amount < 0 ){
throw new Exception("Must enter a amount greater then 0.");
}
else {
MoneyMarket.miniumBalance = miniumBalance;
}
}
public static double getMiniumBalance() {
return miniumBalance;
}
@Override
public void withdraw( double amount ) throws Exception {
if ( getBalance() - amount > miniumBalance ) {
super.withdraw(amount);
}
else {
throw new Exception("Amount is greater then minimum balance.");
}
}
public String toString() {
return "MMA " +getAccountNum()+" "+getBalance();
}
}
//=====================================================================================\\
class Customer{
private String customerName;
private String customerNum;
private ArrayList<Account> accounts = new ArrayList<Account>();
public Customer( String customerName, String cutomerNum) {
setCustomerName(customerName);
setCustomerNum(cutomerNum);
}
public void setCustomerName( String customerName ) { this.customerName = customerName; }
public void setCustomerNum( String customerNum ) { this.customerNum = customerNum; }
public String getCustomerName() { return customerName; }
public String getCustomerNum() { return customerNum; }
public void addAccount(Account a) throws Exception {
if (a != null) {
accounts.add(a);
}
else {
throw new Exception("Invalid entry");
}
}
public static void saveCustomer(Customer c) throws Exception {
if ( c != null) {
String fileName = c.customerNum + ".txt";
OutputStream file = new FileOutputStream(fileName);
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
output.writeObject(c);
OutputStream close;
}
else {
throw new Exception("Customer does not exist.");
}
System.out.println("\nCustomer Saved.");
}
public static Customer loadCustomer(String customerNum) {
try {
String fileName = customerNum + ".txt";
InputStream file = new FileInputStream(fileName);
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);
Customer x = (Customer) input.readObject();
InputStream close;
return x;
}
catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
public String getCustomerDetails() {
String dets;
dets = "\nCUSTOMER DETAIL REPORT\n" +
"-------------------------------------------\n";
dets += String.format("%s (#%s)\n", this.getCustomerName(), this.getCustomerNum());
for (Account a : accounts) {
dets += String.format("\t%s\n", a);
}
return dets;
}
@Override public String toString() { return customerName; }
}
//=====================================================================================\\