⭐ Let's code an ALARM CLOCK with Java! ⏰

Поделиться
HTML-код
  • Опубликовано: 31 дек 2024

Комментарии • 13

  • @BroCodez
    @BroCodez  3 дня назад +3

    import java.time.LocalTime;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeParseException;
    import java.util.Scanner;
    public class Main {
    public static void main(String[] args) {
    // JAVA ALARM CLOCK
    Scanner scanner = new Scanner(System.in);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
    LocalTime alarmTime = null;
    String filePath = "A Caring Friend.wav";
    while(alarmTime == null){
    try{
    System.out.print("Enter an alarm time (HH:MM:SS): ");
    String inputTime = scanner.nextLine();
    alarmTime = LocalTime.parse(inputTime, formatter);
    System.out.println("Alarm set for " + alarmTime);
    }
    catch(DateTimeParseException e){
    System.out.println("Invalid format. Please use HH:MM:SS");
    }
    }
    AlarmClock alarmClock = new AlarmClock(alarmTime, filePath, scanner);
    Thread alarmThread = new Thread(alarmClock);
    alarmThread.start();
    }
    }
    import javax.sound.sampled.*;
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    import java.time.LocalTime;
    import java.util.Scanner;
    public class AlarmClock implements Runnable{
    private final LocalTime alarmTime;
    private final String filePath;
    private final Scanner scanner;
    AlarmClock(LocalTime alarmTime, String filePath, Scanner scanner){
    this.alarmTime = alarmTime;
    this.filePath = filePath;
    this.scanner = scanner;
    }
    @Override
    public void run(){
    while(LocalTime.now().isBefore(alarmTime)){
    try {
    Thread.sleep(1000);
    LocalTime now = LocalTime.now();
    System.out.printf("
    %02d:%02d:%02d",
    now.getHour(),
    now.getMinute(),
    now.getSecond());
    }
    catch (InterruptedException e) {
    System.out.println("Thread was interrupted");
    }
    }
    System.out.println("
    *ALARM NOISES*");
    playSound(filePath);
    }
    private void playSound(String filePath){
    File audioFile = new File(filePath);
    try(AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile)){
    Clip clip = AudioSystem.getClip();
    clip.open(audioStream);
    clip.start();
    System.out.print("Press *Enter* to stop the alarm: ");
    scanner.nextLine();
    clip.stop();
    scanner.close();
    }
    catch(UnsupportedAudioFileException e){
    System.out.println("Audio file format is not supported");
    }
    catch(LineUnavailableException e){
    System.out.println("Audio is unavailable");
    }
    catch(IOException e){
    System.out.println("Error reading audio file");
    }
    }
    }

    • @faisalhrbk
      @faisalhrbk 2 дня назад

      thank god you are back!!! can i have your linkedIn sir

  • @ryan_millard
    @ryan_millard 2 дня назад +3

    I love Bro Code. This guy helped me through my first year of university. Without his Java course, I would have done terribly. I love watching his videos.

  • @georgendalumakokha2409
    @georgendalumakokha2409 2 дня назад

    Hello bro have followed you since html 4hours to today I have loved learning coding

  • @LEE_MILLARD
    @LEE_MILLARD 3 дня назад

    Great videos that you do.
    Just watched a doc about George boole.
    The coding teacher on this doc said the best coders are poets & boole had learned 5 language's by the age of 12!!!

  • @Sachin-kh8ri
    @Sachin-kh8ri 3 дня назад

    Hey bro i just finished watching your javascript video and it was best video to learn javascript with exiting projects

  • @DylanGamer4848
    @DylanGamer4848 3 дня назад +1

    Hey bro code was wondering if you can do a tutorial for Tailwind css for 2024/2025 love your video your videos help me a lot?

  • @NazneenNaharNishi
    @NazneenNaharNishi 3 дня назад

    Hello Bro, please make an depth tutorial on Android development.............

  • @Seif285
    @Seif285 3 дня назад

    Thank you bro ❤

  • @Saalltt
    @Saalltt 3 дня назад

    Hey, bro 👋

  • @LSTIMEGO
    @LSTIMEGO 3 дня назад

    Hello teacher please make a new series about Kali Linux terminal command

  • @ommmmmmmmo
    @ommmmmmmmo 3 дня назад

    Hello , bro

  • @SplashAni_
    @SplashAni_ 3 дня назад

    ily bro code