mock credit card with mysql & filewriter INSERT into mysql

Reply

Join Date: Aug 2007
Posts: 238
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

mock credit card with mysql & filewriter INSERT into mysql

 
0
  #1
Sep 17th, 2007
I made more progress ong CreditCard program I am trying to insert a row into mysql from a creditcard account number. Could I get some feed back on why it as a problem with the url
it looks like it wants to insert a record.


  1. package creditcard;
  2. import db.DBConnection;
  3. import java.lang.Object.*;
  4. import java.io.*;
  5. import java.sql.*;
  6. import creditcard.CreditCardService;
  7. import java.util.Date;
  8. import tio.*;
  9. import java.io.File.*;
  10. import java.io.IOException;
  11.  
  12.  
  13. class CreditCard {
  14. private long AccountNo;
  15. private double CreditLimit;
  16. public double TotalCharges;
  17. public double TotalPayments;
  18. private double Amount;
  19. private int TransResult;
  20. private String TransErr;
  21. private String Description;
  22. private String log=null;
  23. private String date;
  24. private FormattedWriter out;
  25.  
  26. private Statement stm = null;
  27. private ResultSet rst = null;
  28. private String tableName = "acctdata";
  29. private String x;
  30. private String query;
  31. private DBConnection db;
  32. private CreditCardService ccs;
  33. public Connection conn = null;
  34.  
  35.  
  36.  
  37. public CreditCard( ) throws FileNotFoundException, IOException {
  38. newAccount();
  39. CreditLimit = 1000;
  40. TotalCharges = 0;
  41. TotalPayments =0;
  42.  
  43. }
  44.  
  45. public CreditCard(long AccountNo) throws FileNotFoundException, IOException {
  46. this.AccountNo = AccountNo;
  47. CreditLimit = 1000;
  48. TotalCharges = 0;
  49. TotalPayments =0;
  50. //validate AccountNO
  51.  
  52. }
  53. private void accountStatus() throws IOException{
  54.  
  55. String f = "CC_Num_status.txt";
  56. File file = new File(f);
  57. FormattedWriter fw=new FormattedWriter(f);
  58. if(f!=null){
  59.  
  60. fw.printfln("Account: " + accountNumber());
  61. fw.printfln("Credit Limit: " + creditLimit());
  62. fw.printfln("Available Credit: " + Available());
  63. fw.printfln("Outstanding Balance: " + Balance());
  64. fw.printfln("Charge: " + Amount);
  65. fw.printfln("Description; " + Description);
  66. fw.printfln("payment: " + Amount);
  67. fw.printfln("Total Charges: " + TotalCharges);
  68. fw.printfln("Total Payments " + TotalPayments);
  69. //fw.printfln("\n");
  70. // fw.printfln("Transaction (0=Quit, +$=charge, -$=payment, 9999=Limit increase): ");
  71.  
  72. }
  73. }
  74. private void newAccount() throws FileNotFoundException, IOException {
  75. double r = Math.random();
  76. AccountNo = (long)(r * 100000000);
  77.  
  78. }
  79.  
  80. public long accountNumber() {
  81. return AccountNo;
  82. }
  83.  
  84.  
  85.  
  86. private void writeLog(double Amount, String Description) throws FileNotFoundException, IOException, SQLException, Exception {
  87. this.AccountNo = AccountNo;
  88. Date dt = new Date();
  89. dt.getTime();
  90. String f = "CC" + AccountNo + ".txt";
  91. File log = new File(f);
  92. FormattedWriter out = new FormattedWriter(new FileWriter(log,true));
  93. out.printfln(AccountNo);
  94. out.printfln(dt.toString());
  95. out.printfln(Amount);
  96. out.printfln(Description);
  97. out.close();
  98. CreditCardService ccs = new CreditCardService();
  99. ccs.getCreditCardDetails();
  100.  
  101. }
  102. public double creditLimit() {
  103. return CreditLimit;
  104. }
  105.  
  106. public void creditIncrease()throws FileNotFoundException, IOException{
  107. TransResult = 0;
  108. if (Math.random() > .25) {
  109. CreditLimit += 100;
  110. accountStatus();
  111.  
  112. } else {
  113. System.out.println("Sorry, credit increase not possible at this time.");
  114. TransResult = 1;
  115. }
  116. }
  117. public double Available() {
  118. double Available = CreditLimit - ( TotalCharges - TotalPayments );
  119. return Available;
  120. }
  121.  
  122. public double Balance() {
  123. double Balance = ( TotalCharges - TotalPayments );
  124. return Balance;
  125. }
  126. public void Transaction( double Amount, String Description ) throws FileNotFoundException, IOException, SQLException, Exception {
  127.  
  128. TransResult = 0;
  129.  
  130. if ( Amount == 0 ) {
  131. TransResult = 1;
  132. TransErr = "Transaction amount is 0.";
  133. return;
  134. }
  135.  
  136.  
  137. if ( Amount > Available() ) {
  138. TransResult = 1;
  139. TransErr = "Transaction amount of $" + Amount + " has exceeded the available credit limit $" + Available();
  140. return;
  141. }
  142.  
  143.  
  144. if ( Description == "" ) {
  145. TransResult = 1;
  146. TransErr = "No transaction description entered.";
  147. return;
  148. }
  149. if ( Amount > 0 ) {
  150.  
  151. TotalCharges += Amount;
  152. accountStatus();
  153. writeLog(Amount, Description);
  154.  
  155.  
  156. } else if ( Amount < 0 ) {
  157.  
  158. TotalPayments += -(Amount);
  159. accountStatus();
  160. writeLog(Amount, Description);
  161.  
  162. }
  163. }
  164.  
  165. long getAccountNo() {
  166. return AccountNo;
  167. }
  168.  
  169. String getDate() {
  170. return date;
  171. }
  172.  
  173. double getTotalCharges() {
  174. return TotalCharges;
  175. }
  176.  
  177. String getDescription() {
  178. return Description;
  179. }
  180.  
  181. double getTotalPayments() {
  182. return TotalPayments;
  183. }
  184.  
  185. void setAccountNo(long AccountNO) {
  186. this.AccountNo=AccountNo;
  187. }
  188.  
  189. void setDate(String string) {
  190. this.date=date;
  191. }
  192.  
  193. void setTotalCharges(double TotalCharges) {
  194. this.TotalCharges=TotalCharges;
  195. }
  196.  
  197. void setDescription(String Description) {
  198. this.Description=Description;
  199. }
  200.  
  201. void setTotalPayments(double TotalPayments) {
  202. this.TotalPayments=TotalPayments;
  203. }
  204.  
  205. }

  1. package creditcard;
  2. //Credit Card Test - tests CreditCard class
  3.  
  4.  
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.RandomAccessFile;
  8. import java.sql.SQLException;
  9. import tio.*;
  10.  
  11. class CreditCardTest {
  12.  
  13. public static void main(String[] args) throws IOException, SQLException {
  14. new CreditCardTest();
  15. }
  16. public CreditCardTest() throws IOException, SQLException{
  17. int inval;
  18. long AccountNo;
  19. double tranval=-1;
  20. String transdesc = null;
  21. CreditCard cc;
  22. String f = "CC_Num_status.txt";
  23. File file = new File(f);
  24. FormattedWriter fw=new FormattedWriter(f);
  25. System.out.println("Welcome to the Credit Card simulator!");
  26. System.out.println("Existing Account, New Account, or Exit? (Existing Account=1, New Account=2, Exit=0): ");
  27. inval = Console.in.readInt();
  28. if (inval == 0)
  29. return;
  30.  
  31. if (inval == 1) {
  32. System.out.println("Existing Account; enter Account #: ");
  33.  
  34. AccountNo = Console.in.readLong();
  35. if (AccountNo == 0)
  36. return;
  37.  
  38. cc = new CreditCard(AccountNo);
  39.  
  40. } else {
  41. cc = new CreditCard();
  42. }
  43.  
  44. while (tranval != 0) {
  45. System.out.println("Account: " + cc.accountNumber());
  46. System.out.println("Credit Limit: " + cc.creditLimit());
  47. System.out.println("Available Credit: " + cc.Available());
  48. System.out.println("Outstanding Balance: " + cc.Balance());
  49. System.out.println("Charge: " + + tranval);
  50. System.out.println("Description; " + transdesc);
  51. System.out.println("payment: " + tranval);
  52. System.out.println("Total Charges: " + cc.TotalCharges);
  53. System.out.println("Total Payments " + cc.TotalPayments);
  54. System.out.println("\n");
  55. System.out.println("Transaction (0=Quit, +$=charge, -$=payment, 9999=Limit increase): ");
  56. tranval = Console.in.readDouble();
  57.  
  58.  
  59. if (tranval == 0)
  60. break;
  61.  
  62. if (tranval == 9999)
  63. cc.creditIncrease();
  64. else {
  65. if (tranval > 0) {
  66. System.out.println("Transaction description: ");
  67. transdesc = Console.in.readLine();
  68. transdesc = Console.in.readLine();
  69.  
  70. } else
  71. transdesc = "payment";
  72.  
  73. cc.Transaction(tranval, transdesc);
  74.  
  75. }
  76. }
  77. }
  78. }

  1. /*
  2.  * DBConnection.java
  3.  *
  4.  * Created on September 11, 2007, 11:50 AM
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9.  
  10.  
  11.  
  12. package db;
  13. import java.sql.*;
  14. import java.sql.DriverManager;
  15. import java.sql.Connection;
  16. /**
  17.  *
  18.  * @author James
  19.  */
  20. public class DBConnection {
  21. private String connURL="jdbc:mysql://localhost:3306";
  22. private String dbusername="root";
  23. private String dbpassword="ceyesuma";
  24. private String dbdriver= "com.mysql.jdbc.Driver";;
  25. private String dbname = "ccdb";
  26. private String port="3306";
  27. private static DBConnection connection=null;
  28. /** Creates a new instance of DBConnection */
  29. private DBConnection() {
  30. }
  31. public String getDbdriver(){
  32. return dbdriver;
  33. }
  34. public String getConnURL(){
  35. return connURL;
  36. }
  37. public String getDbusername(){
  38. return dbusername;
  39. }
  40. public String getDbpassword(){
  41. return dbpassword;
  42. }
  43. public void setDbdriver(String dbdriver){
  44. this.dbdriver=dbdriver;
  45. }
  46. public void setConnURL(String url){
  47. this.connURL=url;
  48. }
  49. public void setDbusername(String dbusername){
  50. this.dbusername=dbusername;
  51. }
  52. public void setDbpassword(String dbpassword){
  53. this.dbpassword=dbpassword;
  54. }
  55. public void setDbname(String dbname){
  56. this.dbname = dbname;
  57. }
  58. public String getDbname(){
  59. return dbname;
  60. }
  61. public static DBConnection getInstance(){
  62. if(connection==null){
  63. connection = new DBConnection();
  64. }
  65. return connection;
  66. }
  67. public Connection getConnection() throws Exception{
  68. Connection connection = null;
  69. Class.forName(dbdriver);
  70. connection = DriverManager.getConnection(connURL +"/" + dbname, dbusername, dbpassword);
  71. return connection;
  72. }
  73.  
  74. public void setDriver(String dbdriver) {
  75. this.dbdriver = dbdriver;
  76. }
  77.  
  78. public void setConnectionURL(String connectionURL) {
  79. this.connURL=connURL;
  80. }
  81.  
  82. public void setDbName(String dbname) {
  83. this.dbname=dbname;
  84. }
  85.  
  86. public void setPort(String port) {
  87. this.port=port;
  88. }
  89. public void setDbUser(String dbusername) {
  90. this.dbusername=dbusername;
  91. }
  92.  
  93. public void setPassword(String dbpassword) {
  94. this.dbpassword=dbpassword;
  95. }
  96. }
  1. /*
  2.  * CreditCardService.java
  3.  *
  4.  * Created on September 17, 2007, 7:44 AM
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9.  
  10. package creditcard;
  11.  
  12.  
  13. import db.DBConnection;
  14. import java.io.FileNotFoundException;
  15. import java.io.IOException;
  16.  
  17. import java.sql.Connection;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. //import model.CreditCard;
  24. /**
  25.  *
  26.  * @author James
  27.  */
  28.  
  29. public class CreditCardService {
  30.  
  31. private Statement stmt = null;
  32. private ResultSet rs = null;
  33. private String tableName = "acctdata";
  34. private String x;
  35. private String query;
  36. private DBConnection db;
  37. private Connection connection;
  38. private String driver = "com.mysql.jdbc.Driver";
  39. private String dbName = "ccdb";
  40. private String connURL = "jdbc:mysql://localhost:";
  41. private String port = "3306";
  42. private String userName = "root";
  43. private String password = "ceyesuma";
  44. public CreditCardService() throws Exception {
  45. db = DBConnection.getInstance();
  46. db.setDriver(driver);
  47. db.setConnectionURL(connURL);
  48. db.setDbName(dbName);
  49. db.setPort(port);
  50. db.setDbUser(userName);
  51. db.setPassword(password);
  52. connection = db.getConnection();
  53. }
  54. private String [] tableFields = {"ACCOUNTNO","TRANSDATE","CHRG","TRANSDESC","PYMNT"};
  55. public void insert(CreditCard card) throws SQLException{
  56. int i = 0;
  57. query = "SELECT * FROM " + tableName;
  58. stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  59. rs = stmt.executeQuery(query);
  60. rs.next();
  61. rs.moveToInsertRow();
  62. rs.updateLong(tableFields[i++], card.getAccountNo());
  63. rs.updateString(tableFields[i++], card.getDate());
  64.  
  65. rs.updateDouble(tableFields[i++], card.getTotalCharges());
  66. rs.updateString(tableFields[i++], card.getDescription());
  67. rs.updateDouble(tableFields[i++], card.getTotalPayments());
  68. rs.insertRow();
  69. rs.moveToCurrentRow();
  70. }
  71.  
  72. public List<CreditCard> getCreditCardDetails() throws SQLException, FileNotFoundException, IOException{
  73. int i=0;
  74. List<CreditCard> creditCards = new ArrayList<CreditCard>();
  75. query = "SELECT * FROM " + tableName;
  76. stmt = connection.createStatement();
  77. rs = stmt.executeQuery(query);
  78. while(rs.next()){
  79. CreditCard card = new CreditCard();
  80. card.setAccountNo(rs.getLong("ACCOUNTNO"));
  81.  
  82. card.setDate(rs.getString("TRANSDATE"));
  83. card.setTotalCharges(rs.getDouble("CHRG"));
  84.  
  85. card.setDescription(rs.getString("TRANSDESC"));
  86. card.setTotalPayments(rs.getDouble("PYMNT"));
  87. creditCards.add(card);
  88. }
  89. return creditCards;
  90. }
  91. }

  1. Enter password: ********
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 1
  4. Server version: 5.0.41-community-nt MySQL Community Edition (GPL)
  5.  
  6. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  7.  
  8. mysql> USE ccdb;
  9. Database changed
  10. mysql>
  11. mysql> SELECT * FROM acctdata;
  12. +----------+-----------+------+-----------+-------+
  13. | acct_num | transdate | chrg | transdesc | pymnt |
  14. +----------+-----------+------+-----------+-------+
  15. | 11111111 | 2007 | 30 | food | 20 |
  16. +----------+-----------+------+-----------+-------+
  17. 1 row in set (0.91 sec)
  18.  
  19. mysql>

  1. C:\Program Files\MySQL\MySQL Server 5.0\bin\mysql.exe Ver 14.12 Distrib 5.0.41,
  2. for Win32 (ia32)
  3.  
  4. Connection id: 1
  5. Current database: ccdb
  6. Current user: root@localhost
  7. SSL: Not in use
  8. Using delimiter: ;
  9. Server version: 5.0.41-community-nt MySQL Community Edition (GPL)
  10. Protocol version: 10
  11. Connection: localhost via TCP/IP
  12. Server characterset: latin1
  13. Db characterset: latin1
  14. Client characterset: latin1
  15. Conn. characterset: latin1
  16. TCP port: 3306
  17. Uptime: 1 hour 52 min 15 sec
  18.  
  19. Threads: 1 Questions: 7 Slow queries: 0 Opens: 13 Flush tables: 1 Open tabl
  20. es: 1 Queries per second avg: 0.001
  21. --------------
  22.  
  23. mysql>
Last edited by ceyesuma; Sep 17th, 2007 at 4:02 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC