×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: bilal baig
Added: Dec 18, 2017 3:23 PM
Views: 2657
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package dom;
  7.  
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11.  
  12. /**
  13.  *
  14.  * @author Bilal
  15.  */
  16. public class Dom {
  17.  
  18.     public static void main(String[] args) throws SQLException {
  19.  
  20.         Ledger ledger = new Ledger();
  21.         DataBaseManager dbm = new DataBaseManager();
  22.  
  23.         Distributor d=new Distributor();
  24.         d=dbm.getDistributor("10fd032d4ea80e8b68ddad72b616c81fa4b6779c83c379bb0cda796ae1cd29e8");
  25.         Scanner input = new Scanner(System.in);
  26.         int loop = 0;
  27.         do {
  28.             System.out.println(""
  29.                     + "Press 1 to register \n"
  30.                     + "Press 2 to login \n"
  31.                     + "Press 3 to view ledger \n"
  32.                     + "Press 4 to exit \n"
  33.                     + "Choice : "
  34.                     + "");
  35.             loop = input.nextInt();
  36.             switch (loop) {
  37.                 case 1: {
  38.  
  39.                     User u = new User();
  40.                     input.nextLine();
  41.  
  42.                     System.out.println("Enter user name : ");
  43.                     String name = input.nextLine();
  44.                     while (true) {
  45.  
  46.                         System.out.println("Enter Primary Currency Type: \n"
  47.                                 + "1- USD\n"
  48.                                 + "2- PKR\n"
  49.                                 + "3- EUR\n"
  50.                                 + "Enter your choice: ");
  51.                         int input1 = input.nextInt();
  52.                         if (input1 == 1) {
  53.                             u.setCurrencyType("USD");
  54.                             break;
  55.                         }
  56.  
  57.                         if (input1 == 2) {
  58.                             u.setCurrencyType("PKR");
  59.                             break;
  60.                         }
  61.  
  62.                         if (input1 == 3) {
  63.                             u.setCurrencyType("EUR");
  64.                             break;
  65.                         } else {
  66.                             System.out.println("Invalid Input!");
  67.                         }
  68.                     }
  69.                     // data base check
  70.                     String digitalSignature = DigitalSignature.randomString();
  71.                     System.out.println("Digital Signature : " + digitalSignature);
  72.                     String hash = SHA256.hash(name);
  73.                     System.out.println("Hash : " + hash);
  74.  
  75.                     u.setName(name);
  76.                     u.setHash(hash);
  77.                     u.setDigitalSignature(digitalSignature);
  78.                     dbm.insertUser(u);
  79.                     // user data
  80.                     // db
  81.                     break;
  82.                 }
  83.                 case 2: {
  84.                     input.nextLine();
  85.                     boolean flag = false;
  86.                     System.out.println("enter name : ");
  87.                     String name = input.nextLine();
  88.                     String hash = SHA256.hash(name);
  89.                     ArrayList<User> list = new ArrayList<>();
  90.                     list = dbm.getAllUsers();
  91.                     User u = new User();
  92.                     for (int i = 0; i < list.size(); i++) {
  93.                         if (list.get(i).getHash().equals(hash)) {
  94.                             flag = true;
  95.                             u = list.get(i);
  96.                             break;
  97.                         }
  98.                     }
  99.                     if (flag == false) {
  100.                         break;
  101.                     }
  102.                     System.out.println(u.toString());
  103.                     input.nextLine();
  104.                     DarkCoin dc = new DarkCoin();
  105.                     int choice = 0;
  106.                     do {
  107.                         System.out.println("Press 1  to edit Profile\n"
  108.                                 + "Press 2 to sell coin\n"
  109.                                 + "Press 3 to buy coin\n"
  110.                                 + "Press 4 to return\n"
  111.                                 + "Enter your choice: ");
  112.                         choice = input.nextInt();
  113.                         switch (choice) {
  114.                             case 1: {
  115.                                 System.out.println("Enter Account Number: ");
  116.                                 input.nextLine();
  117.                                 u.setAccountNumber(input.nextLine());
  118.                                 System.out.println("Enter Amount: ");
  119.                                 u.setAmount(input.nextDouble());
  120.                                 dbm.updateAccountNumber(u);
  121.                                 dbm.updateAccountAmount(u);
  122.                             }
  123.                             break;
  124.                             case 2: {
  125.  
  126.                                 System.out.println("Enter amount: ");
  127.                                 double coin = input.nextDouble();
  128.                                 input.nextLine();
  129.  
  130.                                 User u2 = new User();
  131.                                 while (true) {
  132.  
  133.                                     System.out.println("Enter username to which you want to sell coin: ");
  134.                                     name = input.nextLine();
  135.                                     hash = SHA256.hash(name);
  136.                                     boolean chk = true;
  137.  
  138.                                     for (int i = 0; i < list.size(); i++) {
  139.                                         if (list.get(i).getHash().equals(hash)) {
  140.                                             u2 = list.get(i);
  141.                                             chk = false;
  142.                                             break;
  143.                                         }
  144.                                     }
  145.                                     if (chk == true) {
  146.                                         System.out.println("User not found\nEnter again..");
  147.                                     } else {
  148.                                         break;
  149.                                     }
  150.                                 }
  151.                                 dc.sellCoin(u, u2, coin);
  152.                             }
  153.                             break;
  154.                             case 3: {
  155.                                 System.out.println("Enter amount: ");
  156.                                 dc.buyCoin(u, input.nextDouble());
  157.                             }
  158.                             break;
  159.                             case 4: {
  160.  
  161.                             }
  162.                             break;
  163.                         }
  164.  
  165.                     } while (choice != 4);
  166.                     System.out.println(u.toString());
  167.                     // db check
  168.                     // profile
  169.                     // options
  170.                     break;
  171.                 }
  172.                 case 3: {
  173.                     System.out.println(ledger.getAll());
  174.                     break;
  175.                 }
  176.                 case 4: {
  177.                     System.out.println("exit");
  178.                     loop = 0;
  179.                     break;
  180.                 }
  181.                 default: {
  182.                     System.out.println("Invalid input");
  183.                     break;
  184.                 }
  185.             }
  186.         } while (loop != 0);
  187.     }
  188. }
  189.