Advertisement
Guest User

Untitled

a guest
Jan 6th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.96 KB | None | 0 0
  1.  
  2. importjava.sql.*;
  3. importjava.util.*;
  4. class Main
  5. {
  6.     public static void main(String a[])
  7.     {
  8.         //Creating the connection
  9.         String url = "jdbc:oracle:thin:@localhost:1521:xe";
  10.         String user = "system";
  11.         String pass = "12345";
  12.  
  13.         //Entering the data
  14.         Scanner k = new Scanner(System.in);
  15.         System.out.println("enter name");
  16.         String name = k.next();
  17.         System.out.println("enter roll no");
  18.         int roll = k.nextInt();
  19.         System.out.println("enter class");
  20.         String cls =  k.next();
  21.  
  22.         //Inserting data using SQL query
  23.         String sql = "insert into student1 values('"+name+"',"+roll+",'"+cls+"')";
  24.         Connection con=null;
  25.         try
  26.         {
  27.             DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  28.  
  29.             //Reference to connection interface
  30.             con = DriverManager.getConnection(url,user,pass);
  31.  
  32.             Statement st = con.createStatement();
  33.             int m = st.executeUpdate(sql);
  34.             if (m == 1)
  35.                 System.out.println("inserted successfully : "+sql);
  36.             else
  37.                 System.out.println("insertion failed");
  38.             con.close();
  39.         }
  40.         catch(Exception ex)
  41.         {
  42.             System.err.println(ex);
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48.  
  49. ////////////////////////////////////////////////////////////////////////
  50.  
  51.  
  52. import java.io.*;
  53. class BankWork
  54. {
  55.     // initialize and declare objects.
  56.     final int max_limit=20;
  57.     final  int min_limit=1;
  58.     final double min_bal=500;
  59.    
  60.     private  String name[]=new String[20];
  61.     private int accNo[]=new int[20];
  62.     private  String accType[]=new String[20];
  63.     private double balamount[]=new double[20];
  64.    
  65.     static int totRec=0;
  66.    
  67.     // create a constructor here of Bank.
  68.        
  69.     BankWork()
  70.     {
  71.         for(int i=0;i<max_limit;i++)
  72.         {
  73.             name[i]="";
  74.             accNo[i]=0;
  75.             accType[i]="";
  76.             balamount[i]=0.0;
  77.         }
  78.     }
  79.  
  80.     // Create method to create New entry.
  81.     public void newEntry()
  82.     {
  83.         String str;
  84.          
  85.         int account;
  86.         double amount;
  87.         boolean permit;
  88.         permit=true;
  89.  
  90.         if (totRec>max_limit)
  91.         {
  92.             System.out.println("\n\n\nSorry we cannot admit you in our bank...\n\n\n");
  93.             permit=false;
  94.         }
  95.  
  96.         // create new entry.
  97.         if(permit = true)
  98.         {
  99.              totRec++;      
  100.              
  101.              // Incrementing Records              
  102.              System.out.println("\n\n\n=====SAVING NEW ENTRY=====");
  103.              try
  104.              {
  105.                  accNo[totRec]=totRec;    //Created  AutoNumber  to accNo so no invalid id occurs
  106.                  System.out.println("Account Number :  "+accNo[totRec]);
  107.                  
  108.                  // create object.
  109.                  BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  110.                  
  111.                  // enter the name of customer here.
  112.                  System.out.print("Enter the name of the Customer : ");
  113.                  System.out.flush();
  114.                  
  115.                  name[totRec]=obj.readLine();
  116.                  
  117.                  // enter the type of account.
  118.                  System.out.print("Enter Account Type : ");
  119.                  System.out.flush();
  120.                  
  121.                  accType[totRec]=obj.readLine();
  122.                  do
  123.                  {
  124.                      // enter the starting amount.
  125.                      // minimum amount must be 1000.
  126.                      System.out.print("Enter Initial  Amount to be deposited : ");
  127.                      System.out.flush();
  128.                      str=obj.readLine();
  129.                      
  130.                      balamount[totRec]=Double.parseDouble(str);
  131.                  }
  132.                  while(balamount[totRec]<min_bal);      
  133.  
  134.                  System.out.println("\n\n\n");
  135.              }
  136.              catch(Exception e)
  137.              {
  138.                  System.out.println("Exception in Entering a record.....");
  139.              }
  140.         }
  141.     }
  142.  
  143.     // create method to display records.
  144.     public void display()
  145.     {    
  146.         String str;
  147.         int account=0;
  148.         boolean valid=true;
  149.                            
  150.         System.out.println("\n\n=====DISPLAYING THE RECORDS=====\n");
  151.         try
  152.         {
  153.             // create object.
  154.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  155.            
  156.             // enter account number.
  157.             System.out.print("Enter the account number for display record : ");
  158.             System.out.flush();
  159.              
  160.             str=obj.readLine();
  161.             account=Integer.parseInt(str);
  162.              
  163.             // check for valid account number
  164.             if (account<min_limit  || account>totRec)  
  165.             {
  166.                 System.out.println("\n\n\nInvalid Account Number \n\n");
  167.                 valid=false;
  168.             }
  169.  
  170.             if (valid==true)
  171.             {    
  172.                 System.out.println("\n\nAccount Number : "+accNo[account]);
  173.                 System.out.println("Name : "+name[account]);
  174.                 System.out.println("Account Type : "+accType[account]);
  175.                 System.out.println("Balance Amount : "+balamount[account]+"\n\n\n");
  176.             }
  177.         }
  178.         catch(Exception e)
  179.         {
  180.             System.out.println("Exception in Displaying record.....");
  181.         }
  182.     }
  183.  
  184.     // create method to deposit amount.
  185.     public void deposit()
  186.     {
  187.         String str;
  188.         double amount;
  189.         int account;
  190.         boolean valid=true;
  191.         System.out.println("\n\n\n=====DEPOSIT AMOUNT=====");
  192.              
  193.         try
  194.         {
  195.             // create object.
  196.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  197.  
  198.             System.out.print("Enter Account No : ");
  199.             System.out.flush();
  200.              
  201.             str=obj.readLine();
  202.             account=Integer.parseInt(str);
  203.            
  204.             // check valid account number.
  205.             if (account<min_limit || account>totRec)  
  206.             {
  207.                 System.out.println("\n\n\nInvalid Account Number \n\n");
  208.                 valid=false;
  209.             }
  210.            
  211.             if (valid==true)
  212.             {
  213.                 System.out.print("Enter Amount you want to Deposit  : ");
  214.                 System.out.flush();
  215.                  
  216.                 str=obj.readLine();
  217.                 amount=Double.parseDouble(str);
  218.  
  219.                 balamount[account]=balamount[account]+amount;
  220.  
  221.                 //Displaying Depsit Details
  222.                 System.out.println("\nAfter Updation...");
  223.                 System.out.println("Account Number :  "+account);
  224.                 System.out.println("Balance Amount :  "+balamount[account]+"\n\n\n");
  225.             }
  226.         }
  227.         catch(Exception e)
  228.         {
  229.             System.out.println("Exception in Depositing record.....");
  230.         }
  231.     }
  232.  
  233.     // creating method for withdraw money.
  234.     public void withdraw()
  235.     {
  236.         String str;
  237.        
  238.         double amount,checkamount;
  239.         int account;
  240.         boolean valid=true;
  241.        
  242.         System.out.println("\n\n\n=====WITHDRAW MONEY=====");
  243.         try
  244.         {
  245.             // create object.
  246.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  247.              
  248.             // enter account number for entering money
  249.             System.out.print("Enter the account number to deposit money : ");
  250.             System.out.flush();
  251.            
  252.             str=obj.readLine();
  253.             account=Integer.parseInt(str);
  254.  
  255.             // check for valid account number.
  256.             if (account<min_limit || account>totRec)  
  257.             {
  258.                  System.out.println("\n\n\nInvalid Account Number \n\n");
  259.                  valid=false;
  260.             }
  261.  
  262.             if (valid==true)
  263.             {
  264.                 System.out.println("Balance is : "+balamount[account]);
  265.                 System.out.print("Enter Amount you want to withdraw  : ");
  266.                 System.out.flush();
  267.                  
  268.                 str=obj.readLine();
  269.                 amount=Double.parseDouble(str);
  270.                 checkamount=balamount[account]-amount;
  271.  
  272.                 if(checkamount >= min_bal)
  273.                 {
  274.                     balamount[account]=checkamount;
  275.                      
  276.                     // Updating the amount after withdraw.
  277.                     System.out.println("\nAfter Updation...");
  278.                     System.out.println("Account Number :  "+account);
  279.                     System.out.println("Balance Amount :  "+balamount[account]+"\n\n\n");
  280.                 }
  281.                 else
  282.                 {
  283.                     System.out.println("\n\nAs per Bank Rule you should maintain minimum balance of Rs 500\n\n\n");
  284.                 }
  285.             }
  286.         }
  287.         catch(Exception e)
  288.         {
  289.             System.out.println("Exception in Withdrawing record.....");
  290.         }
  291.     }      
  292. };
  293.  
  294. class  Bank
  295. {
  296.     public static void main(String args[])
  297.     {
  298.         String str;
  299.         int choice;
  300.         choice=0;
  301.  
  302.         BankWork BW_obj = new BankWork();
  303.        
  304.         do
  305.         {
  306.             // creating Menu.
  307.             System.out.println("Choose Your Choices ...");
  308.             System.out.println("1) New Record Entry ");
  309.             System.out.println("2) Display Record Details ");
  310.             System.out.println("3) Deposit...");
  311.             System.out.println("4) Withdraw...");
  312.             System.out.println("5) Exit");
  313.             System.out.print("Enter your choice :  ");
  314.             System.out.flush();
  315.             try
  316.             {
  317.                 // creating objects.
  318.                 BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  319.                 str=obj.readLine();
  320.                 choice=Integer.parseInt(str);
  321.  
  322.                 switch(choice)
  323.                 {
  324.                     case 1 :
  325.                         // for new entry.
  326.                         BW_obj.newEntry();
  327.                         break;
  328.                            
  329.                     case 2 :
  330.                         // for display.
  331.                         BW_obj.display();
  332.                         break;
  333.                            
  334.                     case 3 :
  335.                         // for deposit.
  336.                         BW_obj.deposit();
  337.                         break;
  338.                            
  339.                     case 4 :
  340.                         // for display.
  341.                         BW_obj.withdraw();
  342.                         break;
  343.                            
  344.                     case 5  :  
  345.                         System.out.println("\n\n.....THANKS FOR VISITING.....");
  346.                         break;
  347.                        
  348.                     default : System.out.println("\nInvalid Choice \n\n");
  349.                 }
  350.             }
  351.             catch(Exception e)
  352.             {
  353.                 System.out.println("Exception in Main....");
  354.             }
  355.         }
  356.         while(choice!=5);
  357.     }
  358. }
  359.  
  360.  
  361.  
  362. //////////////////////////////////////////////////////////////////////
  363.  
  364. import java.io.*;
  365.  
  366. class DecisionTree {
  367.  
  368.  
  369.  
  370.     private class BinTree {
  371.        
  372.    
  373.     private int     nodeID;
  374.         private String  questOrAns = null;
  375.         private BinTree yesBranch  = null;
  376.         private BinTree noBranch   = null;
  377.    
  378.    
  379.    
  380.     public BinTree(int newNodeID, String newQuestAns) {
  381.         nodeID     = newNodeID;
  382.         questOrAns = newQuestAns;
  383.             }
  384.     }
  385.  
  386.     /* OTHER FIELDS */
  387.  
  388.     static BufferedReader    keyboardInput = new
  389.                            BufferedReader(new InputStreamReader(System.in));
  390.     BinTree rootNode = null;
  391.  
  392.    
  393.  
  394.     public DecisionTree() {
  395.     }
  396.  
  397.    
  398.  
  399.     public void createRoot(int newNodeID, String newQuestAns) {
  400.     rootNode = new BinTree(newNodeID,newQuestAns); 
  401.     System.out.println("Created root node " + newNodeID);  
  402.     }
  403.            
  404.    
  405.  
  406.     public void addYesNode(int existingNodeID, int newNodeID, String newQuestAns) {
  407.     // If no root node do nothing
  408.    
  409.     if (rootNode == null) {
  410.         System.out.println("ERROR: No root node!");
  411.         return;
  412.         }
  413.    
  414.     // Search tree
  415.    
  416.     if (searchTreeAndAddYesNode(rootNode,existingNodeID,newNodeID,newQuestAns)) {
  417.         System.out.println("Added node " + newNodeID +
  418.                 " onto \"yes\" branch of node " + existingNodeID);
  419.         }
  420.     else System.out.println("Node " + existingNodeID + " not found");
  421.     }
  422.  
  423.  
  424.     private boolean searchTreeAndAddYesNode(BinTree currentNode,
  425.                 int existingNodeID, int newNodeID, String newQuestAns) {
  426.         if (currentNode.nodeID == existingNodeID) {
  427.         // Found node
  428.         if (currentNode.yesBranch == null) currentNode.yesBranch = new
  429.                 BinTree(newNodeID,newQuestAns);
  430.         else {
  431.             System.out.println("WARNING: Overwriting previous node " +
  432.             "(id = " + currentNode.yesBranch.nodeID +
  433.             ") linked to yes branch of node " +
  434.             existingNodeID);
  435.         currentNode.yesBranch = new BinTree(newNodeID,newQuestAns);
  436.         }      
  437.             return(true);
  438.         }
  439.     else {
  440.         // Try yes branch if it exists
  441.         if (currentNode.yesBranch != null) {    
  442.             if (searchTreeAndAddYesNode(currentNode.yesBranch,
  443.                     existingNodeID,newNodeID,newQuestAns)) {       
  444.                 return(true);
  445.             }  
  446.         else {
  447.                 // Try no branch if it exists
  448.                 if (currentNode.noBranch != null) {
  449.                     return(searchTreeAndAddYesNode(currentNode.noBranch,
  450.                 existingNodeID,newNodeID,newQuestAns));
  451.             }
  452.             else return(false);
  453.             }
  454.             }
  455.         return(false);     
  456.         }
  457.     }  
  458.            
  459.  
  460.  
  461.     public void addNoNode(int existingNodeID, int newNodeID, String newQuestAns) {
  462.    
  463.    
  464.     if (rootNode == null) {
  465.         System.out.println("ERROR: No root node!");
  466.         return;
  467.         }
  468.    
  469.  
  470.    
  471.     if (searchTreeAndAddNoNode(rootNode,existingNodeID,newNodeID,newQuestAns)) {
  472.         System.out.println("Added node " + newNodeID +
  473.                 " onto \"no\" branch of node " + existingNodeID);
  474.         }
  475.     else System.out.println("Node " + existingNodeID + " not found");
  476.     }
  477.    
  478.    
  479.  
  480.     private boolean searchTreeAndAddNoNode(BinTree currentNode,
  481.                 int existingNodeID, int newNodeID, String newQuestAns) {
  482.         if (currentNode.nodeID == existingNodeID) {
  483.          if (currentNode.noBranch == null) currentNode.noBranch = new
  484.                 BinTree(newNodeID,newQuestAns);
  485.         else {
  486.             System.out.println("WARNING: Overwriting previous node " +
  487.             "(id = " + currentNode.noBranch.nodeID +
  488.             ") linked to yes branch of node " +
  489.             existingNodeID);
  490.         currentNode.noBranch = new BinTree(newNodeID,newQuestAns);
  491.         }      
  492.             return(true);
  493.         }
  494.     else {
  495.        
  496.         if (currentNode.yesBranch != null) {    
  497.             if (searchTreeAndAddNoNode(currentNode.yesBranch,
  498.                     existingNodeID,newNodeID,newQuestAns)) {       
  499.                 return(true);
  500.             }  
  501.         else {
  502.                         if (currentNode.noBranch != null) {
  503.                     return(searchTreeAndAddNoNode(currentNode.noBranch,
  504.                 existingNodeID,newNodeID,newQuestAns));
  505.             }
  506.             else return(false); // Not found here
  507.             }
  508.          }
  509.         else return(false); // Not found here
  510.         }
  511.     }  
  512.  
  513.  
  514.  
  515.     public void queryBinTree() throws IOException {
  516.         queryBinTree(rootNode);
  517.         }
  518.  
  519.     private void queryBinTree(BinTree currentNode) throws IOException {
  520.  
  521.         // Test for leaf node (answer) and missing branches
  522.  
  523.         if (currentNode.yesBranch==null) {
  524.             if (currentNode.noBranch==null) System.out.println(currentNode.questOrAns);
  525.             else System.out.println("Error: Missing \"Yes\" branch at \"" +
  526.                     currentNode.questOrAns + "\" question");
  527.             return;
  528.             }
  529.         if (currentNode.noBranch==null) {
  530.             System.out.println("Error: Missing \"No\" branch at \"" +
  531.                     currentNode.questOrAns + "\" question");
  532.             return;
  533.             }
  534.  
  535.         // Question
  536.  
  537.         askQuestion(currentNode);
  538.         }
  539.  
  540.     private void askQuestion(BinTree currentNode) throws IOException {
  541.         System.out.println(currentNode.questOrAns + " (enter \"Yes\" or \"No\")");
  542.         String answer = keyboardInput.readLine();
  543.         if (answer.equals("Yes")) queryBinTree(currentNode.yesBranch);
  544.         else {
  545.             if (answer.equals("No")) queryBinTree(currentNode.noBranch);
  546.             else {
  547.                 System.out.println("ERROR: Must answer \"Yes\" or \"No\"");
  548.                 askQuestion(currentNode);
  549.                 }
  550.             }
  551.         }
  552.  
  553.  
  554.  
  555.     public void outputBinTree() {
  556.  
  557.         outputBinTree("1",rootNode);
  558.         }
  559.  
  560.     private void outputBinTree(String tag, BinTree currentNode) {
  561.  
  562.      
  563.         if (currentNode == null) return;
  564.  
  565.  
  566.  
  567.         System.out.println("[" + tag + "] nodeID = " + currentNode.nodeID +
  568.                 ", question/answer = " + currentNode.questOrAns);
  569.                
  570.    
  571.         outputBinTree(tag + ".1",currentNode.yesBranch);
  572.  
  573.  
  574.         outputBinTree(tag + ".2",currentNode.noBranch);
  575.     }          
  576.     }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement