Guest User

Untitled

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