0

I'm always getting the same output from

System.out.println(Software1.toString());

Class:

public class Software{  /**Dichiarazione                                */
private String denominazione;
private String produttore;
private String versione;
private String sistemaOperativo;
private static int anno;
private static double prezzo;

    Software(){         /**Costruttore di default                            */
        denominazione = "Software";
        produttore = "D'Avanzo";
        versione = "1.0";
        sistemaOperativo = "Windows";
        anno = 2021;
        prezzo = 100.00;
    }                   /**Costruttore non di default                        */
    Software(String denominazione, String produttore, String versione, String sistemaOperativo, int anno, double prezzo){
        this.denominazione = denominazione;
        this.produttore = produttore;
        this.versione = versione;
        this.sistemaOperativo = sistemaOperativo;
        this.prezzo = prezzo;
        this.anno = anno;
    }
    /** Setter e getter                                                     */
    public String getDenominazione(){   
        return this.denominazione;
    }
    public void setDenominazione(String denominazione){
        denominazione = denominazione;
    }
    
    public String getProduttore(){
        return this.produttore;
    }
    public void setProduttore(String produttore){
        produttore = produttore;
    }
    
    public String getVersione(){
        return this.versione;
    }
    public void setVersione(String versione){
        versione = versione;
    }
    
    public String getSistemaOperativo(){
        return this.sistemaOperativo;
    }
    public void setSistemaOperativo(String sistemaOperativo){
        sistemaOperativo = sistemaOperativo;
    }
    
    public int getAnno(){
        return this.anno;
    }
    public void setAnno(int anno){
        anno = anno;
    }
    
    public double getPrezzo(){
        return this.prezzo;
    }
    public void setPrezzo(double prezzo){
        prezzo = prezzo;
    }
    
    public String toString(){   
      return "Denominazione: "+ denominazione +"\nProduttore: " + produttore + "\nVersione: " + versione + "\nSistema Operativo: "
      + sistemaOperativo + "\nAnno: " + anno + "\nPrezzo: " + prezzo;
    }
    
    public int compareAnno(Software Software2){
        if (this.anno == Software.anno) return 0;
        else if (this.anno < Software.anno) return -1;
        else return 1;
    }
    public int comparePrezzo(Software Software2){
        if (this.prezzo == Software.prezzo) return 0;
        else if (this.prezzo < Software.prezzo) return -1;
        else return 1;
    }
} 

main:

import java.util.Scanner;
public class mainSoftware{
    public static void main(String[] args){
       Scanner input = new Scanner(System.in);
       int scelta;
       Software Software1 = new Software();
       Software Software2 = new Software();
       System.out.println("Inserisci la scelta");
       System.out.println("0) Uscire");
       System.out.println("1) Inserimento di tutti i dati primo oggetto");
       System.out.println("2) Informazioni primo oggetto");
       System.out.println("3) Comparazione Anno d'uscita");
       System.out.println("4) Comparazione prezzo");
       System.out.println("5) Inserimento di tutti i dati secondo oggetto");
       System.out.println("6) Informazioni secondo oggetto");
       do{
           try{  
               int a = Integer.parseInt(input.nextLine());  
           }catch(NumberFormatException ex){};
           scelta = Integer.parseInt(input.nextLine());
           switch(scelta){
               case 1:
                    System.out.println("Inserisci la denominazione: ");
                    Software1.setDenominazione(input.nextLine());
                    System.out.println("Inserisci il produttore: ");
                    Software1.setProduttore(input.nextLine());
                    System.out.println("Inserisci la versione: ");
                    Software1.setVersione(input.nextLine());
                    System.out.println("Inserisci il sistema operativo: ");
                    Software1.setSistemaOperativo(input.nextLine());
                    System.out.println("Inserisci il prezzo: ");
                    Software1.setPrezzo(Double.parseDouble(input.nextLine()));
                    System.out.println("Inserisci l'anno: ");
                    Software1.setAnno(input.nextInt());
                    break;
               case 2:
                    System.out.println(Software1.toString());
                    break;
               case 3: 
                    if(Software1.compareAnno(Software2) == 0) System.out.println("Sono usciti lo stesso anno");
                    else if (Software1.compareAnno(Software2) < 0) System.out.println("Il Software 2 è uscito prima");
                    else System.out.println("Il Software 1 è uscito prima");
                    break;
               case 4:
                    if(Software1.comparePrezzo(Software2) == 0) System.out.println("Costano uguale");
                    else if (Software1.comparePrezzo(Software2) < 0) System.out.println("Il software 2 costa di più");
                    else System.out.println("Il software 1 costa di più");
                    break;
               case 5:
                    System.out.println("Inserisci la denominazione: ");
                    Software2.setDenominazione(input.nextLine());
                    System.out.println("Inserisci il produttore: ");
                    Software2.setProduttore(input.nextLine());
                    System.out.println("Inserisci la versione: ");
                    Software2.setVersione(input.nextLine());
                    System.out.println("Inserisci il sistema operativo: ");
                    Software2.setSistemaOperativo(input.nextLine());
                    System.out.println("Inserisci l'anno: ");
                    Software2.setAnno(input.nextInt());
                    System.out.println("Inserisci il prezzo: ");
                    Software2.setPrezzo(Double.parseDouble(input.nextLine()));
                    break;
               case 6:
                    System.out.println(Software2.toString());
                    break;
               default: 
                    System.out.println("Numero errato");
                    break;
           }
           if(scelta != 0) System.out.println("Inserisci la scelta");
        }while(scelta != 0);
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
epiclolHD
  • 11
  • 1
  • 2
    `denominazione = denominazione;` sets the variable to itself. You have to use `this` for the field: `this.denominazione = denominazione;` – QBrute Mar 29 '21 at 10:27
  • 2
    `anno = anno;` is assigning a variable to itself. You'll need to use `this` to tell yoour compiler that one of those refers to the field. See: [The Java Tutorials - Using this with a Field](https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html) – OH GOD SPIDERS Mar 29 '21 at 10:27
  • 1
    Any reason why `anno` and `prezzo` are `static`? That doesn't make sense to me. – MC Emperor Mar 29 '21 at 10:29
  • @MCEmperor because in compareAnno it says Non static variable anno cannot be referenced from a static context, that's the only fix i've found by myself – epiclolHD Mar 29 '21 at 10:31
  • @OHGODSPIDERS Ty now setters are working, i didn't remember i needed to put this. on setters too. – epiclolHD Mar 29 '21 at 10:33
  • 1
    @IgorMiti Well, that is the wrong fix for the problem. In your compare method, you should refer to `Software2`. This brings me to an important thing, because this is related to your problem about the `static` members: you should follow the Java Naming Conventions: variable names and method names are written in camelCase (so they start with lowercase). Otherwise, many programmers (including the future you) will easily confuse them with class names. I also suggest you study [the basics of the `static` keyword](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html). – MC Emperor Mar 29 '21 at 10:35
  • @OffByOne you deleted your answer before I could answer your comment: tips for OP are ok, but they should either be part of a full answer or posted as a comment. Understandably, your rep is too low to comment, but that still does not mean answers should be used to make comments as an alternative. – Federico klez Culloca Mar 29 '21 at 10:35
  • @MCEmperor Why does in the compare method refering to Software2 work? Where's the theory behind that? – epiclolHD Mar 29 '21 at 10:44
  • @IgorMiti because you're comparing the current instance (`this`) with the instance you're passing as an argument to the `compareAnno` and `comparePrezzo` methods (`Software2`). – Federico klez Culloca Mar 29 '21 at 10:51
  • @IgorMiti Exactly what Federico says. Those two compare methods both accept a parameter called `Software2`, but you are not even using it within your method body. Instead, you seem to use the class name to qualify your variables, but doing so doesn't have the effect what you're probably hoping for. That's why I also suggested to rename your variables to camelCase (as opposed to PascalCase, which in turn start with uppercase). `Software2` should be `software2`. Hereafter, all camelCase names are variables, and all PascalCase names are class names. – MC Emperor Mar 29 '21 at 11:17
  • @IgorMiti Your `compareAnno(Software Software2)` method should use both `this` (the current instance of `Software` and *another* `Software`, which are to be compared. So I would expect something like `if (this.anno == software2.anno)` (of course, after you renamed the variable `Software2` to `software2`). – MC Emperor Mar 29 '21 at 11:20

1 Answers1

1

If you use the same names for parameters and fields, you shoud write your setters like this:

public void setVersione(String versione){
    this.versione = versione;
}

Otherwise, you are setting a variable to itself.

jurez
  • 4,436
  • 2
  • 12
  • 20