How do you use methods as args? I can not seem to get the string or file stored in the getters and setters to work as args for the printWriter.etc. Unless something else in this code is causing it. all the getters and setters work everywher else

/*
 * CardData.java
 *
 * Created on October 1, 2007, 11:28 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package creditcard;

//import db.CreditCardService;

import java.io.File;


import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import creditcard.ProcessCard;
import tio.FormattedWriter;
import java.io.File;

/**
 *
 * @author James
 */
public class CardData extends ProcessCard{
    private ProcessCard pc;
    private File file;
    
    /** Creates a new instance of CardData */
    public CardData() throws FileNotFoundException, IOException, SQLException, Exception {
        super();
        setStatusFileString(getStatusFileName());
        setLogFileString(getLogFileName());
    }
    public void accountStatus() throws IOException, SQLException, Exception{
        FileWriter fw=new FileWriter(getStatusFile(),true);
        fw.write(("Account Type: "+getAcctType()));
        fw.write("Account: " + getAccountNo());
        fw.write("Date: "+getDate());
        if(AcctType==("Credit")){
            fw.write("Credit Limit: " + getCreditLimit());
            fw.write("Account Balance: "+getBank());
            fw.write("Available Credit: " +getAvailable());
            fw.write("Outstanding Balance: " +getBalance());
        }
        fw.write("Last Transaction was Charge of: " + getCharge()+ "on "+getDate());
        fw.write("Description; " + getDescription());
        if(AcctType==("Debit")){
            fw.write("Last Transaction was charge of: " + getPayment()+ "on "+getDate());
        }else{
            fw.write("Last Transaction was payment of: " + getPayment()+ "on "+getDate());
            fw.write("Total Charges: " + getTotalCharges());
            fw.write("Total Payments " + getTotalPayments());
        }
    }
    public void writeLog() throws FileNotFoundException, IOException, SQLException, Exception {
       File f = new File(getLogFileString());
        FormattedWriter out = new FormattedWriter(new FileWriter(getLogFile().toString(),true));
        out.printfln(getAcctType());
        out.printfln(getAccountNo());
        out.printfln(getDate());
        out.printfln(getBank());
        out.printfln(getPayment());
        if(getPayment()==0.0){
            setDescription("Deposit");
        }
        out.printfln(getDescription());
        out.printfln(getPayment());
        out.printfln(getDescription());
        out.close();
        CreditCardService ccs = new CreditCardService();
    }
}

Thanks

Recommended Answers

All 5 Replies

sorry i could not follow you... probably i m too sleepy... are you trying to say your constructor is not actually working bcoz set/get methods are not working? as i can't see the methods, i can't tell u why and what is not working... li'l advice for u... remove "throws Exception" clause... it will give you at least some IMPORTANT error msg by which u can debug ur program easily.

are you sure you've actually stored data in the object before using the getter?

Yes there is another class and it has all the variable .But I need to get those values and create a file with the value in one of the getters of another class
FileWriter file = new File(getStatusFile(),true) and that getStatusFile should return the name of the file to create but it creates a file called "getStatusFile" instead. Any Help?
Thanks

well... getStatusFile() will only return something if it has something to return, otherwise it may or may not return null based upon your code.
so plz check if you have set up your value correctly at the moment you are calling your get method. Having the same variable in your super class does not guarantee any value to be there. Besides, if your values are already set, then what is the purpose of getting the same value and setting it again?
Your question has insufficient information for any one to solve it... but you still can do one thing... print the returned value of getStatusFile(), and thus go back, print the same thing in the super constructor... you can find out what is wrong.

I have to tasks to write to a StatusFile and to a LogFile. When I insert getStatusFile() for instance, into

FileWriter fw=new FileWriter(getStatusFile(),true);

I would like it to write to the file that was created in the ProcessCard.class in which I am trying to call getStatusFile() and whatever file that happens to be there it will write to it.
Is This possible?

/*
 * TestClass.java
 *
 * Created on October 1, 2007, 11:28 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package creditcard;
import java.io.*;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.sql.SQLException;
import creditcard.CreditCard.*;
import tio.*;
import creditcard.ProcessCard.*;

/**
 *
 * @author James
 */
public class TestClass extends ProcessCard{
    private ProcessCard pc;
        public static void main(String[] args) throws IOException, SQLException, Exception {
            new TestClass();
        }
        public TestClass() throws IOException, SQLException, Exception{
            super();
            System.out.println("Welcome to the Credit Card simulator!");
            System.out.println("Debit or Credit transaction (1=Debit, 2=Credit, 0=Exit): ");
            setAcctTypeCode(tio.Console.in.readInt());
            
            System.out.println("Existing Account, New Account, or Exit? (Existing Account=1, New Account=2, Exit=0): ");
            setAcctStatusCode(tio.Console.in.readInt());
            if(getAcctStatusCode()==0){
                System.exit(0);
            }
            if(getAcctStatusCode()==1){
                System.out.println("Existing Account: enter Account #: ");
                setAccountNo(tio.Console.in.readLong());
                setData();
            }
            if(getAcctStatusCode()==2){
                newAccount();
                setData();
            }
        }

}
/*
 * ProcessCard.java
 *
 * Created on September 29, 2007, 11:53 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package creditcard;
import creditcard.TestClass;
import creditcard.CardData;
//import db.CreditCardService;
//import db.DBConnection;
import java.lang.Object.*;
import java.io.*;
import java.sql.*;
import java.util.Date;
import tio.FormattedWriter;
import java.io.Writer;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

/**
 *
 * @author James
 */
public class ProcessCard {
    private CardData cd;
    private TestClass cct;
    private CreditCardService ccs;
    private Connection conn = null;
  //  private DBConnection db;
    private long AccountNo;
    private double CreditLimit;
    private double Payment;
    private double TotalCharges;
    private double TotalPayments;
    private double Amount;
    private double Charge;
    private double Available;
    private double Balance;
    private int TransResult;
    private String TransErr;
    private String Description;
    private double increase=100;
    private String log;
    public String AcctType;
    private int Type;
    private FormattedWriter out;
    private Statement stm = null;
    private ResultSet rst = null;
    private String tableName = "acctdata";
    private String query;
    private String date;
    private int AcctTypeCode;
    private int AcctStatusCode;
    private int Inval;
    private int Transval;
    private double Bank;
    private String Transdesc;
    private String StatusFileString;
    private String LogFileString;
    private File StatusFile;
    private File LogFile;
    private String LogFileName;
    private String LogFilePath;
    private File file;
    
  private String StatusFileName;
  private String getLogFileString;

    private String Status;
    
