Latest from community today
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@SpringBootApplication
@RestController
public class DemoApplication {
private Map<String, User> users = new HashMap<>();
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
// Endpoint to fetch all users
@GetMapping("/users")
public List<User> getUsers() {
return new ArrayList<>(users.values());
}
// Endpoint to add a user
@PostMapping("/add-user")
public User addUser(@RequestParam String name, @RequestParam String email, @RequestParam String age) {
if (name.isEmpty() || email.isEmpty()) {
throw new IllegalArgumentException("Name and email are required");
}
int ageValue;
try {
ageValue = Integer.parseInt(age);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Age must be a number");
}
String id = UUID.randomUUID().toString();
User user = new User(id, name, email, ageValue);
users.put(id, user);
return user;
}
}
// User model class
class User {
private String id;
private String name;
private String email;
private int age;
public User(String id, String name, String email, int age) {
this.id = id;
this.name = name;
this.email = email;
this.age = age;
}
// Getters and Setters
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public int getAge() {
return age;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setAge(int age) {
this.age = age;
}
}

/*
# 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) {
}
}

//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();
}
}
function helloWorld() {
console.log('Hello, World');
}
helloWorld();
// 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();
}
}