/* # LLD Question: Design a system to process VIP and regular customers in the order they arrive. You have to process customers in 2:1 ratio i.e. first 2 vip customer then 1 regular customer. Explain your choice of data structures and algorithms. Desgin classes and methods to solve the problem statement. Add testcases in the end to validate the system. you should have two methods: 1 - addCustomer(): This will add customer to be processed 2 - getNextCustomer() : This will give you the next customer to process ########### sample run ######## addCustomer([A:r]) addCustomer([B:v]) addCustomer([C:r]) addCustomer([D:v]) addCustomer([E:v]) addCustomer([F:r]) addCustomer([G:r]) addCustomer([H:r]) getNextCustomer() - [B:v] getNextCustomer() - [D:v] getNextCustomer() - [A:r] getNextCustomer() - [E:v] getNextCustomer() - [C:r] getNextCustomer() - [F:r] getNextCustomer() - [G:r] getNextCustomer() - [H:r] */ import java.io.*; import java.util.*; /* * To execute Java, please define "static void main" on a class * named Solution. * * If you need more classes, simply define them inline. */ class Solution { public static void main(String[] args) { } }
Latest from community today
//HW8: HANGMAN GAME import java.util.Scanner; public class Main { static Scanner scanner = new Scanner(System.in); static int lives = 5; static String inputWord; static String playerInputLetter; static char[] hangmasterInputWordArray; static boolean letterFound = false; static boolean youWin = false; public static void main(String[] args) { System.out.println("~*]HANGMAN[*~"); System.out.println("Good day, Hangmaster. Please enter the word that your victim will have to guess. =)"); System.out.println("Make sure the word only uses lowercase letters (a-z). No numbers or special characters! And NO SPACEBAR D:<"); System.out.println("Of course make sure that your precious victim isn't looking! ;]"); do { inputWord = scanner.nextLine().trim(); hangmasterInputWordArray = inputWord.toCharArray(); if (!isHangmasterInputValid()) { System.out.println("Invalid input! >=( Please enter a word using only lowercase letters (a-z). No numbers or special characters! And NO SPACEBAR D:<"); } } while (!isHangmasterInputValid()); drawHangman(); play(); } public static boolean isHangmasterInputValid() { if (inputWord == null || inputWord.isBlank()) { return false; } for(int i =0; i<hangmasterInputWordArray.length; i++) { char checkInputWordChar = hangmasterInputWordArray[i]; if (checkInputWordChar < 'a' || checkInputWordChar > 'z' ) { return false; } } return true; } public static boolean isPlayerInputValid() { if (playerInputLetter == null || playerInputLetter.isBlank()) { return false; } if (playerInputLetter.length() != 1) { System.out.println("Invalid input! Please input a valid lowercase LETTER! No capital letter, symbols or numbers =) And just ONE letter! AND NO SPACEBAR D:<"); return false; } char checkInputLetterChar = playerInputLetter.charAt(0); if (checkInputLetterChar < 'a' || checkInputLetterChar > 'z') { return false; } return true; } public static void drawHangman() { System.out.println("~*]HANGMAN[*~"); if(youWin) { System.out.println("*******************************************"); System.out.println("*__ _____ _ _ __ _____ _ _ *"); System.out.println("*\\ \\ / / _ \\| | | | \\ \\ / /_ _| \\ | |*"); System.out.println("* \\ V / | | | | | | \\ \\ /\\ / / | || \\| |*"); System.out.println("* | || |_| | |_| | \\ V V / | || |\\ |*"); System.out.println("* |_| \\___/ \\___/ \\_/\\_/ |___|_| \\_|*"); System.out.println("*******************************************"); System.out.println(); System.out.println(" \\😄/"); System.out.println(" ██"); System.out.println(" / \\"); return; } if(lives<3) { System.out.println("______________"); System.out.println("=============="); } else { System.out.println(); } if(lives<2) { System.out.println(" | ||"); } else if(lives<4) { System.out.println(" ||"); } else { System.out.println(); } if(lives == 5) { System.out.println(); System.out.println(); } switch(lives) { case 5: System.out.println(); System.out.println(" 🙂"); break; case 4: System.out.println(" 😐"); break; case 3: System.out.println(" ☹️ ||"); break; case 2: System.out.println(" 😨 ||"); break; case 1: System.out.println(" 😭 ||"); break; case 0: System.out.println(" | ||"); System.out.println(" ☠️ ||"); break; } if(lives>=4) { System.out.println(" /██\\"); System.out.println(" / \\"); } if(lives<4) { System.out.println(" /██\\ ||"); System.out.println(" / \\ ||"); } if(lives==4) { System.out.println(" ======"); System.out.println(" | |"); } if(lives==0) { System.out.println(" ||"); } else if(lives<1) { System.out.println(" ||"); System.out.println(" ||"); } else if(lives<4) { System.out.println(" ====== ||"); System.out.println(" | | ||"); } } public static void clearScreen() { try { if (System.getProperty("os.name").contains("Windows")) { new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); } else { System.out.print("\033[H\033[2J"); System.out.flush(); } } catch (Exception e) { System.out.println("Failed to clear screen."); } } public static void play() { char[] playerInputProgressArray = new char[hangmasterInputWordArray.length]; for (int i = 0; i < playerInputProgressArray.length; i++) { playerInputProgressArray[i] = '_'; } while (!(lives ==0)) { letterFound = false; if((new String(playerInputProgressArray).equals(new String(hangmasterInputWordArray)))) { youWin = true; drawHangman(); break; } System.out.println(playerInputProgressArray); if(lives>0) { System.out.println("Please input a valid lowercase LETTER! No capital letter, symbols or numbers =) And just ONE letter! AND NO SPACEBAR D:<"); } playerInputLetter = scanner.nextLine().trim(); while(!isPlayerInputValid()) { System.out.println("Invalid input! Please input a valid lowercase LETTER! No capital letter, symbols or numbers =) And just ONE letter! AND NO SPACEBAR D:<"); playerInputLetter= scanner.nextLine(); } System.out.println("Your choice is the letter: "+playerInputLetter); char playerInputLetterChar = playerInputLetter.charAt(0); for (int i = 0; i < hangmasterInputWordArray.length; i++) { if(playerInputLetterChar == hangmasterInputWordArray[i]) { playerInputProgressArray[i] = playerInputLetterChar; letterFound = true; } } if(!letterFound) { lives--; } drawHangman(); } if (youWin) { System.out.println("The word is: "+ inputWord); System.out.println("Congratulations, you survived! =D"); } else { System.out.println("TOO LATE, YOU FAILED >=]"); System.out.println("The word was: "+inputWord); System.out.println("☠-GAME OVER-☠"); } } }
import java.io.*; import java.util.*; /* * To execute Java, please define "static void main" on a class * named Solution. * * If you need more classes, simply define them inline. */ class Solution { public static void main(String[] args) { ArrayList<String> strings = new ArrayList<String>(); strings.add("Hello, World!"); strings.add("Running Java " + Runtime.version().feature()); for (String string : strings) { System.out.println(string); } } }
class Solution implements Runnable { private String name; static private Integer val = 0; public Solution(String name) { this.name = name; } @Override public void run() { for(int i = 0; i<10000; i++) { add(); print(i); } } public synchronized void add() { int tmp = val; try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } val = tmp + 1; } public void print(int index) { System.out.println( this.name + " " + index + ": " + val); } public static void main(String[] args) { new Thread( new Solution("Proc-1") ).start(); new Thread( new Solution("Proc-2") ).start(); } }
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); // all four possible moves int[][] moves = new int[4][2]; moves[0] = new int[]{0,1}; moves[1] = new int[]{1,0}; moves[2] = new int[]{0,-1}; moves[3] = new int[]{-1,0}; // define matrix int[][] mat = new int[N][N]; int k = 1; int direction = 0; int x = 0 , y = 0; for(int i = 0 ; i < N*N ; i++) { //assign and take a step mat[x][y] = k; k++; x += moves[direction][0]; y += moves[direction][1]; // check boundaries and already visited if (0 <= x && 0<= y && x < N && y <N && mat[x][y] == 0) { continue; } else { x -= moves[direction][0]; y -= moves[direction][1]; //change direction direction = direction == 3 ? 0 : direction +1; x += moves[direction][0]; y += moves[direction][1]; } //change k } for(int[] arr : mat) { System.out.println(Arrays.toString(arr)); } } }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { private final UserRepository userRepository; private final EmailService emailService; @Autowired public UserService(UserRepository userRepository, EmailService emailService) { this.userRepository = userRepository; this.emailService = emailService; } public boolean registerUser(String email) { if (userRepository.existsByEmail(email)) { return false; } User newUser = new User(email); userRepository.save(newUser); emailService.sendWelcomeEmail(email); return true; } }
class Solution implements Runnable { private String name; static private Integer val = 0; public Solution(String name) { this.name = name; } @Override public void run() { for(int i = 0; i<10000; i++) { add(); print(i); } } public synchronized void add() { int tmp = val; try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } val = tmp + 1; } public void print(int index) { System.out.println( this.name + " " + index + ": " + val); } public static void main(String[] args) { new Thread( new Solution("Proc-1") ).start(); new Thread( new Solution("Proc-2") ).start(); } }
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class PhonePeController extends Controller { public function index() { return view('phonepe_form'); // A simple form to enter amount and pay } private function getPhonePeAccessToken() { $clientId = env('PHONEPE_CLIENT_ID'); $clientSecret = env('PHONEPE_CLIENT_SECRET'); Log::info('[PhonePe] Generating access token', [ 'client_id' => $clientId, 'client_version' => '1' ]); $response = Http::asForm()->post('https://api.phonepe.com/apis/identity-manager/v1/oauth/token', [ 'client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'client_credentials', 'client_version' => '1', ]); Log::info('[PhonePe] Token API Raw Response:', $response->json()); $responseJson = $response->json(); $accessToken = $responseJson['access_token'] ?? $responseJson['data']['accessToken'] ?? null; if ($accessToken) { Log::info('[PhonePe] Access token generated successfully'); return $accessToken; } Log::error('[PhonePe] Failed to generate access token', [ 'response' => $responseJson ]); return null; } public function createOrder(Request $request) { $request->validate([ 'amount' => 'required|numeric|min:1' ]); $accessToken = $this->getPhonePeAccessToken(); if (!$accessToken) { return back()->with('error', 'Access token generation failed.'); } $orderId = uniqid('TX'); // Unique Order ID $amount = $request->input('amount') * 100; // INR to paise $payload = [ "merchantOrderId" => $orderId, "amount" => $amount, "paymentFlow" => [ "type" => "PG_CHECKOUT", "message" => "Order #" . $orderId, "merchantUrls" => [ "redirectUrl" => route('phonepe.confirm') ] ] ]; Log::info('[PhonePe Production PG Checkout] Request Payload:', $payload); $response = Http::withHeaders([ 'Authorization' => 'O-Bearer ' . $accessToken, 'Content-Type' => 'application/json' ])->post('https://api.phonepe.com/apis/pg/checkout/v2/pay', $payload); Log::info('[PhonePe Production PG Checkout] Response:', $response->json()); $responseData = $response->json(); if ($response->ok() && isset($responseData['redirectUrl'])) { session(['phonepe_order_id' => $orderId]); return redirect()->away($responseData['redirectUrl']); } return back()->with('error', 'Payment initiation failed. Check logs.'); } public function confirmPayment(Request $request) { $orderId = $request->input('merchantOrderId') ?? session('phonepe_order_id'); Log::info('[PhonePe] Returned to confirm route', [ 'merchantOrderId' => $orderId, 'input' => $request->all() ]); if (!$orderId) { return view('phonepe_confirm')->with('status', 'Order ID missing.'); } // You can optionally check status here using status API return view('phonepe_confirm', [ 'status' => 'Redirected back from PhonePe.', 'orderId' => $orderId ]); } }