#2574 Help ! Java for fantom

Andrey Fri 28 Oct 2016

I have a job to perform on the Fantom language and I have a bit of trouble, I've done it in java need more help to turn it into fantom.E a simple rental system, using only object orientation Can anyone help?

// Main class.//

package exec;

import javax.swing.JOptionPane;

import negocio.AluguelQuarto;

public class GerenciarAluguel {

private AluguelQuarto aluguel; private static int numeroPessoas; /**

* @param args
*/

public static void main(String[] args) {

/* Recebe os dados do alubguel pelo teclado e retorna em um objeto Aluguel */
AluguelQuarto aluguel = registrarAluguel();

/* Imprimir o Recibo do Aluguel*/
imprimirRecibo(aluguel);

}

public static void imprimirRecibo(AluguelQuarto aluguel) {

StringBuilder sb = new StringBuilder();
sb.append("\n =======================================================");
sb.append("\n Nome do Locatario        : "+aluguel.getNomeLocatorio());
sb.append("\n Data e Horario de Entrada: "+aluguel.getDataEntrada().getDia()+"/"+aluguel.getDataEntrada().getMes()+"/"+aluguel.getDataEntrada().getAno()+" "+aluguel.getHoraEntrada().getHora()+":"+aluguel.getHoraEntrada().getMinuto());
sb.append("\n Data e Horario de Saida  : "+aluguel.getDataSaida().getDia()+"/"+aluguel.getDataSaida().getMes()+"/"+aluguel.getDataSaida().getAno()+" "+aluguel.getHoraSaida().getHora()+":"+aluguel.getHoraSaida().getMinuto());
sb.append("\n Numero de Diarias        : "+aluguel.calcularNumeroDiarias());
sb.append("\n Total Devido             : "+aluguel.calcularValorAPagar());
sb.append("\n =======================================================");

JOptionPane.showMessageDialog(null, sb.toString());

}

public static AluguelQuarto registrarAluguel() {

String strNomeLocatorario;
String strDiaEntrada;
String strMesEntrada;
String strAnoENtrada ;
String strDiaSaida;
String strMesSaida;
String strAnoSaida ;

int numero = 0;
int mesEntrada = 0;
int anoEntrada = 0;

int diaSaida = 0;
int mesSaida = 0;
int anoSaida = 0;

String strValorDiaria;
float valorDiaria = 00.00f;

String strNumeroPessoas;
setNumeroPessoas(0);

strNomeLocatorario = JOptionPane.showInputDialog("Entre com o Nome do Locatario");

/* Ler Dia de Entrada */ 
strDiaEntrada = JOptionPane.showInputDialog("Entre com o dia de entrada ");
numero = Integer.valueOf(strDiaEntrada);

/* Ler Dia de Entrada */ 
strMesEntrada = JOptionPane.showInputDialog("Entre com o mes de entrada ");
mesEntrada = Integer.valueOf(strMesEntrada);

/* Ler Dia de Entrada */ 
strAnoENtrada = JOptionPane.showInputDialog("Entre com o ano de entrada");	
anoEntrada = Integer.valueOf(strAnoENtrada);

strDiaSaida = JOptionPane.showInputDialog("Entre com o dia de Saida ");
diaSaida = Integer.valueOf(strDiaSaida);

strMesSaida = JOptionPane.showInputDialog("Entre com o mes de Saida ");
mesSaida = Integer.valueOf(strMesSaida);

strAnoSaida = JOptionPane.showInputDialog("Entre com o ano de Saida ");
anoSaida = Integer.valueOf(strAnoSaida);

strNumeroPessoas = JOptionPane.showInputDialog("Entre com o Número de Pessoas ");
setNumeroPessoas(Integer.valueOf(strNumeroPessoas));

strValorDiaria = JOptionPane.showInputDialog("Entre com o Valor da Diária ");
valorDiaria = Integer.valueOf(strValorDiaria);

/* Cria o Aluguel */
AluguelQuarto aluguel = new AluguelQuarto();

aluguel.setNomeLocatorio(strNomeLocatorario);
aluguel.getDataEntrada().setDia(numero);
aluguel.getDataEntrada().setMes(mesEntrada);
aluguel.getDataEntrada().setAno(anoEntrada);
aluguel.getDataSaida().setDia(diaSaida);
aluguel.getDataSaida().setMes(mesSaida);
aluguel.getDataSaida().setAno(anoSaida);
aluguel.setValorDiaria(valorDiaria);

return aluguel;		

}

public AluguelQuarto getAluguel() {

return aluguel;

}

public void setAluguel(AluguelQuarto aluguel) {

this.aluguel = aluguel;

}

public static int getNumeroPessoas() {

return numeroPessoas;

}

public static void setNumeroPessoas(int numeroPessoas) {

GerenciarAluguel.numeroPessoas = numeroPessoas;

} }

// Pousada Class \\

package negocio;

import java.util.ArrayList; import java.util.List;

public class Pousada {

private List<AluguelQuarto> acomodacoes = new ArrayList<AluguelQuarto>();

private static int numeroQuartos = 50; private static int numeroChales = 10;

public List<AluguelQuarto> getAcomodacoes() {

return acomodacoes;

}

public void setAcomodacoes(List<AluguelQuarto> acomodacoes) {

this.acomodacoes = acomodacoes;

}

public static int getNumeroQuartos() {

return numeroQuartos;

}

public static void setNumeroQuartos(int numeroQuartos) {

Pousada.numeroQuartos = numeroQuartos;

}

public static int getNumeroChales() {

return numeroChales;

}

public static void setNumeroChales(int numeroChales) {

Pousada.numeroChales = numeroChales;

}

}

These are the two main classes, the other I'm trying to do

Thanks

SlimerDude Fri 28 Oct 2016

Thanks for posting your Java program. Which part are you have trouble re-writing in Fantom?

Andrey Sat 29 Oct 2016

@SlimerDude

Thanks for answering me,i'm newbie to programming and more newbie in fantom, I have difficulty convertes reserved words,i looked at the documentation but still remain some difficulties , you can help me?.

SlimerDude Sat 29 Oct 2016

I afraid I can't teach you programming or Fantom in a couple of posts, but from looking at the code I will say that in Fantom:

  1. All static data is const meaning it can not be changed. So for now, avoid it where you can.
  2. Fantom does not have / allow inner classes.
  3. There is no need for getter / setters in Fantom
  4. I would stick with cmd line input / output until everything is working - Windowing is an extra layer of complexity.

Some key word comparisons:

Java   <=> Fantom 
------------------
String <=> Str
int    <=> Int
float  <=> Float
void   <=> Void

For comparison, here's some of your code converted to Fantom so you can see how it looks:

class GerenciarAluguel {
    private AluguelQuarto? aluguel

    Void main(Str[] args) {
        /* Recebe os dados do alubguel pelo teclado e retorna em um objeto Aluguel */
        aluguel = registrarAluguel()
    
        /* Imprimir o Recibo do Aluguel*/
        echo(aluguel)        
    }

    AluguelQuarto registrarAluguel() {
        nomeLocatorario := Env.cur.prompt("Entre com o Nome do Locatario ")
        diaEntrada      := Env.cur.prompt("Entre com o dia de entrada ").toInt
        mesEntrada      := Env.cur.prompt("Entre com o mes de entrada ").toInt
        anoEntrada      := Env.cur.prompt("Entre com o ano de entrada ").toInt
    
        return AluguelQuarto {
            it.nomeLocatorario = nomeLocatorario
            it.entrada         = Date(anoEntrada, Month.vals[mesEntrada + 1], diaEntrada)
        }
    }
}

class AluguelQuarto {
    Str  nomeLocatorario
    Date entrada
    
    new make(|This| f) { f(this) }
    
    override Str toStr() {
        sb := ""
        sb += "\n ======================================================="
        sb += "\n Nome do Locatario        : ${nomeLocatorario}"
        sb += "\n Data e Horario de Entrada: ${entrada.toLocale}"
        sb += "\n ======================================================="
        return sb
    }
}

And some sample input / output when I run it:

Entre com o Nome do Locatario SlimerDude
Entre com o dia de entrada 12
Entre com o mes de entrada 05
Entre com o ano de entrada 2016

 =======================================================
 Nome do Locatario        : SlimerDude
 Data e Horario de Entrada: 12-Jul-2016
 =======================================================

If you're still stuck, start with a simple Hello World program (some tutorials here) and slowly add code to it.

Going forward, I can't write your program for you, but I can help with specific questions.

Andrey Sat 29 Oct 2016

@SlimerDude Thank you for helping me, and so little time, I begin to better study the language program to get better on it. Thank you was of great help. however appear specific questions of my code I'll try to search it.

Thank you very much

SlimerDude Sat 29 Oct 2016

There are a couple of links on Fantom-Lang that may help you.

Specifically I'm thinking of - Programming Fantom 1.0 by Hertz.

Andrey Sat 29 Oct 2016

@SlimerDude

Thank you very much for sharing the material support, I will study based on material provided.

Tyy.

Andrey Tue 15 Nov 2016

Hello .

I have a question in the code. I need to get two dates and subtracts both to know the total amount of days. there is any fantom function that does it?.

Thanks again.

class GerenciarAluguel {

private AluguelQuarto? aluguel

Void main(Str[] args) {
    /* Recebe os dados do aluguel pelo teclado e retorna em um objeto Aluguel */
    aluguel = registrarAluguel()

    /* Imprimir o Recibo do Aluguel*/
    echo(aluguel)        
}

AluguelQuarto registrarAluguel() {
    nomeLocatorario := Env.cur.prompt("Entre com o Nome do Locatario ")
valorDiaria:= Env.cur.prompt("Valor da diaria ").toFloat

      diaEntrada:= Env.cur.prompt("Entre com o dia de entrada ").toInt
      mesEntrada:= Env.cur.prompt("Entre com o mes de entrada ").toInt
      anoEntrada:= Env.cur.prompt("Entre com o ano de entrada ").toInt

diaSaida:= Env.cur.prompt("Entre com o dia de saida ").toInt
      mesSaida:= Env.cur.prompt("Entre com o mes de saida ").toInt
      anoSaida:= Env.cur.prompt("Entre com o ano de saida ").toInt




      return AluguelQuarto {
          it.nomeLocatorario = nomeLocatorario
          it.entrada         = Date(anoEntrada, Month.vals[mesEntrada - 1], diaEntrada)
	it.saida           = Date(anoSaida, Month.vals[mesSaida - 1], diaSaida)
	it.diaria 		   = valorDiaria
	it.diaFinal		   =(diaSaida - diaEntrada  )



      }
  }

}

class AluguelQuarto {

Str  nomeLocatorario
Date entrada

Date saida Float diaria Int diaFinal

new make(|This| f) { f(this) }

override Str toStr() {
    sb := ""
    sb += "\n ======================================================="
    sb += "\n Nome do Locatario        : ${nomeLocatorario }    "
sb += "\n Valor Diaria             : ${diaria          }    "
      sb += "\n Data e Horario de Entrada: ${entrada.toLocale}    "
sb += "\n Data e Horario de Saida  : ${saida.toLocale  }    "
   sb += "\n DiaFinal                 : ${diaFinal.toLocale  }    "


      sb += "\n ======================================================="
      return sb
  }

}

SlimerDude Tue 15 Nov 2016

Hi Andrey, try Date.minusDate(...) which returns a Duration.

Example:

date1 := Date(2016, Month.mar, 23)
date2 := Date(2016, Month.feb, 22)
days  := (date1.minusDate(date2)).toDay
echo(days)  // --> 30

The last expression may also be written as:

days := (date1 - date2).toDay
echo(days)  // --> 30

Andrey Wed 16 Nov 2016

Thanks for answering me.

I was able to perform the date operation and complete what I was doing. Now I wanted to learn how to create a simple window for printing the generated results. Is it something very complex?.

Thank you for helping me with my learning.

SlimerDude Wed 16 Nov 2016

Hi Andrey,

No worries, I hope the project is going well!

For a simple message window, try the Dialog class in the fwt pod. It has convenience methods for opening some common basic windows.

Example:

using fwt::Dialog

...

name := Dialog.openPromptStr(null, "Enter your name:")
		
msg  := "Line1\n" + "Your name is ${name}\n" + "Line3"
Dialog.openInfo(null, msg)

Note, assuming you're building a pod, you'll also have to add "fwt 1.0" to the depends list in your build.fan.

Login or Signup to reply.