    public void setData() throws FileNotFoundException, IOException, SQLException, Exception{
        if(getAcctTypeCode()==2){
            setAcctType("Credit");
            setCreditLimit(1000);
            setTotalCharges(0);
            setTotalPayments(0);
            setStatusFileName("CC_Num_status.txt");
            File StatusFile = new File(getStatusFileName());
            setStatusFile(StatusFile);
            setLogFileName("CC" + AccountNo + ".txt");
            File LogFile = new File(getLogFileName());
            setLogFile(LogFile);
          
            
            Available();
            Balance();
        }
        if(getAcctTypeCode()==1){
            setAcctType("Debit");
            setCreditLimit(1000);
            setTotalCharges(0);
            setTotalPayments(0);
             setStatusFileName("DC_Num_status.txt");
            //File StatusFile = new File(getStatusFileName());
            //setStatusFile(StatusFile);
            setLogFileName("DC" + AccountNo + ".txt");
            //File LogFile = new File(getLogFileName());
            //setLogFile(StatusFile);
            Available();
            Balance();
        }
        
        if(getAcctType()==("Exit")){
            System.exit(0);
        }
        if(getAcctType()==("Credit")){
            if(getAcctTypeCode()==1){
                creditTemp();
            }else{
                newAccount();
                creditTemp();
            }
        }
        if(getAcctType()==("Debit")){
            if(getAcctTypeCode()==1){
                debitTemp();
            }else{
                newAccount();
                debitTemp();
            }
        }
        if(getAcctTypeCode()==0){
            System.exit(0);
        }
    }
    public void newAccount() throws FileNotFoundException, IOException {
        double r = Math.random();
        long x = (long)(r * 100000000);
        setAccountNo(x);
    }
    public void creditIncrease()throws FileNotFoundException, IOException, SQLException, Exception{
        TransResult = 0;
        if (Math.random() > .25) {
            CreditLimit += increase;
            setCreditLimit(CreditLimit);
            setDescription("Creditlimit increase of: $"+increase);
            System.out.println("Creditlimit increase of: $"+increase);
            cd.accountStatus();
        } else {
            setDescription("Creditlimit increase denied: "+getDate());
            System.out.println("Sorry, credit increase not possible at this time.");
            TransResult = 1;
            cd.accountStatus();
        }
    }
    public void TotalPayments(){
        TotalPayments=TotalPayments+Payment;
        setTotalPayments(TotalPayments);
        setCharge(0);
        
    }
    public void TotalCharges(){
        TotalCharges=TotalCharges+Charge;
        setTotalCharges(TotalCharges);
        setPayment(0);
    }
    public void Available() {
        double Available = CreditLimit - ( TotalCharges - TotalPayments );
        
        setAvailable(Available);
        if(getAcctType()=="Debit"){
            setBank(Available);
        }
    }
    
    public void Balance() {
        double Balance = ( TotalCharges - TotalPayments );
        
        setBalance(Balance);
    }
    
    public void creditTemp() throws FileNotFoundException, IOException, SQLException, Exception{
        while(getAcctStatusCode()!=0){
            System.out.println("Account Type: "+getAcctType());
            System.out.println("Account#: " +getAccountNo());
            System.out.println("Date: " +getDate());
            System.out.println("Credit Limit: " +getCreditLimit());
            System.out.println("Available Credit: " +getAvailable());
            System.out.println("Outstanding Balance: " +getBalance());
            System.out.println("Charge: " +getCharge());
            System.out.println("Description; " +getDescription());
            System.out.println("payment: " +getPayment());
            System.out.println("Total Charges: " +getTotalCharges());
            System.out.println("Total Payments " +getTotalPayments());
            System.out.println("\n");
            System.out.println("Transaction (0=Quit, +=$=charge, -$=payment, 9999=Limit increase): ");
            setTransval(System.in.read());
            if (getTransval() == 0){
                break;
            }
            if (getTransval() == 9999){
                creditIncrease();
            }else{
                if (getTransval() > 0){
                    System.out.println("Transaction description: ");
                    setTransdesc(tio.Console.in.readLine());
                    setTransdesc(tio.Console.in.readLine());
                    setAmount(getTransval());
                    Transaction();
                }else{
                    if(getTransval()==-1){
                        return;//////////////pending
                    }else{
                        if(getTransval() < 0){
                            setTransdesc("Payment");
                            setAmount(getTransval());
                            Transaction();
                        }
                    }
                }
            }
        }
    }
    
    public void debitTemp() throws FileNotFoundException, IOException, SQLException, Exception{
        while(getAcctStatusCode()!=0){
            System.out.println("Account Type: "+getAcctType());
            System.out.println("Debit Account Balance: "+getBank());
            System.out.println("Account#: " +getAccountNo());
            System.out.println("Date: " +getDate());
            System.out.println("Charge: " +getCharge());
            System.out.println("Description; " +getDescription());
            System.out.println("Deposit; "+getPayment());
            System.out.println("Total Charges: " + getTotalCharges());
            System.out.println("Total Deposits " + getTotalPayments());
            System.out.println("\n");
            System.out.println("Transaction (0=no transaction, -=deposit, +=new charge, -1=Display Logs): ");
            setTransval(tio.Console.in.readInt());
            if (getTransval() == 0){
                break;
            }
            if (getTransval() == -1){
                //pending
            }else{
                if (getTransval() > 0){
                    System.out.println("Transaction description: ");
                    setTransdesc(tio.Console.in.readLine());
                    setTransdesc(tio.Console.in.readLine());
                    setAmount(getTransval());
                    Transaction();
                }else
                    if(getTransval() < 0){
                    setTransdesc("Payment");
                    setAmount(getTransval());
                    Transaction();
                    }
            }
        }
    }
    public void Transaction() throws FileNotFoundException, IOException, SQLException, Exception {
        CardData cd = new CardData();
        TransResult = 0;
        if ( Amount == 0 ) {
            TransResult = 1;
            setAmount(Amount);
            setDescription("Transaction amount is 0.");
            return;
        }
        if ( Amount > Available ) {
            setAmount(Amount);
            TransResult = 1;
            setDescription("Transaction amount of $" + Amount + " has exceeded the available credit limit $" + getAvailable());
            return;
        }
        if ( Description == "" ) {
            TransResult = 1;
            setDescription("No transaction description entered.");
            return;
        }
        if ( Amount > 0 ) {
            setCharge(Amount);
            TotalCharges();
            Available();
            Balance();
            if(AcctType==("Debit")){
                setBank(getAvailable());
            }
            cd.accountStatus();
            cd.writeLog();
        }
        if ( Amount < 0 ) {
            setPayment(-Amount);//this actually is set up to increase Payment variable positively.
            if(getAcctType()==("Debit")){
                setDescription("Deposit");
            }else{
                setDescription("Payment");
            }
            TotalPayments();
            Available();
            Balance();
            if(AcctType.equals("Debit")){
                setBank(getAvailable());
            }
            cd.accountStatus();
            cd.writeLog();
        }
    }
   
    
    int getAcctTypeCode() {
        return AcctTypeCode;
    }
    void setAcctTypeCode(int AcctTypeCode){
        this.AcctTypeCode=AcctTypeCode;
    }
    void setAcctType(String AcctType){
        this.AcctType=AcctType;
    }
    String getAcctType(){
        return AcctType;
    }
    long getAccountNo(){
        return AccountNo;
    }
    String getDate() {
        return date;
    }
    void setDate(String date) {
        this.date=date;
    }
    double getAmount(){
        return Amount;
    }
    double getBank(){
        return Bank;
    }
    double getPayment(){
        return Payment;
    }
    double getCreditLimit(){
        return CreditLimit;
    }
    
    double getTotalCharges() {
        return TotalCharges;
    }
    
    String getDescription() {
        return Description;
    }
    
    double getTotalPayments() {
        return TotalPayments;
    }
    double getAvailable(){
        return Available;
    }
    double getBalance(){
        return Balance;
    }
    double getCharge() {
        return Charge;
    }
    
    
    void setAccountNo(long AccountNo) {
        this.AccountNo=AccountNo;
    }
    
    void setAmount(double Amount){
        this.Amount=Amount;
    }
    void setBank(double Bank){
        this.Bank=Bank;
    }
    void setPayment(double Payment){
        this.Payment=Payment;
    }
    void setCreditLimit(double CreditLimit){
        this.CreditLimit=CreditLimit;
    }
    
    void setTotalCharges(double TotalCharges) {
        this.TotalCharges=TotalCharges;
    }
    
    void setDescription(String Description) {
        this.Description=Description;
    }
    
    void setTotalPayments(double TotalPayments) {
        this.TotalPayments=TotalPayments;
    }
    
    void setAvailable(double Available) {
        this.Available=Available;
    }
    
    void setBalance(double Balance) {
        this.Balance=Balance;
    }
    void setCharge(double Charge){
        this.Charge=Charge;
    }
    
