⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileprocessing.java

📁 A bank system
💻 JAVA
字号:


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * This Class is designed to cope with all file operation
 * including reading and writing from file.
 *
 */
public class FileProcessing {
	
	
	/**
	 * This method deals with reading constants value from BankSys.ini 
	 * and assigns those value to Bank system
	 */
	public void preBankSysInI()
	{
		SavingAccount.minimumBalance = 100.00;
		CreditAccount.creditLimit = 1000.00;
		LoanAccount.minimumLoanAmount = 100000.00;
		Command.maxOwner = 2;
		Command.maxAccount = 5;
		CreditAccount.minCreditAge = 18;
		
		String delimiters = " ";
		try
	       {
	           BufferedReader inputStream = 
	        	   new BufferedReader(new FileReader("BankSys.ini"));

	           String constantFromBankSysINI = "STARTREADING";
	           String constant;
	           String constantValue;
	           while(constantFromBankSysINI != null){
	        	   	constantFromBankSysINI = inputStream.readLine( );
	        	   	if(constantFromBankSysINI != null){ 
	        	   		StringTokenizer constantFactory = new StringTokenizer(constantFromBankSysINI, delimiters);
	        	   		constant 		= constantFactory.nextToken();
	        	   		constantValue 	= constantFactory.nextToken();
	        	   		if(constant.equals("MIN_SAVING"))
						{
	        	   			SavingAccount.minimumBalance = Double.parseDouble(constantValue);
						}
						else if(constant.equals("MIN_CREDIT_LIMIT"))
						{
							CreditAccount.creditLimit = Double.parseDouble(constantValue);
						}
						else if(constant.equals("MIN_LOAN"))
						{
							LoanAccount.minimumLoanAmount = Double.parseDouble(constantValue);
						}
						else if(constant.equals("MAX_OWNERS"))
						{
							Command.maxOwner = Integer.parseInt(constantValue);
						}
						else if(constant.equals("MAX_ACCOUNTS"))
						{
							Command.maxAccount = Integer.parseInt(constantValue);
						}
						else if(constant.equals("MIN_CREDIT_AGE"))
						{
							CreditAccount.minCreditAge = Integer.parseInt(constantValue);
						}
	        	   	}
	           }
	           inputStream.close( );
	       }
	       catch(FileNotFoundException e)
	       {
	           System.out.println("WARNING:File BankSys.ini was not found or could not be opened.");
	           System.out.println("Constant values are set as default value");
	       }	       
	       catch(IOException e)
	       {
	           System.out.println("Error reading from BankSys.ini.");
	       }

	}
	
	/**
	 * writeToFile method has responsible for writing any object to file
	 * @param writeObject	: Any objects that system will write to file
	 * @param fileName		: File name of the file
	 */
	public void writeToFile(Object writeObject, String fileName ){
		try{
			ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
			outputStream.writeObject(writeObject);
			outputStream.close();
			}
		catch(IOException io){
			System.out.println("I/O exception"+io );
		}
	}
	/**
	 * readAccountFromFile read all objects in the Account file and assign those object to ArrayList
	 * @param accountArrayListObject: ArrayList of Account class
	 * @param fileName				: the name of file which will be read
	 * @return accountArrayListObject
	 */
	public ArrayList<Account> readAccountFromFile(ArrayList<Account> accountArrayListObject, String fileName){
		try
        {
		   ObjectInputStream accountInputStream = new ObjectInputStream(new FileInputStream(fileName));	
           accountArrayListObject = (ArrayList<Account>)accountInputStream.readObject();
       	  
       	   accountInputStream.close();
       	   return accountArrayListObject;
        }
        catch(FileNotFoundException e)
        {
            System.out.print("Cannot find "+fileName+" file. ");
            System.out.println("'"+fileName+"' will be created.");
            return accountArrayListObject;
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Problems with file input.");
        }
        catch(IOException e)
        {
            System.out.println("Problems with file input.");
        }
        return accountArrayListObject;
	}
	
	
	/**
	 * readCustomerFromFile read all objects in the Customer file and assign those object to ArrayList
	 * @param customerArrayListObject	: ArrayList of Customer class
	 * @param fileName					: the name of file which will be read
	 * @return customerArrayListObject
	 */
	public ArrayList<Customer> readCustomerFromFile(ArrayList<Customer> customerArrayListObject, String fileName){
		try
        {
		   ObjectInputStream customerInputStream = new ObjectInputStream(new FileInputStream(fileName));	
           customerArrayListObject = (ArrayList<Customer>)customerInputStream.readObject();
       	   
       	   customerInputStream.close();
       	   return customerArrayListObject;
        }
        catch(FileNotFoundException e)
        {
        	System.out.print("Cannot find "+fileName+" file. ");
        	System.out.println("'"+fileName+"' will be created.");
            return customerArrayListObject;
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Problems with file input.");
        }
        catch(IOException e)
        {
            System.out.println("Problems with file input.");
        }
        return customerArrayListObject;
	}
	
	
	/**
	 * readTransactionFromFile read all objects in the Transaction file and assign those object to ArrayList
	 * @param transactionArrayListObject: ArrayList of Transaction class
	 * @param fileName					: the name of file which will be read
	 * @return transactionArrayListObject
	 */
	public ArrayList<Transaction> readTransactionFromFile(ArrayList<Transaction> transactionArrayListObject, String fileName){
		try
        {
		   ObjectInputStream customerInputStream = new ObjectInputStream(new FileInputStream(fileName));
           transactionArrayListObject = (ArrayList<Transaction>)customerInputStream.readObject();
       	   
       	   customerInputStream.close();
       	   return transactionArrayListObject;
        }
        catch(FileNotFoundException e)
        {
        	System.out.print("Cannot find "+fileName+" file. ");
        	System.out.println("'"+fileName+"' will be created.");
            return transactionArrayListObject;
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Problems with file input.");
        }
        catch(IOException e)
        {
            System.out.println("Problems with file input.");
        }
        return transactionArrayListObject;
	}
	


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -