I have an object receiving a short in argument. This argument is a code for a product and I want this number to be only between 1 and 1000.
If I do this:
Class a = new Class((short)96 5);
System.out.prin tln(a.getArgume nt());
This will return 965 as supposed
Class a = new Class((short)12 00);
System.out.prin tln(a.getArgume nt());
This will cause my exception to pop and tell the user to use a code between 1 and 1000. Until here everything is fine. Now comes my problem:
Class a = new Class((short)66 000);
System.out.prin tln(a.getArgume nt());
This will return 464. I want my exception to pop here too. I want to keep my short typevbecause it is useless to take 32 bytes for only a number in the 4 digits mamxium.
How to solve this? For reference, this is my Class. Sorry it's in French but you will be able to understand easily my code don't worry !
Thanks a lot
package TP1;
public class Article
{
public static void main(String[] args)
{
Article a = new Article((short) 66000,"Pen",(fl oat)23.45);
System.out.prin tln(a.getNumero Article());
}
private short numeroArticle;
private String descriptionArti cle;
private float prixArticle;
public Article(short pNumeroArticle, String pDescriptionArt icle, float pPrixArticle)
{
setNumeroArticl e(pNumeroArticl e);
setDescriptionA rticle(pDescrip tionArticle);
setPrixArticle( pPrixArticle);
}
public short getNumeroArticl e()
{
return numeroArticle;
}
public String getDescriptionA rticle()
{
return descriptionArti cle;
}
public float getPrixArticle( )
{
return prixArticle;
}
public void setNumeroArticl e(short pNumeroArticle)
{
if(pNumeroArtic le > 1000 || pNumeroArticle <= 0)
throw new IllegalArgument Exception("Le numéro de l'article doit être compris entre 1 et 1000");
else
numeroArticle = pNumeroArticle;
}
public void setDescriptionA rticle(String pDescriptionArt icle)
{
descriptionArti cle = pDescriptionArt icle;
}
public void setPrixArticle( float pPrixArticle)
{
prixArticle = pPrixArticle;
}
}
If I do this:
Class a = new Class((short)96 5);
System.out.prin tln(a.getArgume nt());
This will return 965 as supposed
Class a = new Class((short)12 00);
System.out.prin tln(a.getArgume nt());
This will cause my exception to pop and tell the user to use a code between 1 and 1000. Until here everything is fine. Now comes my problem:
Class a = new Class((short)66 000);
System.out.prin tln(a.getArgume nt());
This will return 464. I want my exception to pop here too. I want to keep my short typevbecause it is useless to take 32 bytes for only a number in the 4 digits mamxium.
How to solve this? For reference, this is my Class. Sorry it's in French but you will be able to understand easily my code don't worry !
Thanks a lot
package TP1;
public class Article
{
public static void main(String[] args)
{
Article a = new Article((short) 66000,"Pen",(fl oat)23.45);
System.out.prin tln(a.getNumero Article());
}
private short numeroArticle;
private String descriptionArti cle;
private float prixArticle;
public Article(short pNumeroArticle, String pDescriptionArt icle, float pPrixArticle)
{
setNumeroArticl e(pNumeroArticl e);
setDescriptionA rticle(pDescrip tionArticle);
setPrixArticle( pPrixArticle);
}
public short getNumeroArticl e()
{
return numeroArticle;
}
public String getDescriptionA rticle()
{
return descriptionArti cle;
}
public float getPrixArticle( )
{
return prixArticle;
}
public void setNumeroArticl e(short pNumeroArticle)
{
if(pNumeroArtic le > 1000 || pNumeroArticle <= 0)
throw new IllegalArgument Exception("Le numéro de l'article doit être compris entre 1 et 1000");
else
numeroArticle = pNumeroArticle;
}
public void setDescriptionA rticle(String pDescriptionArt icle)
{
descriptionArti cle = pDescriptionArt icle;
}
public void setPrixArticle( float pPrixArticle)
{
prixArticle = pPrixArticle;
}
}
Comment