Advertisement
nuckrieg

BdDisney Collection - Fixed possible user input errors

Feb 27th, 2020
1,524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.67 KB | None | 0 0
  1. import jdk.swing.interop.SwingInterOpUtils;
  2.  
  3. import java.sql.*;
  4. import java.util.LinkedList;
  5. import java.util.Scanner;
  6.  
  7. // import static org.postgresql.jdbc.TimestampUtils.number;
  8.  
  9. /*final class Countries{
  10.     public String countryname;
  11.     public Countries(String countryname) {
  12.         this.countryname = countryname;
  13.     }
  14. }
  15.  
  16.  */
  17.  
  18.  
  19. final class Story{
  20.     public String storychars;
  21.     public String storyname;
  22.     public String storycode;
  23.     public String storyartist;
  24.     public String storyargument;
  25.     public Story(String storychars, String storyname, String storycode, String storyartist, String storyargument) {
  26.         this.storychars = storychars;
  27.         this.storyname = storyname;
  28.         this.storycode = storycode;
  29.         this.storyartist = storyartist;
  30.         this.storyargument = storyargument;
  31.     }
  32. }
  33.  
  34. final class Books{
  35.     public String bookname;
  36.     public String bookcountry;
  37.     public String booktype;
  38.     public String bookeditor;
  39.     public String bookdate;
  40.     public String booknumber;
  41.  
  42.     public Books(String bookname, String bookcountry, String booktype, String bookeditor, String bookdate, String booknumber) {
  43.         this.bookname = bookname;
  44.         this.bookcountry = bookcountry;
  45.         this.booktype = booktype;
  46.         this.bookeditor = bookeditor;
  47.         this.bookdate = bookdate;
  48.         this.booknumber = booknumber;
  49.     }
  50. }
  51.  
  52. class Passer {
  53.     public static Books bookdetails() throws Exception {
  54.         LinkedList<String> countryList = new LinkedList<>();
  55.         LinkedList<String> typeList = new LinkedList<>();
  56.         LinkedList<String> editorList = new LinkedList<>();
  57.         Statement st = null;
  58.         ResultSet rs = null;
  59.         System.out.print("Nome: ");
  60.         String bookname = BdDisney.readInput();
  61.         System.out.print("País: ");
  62.         String bookcountry = BdDisney.readInput().toUpperCase();
  63.         try {
  64.             st = BdDisney.connect().createStatement();
  65.             String selectBookCountry = "select bookcountry from countries";
  66.             rs = st.executeQuery(selectBookCountry);
  67.             while(rs.next()) {
  68.                 String countryData = rs.getString(1);
  69.                 countryList.add(countryData);
  70.             }
  71.         } catch (Exception xD) {
  72.             xD.printStackTrace();
  73.         }
  74.         while (!countryList.contains(bookcountry)) {
  75.             System.out.println(bookcountry + " não faz parte da lista. Por favor escolha uma das seguintes: ");
  76.             for (String s : countryList) {
  77.                 System.out.println(s);
  78.             }
  79.             bookcountry = BdDisney.readInput().toUpperCase();
  80.         }
  81.         System.out.print("Tipo: ");
  82.         String booktype = BdDisney.readInput().toUpperCase();
  83.         try {
  84.             st = BdDisney.connect().createStatement();
  85.             String selectBookType = "select booktype from types";
  86.             rs = st.executeQuery(selectBookType);
  87.             while(rs.next()) {
  88.                 String typeData = rs.getString(1);
  89.                 typeList.add(typeData);
  90.             }
  91.         } catch (Exception xD) {
  92.             xD.printStackTrace();
  93.         }
  94.         while (!typeList.contains(booktype)) {
  95.             System.out.println(booktype + " não faz parte da lista. Por favor escolha uma das seguintes: ");
  96.             for (String s : typeList) {
  97.                 System.out.println(s);
  98.             }
  99.             booktype = BdDisney.readInput().toUpperCase();
  100.         }
  101.         System.out.print("Numero: ");
  102.         String booknumber = BdDisney.readInput();
  103.         while (true) {
  104.             try {
  105.                 int checkIfInt = Integer.parseInt(booknumber);
  106.                 break;
  107.             } catch (Exception xD) {
  108.                 System.out.print(booknumber+" is not a number. Please insert a number: ");
  109.                 booknumber = BdDisney.readInput();
  110.                 continue;
  111.             }
  112.         }
  113.         System.out.print("Data: ");
  114.         String bookdate = BdDisney.readInput();
  115.         // numeric parse (4,0)
  116.         // now the code will stay like this
  117.         // but next time just use bookdate.length
  118.         // and don't spend half an hour
  119.         // trying to reinvent the wheel xd
  120.         while (true) {
  121.             try {
  122.                 int checkIfInt = Integer.parseInt(bookdate);
  123.                 int checkLength = (int) (Math.log10(checkIfInt) + 1);
  124.                 while (checkLength != 4) {
  125.                     System.out.println(bookdate+ " is not a valid date format, should be YYYY. Please repeat: ");
  126.                     bookdate = BdDisney.readInput();
  127.                     checkIfInt = Integer.parseInt(bookdate);
  128.                     checkLength = (int) (Math.log10(checkIfInt) + 1);
  129.                 }
  130.                 break;
  131.             } catch (Exception xD) {
  132.                 System.out.println(bookdate+" is not a number! Please repeat: ");
  133.                 bookdate = BdDisney.readInput();
  134.                 continue;
  135.             }
  136.         }
  137.         System.out.print("Editor: ");
  138.         String bookeditor = BdDisney.readInput().toUpperCase();
  139.         try {
  140.             st = BdDisney.connect().createStatement();
  141.             String selectBookEditor = "select bookeditor from editors";
  142.             rs = st.executeQuery(selectBookEditor);
  143.             while(rs.next()) {
  144.                 String editorData = rs.getString(1);
  145.                 editorList.add(editorData);
  146.             }
  147.         } catch (Exception xD) {
  148.             xD.printStackTrace();
  149.         }
  150.         while (!editorList.contains(bookeditor)) {
  151.             System.out.println(bookeditor + " não faz parte da lista. Por favor escolha uma das seguintes: ");
  152.             for (String s : editorList) {
  153.                 System.out.println(s);
  154.             }
  155.             bookeditor = BdDisney.readInput().toUpperCase();
  156.         }
  157.         return new Books(bookname,bookcountry,booktype,booknumber,bookdate,bookeditor);
  158.     }
  159.     public static Story storydetails() throws Exception {
  160.         System.out.print("Personagens: ");
  161.         String storychars = BdDisney.readInput();
  162.         System.out.print("Nome: ");
  163.         String storyname = BdDisney.readInput();
  164.         System.out.print("Codigo: ");
  165.         String storycode = BdDisney.readInput();
  166.         System.out.print("Arte: ");
  167.         String storyartist = BdDisney.readInput();
  168.         System.out.print("Argumento: ");
  169.         String storyargument = BdDisney.readInput();
  170.         return new Story(storychars,storyname,storycode,storyartist,storyargument);
  171.     }
  172. }
  173.  
  174.  
  175.  
  176.  
  177. public class BdDisney {
  178.  
  179.     public static String createBook() throws Exception {
  180.         Books book = Passer.bookdetails();
  181.         String name = book.bookname;
  182.         String country = book.bookcountry;
  183.         String type = book.booktype;
  184.         String number = book.booknumber;
  185.         String date = book.bookdate;
  186.         String editor = book.bookeditor;
  187.         String sqlBookStat = "insert into books values (default,"+"'"+name+"','"+country+"','"+type+"','"+editor+"','"+date+"','"+number+"');";
  188.         return sqlBookStat;
  189.     }
  190.  
  191.     public static String createStory() throws Exception {
  192.         Story story = Passer.storydetails();
  193.         String characters = story.storychars;
  194.         String name = story.storyname;
  195.         String code = story.storycode;
  196.         String artist = story.storyartist;
  197.         String argument = story.storyargument;
  198.         String sqlStoryStat = "insert into stories values (default,"+"'"+characters+"','"+name+"','"+code+"','"+artist+"','"+argument+"');";
  199.         return sqlStoryStat;
  200.     }
  201.  
  202.     // no searchBook e searchStory meter case
  203.     // de search by what e depois é so adicionar isso
  204.     // ao statement para o psql
  205.     // NAO ESQUECER OS BREAKS NABO!
  206.     public static String searchBook() {
  207.         return "A escolha feita foi PESQUISAR LIVRO";
  208.         // isto é temporario
  209.     }
  210.  
  211.     public static String searchStory() {
  212.         return "A escolha feita foi PESQUISAR HISTORIA";
  213.         // isto é temporario
  214.     }
  215.  
  216.     public static String readInput() {
  217.         Scanner reader = new Scanner(System.in);
  218.         return reader.nextLine();
  219.     }
  220.  
  221.     public static void clearScreen()
  222.     {
  223.         try
  224.         {
  225.             String os = System.getProperty("os.name");
  226.  
  227.             if (os.contains("Windows"))
  228.             {
  229.                 new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
  230.                 // Runtime.getRuntime().exec("cls");
  231.             }
  232.             else
  233.             {
  234.                 Runtime.getRuntime().exec("clear");
  235.             }
  236.         }
  237.         catch (Exception xD)
  238.         {
  239.             xD.printStackTrace();
  240.         }
  241.     }
  242.  
  243.     public static Connection connect() {
  244.         String url = "jdbc:postgresql://localhost:5432/bandadesenhada";
  245.         String user = "dap";
  246.         String password= "foobar";
  247.         Connection conn = null;
  248.         try {
  249.             conn = DriverManager.getConnection(url,user,password);
  250.             // System.out.println("Bem-vindo a base de dados BandaDesenhada!");
  251.         }
  252.         catch (SQLException xD) {
  253.             xD.printStackTrace();
  254.         }
  255.         return conn;
  256.     }
  257.  
  258.     public static void main(String[] args) throws Exception {
  259.         String USERINPUT = "";
  260.         String selStoryNumber = "select number as \"NUMERO\", type as \"LIVRO\", name as \"TEMA\", date as \"ANO\", editor as \"EDITORA\", country as \"PAIS\" from books where number = "+USERINPUT+";";
  261.         String selStoryType = "select number as \"NUMERO\", type as \"LIVRO\", name as \"TEMA\", date as \"ANO\", editor as \"EDITORA\", country as \"PAIS\" from books where type = "+USERINPUT+";";
  262.         String selStoryName = "select number as \"NUMERO\", type as \"LIVRO\", name as \"TEMA\", date as \"ANO\", editor as \"EDITORA\", country as \"PAIS\" from books where name = "+USERINPUT+";";
  263.         String selStoryDate = "select number as \"NUMERO\", type as \"LIVRO\", name as \"TEMA\", date as \"ANO\", editor as \"EDITORA\", country as \"PAIS\" from books where date = "+USERINPUT+";";
  264.         String selStoryEditor = "select number as \"NUMERO\", type as \"LIVRO\", name as \"TEMA\", date as \"ANO\", editor as \"EDITORA\", country as \"PAIS\" from books where editor = "+USERINPUT+";";
  265.         String selStoryCountry = "select number as \"NUMERO\", type as \"LIVRO\", name as \"TEMA\", date as \"ANO\", editor as \"EDITORA\", country as \"PAIS\" from books where country = "+USERINPUT+";";
  266.  
  267.         Connection conn = connect();
  268.         // clearScreen();
  269.         Statement stmt = conn.createStatement();
  270.         //ResultSet rs = stmt.executeQuery("XPTO");
  271.         System.out.print("O que deseja fazer? Criar ou Procurar? ");
  272.         String input = readInput();
  273.         while (!input.toLowerCase().equals("criar") && !input.toLowerCase().equals("procurar")) {
  274.             System.out.print(input + " não é uma escolha válida! Por favor repita: ");
  275.             input = readInput();
  276.         }
  277.         switch (input.toLowerCase()) {
  278.             case "criar":
  279.                 System.out.print("Criar LIVRO ou HISTORIA? ");
  280.                 String createInput = readInput();
  281.                 while (!createInput.toLowerCase().equals("livro") && !createInput.toLowerCase().equals("historia")) {
  282.                     System.out.print(createInput + " não é uma escolha válida! Por favor repita: ");
  283.                     createInput = readInput();
  284.                 }
  285.                 switch (createInput.toLowerCase()) {
  286.                     case "livro":
  287.                         String sqlBook = createBook();
  288.                         // System.out.println(sqlBook);
  289.                         try {
  290.                             stmt.execute(sqlBook);
  291.                         } catch (SQLException xD) {
  292.                             Throwable xDcause = xD.getCause();
  293.                             if (xDcause == null) {
  294.                                 System.out.println("NÃO SEI A CAUSA DA EXCEPTION, ANALISA O STACK TRACE ABAIXO:");
  295.                             } else {
  296.                                 System.out.println("CAUSA DA EXCEPTION:");
  297.                                 System.out.println(xDcause);
  298.                             }
  299.                             xD.printStackTrace();
  300.                         }
  301.                         break;
  302.                     case "historia":
  303.                         String sqlStory = createStory();
  304.                         // e com isto temos o statement pronto para o PSQL (acho eu xd)
  305.                         System.out.println(sqlStory);
  306.                         break;
  307.                     default:
  308.                         throw new Exception(createInput+" não é uma escolha válida!");
  309.                 }
  310.                 break;
  311.             case "procurar":
  312.                 System.out.print("Procurar LIVRO ou HISTORIA? ");
  313.                 String searchInput = readInput();
  314.                 while (!searchInput.toLowerCase().equals("livro") && !searchInput.toLowerCase().equals("historia")) {
  315.                     System.out.print(searchInput + " não é uma escolha válida! Por favor repita: ");
  316.                     searchInput  = readInput();
  317.                 }
  318.                 switch (searchInput.toLowerCase()) {
  319.                     case "livro":
  320.                         System.out.println(searchBook());
  321.                         break;
  322.                     case "historia":
  323.                         System.out.println(searchStory());
  324.                         break;
  325.                     default:
  326.                         throw new Exception(searchInput+" não é uma escolha válida!");
  327.                 }
  328.                 break;
  329.             default:
  330.                 throw new Exception(input+" não é uma escolha válida!");
  331.  
  332.  
  333.         }
  334.     }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement