• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Vector definition

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am starting to learn java. The first thing inconsistent is that after the declared the primitive elements dont have nothing to identify them such as the letter p before their website name.
Like String pvehiculos or int double pprice

Also the examples are all refer to primitive values.

I want to do my first program. A little database but with the posiblility of serialize some data.

As an example I did a class vehicle abstract
with four private String fields . Method with construtor and get and set. to modify the primitive values of the object vehicles. and It get compiled. So I made the Class Vehicle, the object vehicle and the attributes vehicle.parribute.
At this stage I didn't have problem.
The next stage is the database. So I have there four posibilites array, array list, vector or set.
I Don want duplicated values but I want to serialize the data later. So I don't really know if better is set or vector. As I readed. I tried with vector but all the examples ar for primitive values. In the base I need to order in different ways and do different groups and I 5tcouldn't find if vector have a references for the arributes (like vectorvehicle.color) and if it is possible do a vector of objects or only keep primitive values. In the array is possible use the reference of and atribute (Like Vehicle.arrayvehicle.color) so I am. Also I had plety problems. to declarate the vector. to sort have I to declare a collection of vector. Well any help is usefull. Thanks
 
Bartender
Posts: 5906
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Vector" has been replaced by "List" (e.g. ArrayList). You can have a List of any non-built-in data types. You can't have a List of built-in types, for example: ArrayList<int> won't compile but ArrayList<Integer> will.

You can have a List of Attributes, as, ArrayList<Attribute>.

If you want to look up an Attribute by key then you can use a Map, as in, Map<Key,Attribute>. With this, keys must be unique.
 
Sheriff
Posts: 6033
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, dario lobos!

If you haven't found the Oracle Java tutorials, they are a good place to learn about the basics.  Here's the link to List section.  

https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html

On the left-hand side you will see that there are links to Collections, Set, and others.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you I am trying to find examples of vector and sortedset with reference instead of primitive values.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector can be serializable and sortedset have order and don't have duplicated data...as I read but I will try to find examples and read more. Thanks again.
 
Saloon Keeper
Posts: 2619
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option would be LinkedHashSet - it does not permit duplicates and retains insertion order.
 
Saloon Keeper
Posts: 3297
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

dario lobos wrote:(...) I Don want duplicated values (...)


When are two vehicles the same? Can you show us your Vehicle class?
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Or TreeSet if you want them in sorted order.



I still learning i did this

package pruebajava;

import java.util.*;
import pruebajava.Bicicleteria;
import pruebajava.Fechalong;
import pruebajava.Fechasegundos;
import pruebajava.intdatetime;

public class Bicicleteria
implements Comparator<Bicicleteria>
{

 //public static void main(String[] args)
String pmarca;
String pmodelo;
String pcolor;
double pprecio;
boolean pdisponible;
int pcantidad;
int pcontador;
static int spcontador;
long pfecha;
long pfechasegundos ;


public Bicicleteria(){};
public Bicicleteria(String pmarca, String pmodelo, String pcolor)
{
  this.pmarca = pmarca;
  this.pmodelo  = pmodelo;
  this.pcolor = pcolor;
  this.pprecio = 0;
  this.pcantidad = 0;
  this.pdisponible = false;
  spcontador++;
this.pcontador = spcontador;
//  this.pfecha = fech.Longdatetime();
// this.pfechasegundos = fechs.LongFechaSegundos();

//static Fechasegundos fechas = ()->{

Fechasegundos fechs = new Fechasegundos()
{

public long LongFechaSegundos()
{

long pdia2;
long pmes2;
long pyear2;
long phora2;
long pminuto2;
long psegundo2;
long pcontfechasegundos;
 
 
GregorianCalendar date = new GregorianCalendar();

pdia2 = (long) date.get(Calendar.DAY_OF_MONTH);

pmes2 = (long) date.get(Calendar.MONTH);

pyear2 = (long) date.get(Calendar.YEAR);

phora2 = (long) date.get(Calendar.HOUR);

pminuto2 = (long) date.get(Calendar.MINUTE);

psegundo2 = (long) date.get(Calendar.SECOND);;


pcontfechasegundos = psegundo2 + pminuto2* 60 + phora2 * 3600 + pdia2 * 24 * 3600 + pmes2 * 365/12 * 24 * 3600 + pyear2 * 365 * 24 * 3600;

return  (long) pcontfechasegundos;


};
};

//static Fechalong fech = ()->{

Fechalong fech = new Fechalong()
{

public long LongDateTime()
{

//public static void main(String[] args)



long pdia;

long pmes;
long pyear;
long phora;
long pminuto;
long psegundo;
long pcontfechahora;
 
 
GregorianCalendar date2 = new GregorianCalendar();


pdia = (long) date2.get(Calendar.DAY_OF_MONTH);

pmes = (long) date2.get(Calendar.MONTH);

pyear = (long) date2.get(Calendar.YEAR);

phora = (long) date2.get(Calendar.HOUR);

pminuto = (long) date2.get(Calendar.MINUTE);

psegundo = (long) date2.get(Calendar.SECOND);

pcontfechahora = psegundo +  pminuto * 100 + phora * 10000 + pdia * 1000000+ pmes * 100000000 + pyear * 10000 * 1000000;


return (long) pcontfechahora;

//this.pfecha =     fech.Longdatetime();

//this.pfechasegundos = fechs.LongFechaSegundos();
};
};
};


public String Getpmarca(){
  return  this.pmarca;
};
public String Getpmodelo(){
  return  this.pmodelo;
};
 public String Getpcolor(){
  return  this.pcolor;
};

@Override

public int compare(Bicicleteria o1 , Bicicleteria o2)
{
if ((!((o1 instanceof Bicicleteria)
&&  (o2 instanceof Bicicleteria)))|| (o1 == null) || (o2 == null)) {return -1;};

if (o1 == o2){return 0;};


Bicicleteria bici1 = (Bicicleteria) o1;
Bicicleteria bici2 = (Bicicleteria) o2;

return (bici1.pmarca.compareToIgnoreCase(bici2.pmarca) + bici1.pmodelo.compareToIgnoreCase(bici2.pmodelo) + bici1.pcolor.compareToIgnoreCase (bici2.pcolor));
};

@Override

public  boolean equals(Object o){
 
 if (!(o instanceof Bicicleteria) || o== null ) {return false ;};
 if (this == o){
   return true;
 };
 
 Bicicleteria bici = (Bicicleteria) o;

return
(this.pmarca.compareToIgnoreCase(bici.pmarca) == 0 && this.pmodelo.compareToIgnoreCase(bici.pmodelo) == 0 && this.pcolor.compareToIgnoreCase(bici.pcolor) == 0 );

};

public Boolean Getbici(Bicicleteria a,Bicicleteria b){

return a.equals(b);

};


@Override

public int hashCode(){
int d = 3;
GregorianCalendar fecha = new GregorianCalendar();
Object o = fecha.get(Calendar.SECOND);

int c = o.hashCode();
int a = d*( Getpmarca().compareTo(Getpmodelo()) + Getpmodelo().compareTo(Getpcolor()))+ c;

return a ;


};

Object o = null ;

@Override
public String toString(){

  return
 "Marca: " + pmarca + " Modelo: " + pmodelo + " Color: " + pcolor + " precio: " + pprecio;
 
};


public static void main(String[] args) {


Bicicleteria bicicleta1 = new Bicicleteria("a", "a", "a");

Bicicleteria bicicleta2 = new Bicicleteria("a","a","a");

Bicicleteria bicicleta3 = new Bicicleteria("a","f","g");


// System.out.println  (fech.LongDateTime());
System.out.println(bicicleta1.pmarca);
System.out.println(bicicleta1.pmodelo);
System.out.println(bicicleta1.pcolor);
System.out.println(bicicleta1.pcantidad);
System.out.println(bicicleta1.pdisponible);  

System.out.println(bicicleta1.pcontador);
System.out.println(bicicleta2.pmarca);
System.out.println(bicicleta2.pcontador);

System.out.println(bicicleta1.equals (bicicleta2));
System.out.println(bicicleta2.equals(bicicleta3));
System.out.println(bicicleta1);
System.out.println("hashCode1: " + bicicleta1.hashCode());
System.out.println(bicicleta2);
System.out.println("hashCode2: " + bicicleta2.hashCode());
System.out.println(bicicleta3);
System.out.println("hashCode3: " + bicicleta3.hashCode());;
System.out.println(bicicleta1 + " compare " + bicicleta2 +": "+ bicicleta1.compare(bicicleta2));
GregorianCalendar df = new  GregorianCalendar();
System.out.println  (df);
System.out.println( df.get(Calendar.SECOND));




}
}

package pruebajava;

import java.util.*;
import java.util.GregorianCalendar;
import pruebajava.Fechalong;
import pruebajava.Fechasegundos;


//public class intdatetime implements Fechasegundos, Fechalong  {
public class intdatetime extends Bicicleteria{
intdatetime (){};
public static long a;

//static Fechasegundos fechas = ()->{

static Fechasegundos fechs = new Fechasegundos(){

public long LongFechaSegundos() {

//public static  void  main(String[] arg){

long pdia2;
long pmes2;
long pyear2;
long phora2;
long pminuto2;
long psegundo2;
long pcontfechasegundos;
 
 
GregorianCalendar date = new GregorianCalendar();

pdia2 = (long) date.get(Calendar.DAY_OF_MONTH);

pmes2 = (long) date.get(Calendar.MONTH);

pyear2 = (long) date.get(Calendar.YEAR);

phora2 = (long) date.get(Calendar.HOUR);

pminuto2 = (long) date.get(Calendar.MINUTE);

psegundo2 = (long) date.get(Calendar.SECOND);;


pcontfechasegundos = psegundo2 + pminuto2* 60 + phora2 * 3600 + pdia2 * 24 * 3600 + pmes2 * 365/12 * 24 * 3600 + pyear2 * 365 * 24 * 3600;

return  (long) pcontfechasegundos;


};
};

//static Fechalong fech = ()->{

static Fechalong fech = new Fechalong(){

public long LongDateTime()
{

//public static void main(String[] args)



long pdia;

long pmes;
long pyear;
long phora;
long pminuto;
long psegundo;
long pcontfechahora;
 
 
GregorianCalendar date2 = new GregorianCalendar();


pdia = (long) date2.get(Calendar.DAY_OF_MONTH);

pmes = (long) date2.get(Calendar.MONTH);

pyear = (long) date2.get(Calendar.YEAR);

phora = (long) date2.get(Calendar.HOUR);

pminuto = (long) date2.get(Calendar.MINUTE);

psegundo = (long) date2.get(Calendar.SECOND);

pcontfechahora = psegundo +  pminuto * 100 + phora * 10000 + pdia * 1000000+ pmes * 100000000 + pyear * 10000 * 1000000;


return (long) pcontfechahora;

};
};


public static void main(String[] args){
//long a = pcontfechahora;
System.out.println (fech.LongDateTime() );
System.out.println (fechs.LongFechaSegundos()
);

}}

package pruebajava;
import java.util.Comparator;
import java.util.NavigableSet;
import java.util.TreeSet;
import pruebajava.Bicicleteria;

public class Navbicicleteria extends Bicicleteria  {

 public static void main(String[] args) {
Comparator <Bicicleteria> bici = new comparator();

NavigableSet<Bicicleteria> navigablebiciset= new TreeSet<Bicicleteria>(Comparator <Bicicleteria> bici);


//navigablebiciset.add("a", "b", "c");

//navigablebiciset.add("d", "e", "f");

//navigablebiciset.add("a", "b", "c");

//System.out.println(navigablebiciset);

}


}
package pruebajava;
//@FunctionalInterface
public interface Fechalong {
long LongDateTime();
public static long fechal =0;
}

package pruebajava;
@FunctionalInterface
interface Fechasegundos {

long LongFechaSegundos();
public static long fechas =0;
}
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

dario lobos wrote:

Carey Brown wrote:Or TreeSet if you want them in sorted order.



...But I couldnt implement a good treeset. Also I overrite the method compare and is not working. I tried to do a field with the date and time together but long. I could do the method but only work in system.out.print. I couldnt use it in the constructor of the class that make the objects for the navigable sorted set. I tried in other class, then in the same class and the last time inside of the constructor.

 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

dario lobos wrote:

dario lobos wrote:

Carey Brown wrote:Or TreeSet if you want them in sorted order.



dario lobos wrote:

dario lobos wrote:

Carey Brown wrote:Or TreeSet if you want them in sorted order.



If someone know where i can find information to how implement the treeset with this object and how can add this date and time in the constructor let me know.

Also i overrite the methods equal and hashcode both work and are congruents (compare, equal and hashcode) but i dont know how i must to use it in the treeset in the navigable sorted set or it the system use them internally. in the constructor i think that i must to use only comparator compare... well any help to this is wellcome. I am starting. I dindt know nothing about programing 20 days ago...

thanks.

Dario

 
Carey Brown
Bartender
Posts: 5906
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid that I have to ask you to repost your code. There are two problems with it:
* You have WAY too many blank lines. You should not use two blank lines in a row. You should not double space your lines unless you are specifically trying to highlight their differences.
* You have not used Code Tags which allow this site to add line numbers and preserve indenting and formatting of the code.

There is a button just above the edit window marked Code. To use it you need to do one of the following:

Drag your cursor over the entire source code (this highlights it in blue) then click Code.

Or, go to the beginning of the code and left-click once in front of the 1st character, then scroll down to the end of the code and shift-left-click after the last character. The code should now be blue and you can click the code button.

You can also click the Code button before pasting it in the edit window then move the cursor between the brackets "...][..." and paste.

Always click the button below the edit window marked "Preview" to see if it worked.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

dario lobos wrote:

Carey Brown wrote:
Sorry in my phone dont appear the code option. I erased the empty lines. is difficult to send the message. The server have problems.

In this part i have problems the rest is working. I am trying to put the date and time  in a long field using the constructor. Also i did a long field with this time and date but all converted in seconds and the last three digists are milliseconds. I did it with an interface but i cant include it in the constructor.I tried in other class, in the same class and inside of the constructor.

