Skip to content

Instantly share code, notes, and snippets.

@asyraffff
Created January 10, 2021 03:56
Show Gist options
  • Save asyraffff/b510fd31304f409b848b3d68b396283b to your computer and use it in GitHub Desktop.
Save asyraffff/b510fd31304f409b848b3d68b396283b to your computer and use it in GitHub Desktop.
Initial stuff that I already working on. But maybe, I will add stuff in future.
package yuu.tube;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
// Console class
public class Console {
private static Scanner scanner = new Scanner(System.in); // scanner private variable to store scanner stuff
// method in Console class, we can access it with => Console.signupOrLogin(...)
public static void signupOrLogin(String answer){
// get the input from the user => s/l
switch (answer.toLowerCase()){ // to lower case if user input Upper Case like S or L, easier
case "s":
System.out.println("\nSign Up 🌈");
System.out.print("Username : "); // user will type username
String username = scanner.next(); // store the username
System.out.print("Email : "); // user will type the email
String emailSignUp = scanner.next(); // store the email
System.out.print("Password : "); // user will type the pss
String passSignUp = scanner.next(); // store the pasword
// SQL_Util class => go for that Class file for detail
// SQL_Util have initConnection() method that we can use
// initialize the connection between this app and database(MySQL)
SQL_Util.initConnection();
// again , method from SQL_Util class
// Basically, this method is to get the input from the user(username, email, pass)
// then , store it to the database
SQL_Util.addUser(username, emailSignUp, passSignUp);
System.out.println("\n 🥳 Welcome to the Yuu-Tube " + username + " 🥳");
System.out.println("");
// This is from FrontPage class, go for that class for detail and search choices() method
FrontPage.choices();
break; // importan to put break in case statement
case "l":
System.out.println("\nGreat 🚀");
System.out.print("Email : ");
String emailLogIn = scanner.next();
System.out.print("Password : ");
String passLogIn = scanner.next();
// same like above
SQL_Util.initConnection();
// Basically, this command is like authorization process
// which is to detect either that user is already signUp or not yet
// That's why we need to pass in email and password
// to check at our database, "is this user is already registered???"
SQL_Util.isSignedUp(emailLogIn, passLogIn);
// choces() method from FrontPage class
FrontPage.choices();
break;
default:
System.out.println("\n🚨 S or L word only 🚨");
String answerAgain = scanner.next();
signupOrLogin(answerAgain); // repeat the process
}
}
// This is our choose method, we can access is with Console.choose()
// Essentially, this method determine the page the user want to go
// either to trending page or add new video or log out
// But I still not implemetn the 2 - 6 page, right now , just have 1, which is go to Trending page only
public static void choose(int answer){
switch (answer) {
case 1:
// go to trending page
FrontPage.trending();
break;
default:
System.out.println("Choose [1 - 6] only 😊");
}
}
}
package yuu.tube;
import java.util.Scanner;
// FrontPage class
public class FrontPage {
private static Scanner scanner = new Scanner(System.in); // scanner private variable to store scanner stuff
// I just hard coded the Trending Video Title name
// not from algorithm
// Why have "_" ?? because Java can't read file that have empty in between
// for example : "Do you love me (Boston Dynamics)" <= Java can'r read this
// That's why I put "_" in between
private static String trend1 = "Pink_Sweat$_At_My_Worst_(Officil_l_Video)";
private static String trend2 = "Do_you_love_me_(Boston_Dynamics)";
private static String trend3 = "Can't_Get_You_out_of_My_Head_(Cover)_AnnenMayKantereit_x_Parcels";
private static String trend4 = "Cat_Vibing_To_Ievan_Polkka_(Official_Video_HD)_Cat_Vibing_To_Music_Cat_Vibing_Meme";
private static String trend5 = "Starship_SN8_High_Altitude_Flight_Recap";
// hi method in FrontPage class => FrontPage.hi()
public static void hi(){
System.out.println("");
System.out.println("🔥🔥 Welcome to Yuu-Tube v1.0 🔥🔥\n");
System.out.println("Trending on Yuu-Tube");
System.out.println("1️⃣ Pink Sweat$ At My Worst (Official Video)");
System.out.println("2️⃣ Do you love me (Boston Dynamics)");
System.out.println("3️⃣ Can't Get You out of My Head (Cover) AnnenMayKantereit x Parcels");
System.out.println("4️⃣ Cat Vibing To Ievan Polkka (Official Video HD) Cat Vibing To Music Cat Vibing Meme");
System.out.println("5️⃣ Starship SN8 High Altitude Flight Recap");
System.out.println("");
System.out.println("What are you up to now ? 🏖");
System.out.println("✅ Sign Up (S)\n✅ Log in (L)");
System.out.print("Please select [S or L] : ");
}
// trending method in FrontPage class => FrontPage.trending()
public static void trending(){
System.out.println("");
System.out.println("🔥 Trending on Yuu-Tube");
System.out.println("1️⃣ Pink Sweat$ At My Worst (Official Video)");
System.out.println("2️⃣ Do you love me (Boston Dynamics)");
System.out.println("3️⃣ Can't Get You out of My Head (Cover) AnnenMayKantereit x Parcels");
System.out.println("4️⃣ Cat Vibing To Ievan Polkka (Official Video HD) Cat Vibing To Music Cat Vibing Meme");
System.out.println("5️⃣ Starship SN8 High Altitude Flight Recap");
System.out.println("");
System.out.print("Please choose [1 - 5] : ");
// get the input from the user [1 - 5]
int chooseVideo = scanner.nextInt();
chooseTrendingVideo(chooseVideo);
// this method is from below , chooseTrendingVideo, see below method
// After the Vido is pop out, this prompt will come out
System.out.print("Do you want to watch more ? y | n : ");
String answer = scanner.next(); // store the user answer y/n
// swith case , if y do something, if n do something.
switch (answer.toLowerCase()){ // lowercase the answer if input is Uppercase like Y or N
case "y":
System.out.print("Please choose [1 - 5] : ");
chooseTrendingVideo(scanner.nextInt());
break;
case "n":
// if not , this prompt will come out
System.out.print("Back to Homepage ? y | n : ");
String result = scanner.next();
if (result.equals("y")){ // if result is equal to "y"
// this choices method is from below, go down and see
choices();
int userChoose = scanner.nextInt();
// This is from Console Class that have choose method, go to that class file for detail
Console.choose(userChoose);
}
break;
default:
// if user give wrong input , this prompt will come out
System.out.println("y | n only 😊");
}
}
// method in Frontpage => FrontPage.chooseTrendingVideo(...)
public static void chooseTrendingVideo(int numberVideo){ // need to get the number video
// switch class to open the video
switch (numberVideo){
case 1:
// This is OpenVideo Class that have trendingVideo method, go to that Class File for detail
// Basically, it open the video based on the trendng number video
OpenVideo.trendingVideo(trend1);
break;
case 2:
OpenVideo.trendingVideo(trend2);
break;
case 3:
OpenVideo.trendingVideo(trend3);
break;
case 4:
OpenVideo.trendingVideo(trend4);
break;
case 5:
OpenVideo.trendingVideo(trend5);
break;
default:
System.out.println("Choose [1 - 5] only 😊");
}
}
// method in FrontPage => FrontPage.choices()
// User HomePage
public static void choices(){
System.out.println("");
System.out.println("-----------------------------");
System.out.println("[ 1 ] 🔥 Watch Trending");
System.out.println("[ 2 ] 🛠 Add New Video");
System.out.println("[ 3 ] 🌈 Watch my Video");
System.out.println("[ 4 ] 🚀 Search Video");
System.out.println("[ 5 ] 💁 Edit account");
System.out.println("[ 6 ] 😔 Log Out");
System.out.println("");
System.out.print("Please choose [1 - 6] : ");
}
}
package yuu.tube;
import java.util.Scanner;
import java.sql.*;
public class Main {
public static void main(String[] args) {
// front page
FrontPage.hi(); // from FrontPage class
// SignUp or Login function
Scanner sc = new Scanner(System.in); // Get the user input => s/l
String answer = sc.next(); // store the input
Console.signupOrLogin(answer); // Sign Up or Log in Process
// from Console class
// Choices page
int userChoose = sc.nextInt();
Console.choose(userChoose);
}
}
package yuu.tube;
import java.awt.*;
import java.io.File;
import java.net.URI;
// OpenVideo class
// This is our important features in our app => to open video
public class OpenVideo {
// methond in OpenVideo class, => OpenVideo.trendingVideo(...)
public static void trendingVideo(String trendNumber){
// we need to wrap this code in try/catch block
// First , we try the code in try block
// if it get an error, the catch block will catch tha error and print it in terminal
try {
// string path of my video file in my laptop
// I use concatination which is the concept of adding some string
// for example , the result will be;
// "/Users/amirulasyraf/Documents/video/" + trendNumber + ".mp4"
String path = "/Users/amirulasyraf/Documents/video/Do_you_love_me_(Boston_Dynamics).mp4";
// File class in Java to handle file stuff
File file = new File(path); // we pass file path to the file object
// Desktop class in Java to interact with my laptop or your pc
Desktop desktop = Desktop.getDesktop(); // get my laptop configuration, sth like that
// open method in Desktop class
// which is , open that file
desktop.open(file);
} catch (Exception e){
// if anything error in try block, print the error
e.printStackTrace();
}
}
}
package yuu.tube;
import java.sql.*;
// SQL_Util class
// This class is basically , do some interaction with our database
// is like a third party between our app and MySql database
public class SQL_Util {
// This is my database path, for now , just ignore
public static final String credentials = "jdbc:mysql://localhost:3306/YoutubeDB?user=root&password=password&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC";
static Connection connection = null;
// Connection class , special in SQL Java
// it will turn true if connection is happen
// method in SQL_Util class => SQL_Util.initConnection()
// initialize the connection between our app and database
public static void initConnection() {
if (connection != null){ // if connection is ok, it will return true, if connection is true or false, not null
System.out.println("[WARN] Connection has already been established.");
// I just print the connection is establised
// means, our app is connected with database
return;
}
try {
// all this stuff is just to make a connection with database
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(credentials);
// System.out.println("Yey, connected to YoutubeDB");
} catch (ClassNotFoundException | SQLException e){
// if error, print that error
e.printStackTrace();
}
}
// method is SQL_Util class => SQL_Util.addUser(.....)
public static void addUser(String username, String email, String password){// nedd to pass all that stuff
// we need to wrap it in try/catch block
try {
// Basically , this is an SQL command in Java syntax
// we want to add the username, email and password to our database
// all this "INSERT....." is saying that ;
// insert these username,email and passw to user database (which you guys see our User Database)
PreparedStatement ps = connection.prepareStatement("INSERT INTO user(username, email, password) VALUES(?,?,?)");
ps.setString(1, username);
ps.setString(2, email);
ps.setString(3,password);
ps.execute();
ps.close();
} catch (SQLException e){
e.printStackTrace();
}
}
// method in SQL_Util class => SQL_Util.addVideo(...)
public static void addVideo(String title, int likeCount, int dislikeCount){
try {
// same like before, we want to add new video to our database
// insert that video details in our database
PreparedStatement ps = connection.prepareStatement("INSERT INTO video(title, likeCount, dislikeCount) VALUES(?,?,?)");
ps.setString(1, title);
ps.setInt(2, likeCount);
ps.setInt(3, dislikeCount);
ps.execute();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// method in SQL_Util class => SQL_Util.addVideoToUser(...)
public static void addVideoToUser(int uid, int vid){
try {
// Let us think about the YOutube, essentially , every user must have a video assouciated with them right ?/
// so , this is telling that, add this video id to that user id
// user1
// |_video1
// I hope you understand it
PreparedStatement ps = connection.prepareStatement("INSERT INTO VideoUser(uid, vid) VALUES(?,?)");
ps.setInt(1, uid);
ps.setInt(2, vid);
ps.execute();
ps.close();
} catch (SQLException e){
e.printStackTrace();
}
}
// this method is related to above method(addVideoToUser)
public static int getUid(String username){
try {
// think of above method(addVideoToUser), how we want to get the user and video Id ??
// we can do this in SQL syntax
// this method is telling that , I want the id for that username
PreparedStatement ps = connection.prepareStatement("SELECT * FROM user WHERE username=?");
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()){
int UID = rs.getInt("uid");
ps.close();
rs.close();
System.out.println("UID for " + username + " : " + UID);
return UID;
}
} catch (SQLException e){
e.printStackTrace();
}
return -1;
}
// this method is related to above method(addVideoToUser)
public static int getVid(String title){
try {
// think of above method(addVideoToUser), how we want to get the user and video Id ??
// we can do this in SQL syntax
// this method is telling that , I want the id for that video title
PreparedStatement ps = connection.prepareStatement("SELECT * FROM video WHERE title=?");
ps.setString(1, title);
ResultSet rs = ps.executeQuery();
if (rs.next()){
int VID = rs.getInt("vid");
ps.close();
rs.close();
System.out.println("VID for " + title + " : " + VID);
return VID;
}
} catch (SQLException e){
e.printStackTrace();
}
return -1;
}
// this method is simple version of authorization in Java
// we want to check if the user is already registered or not
public static void isSignedUp(String email, String password){
try {
PreparedStatement ps = connection.prepareStatement("SELECT uid FROM user WHERE email=? AND password=?");
ps.setString(1,email);
ps.setString(2,password);
ResultSet rs = ps.executeQuery();
if(rs.next()){
int isExist = rs.getInt("uid");
String stringIsExist = Integer.toString(isExist);
// System.out.println(stringIsExist);
if (!stringIsExist.equals("null")) {
System.out.println("Welcome back 👋");
} else {
System.out.println("you need to Sign Up first 😊");
}
ps.close();
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package yuu.tube;
import java.awt.*;
import java.io.File;
// class for open other video format => AVI/FLV/WMV
public class TryAnotherVideoFormat {
public static void main(String[] args) {
try {
String path = "/Users/amirulasyraf/Documents/video/avi/Bear_In_The_Woods.avi"; // change this path to your path file name
File file = new File(path);
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
} catch (Exception e){
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment