Reloj.java 3.89 KB
Newer Older
Yovan Martinez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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());
        
    }

}