package pruebajava;
import java.util.*;
import pruebajava.Fechalong;
import pruebajava.Fechasegundos;
import pruebajava.intdatetime;
public class Bicicleteria
{
  String pmarca;
  String pmodelo;
  String pcolor;
  double pprecio;
  boolean pdisponible;
  int pcantidad;
  int pcontador;
  static int spcontador;
  long pfecha;
  long pfechasegundos ;
public Bicicleteria(){};
public Bicicleteria(String pmarca, String pmodelo, String pcolor)
{
  this.pmarca = pmarca;
  this.pmodelo  = pmodelo;
  this.pcolor = pcolor;
  this.pprecio = 0;
  this.pcantidad = 0;
  this.pdisponible = false;
 spcontador++;
  this.pcontador = spcontador;
//  this.pfecha = fech.Longdatetime();
// this.pfechasegundos = fechs.LongFechaSegundos();
//static Fechasegundos fechas = ()->{
Fechasegundos fechs = new Fechasegundos()
{
public long LongFechaSegundos()
{
  long pdia2;
  long pmes2;
  long pdiasmes;
  long pyear2;
  long pyviciesto;
  long phora2;
  long pminuto2;
  long psegundo2;
  long pcontfechasegundos;
  long pmilis;
GregorianCalendar date = new GregorianCalendar();
pdia2 = (long) date.get(Calendar.DAY_OF_MONTH);
pmes2 = (long) date.get(Calendar.MONTH);
switch ( (int) pmes2){
case 1:  pdiasmes = 31;
break;
case 2:  pdiasmes = 28;
break;
case 3:  pdiasmes = 31;
break;
case 4:  pdiasmes = 30;
break;
case 5:  pdiasmes = 31;
break;
case 6:  pdiasmes = 30;
break;
case 7:  pdiasmes = 31;
break;
case 8:  pdiasmes = 31;
break;
case 9:  pdiasmes = 30;
break;
case 10: pdiasmes = 31;
break;
case 11: pdiasmes = 30;
break;
default: pdiasmes = 31;};
pyear2 = (long) date.get(Calendar.YEAR);
if
((pmes2 > 1) && (((pyear2-2016) % 4) == 0))
pyviciesto = 24 * 3600;
else
pyviciesto = 0;
phora2 = (long) date.get(Calendar.HOUR);
pminuto2 = (long) date.get(Calendar.MINUTE);
psegundo2 = (long) date.get(Calendar.SECOND);
pmilis = (long) date.get(Calendar.MILLISECOND);
pcontfechasegundos = (psegundo2 + pminuto2* 60 + phora2 * 3600 + pdia2 * 24 * 3600 + pdiasmes * 24 * 3600 + pyear2 * 365 * 24 * 3600 + pyviciesto)*1000 + pmilis;
return  (long) pcontfechasegundos;
};
};
//static Fechalong fech = ()->{
Fechalong fech = new Fechalong()
{
public long LongDateTime()
{
  long pdia;
  long pmes;
  long pyear;
  long phora;
  long pminuto;
  long psegundo;
  long pcontfechahora;
GregorianCalendar date2 = new GregorianCalendar();
pdia = (long) date2.get(Calendar.DAY_OF_MONTH);
pmes = (long) date2.get(Calendar.MONTH);
pyear = (long) date2.get(Calendar.YEAR);
phora = (long) date2.get(Calendar.HOUR);
pminuto = (long) date2.get(Calendar.MINUTE);
psegundo = (long) date2.get(Calendar.SECOND);
pcontfechahora = psegundo +  pminuto * 100 + phora * 10000 + pdia * 1000000+ pmes * 100000000 + pyear * 10000 * 1000000;
return (long) pcontfechahora;
//this.pfecha =     fech.Longdatetime();
//this.pfechasegundos = fechs.LongFechaSegundos();
};
};
};
public String Getpmarca(){
  return  this.pmarca;
};
public String Getpmodelo(){
  return  this.pmodelo;
};
public String Getpcolor(){
  return  this.pcolor;
};
public Double Getpprecio(){
  return this.pprecio;
};
public int Getpcantidad(){
  return  this.pcantidad;
};
public boolean Getpdisponible(){
  return  this.pdisponible;
};
public void Setpprecio(double pprecio){
  this.pprecio = pprecio;
};
public void Setpdisponible(boolean pdisponible){
 this.pdisponible = pdisponible;
};
public void Setpcantidad(int pcantidad){
 this.pcantidad = pcantidad;
};
@Override
public  boolean equals(Object o){
 if (!(o instanceof Bicicleteria) || o== null ) {return false ;};
 if (this == o){
   return true;
};
Bicicleteria bici = (Bicicleteria) o;
return
(this.pmarca.compareToIgnoreCase(bici.pmarca) == 0 && this.pmodelo.compareToIgnoreCase(bici.pmodelo) == 0 && this.pcolor.compareToIgnoreCase(bici.pcolor) == 0 );
};
public Boolean Getbici(Bicicleteria a,Bicicleteria b){
return a.equals(b);
};
@Override
public int hashCode(){
int d = 3;
GregorianCalendar fecha = new GregorianCalendar();
int c = fecha.get(Calendar.SECOND);
int a = d*( Getpmarca().compareTo(Getpmodelo()) + Getpmodelo().compareTo(Getpcolor()))+ c;
return a ;
};
Object o = null ;
@Override
public String toString(){
return
" Marca: " + pmarca + " Modelo: " + pmodelo + " Color: " + pcolor + " precio: " + String.format("%.2f%n",pprecio);
};
public static void main(String[] args) {
Bicicleteria bicicleta1 = new Bicicleteria("a", "a", "a");
Bicicleteria bicicleta2 = new Bicicleteria("a","a","a");
Bicicleteria bicicleta3 = new Bicicleteria("a","f","g");
// System.out.println  (fech.LongDateTime());
System.out.println(bicicleta1.pmarca); System.out.println(bicicleta1.pmodelo);
System.out.println(bicicleta1.pcolor);
System.out.println(bicicleta1.pcantidad); System.out.println(bicicleta1.pdisponible);  
System.out.println(bicicleta1.pcontador);
System.out.println(bicicleta2.pmarca);
System.out.println(bicicleta2.pcontador);
System.out.println(bicicleta1.equals (bicicleta2));
System.out.println(bicicleta2.equals(bicicleta3));
System.out.println(bicicleta1);
System.out.println("hashCode1: " + bicicleta1.hashCode());
System.out.println(bicicleta2);
System.out.println("hashCode2: " + bicicleta2.hashCode());
System.out.println(bicicleta3);
System.out.println("hashCode3: " + bicicleta3.hashCode());;
GregorianCalendar df = new  GregorianCalendar();
System.out.println  (df);
System.out.println( df.get(Calendar.SECOND));
}
}
package pruebajava;
import java.util.*;
import pruebajava.Bicicleteria;
public class Navbicicleteria extends Bicicleteria  implements Comparator<Bicicleteria>{
@Override
public int compare(Bicicleteria o1 , Bicicleteria o2)
{
if ((!((o1 instanceof Bicicleteria)
&&  (o2 instanceof Bicicleteria)))|| (o1 == null) || (o2 == null)) {return -1;};
if (o1 == o2){return 0;};
Bicicleteria bici1 = (Bicicleteria) o1;
Bicicleteria bici2 = (Bicicleteria) o2;
return (bici1.Getpmarca().compareToIgnoreCase(bici2.Getpmarca()) + bici1.Getpmodelo().compareToIgnoreCase(bici2.Getpmodelo()) + bici1.Getpcolor().compareToIgnoreCase (bici2.Getpcolor()));
};
@Override
public  boolean equals(Object o){
 if (!(o instanceof Bicicleteria) || o== null ) {return false ;};
 if (this == o){
   return true;
};  
 Bicicleteria bici = (Bicicleteria) o;
return
(this.pmarca.compareToIgnoreCase(bici.pmarca) == 0 && this.pmodelo.compareToIgnoreCase(bici.pmodelo) == 0 && this.pcolor.compareToIgnoreCase(bici.pcolor) == 0 );
};
public Boolean Getbici(Bicicleteria a,Bicicleteria b){
return a.equals(b);
};
@Override
public int hashCode(){
int d = 3;
GregorianCalendar fecha = new GregorianCalendar();
int c = fecha.get(Calendar.SECOND);
int a = d*( Getpmarca().compareTo(Getpmodelo()) + Getpmodelo().compareTo(Getpcolor()))+ c;
return a ;
};
Object o = null ;
@Override
public String toString(){
return
" Marca: " + pmarca + " Modelo: " + pmodelo + " Color: " + pcolor + " precio: " + String.format("%.2f%n",pprecio)  ;
};
//public static void main(String[] args) {
static NavigableSet<Bicicleteria> navigablebiciset= new TreeSet<>(new Navbicicleteria());
static public void precioCambiado(){System.out.println("Precio modificado");};
static public void biciInexiste(){System.out.println("Tipo de bicicleta inexistente");};
static public void CambioPrecio( String pmarca, String pmodelo,String pcolor, double newprecio ){
Bicicleteria getbici= new Bicicleteria (pmarca ,pmodelo , pcolor );
if ((!(navigablebiciset.ceiling(getbici)==null)) &&
(navigablebiciset.ceiling(getbici).equals(getbici)==true)){ navigablebiciset.ceiling(getbici).pprecio = newprecio;
precioCambiado();}
else {biciInexiste();};
getbici = null;
};
static public void cantCambiada(){
 System.out.println("Cantidad de bicicletas cambiada");
};
static public void Cambiocantidad( String pmarca, String pmodelo, String pcolor, int newcantidad){
Bicicleteria getbici = new Bicicleteria(pmarca,pmodelo,pcolor);
if((!(navigablebiciset.ceiling(getbici)==null)&& (navigablebiciset.ceiling(getbici).equals(getbici)==true))){navigablebiciset.ceiling(getbici).pcantidad = newcantidad;
cantCambiada();}
else{ biciInexiste();};
getbici = null;
};
public static void main(String[] args){
navigablebiciset.add(new Bicicleteria("a", "b", "c"));
navigablebiciset.add(new Bicicleteria("d", "e", "f"));
navigablebiciset.add(new Bicicleteria("a", "b", "c"));
System.out.println(navigablebiciset);
CambioPrecio("d","e","f",20000);
System.out.println("nuevoprecio: ");
System.out.println(navigablebiciset);
}
}

 
Knute Snortum
Sheriff
Posts: 6033
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not quite right either.  Did you press Preview first?

