package creditcard; import db.DBConnection; import java.lang.Object.*; import java.io.*; import java.sql.*; import creditcard.CreditCardService; import java.util.Date; import tio.*; import java.io.File.*; import java.io.IOException; class CreditCard { private long AccountNo; private double CreditLimit; public double TotalCharges; public double TotalPayments; private double Amount; private int TransResult; private String TransErr; private String Description; private String log=null; private String date; private FormattedWriter out; private Statement stm = null; private ResultSet rst = null; private String tableName = "acctdata"; private String x; private String query; private DBConnection db; private CreditCardService ccs; public Connection conn = null; public CreditCard( ) throws FileNotFoundException, IOException { newAccount(); CreditLimit = 1000; TotalCharges = 0; TotalPayments =0; } public CreditCard(long AccountNo) throws FileNotFoundException, IOException { this.AccountNo = AccountNo; CreditLimit = 1000; TotalCharges = 0; TotalPayments =0; //validate AccountNO } private void accountStatus() throws IOException{ String f = "CC_Num_status.txt"; File file = new File(f); FormattedWriter fw=new FormattedWriter(f); if(f!=null){ fw.printfln("Account: " + accountNumber()); fw.printfln("Credit Limit: " + creditLimit()); fw.printfln("Available Credit: " + Available()); fw.printfln("Outstanding Balance: " + Balance()); fw.printfln("Charge: " + Amount); fw.printfln("Description; " + Description); fw.printfln("payment: " + Amount); fw.printfln("Total Charges: " + TotalCharges); fw.printfln("Total Payments " + TotalPayments); //fw.printfln("\n"); // fw.printfln("Transaction (0=Quit, +$=charge, -$=payment, 9999=Limit increase): "); } } private void newAccount() throws FileNotFoundException, IOException { double r = Math.random(); AccountNo = (long)(r * 100000000); } public long accountNumber() { return AccountNo; } private void writeLog(double Amount, String Description) throws FileNotFoundException, IOException, SQLException, Exception { this.AccountNo = AccountNo; Date dt = new Date(); dt.getTime(); String f = "CC" + AccountNo + ".txt"; File log = new File(f); FormattedWriter out = new FormattedWriter(new FileWriter(log,true)); out.printfln(AccountNo); out.printfln(dt.toString()); out.printfln(Amount); out.printfln(Description); out.close(); CreditCardService ccs = new CreditCardService(); ccs.getCreditCardDetails(); } public double creditLimit() { return CreditLimit; } public void creditIncrease()throws FileNotFoundException, IOException{ TransResult = 0; if (Math.random() > .25) { CreditLimit += 100; accountStatus(); } else { System.out.println("Sorry, credit increase not possible at this time."); TransResult = 1; } } public double Available() { double Available = CreditLimit - ( TotalCharges - TotalPayments ); return Available; } public double Balance() { double Balance = ( TotalCharges - TotalPayments ); return Balance; } public void Transaction( double Amount, String Description ) throws FileNotFoundException, IOException, SQLException, Exception { TransResult = 0; if ( Amount == 0 ) { TransResult = 1; TransErr = "Transaction amount is 0."; return; } if ( Amount > Available() ) { TransResult = 1; TransErr = "Transaction amount of $" + Amount + " has exceeded the available credit limit $" + Available(); return; } if ( Description == "" ) { TransResult = 1; TransErr = "No transaction description entered."; return; } if ( Amount > 0 ) { TotalCharges += Amount; accountStatus(); writeLog(Amount, Description); } else if ( Amount < 0 ) { TotalPayments += -(Amount); accountStatus(); writeLog(Amount, Description); } } long getAccountNo() { return AccountNo; } String getDate() { return date; } double getTotalCharges() { return TotalCharges; } String getDescription() { return Description; } double getTotalPayments() { return TotalPayments; } void setAccountNo(long AccountNO) { this.AccountNo=AccountNo; } void setDate(String string) { this.date=date; } void setTotalCharges(double TotalCharges) { this.TotalCharges=TotalCharges; } void setDescription(String Description) { this.Description=Description; } void setTotalPayments(double TotalPayments) { this.TotalPayments=TotalPayments; } }
package creditcard; //Credit Card Test - tests CreditCard class import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.sql.SQLException; import tio.*; class CreditCardTest { public static void main(String[] args) throws IOException, SQLException { new CreditCardTest(); } public CreditCardTest() throws IOException, SQLException{ int inval; long AccountNo; double tranval=-1; String transdesc = null; CreditCard cc; String f = "CC_Num_status.txt"; File file = new File(f); FormattedWriter fw=new FormattedWriter(f); System.out.println("Welcome to the Credit Card simulator!"); System.out.println("Existing Account, New Account, or Exit? (Existing Account=1, New Account=2, Exit=0): "); inval = Console.in.readInt(); if (inval == 0) return; if (inval == 1) { System.out.println("Existing Account; enter Account #: "); AccountNo = Console.in.readLong(); if (AccountNo == 0) return; cc = new CreditCard(AccountNo); } else { cc = new CreditCard(); } while (tranval != 0) { System.out.println("Account: " + cc.accountNumber()); System.out.println("Credit Limit: " + cc.creditLimit()); System.out.println("Available Credit: " + cc.Available()); System.out.println("Outstanding Balance: " + cc.Balance()); System.out.println("Charge: " + + tranval); System.out.println("Description; " + transdesc); System.out.println("payment: " + tranval); System.out.println("Total Charges: " + cc.TotalCharges); System.out.println("Total Payments " + cc.TotalPayments); System.out.println("\n"); System.out.println("Transaction (0=Quit, +$=charge, -$=payment, 9999=Limit increase): "); tranval = Console.in.readDouble(); if (tranval == 0) break; if (tranval == 9999) cc.creditIncrease(); else { if (tranval > 0) { System.out.println("Transaction description: "); transdesc = Console.in.readLine(); transdesc = Console.in.readLine(); } else transdesc = "payment"; cc.Transaction(tranval, transdesc); } } } }
/* * DBConnection.java * * Created on September 11, 2007, 11:50 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package db; import java.sql.*; import java.sql.DriverManager; import java.sql.Connection; /** * * @author James */ public class DBConnection { private String connURL="jdbc:mysql://localhost:3306"; private String dbusername="root"; private String dbpassword="ceyesuma"; private String dbdriver= "com.mysql.jdbc.Driver";; private String dbname = "ccdb"; private String port="3306"; private static DBConnection connection=null; /** Creates a new instance of DBConnection */ private DBConnection() { } public String getDbdriver(){ return dbdriver; } public String getConnURL(){ return connURL; } public String getDbusername(){ return dbusername; } public String getDbpassword(){ return dbpassword; } public void setDbdriver(String dbdriver){ this.dbdriver=dbdriver; } public void setConnURL(String url){ this.connURL=url; } public void setDbusername(String dbusername){ this.dbusername=dbusername; } public void setDbpassword(String dbpassword){ this.dbpassword=dbpassword; } public void setDbname(String dbname){ this.dbname = dbname; } public String getDbname(){ return dbname; } public static DBConnection getInstance(){ if(connection==null){ connection = new DBConnection(); } return connection; } public Connection getConnection() throws Exception{ Connection connection = null; Class.forName(dbdriver); connection = DriverManager.getConnection(connURL +"/" + dbname, dbusername, dbpassword); return connection; } public void setDriver(String dbdriver) { this.dbdriver = dbdriver; } public void setConnectionURL(String connectionURL) { this.connURL=connURL; } public void setDbName(String dbname) { this.dbname=dbname; } public void setPort(String port) { this.port=port; } public void setDbUser(String dbusername) { this.dbusername=dbusername; } public void setPassword(String dbpassword) { this.dbpassword=dbpassword; } }
/* * CreditCardService.java * * Created on September 17, 2007, 7:44 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package creditcard; import db.DBConnection; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; //import model.CreditCard; /** * * @author James */ public class CreditCardService { private Statement stmt = null; private ResultSet rs = null; private String tableName = "acctdata"; private String x; private String query; private DBConnection db; private Connection connection; private String driver = "com.mysql.jdbc.Driver"; private String dbName = "ccdb"; private String connURL = "jdbc:mysql://localhost:"; private String port = "3306"; private String userName = "root"; private String password = "ceyesuma"; public CreditCardService() throws Exception { db = DBConnection.getInstance(); db.setDriver(driver); db.setConnectionURL(connURL); db.setDbName(dbName); db.setPort(port); db.setDbUser(userName); db.setPassword(password); connection = db.getConnection(); } private String [] tableFields = {"ACCOUNTNO","TRANSDATE","CHRG","TRANSDESC","PYMNT"}; public void insert(CreditCard card) throws SQLException{ int i = 0; query = "SELECT * FROM " + tableName; stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery(query); rs.next(); rs.moveToInsertRow(); rs.updateLong(tableFields[i++], card.getAccountNo()); rs.updateString(tableFields[i++], card.getDate()); rs.updateDouble(tableFields[i++], card.getTotalCharges()); rs.updateString(tableFields[i++], card.getDescription()); rs.updateDouble(tableFields[i++], card.getTotalPayments()); rs.insertRow(); rs.moveToCurrentRow(); } public List<CreditCard> getCreditCardDetails() throws SQLException, FileNotFoundException, IOException{ int i=0; List<CreditCard> creditCards = new ArrayList<CreditCard>(); query = "SELECT * FROM " + tableName; stmt = connection.createStatement(); rs = stmt.executeQuery(query); while(rs.next()){ CreditCard card = new CreditCard(); card.setAccountNo(rs.getLong("ACCOUNTNO")); card.setDate(rs.getString("TRANSDATE")); card.setTotalCharges(rs.getDouble("CHRG")); card.setDescription(rs.getString("TRANSDESC")); card.setTotalPayments(rs.getDouble("PYMNT")); creditCards.add(card); } return creditCards; } }
Enter password: ******** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.41-community-nt MySQL Community Edition (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> USE ccdb; Database changed mysql> mysql> SELECT * FROM acctdata; +----------+-----------+------+-----------+-------+ | acct_num | transdate | chrg | transdesc | pymnt | +----------+-----------+------+-----------+-------+ | 11111111 | 2007 | 30 | food | 20 | +----------+-----------+------+-----------+-------+ 1 row in set (0.91 sec) mysql>
C:\Program Files\MySQL\MySQL Server 5.0\bin\mysql.exe Ver 14.12 Distrib 5.0.41, for Win32 (ia32) Connection id: 1 Current database: ccdb Current user: root@localhost SSL: Not in use Using delimiter: ; Server version: 5.0.41-community-nt MySQL Community Edition (GPL) Protocol version: 10 Connection: localhost via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: latin1 Conn. characterset: latin1 TCP port: 3306 Uptime: 1 hour 52 min 15 sec Threads: 1 Questions: 7 Slow queries: 0 Opens: 13 Flush tables: 1 Open tabl es: 1 Queries per second avg: 0.001 -------------- mysql>
| DaniWeb Message | |
| Cancel Changes | |