Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package task4;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Scanner;
- public class TransactionTask {
- //Scanner init
- private static Scanner scan;
- static String input;
- // SQL Connection inits
- private static Connection connection = null;
- public static Statement statement = null;
- static final String dbuser = "root";
- static final String dbpassword = "";
- static final String dbname = "school";
- static final String dbport = "3306";
- public static void main(String[] args) {
- scan = new Scanner(System.in);
- HashMap<String, String> transactionDetails = new HashMap<String, String>();
- System.out.println("Please fill in all the fields required:");
- System.out.println("(TERMINAL ID ~ CARD NUMBER ~ EXP DATE ~ PIN BLOCK ~ AMOUNT ~ PROMO CODE)");
- System.out.println("<-==============================================->");
- String input = scan.nextLine();
- String[] fields = input.split("~");
- transactionDetails.put("TerminalID", fields[0].trim());
- transactionDetails.put("Card NR", fields[1]);
- transactionDetails.put("Expiration Date", fields[2]);
- transactionDetails.put("PinBlock", fields[3]);
- transactionDetails.put("Amount", fields[4]);
- transactionDetails.put("PromoCode", fields[5]);
- System.out.println("<-==============================================->");
- // Assigning variables to use for validation
- String cardnr = transactionDetails.get("Card NR");
- String promocode = transactionDetails.get("PromoCode");
- String expdate = transactionDetails.get("Expiration Date");
- String amount = transactionDetails.get("Amount");
- String pin = transactionDetails.get("PinBlock");
- // Validation begin
- if (cardnr.length() < 12 && cardnr.length() > 18 && !cardnr.matches("^-?\\d+$")) {
- System.out.println("Card number should be between 12 and 18 numbers.");
- return;
- } else if (promocode.length() > 13) {
- System.out.println("Invalid promotion code");
- return;
- } else if (expdate.length() > 0 && !validateDate(expdate)) {
- System.out.println("Expiration date is not in valid format or has expired.");
- return;
- } else if (amount.length() > 0 && !isFloat(amount)) {
- System.out.println("Invalid amount entered.");
- return;
- } else if (pin.length() > 0 && !isHex(pin)) {
- System.out.println("You have entered an invalid PIN Block");
- return;
- } else
- System.out.println("Everything is valid!");
- UpdateDB(transactionDetails);
- }
- // Checking if a value is HEX
- public static boolean isHex(String hex) {
- try {
- Long.parseLong(hex, 16);
- return true;
- } catch (NumberFormatException ex) {
- return false;
- }
- }
- // Checking if value is float
- public static boolean isFloat(String f) {
- try {
- Float.parseFloat(f);
- return true;
- } catch (NumberFormatException ex) {
- return false;
- }
- }
- // Method for validating date if its in correct format
- public static boolean validateDate(String expdate) {
- SimpleDateFormat sdf = new SimpleDateFormat("MM/yy");
- Date date = new Date();
- try {
- Date date1 = sdf.parse(expdate);
- return date1.after(date);
- } catch (ParseException ex) {
- return false;
- }
- }
- // Method for checking if String is Integer
- // Not going to use parseInt, cuz this is simpler and doesnt throw anything.
- static boolean isInt(String s) {
- if (s.matches("^[+-]?\\d+$"))
- return true;
- else
- return false;
- }
- //SQL Connection
- private static void connect()
- {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://localhost:" + dbport + "/" + dbname, dbuser, dbpassword);
- }
- catch (Exception e){
- System.out.println(e);
- }
- }
- private static void disconnect()
- {
- try
- {
- connection.close();
- connection = null;
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- }
- private static void UpdateDB(HashMap<String, String> data)
- {
- // try to create table if not exists
- initTable();
- connect();
- try {
- statement = connection.createStatement();
- String sql = String.format("INSERT INTO `transactions` (`terminal_id`, `cardnr`, `expdate`, `pinblock`, `amount`, `promocode`) VALUES "
- + "('%s', '%s', '%s', '%s', '%s', '%s');",
- data.get("TerminalID"),
- data.get("Card NR"),
- data.get("Expiration Date"),
- data.get("PinBlock"),
- data.get("Amount"),
- data.get("PromoCode")
- );
- System.out.println(sql);
- statement.executeUpdate(sql);
- }
- catch(SQLException e)
- {
- // Custom error handling for 1062 (sql - duplicate entry)
- // Just for example how to handle and output custom error messages :D
- if (e.getErrorCode() == 1062)
- {
- System.out.println("This transaction already exists!");
- }
- else
- {
- System.out.println(e);
- }
- }
- disconnect();
- }
- // initialize if the table transactions doesnt exist
- private static void initTable()
- {
- connect();
- try{
- statement = connection.createStatement();
- String sql = "CREATE TABLE IF NOT EXISTS `transactions` ( `terminal_id` VARCHAR(12) NOT NULL , `cardnr` VARCHAR(16) NOT NULL , `expdate` VARCHAR(12) NOT NULL , `pinblock` VARCHAR(12) NOT NULL , `amount` VARCHAR(12) NOT NULL , `promocode` VARCHAR(12) NOT NULL , PRIMARY KEY (`terminal_id`)) ENGINE = InnoDB;";
- statement.execute(sql);
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement