From 8412c34f59e33cc692a7a9088113482549e09812 Mon Sep 17 00:00:00 2001 From: Yovan Martinez Date: Mon, 25 Apr 2022 12:13:29 -0400 Subject: [PATCH] Finalizado reloj --- Reloj.java | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 Reloj.java diff --git a/Reloj.java b/Reloj.java new file mode 100644 index 0000000..b434754 --- /dev/null +++ b/Reloj.java @@ -0,0 +1,139 @@ +import java.math.BigDecimal; +import java.time.LocalTime; +import java.util.Scanner; +public class Reloj{ + + private int horas; + private int minutos; + private int segundos; + public Reloj(){ + horas = 12; + minutos = 00; + segundos = 00; + + } + + public Reloj(int hours ,int minutes ,int seconds){ + horas = hours; + minutos = minutes; + segundos = seconds; + } + + public Reloj(int seconds){ + horas = seconds/3600; + minutos = (seconds % 3600)/60; + segundos = (seconds % 60); + } + + public int setReloj(int seconds){ + setHoras((int)(seconds/3600)); + setMinutos((int)(seconds % 3600)/60); + setSegundos((int)(seconds % 60)); + return seconds; + } + + public int getHoras(){ + return horas; + } + + public int getMinutos(){ + return minutos; + } + + public int getSegundos(){ + return segundos; + } + + public void setHoras(int hours){ + if (hours >= 0 && hours<=23){ + horas = hours; + }else{ + System.out.println("Valor en horas invalido"); + } + } + + public void setMinutos(int value){ + if (value >= 0 && value<=59){ + minutos = value; + }else{ + System.out.println("Valor en minutos invalido"); + } + } + + public void setSegundos(int value){ + if (value >= 0 && value<=59){ + segundos = value; + }else{ + System.out.println("Valor en segundos invalido"); + } + } + + public int tick(){ + if(segundos >= 0 && segundos < 59){ + segundos++; + return segundos; + }else{ + segundos = 0; + return segundos; + } + } + //Privado ya que solo ayuda a transformar en segundos para evitar conflictos + private int toSegundos(Reloj time){ + int seconds = 0; + seconds = (time.getHoras()*(3600)) + (time.getMinutos()*60) + time.getSegundos(); + return seconds; + } + + public void addReloj(Reloj time){ + int newTime = 0; + newTime = (getHoras()*3600)+(getMinutos()*60) + getSegundos() + (time.getHoras()*(3600)) + (time.getMinutos()*60) + time.getSegundos(); + while(newTime > 86400){ + newTime = newTime - 86400; + } + horas = newTime/3600; + minutos = (newTime % 3600)/60; + segundos = (newTime % 60); + } + + public String toString(){ + String reloj = ""; + reloj = "["+ String.valueOf(getHoras())+ ":" + String.valueOf(getMinutos())+ ":" + String.valueOf(getSegundos())+ "]"; + return reloj; + } + + public void restaReloj(Reloj time){ + int resta = 0; + resta = toSegundos(time); + resta = (getHoras()*3600)+(getMinutos()*60) + getSegundos() - ((time.getHoras()*(3600)) + (time.getMinutos()*60) + time.getSegundos()); + + + if(resta< 0){ + resta = resta * -1; + } + horas = resta/3600; + minutos = (resta % 3600)/60; + segundos = (resta % 60); + } + public static void main(String[] args) { + Reloj relojUsuario = new Reloj(); + Reloj relojMaquina = new Reloj(); + Scanner in = new Scanner(System.in); + int lector = 0; + System.out.print("Por favor ingrese los segundos = "); + lector = Integer.parseInt(System.console().readLine()); + relojUsuario.setReloj(lector); + System.out.println(lector + " Segundos son =" + relojUsuario.toString()); + relojUsuario.tick(); + for (int i = 0; i < 9; i++) { + System.out.println(relojUsuario.toString()); + relojUsuario.tick(); + } + System.out.println(relojUsuario.toString()); + relojUsuario.addReloj(relojMaquina); + System.out.println("La suma es = " + relojUsuario.toString()); + relojUsuario.restaReloj(relojMaquina); + System.out.println("La resta es = " + relojUsuario.toString()); + + } + +} -- libgit2 0.26.0