First of all, don't quote yourself, just click Reply.  Second, please UseCodeTags (that's a link), not quotes, to surround your code with.
 
Carey Brown
Bartender
Posts: 5906
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, I ran this through Eclipse's formatter.



 
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't start thinking about the serialization and in particular persisting results to database yet. You want to store to database correct data.

I'd at least start testing your code first. Code in general isn't readable to me, so that is the biggest worry as of now in order to understand it, lots of parts do not make sense, i.e. you have Bicicleteria type variable defined, then you cast Bicicleteria type veriable to Bicicleteria (same one), then do some other weird things, on quite a few cases I'm unsure whether the code even compiles.

So I'd really advice you to stop considering writting extra code and start cleaning up this one and writing tests to verify it works. Your classes have code ranging from 150 - 250 lines of code, in my opinion they are too big and do too many things which likely are unrelated, hence could be extracted to classes where they supposed to be.

Not sure about your time constraints and how much time you got for that, but that would be a lot of work to re-do things. On the other hand, moving forward with what you have, you may end up nowhere at all. I think former approach is more promising.
 
Ranch Hand
Posts: 84
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are prefixing all variables with "p", why? You just make your code harder to read. Also, you can make your fields private.

What are the Fechalong and Fechasegundos for? Are you trying to get the timestamp from the date or something similar?


 
Carey Brown
Bartender
Posts: 5906
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are placing a semicolon (;) after most of your closing braces ( }; ). This is almost never correct. You are getting away with it because a single semicolon by itself is an empty statement, but if you continue to do this you add confusion for your readers and could introduce bugs.

One case where it is necessary is
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now work all. The absolute millisecond long that i did i corrected yesterday is only to make an excersice. I only have an old phone to program. In the phones dont appear the options that you are talking about. Now I gonna learn exceptions. I try to make methods to add and modify to live the excemptions only for data. I dont learn data transfers already. I speak spanish. To share with the forum i only can copy and paste. I am overriding methods more than it needed this is to be shure that this method is that is being used and to have it in the class to see wich method that i am using. The p is to say that this storage a PRIMITIVE VALUE.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The files are there:

https://1drv.ms/f/s!Asr3Ewn45KOLtFhrZ8iWbRFMJ905
 
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the files; people are reluctant to download something from an unfamiliar location.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is safe microsoft Onedrive. Have the java files. I cant attach the files and the window now dont let me paste the code. in my phone dont have the code option.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package pruebajava;
import java.util.*;
import java.util.GregorianCalendar;
import pruebajava.Fechalong;
import pruebajava.Fechasegundos;
public class Bicicleteria
{
  String pmarca;
  String pmodelo;
  String pcolor;
  double pprecio;
  boolean pdisponible;
  int pcantidad;
  int pcontador;
  static int spcontador;
  long pfecha;
  long pfechasegundos ;
public Bicicleteria(){};

public static Calendar haceCalendario(){
 Calendar date = new GregorianCalendar();
 return date;
};
public Bicicleteria(String pmarca, String pmodelo, String pcolor)
{
  this.pmarca = pmarca;
  this.pmodelo  = pmodelo;
  this.pcolor = pcolor;
  this.pprecio = 0;
  this.pcantidad = 0;
  this.pdisponible = false;
 spcontador++;
  this.pcontador = spcontador;
//static Fechasegundos fechas = ()->{
Fechasegundos fechs = new Fechasegundos()
{
public long LongFechaSegundos()
{
  long pdia2;
  long pmes2;
  long pdiasmes;
  long pyear2;
  long pyviciesto;
  long phora2;
  long pminuto2;
  long psegundo2;
  long pcontfechasegundos;
  long pmilis;
Calendar date = haceCalendario();
pdia2 = (long) date.get(Calendar.DAY_OF_MONTH);
pmes2 = (long) date.get(Calendar.MONTH);
switch ( (int) pmes2){
case 1:  pdiasmes = 31;
break;
case 2:  pdiasmes = 28;
break;
case 3:  pdiasmes = 31;
break;
case 4:  pdiasmes = 30;
break;
case 5:  pdiasmes = 31;
break;
case 6:  pdiasmes = 30;
break;
case 7:  pdiasmes = 31;
break;
case 8:  pdiasmes = 31;
break;
case 9:  pdiasmes = 30;
break;
case 10: pdiasmes = 31;
break;
case 11: pdiasmes = 30;
break;
default: pdiasmes = 31;};
pyear2 = (long) date.get(Calendar.YEAR);
if
(((pyear2-2016) % 4) == 0)
pyviciesto = ((pyear2/4)-1)*24* 3600;
else
pyviciesto = (pyear2/4)*24*3600;
phora2 = (long) date.get(Calendar.HOUR);
pminuto2 = (long) date.get(Calendar.MINUTE);
psegundo2 = (long) date.get(Calendar.SECOND);
pmilis = (long) date.get(Calendar.MILLISECOND);
pcontfechasegundos = (psegundo2 + pminuto2* 60 + phora2 * 3600 + pdia2 * 24 * 3600 + pdiasmes * 24 * 3600 + pyear2 * 365 * 24 * 3600 + pyviciesto)*1000 + pmilis;
return  (long) pcontfechasegundos;
};
};
//static Fechalong fech = ()->{
Fechalong fech = new Fechalong()
{
public long LongDateTime()
{
  long pdia;
  long pmes;
  long pyear;
  long phora;
  long pminuto;
  long psegundo;
  long pcontfechahora;
Calendar date = haceCalendario();
pdia = (long) date.get(Calendar.DAY_OF_MONTH);
pmes = (long) date.get(Calendar.MONTH);
pyear = (long) date.get(Calendar.YEAR);
phora = (long) date.get(Calendar.HOUR);
pminuto = (long) date.get(Calendar.MINUTE);
psegundo = (long) date.get(Calendar.SECOND);
pcontfechahora = psegundo +  pminuto * 100 + phora * 10000 + pdia * 1000000+ pmes * 100000000 + pyear * 10000 * 1000000;
return (long) pcontfechahora;
};
};
this.pfecha = fech.LongDateTime();
this.pfechasegundos = fechs.LongFechaSegundos();
};
public String Getpmarca(){
  return  this.pmarca;
};
public String Getpmodelo(){
  return  this.pmodelo;
};
public String Getpcolor(){
  return  this.pcolor;
};
public Double Getpprecio(){
  return this.pprecio;
};
public int Getpcantidad(){
  return  this.pcantidad;
};
public boolean Getpdisponible(){
  return  this.pdisponible;
};
public void Setpprecio(double pprecio){
  this.pprecio = pprecio;
};
public void Setpdisponible(boolean pdisponible){
 this.pdisponible = pdisponible;
};
public void Setpcantidad(int pcantidad){
 this.pcantidad = pcantidad;
};
@Override
public  boolean equals(Object o){
 if (!(o instanceof Bicicleteria) || o== null ) {return false ;};
 if (this == o){
   return true;
};
Bicicleteria bici = (Bicicleteria) o;
return
(this.pmarca.compareToIgnoreCase(bici.pmarca) == 0 && this.pmodelo.compareToIgnoreCase(bici.pmodelo) == 0 && this.pcolor.compareToIgnoreCase(bici.pcolor) == 0 );
};
public Boolean Getbici(Bicicleteria a,Bicicleteria b){
return a.equals(b);
};
@Override
public int hashCode(){
int d = 3;
GregorianCalendar fecha = new GregorianCalendar();
int c = fecha.get(Calendar.SECOND);
int a = d*( Getpmarca().compareTo(Getpmodelo()) + Getpmodelo().compareTo(Getpcolor()))+ c;
return a ;
};
Object o = null ;
@Override
public String toString(){
return
"Fecha: " + pfecha + " control nro.: " + pfechasegundos + " Marca: " + pmarca + " Modelo: " + pmodelo + " Color: " + pcolor +" Cantidad: " + pcantidad + " Disponible: " + pdisponible + " precio: " + String.format("%.2f%n",pprecio);
};
public static void main(String[] args) {
Bicicleteria bicicleta1 = new Bicicleteria("a", "a", "a");
Bicicleteria bicicleta2 = new Bicicleteria("a","a","a");
Bicicleteria bicicleta3 = new Bicicleteria("a","f","g");
// System.out.println  (fech.LongDateTime());
System.out.println(bicicleta1.pmarca); System.out.println(bicicleta1.pmodelo);
System.out.println(bicicleta1.pcolor);
System.out.println(bicicleta1.pcantidad); System.out.println(bicicleta1.pdisponible);  
System.out.println(bicicleta1.pcontador);
System.out.println(bicicleta2.pmarca);
System.out.println(bicicleta2.pcontador);
System.out.println(bicicleta1.equals (bicicleta2));
System.out.println(bicicleta2.equals(bicicleta3));
System.out.println(bicicleta1);
System.out.println("hashCode1: " + bicicleta1.hashCode());
System.out.println(bicicleta2);
System.out.println("hashCode2: " + bicicleta2.hashCode());
System.out.println(bicicleta3);
System.out.println("hashCode3: " + bicicleta3.hashCode());;
GregorianCalendar df = new  GregorianCalendar();
System.out.println  (haceCalendario());
System.out.println( df.get(Calendar.SECOND));
}
}
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package pruebajava;
import java.util.*;
import pruebajava.Bicicleteria;
public class Navbicicleteria extends Bicicleteria  implements Comparator<Bicicleteria>{
@Override
public int compare(Bicicleteria o1 , Bicicleteria o2)
{
if ((!((o1 instanceof Bicicleteria)
&&  (o2 instanceof Bicicleteria)))|| (o1 == null) || (o2 == null)) {return -1;};
if (o1 == o2){return 0;};
Bicicleteria bici1 = (Bicicleteria) o1;
Bicicleteria bici2 = (Bicicleteria) o2;
return (bici1.Getpmarca().compareToIgnoreCase(bici2.Getpmarca()) + bici1.Getpmodelo().compareToIgnoreCase(bici2.Getpmodelo()) + bici1.Getpcolor().compareToIgnoreCase (bici2.Getpcolor()));
};
@Override
public  boolean equals(Object o){
 if (!(o instanceof Bicicleteria) || o== null ) {return false ;};
 if (this == o){
   return true;
};  
 Bicicleteria bici = (Bicicleteria) o;
return
(this.pmarca.compareToIgnoreCase(bici.pmarca) == 0 && this.pmodelo.compareToIgnoreCase(bici.pmodelo) == 0 && this.pcolor.compareToIgnoreCase(bici.pcolor) == 0 );
};
public Boolean Getbici(Bicicleteria a,Bicicleteria b){
return a.equals(b);
};
@Override
public int hashCode(){
int d = 3;
GregorianCalendar fecha = new GregorianCalendar();
int c = fecha.get(Calendar.SECOND);
int a = d*( Getpmarca().compareTo(Getpmodelo()) + Getpmodelo().compareTo(Getpcolor()))+ c;
return a ;
};
Object o = null ;
@Override
public String toString(){
return
"Fecha/hora: " + pfecha + " control nro.: "+ pfechasegundos + " Marca: " + pmarca + " Modelo: " + pmodelo + " Color: " + pcolor + " Cantidad: " + pcantidad + " Disponible: " + pdisponible + " precio: " + String.format("%.2f%n",pprecio)  ;
};
//public static void main(String[] args) {
static NavigableSet<Bicicleteria> navigablebiciset= new TreeSet<>(new Navbicicleteria());
static void bicirepetida(){
 System.out.println("La bicicileta que intenta ingresar ya existe");
};
static void biciingresada(){
 System.out.println("La bicicleta ha sido ingresada");
}
static public void Nuevabici( String pmarca,String pmodelo,String pcolor){
Bicicleteria newbici = new Bicicleteria(pmarca,pmodelo,pcolor);
if (navigablebiciset.ceiling(newbici)==null){navigablebiciset.add(newbici);
biciingresada();}
else {bicirepetida();};
};
static public void precioCambiado(){System.out.println("Precio modificado");};
static public void biciInexiste(){System.out.println("Tipo de bicicleta inexistente");};
static public void CambioPrecio( String pmarca, String pmodelo,String pcolor, double newprecio ){
Bicicleteria getbici= new Bicicleteria (pmarca ,pmodelo , pcolor );
if ((!(navigablebiciset.ceiling(getbici)==null)) &&
(navigablebiciset.ceiling(getbici).equals(getbici)==true)){ navigablebiciset.ceiling(getbici).pprecio = newprecio;
precioCambiado();}
else {biciInexiste();};
getbici = null;
};
static public void cantCambiada(){
 System.out.println("Cantidad de bicicletas cambiada");
};
static public void Cambiocantidad( String pmarca, String pmodelo, String pcolor, int newcantidad){
Bicicleteria getbici = new Bicicleteria(pmarca,pmodelo,pcolor);
if((!(navigablebiciset.ceiling(getbici)==null)&& (navigablebiciset.ceiling(getbici).equals(getbici)==true))){navigablebiciset.ceiling(getbici).pcantidad = newcantidad;
if (navigablebiciset.ceiling(getbici).pcantidad > 0){navigablebiciset.ceiling(getbici).pdisponible = true;}
else
{navigablebiciset.ceiling(getbici).pdisponible = false;};
cantCambiada();}
else{ biciInexiste();};
getbici = null;
};
public static void main(String[] args){
navigablebiciset.add(new Bicicleteria("a", "b", "c"));
navigablebiciset.add(new Bicicleteria("d", "e", "f"));
navigablebiciset.add(new Bicicleteria("a", "b", "c"));
System.out.println(navigablebiciset);
CambioPrecio("d","e","f",20000);
System.out.println("nuevoprecio: ");
System.out.println(navigablebiciset);
Cambiocantidad("a","b","c",25);
System.out.println("nuevacantidad: ");
System.out.println(navigablebiciset);
System.out.println("nueva Bicicleta;");
Nuevabici("g","h","j");
System.out.println(navigablebiciset);
Nuevabici("a","b","c");
System.out.println(navigablebiciset);
}
}
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

package pruebajava;
//@FunctionalInterface
public interface Fechalong {
long LongDateTime();
public static long fechal =0;
}
package pruebajava;
@FunctionalInterface
interface Fechasegundos {
long LongFechaSegundos();
public static long fechas =0;
}
 
Knute Snortum
Sheriff
Posts: 6033
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start using code tags.  It really increases the readability of the code.  The more effort you put into posting correctly, the more people will be willing to help you.
 
Carey Brown
Bartender
Posts: 5906
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are doing a lot of work with date and time. It looks like you're trying to convert 'now' into milliseconds. Is this a local date/time or UTC date/time?

You seem to be using the current milliseconds in your hashCode() method. This means that every time you call hashCode() that you'll get a different value. HashCode() is supposed to return the same value for an object regardless of time.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i did a hashcode with time. This depends on what it used for. I tried with millisecond and dont work the next seach do one millisecond more. Was a practice with java timing. The java is not so fast. Cant be implemented a database to control a secure fast device the answer is over 1 millisecond. So can warranty only 100 hz. An engine module needs at least 100 hz (6000 rpm / 60 seconds) so is to slow fot that. the next learning will be exceptions if i found something i interesting i can share it with you.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also i tried with random seed and second to do a hashcode with fast expire date and with no calculation at all.
 
Carey Brown
Bartender
Posts: 5906
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

dario lobos wrote:Also i tried with random seed and second to do a hashcode with fast expire date and with no calculation at all.


In general, the equals() method and the hashCode() methods are designed at the same time. The rule is that any two objects that are equal MUST generate the SAME hash code. If you are adding in time or random numbers into the hash code then you are breaking this design rule. Note that two objects with same hash value are not necessarily equal.

Equals() and hashCode() can take all the object's fields into account or just use the fields that make up a unique key for the object, something like: make, model, and color (assuming the three together are unique).
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The definition of the rule dont speak nothing about time if you do with my hashcode

a.hascode().equals(b.hascode())
With the proper equal will work

Also if a.equals(b) with my hascode
during a second a.hashcode() = b.hashcode()


AND ANY TIME THAT YOU NEED YOU CAN OVERRIDE THE HASHCODE.

IF YOU GONNA STORE a.hascode() and later you gonna request a searching as a.hascode() this will not usefull. The request of hashcode have to be at the same time and  for comparations.

This dont brake the law i only apply the law with a restiction in a period of time to see if it work and during which period of time.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I the hascode that i did and the equals i acomplish that if a.equals(b)  a.hascode() = b.hascode() doing the requests of hascode at the same time.
 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

You seems do not understand what the hashcode is and why is it for. Your last posts do not make sense to me.

Please explain why you override hashcode and equals?
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give an example of usage. you can have a module of injection with a database that have

Air flow input
throttle position input
lamnda sensor value input
barometer input
gas to injector(time for solenoid) output

The output is no more usefull after a second and you have to discard this output with the hashcode with time you can do it fast.
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To compare parts of a particular object and not all the object. Hash you can use it for encription. Is depends on what you use it for. if you dont like to use time in hashcode feel free to dont use it
 
dario lobos
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a hashcode with time and you are requesting a data with 4 parameters
data.1
data.2
data.3
data.4
When you request you do data.hascode() and store it.
When you receive the data as receiveddata
if !(storeddatahascode = receiveddata.hashcode())
YOU HAVE TO REQUEST THE DATA AGAIN maybe with other parameters.
When you store data in the table YOU CAN OVERRIDE THE HASCODE and use a hashcode without time if you need it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!