Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jdk.swing.interop.SwingInterOpUtils;
- import java.sql.*;
- import java.util.LinkedList;
- import java.util.Scanner;
- // import static org.postgresql.jdbc.TimestampUtils.number;
- /*final class Countries{
- public String countryname;
- public Countries(String countryname) {
- this.countryname = countryname;
- }
- }
- */
- final class Story{
- public String storychars;
- public String storyname;
- public String storycode;
- public String storyartist;
- public String storyargument;
- public Story(String storychars, String storyname, String storycode, String storyartist, String storyargument) {
- this.storychars = storychars;
- this.storyname = storyname;
- this.storycode = storycode;
- this.storyartist = storyartist;
- this.storyargument = storyargument;
- }
- }
- final class Books{
- public String bookname;
- public String bookcountry;
- public String booktype;
- public String bookeditor;
- public String bookdate;
- public String booknumber;
- public Books(String bookname, String bookcountry, String booktype, String bookeditor, String bookdate, String booknumber) {
- this.bookname = bookname;
- this.bookcountry = bookcountry;
- this.booktype = booktype;
- this.bookeditor = bookeditor;
- this.bookdate = bookdate;
- this.booknumber = booknumber;
- }
- }
- class Passer {
- public static Books bookdetails() throws Exception {
- LinkedList<String> countryList = new LinkedList<>();
- LinkedList<String> typeList = new LinkedList<>();
- LinkedList<String> editorList = new LinkedList<>();
- Statement st = null;
- ResultSet rs = null;
- System.out.print("Nome: ");
- String bookname = BdDisney.readInput();
- System.out.print("País: ");
- String bookcountry = BdDisney.readInput().toUpperCase();
- try {
- st = BdDisney.connect().createStatement();
- String selectBookCountry = "select bookcountry from countries";
- rs = st.executeQuery(selectBookCountry);
- while(rs.next()) {
- String countryData = rs.getString(1);
- countryList.add(countryData);
- }
- } catch (Exception xD) {
- xD.printStackTrace();
- }
- while (!countryList.contains(bookcountry)) {
- System.out.println(bookcountry + " não faz parte da lista. Por favor escolha uma das seguintes: ");
- for (String s : countryList) {
- System.out.println(s);
- }
- bookcountry = BdDisney.readInput().toUpperCase();
- }
- System.out.print("Tipo: ");
- String booktype = BdDisney.readInput().toUpperCase();
- try {
- st = BdDisney.connect().createStatement();
- String selectBookType = "select booktype from types";
- rs = st.executeQuery(selectBookType);
- while(rs.next()) {
- String typeData = rs.getString(1);
- typeList.add(typeData);
- }
- } catch (Exception xD) {
- xD.printStackTrace();
- }
- while (!typeList.contains(booktype)) {
- System.out.println(booktype + " não faz parte da lista. Por favor escolha uma das seguintes: ");
- for (String s : typeList) {
- System.out.println(s);
- }
- booktype = BdDisney.readInput().toUpperCase();
- }
- System.out.print("Numero: ");
- String booknumber = BdDisney.readInput();
- while (true) {
- try {
- int checkIfInt = Integer.parseInt(booknumber);
- break;
- } catch (Exception xD) {
- System.out.print(booknumber+" is not a number. Please insert a number: ");
- booknumber = BdDisney.readInput();
- continue;
- }
- }
- System.out.print("Data: ");
- String bookdate = BdDisney.readInput();
- // numeric parse (4,0)
- // now the code will stay like this
- // but next time just use bookdate.length
- // and don't spend half an hour
- // trying to reinvent the wheel xd
- while (true) {
- try {
- int checkIfInt = Integer.parseInt(bookdate);
- int checkLength = (int) (Math.log10(checkIfInt) + 1);
- while (checkLength != 4) {
- System.out.println(bookdate+ " is not a valid date format, should be YYYY. Please repeat: ");
- bookdate = BdDisney.readInput();
- checkIfInt = Integer.parseInt(bookdate);
- checkLength = (int) (Math.log10(checkIfInt) + 1);
- }
- break;
- } catch (Exception xD) {
- System.out.println(bookdate+" is not a number! Please repeat: ");
- bookdate = BdDisney.readInput();
- continue;
- }
- }
- System.out.print("Editor: ");
- String bookeditor = BdDisney.readInput().toUpperCase();
- try {
- st = BdDisney.connect().createStatement();
- String selectBookEditor = "select bookeditor from editors";
- rs = st.executeQuery(selectBookEditor);
- while(rs.next()) {
- String editorData = rs.getString(1);
- editorList.add(editorData);
- }
- } catch (Exception xD) {
- xD.printStackTrace();
- }
- while (!editorList.contains(bookeditor)) {
- System.out.println(bookeditor + " não faz parte da lista. Por favor escolha uma das seguintes: ");
- for (String s : editorList) {
- System.out.println(s);
- }
- bookeditor = BdDisney.readInput().toUpperCase();
- }
- return new Books(bookname,bookcountry,booktype,booknumber,bookdate,bookeditor);
- }
- public static Story storydetails() throws Exception {
- System.out.print("Personagens: ");
- String storychars = BdDisney.readInput();
- System.out.print("Nome: ");
- String storyname = BdDisney.readInput();
- System.out.print("Codigo: ");
- String storycode = BdDisney.readInput();
- System.out.print("Arte: ");
- String storyartist = BdDisney.readInput();
- System.out.print("Argumento: ");
- String storyargument = BdDisney.readInput();
- return new Story(storychars,storyname,storycode,storyartist,storyargument);
- }
- }
- public class BdDisney {
- public static String createBook() throws Exception {
- Books book = Passer.bookdetails();
- String name = book.bookname;
- String country = book.bookcountry;
- String type = book.booktype;
- String number = book.booknumber;
- String date = book.bookdate;
- String editor = book.bookeditor;
- String sqlBookStat = "insert into books values (default,"+"'"+name+"','"+country+"','"+type+"','"+editor+"','"+date+"','"+number+"');";
- return sqlBookStat;
- }
- public static String createStory() throws Exception {
- Story story = Passer.storydetails();
- String characters = story.storychars;
- String name = story.storyname;
- String code = story.storycode;
- String artist = story.storyartist;
- String argument = story.storyargument;
- String sqlStoryStat = "insert into stories values (default,"+"'"+characters+"','"+name+"','"+code+"','"+artist+"','"+argument+"');";
- return sqlStoryStat;
- }
- // no searchBook e searchStory meter case
- // de search by what e depois é so adicionar isso
- // ao statement para o psql
- // NAO ESQUECER OS BREAKS NABO!
- public static String searchBook() {
- return "A escolha feita foi PESQUISAR LIVRO";
- // isto é temporario
- }
- public static String searchStory() {
- return "A escolha feita foi PESQUISAR HISTORIA";
- // isto é temporario
- }
- public static String readInput() {
- Scanner reader = new Scanner(System.in);
- return reader.nextLine();
- }
- public static void clearScreen()
- {
- try
- {
- String os = System.getProperty("os.name");
- if (os.contains("Windows"))
- {
- new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
- // Runtime.getRuntime().exec("cls");
- }
- else
- {
- Runtime.getRuntime().exec("clear");
- }
- }
- catch (Exception xD)
- {
- xD.printStackTrace();
- }
- }
- public static Connection connect() {
- String url = "jdbc:postgresql://localhost:5432/bandadesenhada";
- String user = "dap";
- String password= "foobar";
- Connection conn = null;
- try {
- conn = DriverManager.getConnection(url,user,password);
- // System.out.println("Bem-vindo a base de dados BandaDesenhada!");
- }
- catch (SQLException xD) {
- xD.printStackTrace();
- }
- return conn;
- }
- public static void main(String[] args) throws Exception {
- String USERINPUT = "";
- 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+";";
- 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+";";
- 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+";";
- 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+";";
- 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+";";
- 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+";";
- Connection conn = connect();
- // clearScreen();
- Statement stmt = conn.createStatement();
- //ResultSet rs = stmt.executeQuery("XPTO");
- System.out.print("O que deseja fazer? Criar ou Procurar? ");
- String input = readInput();
- while (!input.toLowerCase().equals("criar") && !input.toLowerCase().equals("procurar")) {
- System.out.print(input + " não é uma escolha válida! Por favor repita: ");
- input = readInput();
- }
- switch (input.toLowerCase()) {
- case "criar":
- System.out.print("Criar LIVRO ou HISTORIA? ");
- String createInput = readInput();
- while (!createInput.toLowerCase().equals("livro") && !createInput.toLowerCase().equals("historia")) {
- System.out.print(createInput + " não é uma escolha válida! Por favor repita: ");
- createInput = readInput();
- }
- switch (createInput.toLowerCase()) {
- case "livro":
- String sqlBook = createBook();
- // System.out.println(sqlBook);
- try {
- stmt.execute(sqlBook);
- } catch (SQLException xD) {
- Throwable xDcause = xD.getCause();
- if (xDcause == null) {
- System.out.println("NÃO SEI A CAUSA DA EXCEPTION, ANALISA O STACK TRACE ABAIXO:");
- } else {
- System.out.println("CAUSA DA EXCEPTION:");
- System.out.println(xDcause);
- }
- xD.printStackTrace();
- }
- break;
- case "historia":
- String sqlStory = createStory();
- // e com isto temos o statement pronto para o PSQL (acho eu xd)
- System.out.println(sqlStory);
- break;
- default:
- throw new Exception(createInput+" não é uma escolha válida!");
- }
- break;
- case "procurar":
- System.out.print("Procurar LIVRO ou HISTORIA? ");
- String searchInput = readInput();
- while (!searchInput.toLowerCase().equals("livro") && !searchInput.toLowerCase().equals("historia")) {
- System.out.print(searchInput + " não é uma escolha válida! Por favor repita: ");
- searchInput = readInput();
- }
- switch (searchInput.toLowerCase()) {
- case "livro":
- System.out.println(searchBook());
- break;
- case "historia":
- System.out.println(searchStory());
- break;
- default:
- throw new Exception(searchInput+" não é uma escolha válida!");
- }
- break;
- default:
- throw new Exception(input+" não é uma escolha válida!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement