Guest User

Untitled

a guest
Mar 25th, 2018
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.sql.*;
  3. import java.util.*;
  4.  
  5. class InputSeeker{
  6.     private Scanner sc = new Scanner(System.in);
  7.     private int userId=0;
  8.     private String name=null;
  9.     private String mobileNumber=null;
  10.     private float amountSpent=0;
  11.     private int groupId;
  12.     private String ghostVar=null;
  13.    
  14.     // Name seeker
  15.     public void nameSeeker(){
  16.         System.out.print("Enter the Name: ");
  17.         name = sc.next();
  18.     }
  19.     public String getName() {
  20.         return name;
  21.     }
  22.     // id seeker
  23.     public void idSeeker() {
  24.         System.out.print("Assign the Participant an id : ");
  25.         userId= sc.nextInt();
  26.     }
  27.     public int getId() {
  28.         return userId;
  29.     }
  30.     //number seeker
  31.     public void amountSpentSeeker() {
  32.         System.out.println("Enter the amount: ");
  33.         amountSpent= sc.nextFloat();
  34.     }
  35.     public float getAmountSeeker() {
  36.         return amountSpent;
  37.     }
  38.     //mobile number
  39.     public void mobileNumberSeeker() {
  40.         System.out.println("Enter the mobile number: ");
  41.         mobileNumber=sc.next();
  42.     }
  43.     public String getMobileNumber() {
  44.         return mobileNumber;
  45.     }
  46.     //group id  
  47.     public void groupIdSeeker() {
  48.         System.out.println("Assign Group id: ");
  49.         groupId= sc.nextInt();
  50.     }
  51.     public int getGroupId() {
  52.         return groupId;
  53.     }
  54.     public void ghosVartSeeker(){
  55.         ghostVar=sc.next();
  56.     }
  57.     public String getghostVar() {
  58.         return ghostVar;
  59.     }
  60. }
  61.  
  62. class Uservar{
  63.     int userId;
  64.     String userName;
  65.     float amountSpent;
  66.     String mobileNumber;
  67.     int groupId;
  68.     float finalDebt;
  69. }
  70.  
  71. class UserDAO{
  72.     Connection con =null;
  73.     public void connect(){
  74.         try {
  75.             Class.forName("com.mysql.jdbc.Driver");
  76.             // while in developers mode use verify Server certificate = false && useSSL=true
  77.             con= DriverManager.getConnection("jdbc:mysql://localhost:3306/markone?verifyServerCertificate=false&useSSL=true","root","nishant97");
  78.         } catch (Exception ex) {
  79.             System.out.println(ex);
  80.         }
  81.             }
  82.    
  83.     public void addUser_catalogue(Uservar u) {
  84.     String query ="insert into user_catalogue value(?,?,?,?)";
  85.        
  86.         try {
  87.            
  88.             PreparedStatement pst = con.prepareStatement(query);
  89.             pst.setInt(1,u.userId);
  90.             pst.setString(2,u.userName);
  91.             pst.setInt(3,u.groupId);
  92.             pst.setInt(4, 0);
  93.             pst.executeUpdate();
  94.            
  95.         } catch (Exception ex) {
  96.             System.out.println(ex);
  97.         }
  98.     }
  99.    
  100.     public void addUser_contact(Uservar u) {
  101.         String query ="insert into user_contact value(?,?)";
  102.         try {
  103.             PreparedStatement pst= con.prepareStatement(query);
  104.             pst.setString(1,u.userName);
  105.             pst.setString(2,u.mobileNumber);
  106.             pst.executeUpdate();
  107.            
  108.         }catch(Exception ex){
  109.             System.out.println(ex);
  110.         }
  111.     }
  112.    
  113.     public void addUser_expenditure(Uservar u) {
  114.         String query ="insert into user_expenditure value(?,?)";
  115.         try {
  116.             PreparedStatement pst= con.prepareStatement(query);
  117.             pst.setString(1,u.mobileNumber);
  118.             pst.setFloat(2,u.amountSpent);
  119.             pst.executeUpdate();
  120.            
  121.         }catch(Exception ex){
  122.             System.out.println(ex);
  123.         }
  124.     }
  125.    
  126.    
  127.     public Uservar getUservar(String name) {
  128.         try {  
  129.         String query="select user_catalogue.User_name,user_catalogue.User_id,user_catalogue.group_id,user_catalogue.final_debt,user_contact.mobile_no,user_expenditure.amount_spent\r\n" +
  130.                 "from user_catalogue,user_contact,user_expenditure\r\n" +
  131.                 "where user_catalogue.User_name='"+name+"'"+
  132.                 "and user_contact.User_name= user_catalogue.User_name\r\n" +
  133.                 "and user_contact.mobile_no= user_expenditure.mobile_no";
  134.         String query2="select user_catalogue.User_name,user_catalogue.User_id,user_catalogue.group_id,user_catalogue.final_debt,user_contact.mobile_no,user_expenditure.amount_spent\r\n" +
  135.                 "from user_catalogue,user_contact,user_expenditure\r\n" +
  136.                 "where user_contact.User_name= user_catalogue.User_name\r\n" +
  137.                 "and user_contact.mobile_no= user_expenditure.mobile_no";
  138.                
  139.         Uservar u= new Uservar();
  140.         u.userName=name;
  141.         Statement st = con.createStatement();
  142.         String QUERY=query;
  143.        
  144.         if(name=="show_all") {
  145.             QUERY=query2;
  146.         }
  147.        
  148.         ResultSet rs = st.executeQuery(QUERY);
  149.         rs.next();
  150.         String nname = rs.getString(1);
  151.         int id= rs.getInt(2);
  152.         int groupiid= rs.getInt(3);
  153.         float debt= rs.getFloat(4);
  154.         String mobileNo=rs.getString(5);
  155.         float amount=rs.getFloat(6);
  156.  
  157.        
  158.         u.userId=id;
  159.         u.userName= nname;
  160.         u.groupId=groupiid;
  161.         u.finalDebt=debt;
  162.         u.mobileNumber=mobileNo;
  163.         u.amountSpent=amount;
  164.        
  165.         return u;
  166.         }
  167.        
  168.         catch(Exception ex) {
  169.             System.out.println(ex);
  170.         }
  171.             return null;
  172.     }
  173.    
  174. }
  175. class EntryShowDelete {
  176.    
  177.     public void addUser() {
  178.         UserDAO dao = new UserDAO();
  179.         Uservar uvar= new Uservar();
  180.         InputSeeker is1 = new InputSeeker();
  181.        
  182.         is1.nameSeeker();
  183.         is1.groupIdSeeker();
  184.         is1.idSeeker();
  185.         is1.mobileNumberSeeker();
  186.         is1.amountSpentSeeker();
  187.         dao.connect();
  188.        
  189.         uvar.userId=is1.getId();
  190.         uvar.userName=is1.getName();
  191.         uvar.groupId=is1.getGroupId();
  192.         uvar.mobileNumber=is1.getMobileNumber();
  193.         uvar.amountSpent=is1.getAmountSeeker();
  194.        
  195.         dao.addUser_catalogue(uvar);
  196.         dao.addUser_contact(uvar);
  197.         dao.addUser_expenditure(uvar);
  198.     }
  199.    
  200.     public void showSeacrhedUser() {
  201.         UserDAO dao1= new UserDAO();
  202.         dao1.connect();
  203.         InputSeeker isvar1 = new InputSeeker();
  204.         isvar1.nameSeeker();
  205.         Uservar uvar2 = dao1.getUservar(isvar1.getName());
  206.         String UserData ="User_id: "+uvar2.userId+" User_Name: "+uvar2.userName+" Group_id: "+uvar2.groupId+" Final_debt: "+uvar2.finalDebt+
  207.                 " mobile no.:"+uvar2.mobileNumber+ " Amount Contributed: "+uvar2.amountSpent+"\n";
  208.         System.out.println(UserData);  
  209.     }  
  210.    
  211. }
  212.  
  213. class MiddleEnd{
  214.     public void algo() {
  215.        
  216.     }
  217. }
  218.  
  219. public class User{
  220.  
  221.     public static void main(String[] args) throws IOException{
  222.    
  223.     // EXPECTED MENU FORMAT
  224.         @SuppressWarnings("resource")
  225.         Scanner fcc = new Scanner(System.in);
  226.         System.out.println("Enter one of the following commands:");
  227.         System.out.println("1 - DataEntry");
  228.         System.out.println("2 - Search For Entery(by name): ");
  229.         System.out.println("3 - Print all users with their amount");
  230.         System.out.println("4 - exit");
  231.         System.out.println("Select option: ");
  232.         int choiceentry = fcc.nextInt();
  233.  
  234.          do {
  235.          switch (choiceentry){
  236.          case 1 : EntryShowDelete esd = new EntryShowDelete();
  237.                 esd.addUser();
  238.                 break; 
  239.          case 2: EntryShowDelete esd1 = new EntryShowDelete();
  240.                  esd1.showSeacrhedUser();
  241.                  break;
  242.          case 4:  System.exit(0);
  243.          
  244.          case 3:
  245.             }
  246.  
  247.          System.out.println("Select option: ");
  248.          choiceentry = fcc.nextInt();
  249.          }
  250.          while (choiceentry!=3);
  251.     }
  252. }
Add Comment
Please, Sign In to add comment