    void setTransval(int Transval) {
        this.Transval=Transval;
    }
     int getTransval() {
        return Transval;
    }
    void setTransdesc(String Transdesc) {
        this.Transdesc=Transdesc;
    }
    String getTransdesc() {
        return Transdesc;
    }
    
    void setStatus(String Status){
        this.StatusFileString=StatusFileString;
    }
    String getStatus(){
        return Status;
    }
   
    void setStatusFileString(String StatusFileString) {
        this.StatusFileString=StatusFileString;
    }
    String getStatusFileString(){
        return StatusFileString;
    }
    void setStatusFile(File StatusFile) {
        this.StatusFile=StatusFile;
    }
    File getStatusFile(File StatusFile){
        return StatusFile;
    }
    void setLogFileString(String LogFileString){
        this.LogFileString=LogFileString;
    }
    String getLogFileString(){
        return LogFileString;
    }
    void setLogFile(File LogFile){
        this.LogFile=LogFile;
    }
    File getLogFile(){
        return LogFile;
    }
   
    void setAcctStatusCode(int AcctStatusCode){
        this.AcctStatusCode=AcctStatusCode;
    }
    int getAcctStatusCode(){
        return AcctStatusCode;
    }
    
    void setStatusFileName( String StatusFileName) {
        this.StatusFileName=StatusFileName;
    }
    String getStatusFileName(){
        return StatusFileName;
    }
    void setLogFileName( String LogFileName) {
        this.LogFileName=LogFileName;
    }
    String getLogFileName(){
        return LogFileName;
    }
    void setLogFilePath(String LogFilePath) {
        this.LogFilePath = LogFilePath;
    }
    String getLogFilePath() {
        return LogFilePath;
    }
}
/*
 * CardData.java
 *
 * Created on October 1, 2007, 11:28 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package creditcard;

//import db.CreditCardService;

import java.io.File;


import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import creditcard.ProcessCard;
import tio.FormattedWriter;
import java.io.File;

/**
 *
 * @author James
 */
public class CardData extends ProcessCard{
    private ProcessCard pc;
    private File file;
    
    /** Creates a new instance of CardData */
    public CardData() throws FileNotFoundException, IOException, SQLException, Exception {
        super();
        setStatusFileString(getStatusFileName());
        setLogFileString(getLogFileName());
    }
    public void accountStatus() throws IOException, SQLException, Exception{
        FileWriter fw=new FileWriter(getStatusFile(),true);
        fw.write(("Account Type: "+getAcctType()));
        fw.write("Account: " + getAccountNo());
        fw.write("Date: "+getDate());
        if(AcctType==("Credit")){
            fw.write("Credit Limit: " + getCreditLimit());
            fw.write("Account Balance: "+getBank());
            fw.write("Available Credit: " +getAvailable());
            fw.write("Outstanding Balance: " +getBalance());
        }
        fw.write("Last Transaction was Charge of: " + getCharge()+ "on "+getDate());
        fw.write("Description; " + getDescription());
        if(AcctType==("Debit")){
            fw.write("Last Transaction was charge of: " + getPayment()+ "on "+getDate());
        }else{
            fw.write("Last Transaction was payment of: " + getPayment()+ "on "+getDate());
            fw.write("Total Charges: " + getTotalCharges());
            fw.write("Total Payments " + getTotalPayments());
        }
    }
    public void writeLog() throws FileNotFoundException, IOException, SQLException, Exception {
       File f = new File(getLogFileString());
        FormattedWriter out = new FormattedWriter(new FileWriter(getLogFile().toString(),true));
        out.printfln(getAcctType());
        out.printfln(getAccountNo());
        out.printfln(getDate());
        out.printfln(getBank());
        out.printfln(getPayment());
        if(getPayment()==0.0){
            setDescription("Deposit");
        }
        out.printfln(getDescription());
        out.printfln(getPayment());
        out.printfln(getDescription());
        out.close();
        CreditCardService ccs = new CreditCardService();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.