Results 1 to 14 of 14
Like Tree6Likes
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829

Thread: CompareTo not working

  1. #1
    Mshelly is offline Member
    Join Date
    Jul 2014
    Location
    New Jersey
    Posts
    21
    Rep Power
    0

    Question CompareTo not working

    My instructor gave me this code to use as my Array sort. I can not get this to work. I do not get any compile errors, the code just does not do anything. Any advise on what I am doing wrong would be great.

    Java Code:
    package inventoryprogram4;
    import java.util.Scanner;
    
    /**
     *
     * @author michelle
     */
    
    public class Inventoryprogram4
    {
     static GUI mainGUI = new GUI();
     static String outText = "";              
        /**
         * @param args the command line arguments
         */
    static int max = 5;
    static final Genre[] DVD = new Genre[max];
    
    //create array
    public static void main(String[] args)
    {    
        Scanner input = new Scanner(System.in);
        
        Genre g;
        
            DVD[0] = new Genre("Dawn of the Dead", 123456, 10, 10,"Horror");
            DVD[1] = new Genre("Walking Dead S1", 234567, 10, 20,"TV");
            DVD[2] = new Genre("Tinkerbell", 345678, 10, 12,"Kids");
            DVD[3] = new Genre("My Sisters Keeper", 456789, 10, 15,"Drama");
            DVD[4] = new Genre("Star Wars", 567890, 5, 30,"Scifi");
            Calculate(DVD);
            print(DVD);
                 
          
      mainGUI.ta.setText(outText);      
       
    }//end array
         //count and print
        static void print(Genre[] DVD)
        {      
            for (int i = 0; i < max; i++)
            {
    outText+=("\nName: " + DVD[i].Name + ", UPC: " + DVD[i].UPC + ", QTY: " + DVD[i].Stock 
            + "Price: $"+DVD[i].Price+" DVD Value: $" + DVD[i].Value +" Genre: " + DVD[i].genre);
            }
         {//print
    Genre g;
     g = new Genre ("Dawn of the Dead", 123456, 10, 10,"Horror");
    outText+=("\n Genre: "+g.getMovieGenre()+" Has a restock fee:$"+g.getFee());
    g = new Genre ("Walking Dead S1", 234567, 10, 20,"TV");
    outText+=("\n Genre: "+g.getMovieGenre()+" Has a restock fee:$"+g.getFee());
    
        g = new Genre ("Tinkerbell", 345678, 10, 12,"Kids");
    outText+=("\n Genre: "+g.getMovieGenre()+" Has a restock fee:$"+g.getFee());
    
        g = new Genre ("My Sisters Keeper", 456789, 10, 15,"Drama");
    outText+=("\n Genre: "+g.getMovieGenre()+" Has a restock fee:$"+g.getFee()); 
    
        g = new Genre ("Star Wars", 567890, 5, 30,"Scifi");
        
    outText+=("\n Genre: "+g.getMovieGenre()+" Has a restock fee:$"+g.getFee());             
    
            }//end print
        }
        //calculate the value of DVD stock
        static void Calculate(Genre[] DVD)
        {
            int ttlvalue = 0;     
            for (int i = 0; i < max; i++)
            {
            ttlvalue += DVD[i].Value;
            }
     
    outText+=("Total Inventory Value is: $" + ttlvalue); 
        }
        static void sort(Genre[]DVD) //Gave me this
        {
      java.util.Arrays.sort(DVD);//Gave me this
        }
     
    }//end InventoryProgram
    
    //create class
    class Product implements Comparable<Product>//Gave me this
    {
        int Stock, UPC, Price,Value;
        String Name, genre;
        int Fee;
     
    public Product(String DVDName, int UPCNum, int StockQty, int Retail,String MovieGenre)
            {
            this.Name = DVDName;
            this.UPC = UPCNum;
            this.Stock = StockQty;
            this.Price = Retail;
            this.genre=MovieGenre;
            this.Value=Stock*Price;
            
            }
     @Override
    public int compareTo(Product sortProduct)//Gave me this NOT doing anything
    {
            return this.genre.compareTo(genre);//Gave me this Not doing anything
    }
    }//end product class
    Java Code:
    package inventoryprogram4;
    
    
    public class Genre extends Product
    {
        public String MovieGenre;
        public int Fee;
     
        public Genre(String DVDName, int UPCNum, int StockQty, int Retail, String MovieGenre) 
        {
            super(DVDName, UPCNum, StockQty, Retail, MovieGenre);
            this.MovieGenre=genre;
            this.Fee=0;
        }
        
        public void setMovieGenre(String genre)
        {
            this.MovieGenre=genre;
        }
        public String getMovieGenre()
        {
            return genre;
        }
        public int setFee()
        {
            super.Fee= (int) (Value *.05);
            return super.Fee;
        }
        public int getFee()
        {
            return super.Fee=(int) (Value*.05);
        }
        @Override
        public String toString()
        {
            return String.format(" Genre:" + getMovieGenre()+" Has a Restock Fee: "+getFee());
        }
    }
    Java Code:
    package inventoryprogram4;
    
    import javax.swing.*;
    import java.awt.FlowLayout;
    
    public class GUI extends JFrame
    {
    	
    	
    	JTextArea ta = new JTextArea(25,40);
    	
    	JScrollPane sp = new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    	
    	JPanel pane = new JPanel();
    	
    	public GUI() 
            {
    		super("DVD Inventory Program");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setBounds(100,100,500,500);
    		pane.add(sp);
    		add(pane);
    		setVisible(true);
    	}

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: CompareTo not working

    Ok, your class structure is messed up. You need to remove any understanding of genre and fee from the Product class. That is the sole responsibility of the Genre class. Think about it. Why should Product know anything about genre or fee? What if some other class subclassed Product?

    Anyway, once you do that, move the Comparable implementation into the Genre class and change the generic type from Product to Genre.

    In your Genre class, you have the following:

    Java Code:
    public int setFee()    {
            super.Fee= (int) (Value *.05);
            return super.Fee;
    }
    public int getFee() {
            return super.Fee=(int) (Value*.05);
    }
    Setters just set and are declared void.
    Getters just get and are declared the type your return.

    And remember, this value should not be in the Product class (otherwise you would have put the setter and getter methods there too and probably passed the fee via the constructor).

    Finally, you need to call sort, which you didn't do (it didn't work anyway).

    Java Code:
    sort(DVD);
    Regards,
    Jim
    Last edited by jim829; 07-16-2014 at 11:00 PM.
    Mshelly likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Mshelly is offline Member
    Join Date
    Jul 2014
    Location
    New Jersey
    Posts
    21
    Rep Power
    0

    Default Re: CompareTo not working

    Hi Jim,

    I made the fixes with my classes (I think). I am not sure where to put the call now. As now it does not match what the teacher had done.
    Thanks in advance

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    import java.util.Scanner;
    
    /**
     *
     * @author michelle
     */
    
    public class Inventoryprogram4
    {
     static GUI mainGUI = new GUI();
     static String outText = "";              
        /**
         * @param args the command line arguments
         */
    static int max = 5;
    static final Genre[] DVD = new Genre[max];
    
    //create array
    public static void main(String[] args)
    {    
        Scanner input = new Scanner(System.in);
        
        Genre g;
        
            DVD[0] = new Genre("Dawn of the Dead", 123456, 10, 10,"Horror");
            DVD[1] = new Genre("Walking Dead S1", 234567, 10, 20,"TV");
            DVD[2] = new Genre("Tinkerbell", 345678, 10, 12,"Kids");
            DVD[3] = new Genre("My Sisters Keeper", 456789, 10, 15,"Drama");
            DVD[4] = new Genre("Star Wars", 567890, 5, 30,"Scifi");
            Calculate(DVD);
            print(DVD);
      mainGUI.ta.setText(outText);     
    }//end array
         //count and print
        static void print(Genre[] DVD)
        {      
            for (int i = 0; i < max; i++)
            {
    outText+=("\nName: " + DVD[i].Name + ", UPC: " + DVD[i].UPC + ", QTY: " + DVD[i].Stock 
            + "Price: $"+DVD[i].Price+" DVD Value: $" + DVD[i].Value +" Genre: " + DVD[i].genre);
            }
         {//print
    Genre g;
     g = new Genre ("Dawn of the Dead", 123456, 10, 10,"Horror");
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee());
    g = new Genre ("Walking Dead S1", 234567, 10, 20,"TV");
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee());
    
        g = new Genre ("Tinkerbell", 345678, 10, 12,"Kids");
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee());
    
        g = new Genre ("My Sisters Keeper", 456789, 10, 15,"Drama");
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee()); 
    
        g = new Genre ("Star Wars", 567890, 5, 30,"Scifi");
        
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee());             
    
           }//end print
        }//end print array
        //calculate the value of DVD stock
        static void Calculate(Genre[] DVD)
        {
            int ttlvalue = 0;     
            for (int i = 0; i < max; i++)
            {
            ttlvalue += DVD[i].Value;
            }
     
    outText+=("Total Inventory Value is: $" + ttlvalue); 
        }//end calculate
    
    }//end InventoryProgram
    
    //create class
    class Product 
    {
        int Stock, UPC, Price,Value;
        String Name;
    
         
    public Product(String DVDName, int UPCNum, int StockQty, int Retail)
            {
            this.Name = DVDName;
            this.UPC = UPCNum;
            this.Stock = StockQty;
            this.Price = Retail;
            this.Value=Stock*Price;
                   
            }
         
    }//end product class
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    
    /**
     *
     * @author michelle
     */
    public class Genre extends Product implements CompareTo<Genre>
    {
        public String genre;
        public int Fee;
        
         public Genre(String DVDName, int UPCNum, int StockQty, int Retail,String genre) 
        {
            super(DVDName, UPCNum, StockQty, Retail);
            this.genre=genre;
            this.Value=Retail*StockQty;
            this.Fee=(int) (Value*.05);        
        }   
    
        public void setgenre(String genre)
        {
            this.genre=genre;
        }
        public String getgenre()
        {
            return genre;
        }
        public int steValue(int StockQty, int Retail)
        {
            this.Value=StockQty*Retail;
            return 0;
        }
        public int getValue()
        {
            return Value;
        }
        public int setFee()
        {
           this.Fee= (int) (Value*.05);
            return 0;
        }
       
        public int getFee()
        {
            return Fee;
        }
         static void sortinv(Genre[] DVD) 
         {
              java.util.Arrays.sort(DVD);
         }
         public int compareTo(Genre sortGenre) 
         {
              return this.genre.compareTo(sortGenre.genre);
         }
        @Override
        public String toString()
        {
            return String.format(" Genre:" + getgenre()+" Has a Restock Fee: "+getFee());
        }
    }
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    
    import javax.swing.*;
    import java.awt.FlowLayout;
    
    public class GUI extends JFrame
    {
    	
    	
    	JTextArea ta = new JTextArea(25,40);
    	
    	JScrollPane sp = new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    	
    	JPanel pane = new JPanel();
    	
    	public GUI() 
            {
    		super("DVD Inventory Program");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setBounds(100,100,500,500);
    		pane.add(sp);
    		add(pane);
    		setVisible(true);
    	}
    	
    }

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: CompareTo not working

    The changes look good. You did change the sort method name to sortinv (not certain why but it doesn't matter).
    Since sorting should not affect the calculations, try this:

    Java Code:
    sortinv(DVD);
    Calculate(DVD);
    print(DVD);
    Regards,
    Jim
    Mshelly likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: CompareTo not working

    I just noticed a couple of things. I believe you changed the location of your sort method. It should be referenced
    via the class Genre.

    Genre.sortinv(DVD).

    Also, your interface declaration is incorrect. It should be ... implements Comparable<Genre>

    I made those changes and it worked for me.

    Regards,
    Jim
    Mshelly likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Mshelly is offline Member
    Join Date
    Jul 2014
    Location
    New Jersey
    Posts
    21
    Rep Power
    0

    Default Re: CompareTo not working

    Hi Jim,
    Thank you. I am so new to this (I am 1 a little over month in) and even though I don't need this for my project I want to learn it and understand it.

    I moved everything to Genre Class, I changed to ... implements Comparable<Genre>. I also put Genre.sortinv(DVD) in (I don't know why I renamed it either). I run and the output is still not sorted. So strange, no errors or anything. Did I miss something?


    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    import java.util.Scanner;
    
    /**
     *
     * @author michelle
     */
    
    public class Inventoryprogram4
    {
     static GUI mainGUI = new GUI();
     static String outText = "";              
        /**
         * @param args the command line arguments
         */
    static int max = 5;
    static final Genre[] DVD = new Genre[max];
    
    //create array
    public static void main(String[] args)
    {    
        Scanner input = new Scanner(System.in);
        
        Genre g;
        
            DVD[0] = new Genre("Dawn of the Dead", 123456, 10, 10,"Horror");
            DVD[1] = new Genre("Walking Dead S1", 234567, 10, 20,"TV");
            DVD[2] = new Genre("Tinkerbell", 345678, 10, 12,"Kids");
            DVD[3] = new Genre("My Sisters Keeper", 456789, 10, 15,"Drama");
            DVD[4] = new Genre("Star Wars", 567890, 5, 30,"Scifi");
            Calculate(DVD);
            print(DVD);
            Genre.sortinv(DVD);
            
      mainGUI.ta.setText(outText);     
    }//end array
         //count and print
        static void print(Genre[] DVD)
        {      
            for (int i = 0; i < max; i++)
            {
    outText+=("\nName: " + DVD[i].Name + ", UPC: " + DVD[i].UPC + ", QTY: " + DVD[i].Stock 
            + "Price: $"+DVD[i].Price+" DVD Value: $" + DVD[i].Value +" Genre: " + DVD[i].genre);
            }
         {//print
    Genre g;
     g = new Genre ("Dawn of the Dead", 123456, 10, 10,"Horror");
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee());
    g = new Genre ("Walking Dead S1", 234567, 10, 20,"TV");
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee());
    
        g = new Genre ("Tinkerbell", 345678, 10, 12,"Kids");
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee());
    
        g = new Genre ("My Sisters Keeper", 456789, 10, 15,"Drama");
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee()); 
    
        g = new Genre ("Star Wars", 567890, 5, 30,"Scifi");
        
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$"+g.getFee());             
    
           }//end print
        }//end print array
        //calculate the value of DVD stock
        static void Calculate(Genre[] DVD)
        {
            int ttlvalue = 0;     
            for (int i = 0; i < max; i++)
            {
            ttlvalue += DVD[i].Value;
            }
     
    outText+=("Total Inventory Value is: $" + ttlvalue); 
        }//end calculate
    
    }//end InventoryProgram
    
    //create class
    class Product 
    {
        int Stock, UPC, Price,Value;
        String Name;
    
         
    public Product(String DVDName, int UPCNum, int StockQty, int Retail)
            {
            this.Name = DVDName;
            this.UPC = UPCNum;
            this.Stock = StockQty;
            this.Price = Retail;
            this.Value=Stock*Price;
                   
            }
         
    }//end product class
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    
    /**
     *
     * @author michelle
     */
    public class Genre extends Product implements Comparable<Genre>
    {
        public String genre;
        public int Fee;
        
         public Genre(String DVDName, int UPCNum, int StockQty, int Retail,String genre) 
        {
            super(DVDName, UPCNum, StockQty, Retail);
            this.genre=genre;
            this.Value=Retail*StockQty;
            this.Fee=(int) (Value*.05);        
        }   
    
        public void setgenre(String genre)
        {
            this.genre=genre;
        }
        public String getgenre()
        {
            return genre;
        }
        public int steValue(int StockQty, int Retail)
        {
            this.Value=StockQty*Retail;
            return 0;
        }
        public int getValue()
        {
            return Value;
        }
        public int setFee()
        {
           this.Fee= (int) (Value*.05);
            return 0;
        }
       
        public int getFee()
        {
            return Fee;
        }
         static void sortinv(Genre[] DVD) 
         {
              java.util.Arrays.sort(DVD);
         }
        @Override
         public int compareTo(Genre sortGenre) 
         {
              return this.genre.compareTo(sortGenre.genre);
         }
        @Override
        public String toString()
        {
            return String.format(" Genre:" + getgenre()+" Has a Restock Fee: "+getFee());
        }
    }
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    
    import javax.swing.*;
    import java.awt.FlowLayout;
    
    public class GUI extends JFrame
    {
    	
    	
    	JTextArea ta = new JTextArea(25,40);
    	
    	JScrollPane sp = new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    	
    	JPanel pane = new JPanel();
    	
    	public GUI() 
            {
    		super("DVD Inventory Program");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setBounds(100,100,500,500);
    		pane.add(sp);
    		add(pane);
    		setVisible(true);
    	}
    	
    }
    My output is:
    Total Inventory Value is: $720
    Name: Dawn of the Dead, UPC: 123456, QTY: 10Price: $10 DVD Value: $100 Genre: Horror
    Name: Walking Dead S1, UPC: 234567, QTY: 10Price: $20 DVD Value: $200 Genre: TV
    Name: Tinkerbell, UPC: 345678, QTY: 10Price: $12 DVD Value: $120 Genre: Kids
    Name: My Sisters Keeper, UPC: 456789, QTY: 10Price: $15 DVD Value: $150 Genre: Drama
    Name: Star Wars, UPC: 567890, QTY: 5Price: $30 DVD Value: $150 Genre: Scifi
    Genre: Horror Has a restock fee:$5
    Genre: TV Has a restock fee:$10
    Genre: Kids Has a restock fee:$6
    Genre: Drama Has a restock fee:$7
    Genre: Scifi Has a restock fee:$7

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: CompareTo not working

    Repost your code and I'll check it in a little while.

    Regards,
    Jim
    Mshelly likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Mshelly is offline Member
    Join Date
    Jul 2014
    Location
    New Jersey
    Posts
    21
    Rep Power
    0

    Default Re: CompareTo not working

    Hi Jim,

    The last post I made had the amended code.

    Thank you again for your help and time.

    Michelle

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: CompareTo not working

    Move the sort call. You are sorting it after printing it out.

    Also, not certain why you are doing the following in your Print method. Creating new instances isn't necessary, just iterate
    thru the existing array and build the outText string using your existing instances (unless I am missing something else that you
    are trying to do).

    Java Code:
    {// print
             Genre g;
             g = new Genre("Dawn of the Dead", 123456, 10, 10, "Horror");
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
             g = new Genre("Walking Dead S1", 234567, 10, 20, "TV");
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
    
             g = new Genre("Tinkerbell", 345678, 10, 12, "Kids");
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
    
             g = new Genre("My Sisters Keeper", 456789, 10, 15, "Drama");
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
    
             g = new Genre("Star Wars", 567890, 5, 30, "Scifi");
    
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
    
          }// end print
    Regards,
    Jim
    Last edited by jim829; 07-17-2014 at 03:47 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Mshelly is offline Member
    Join Date
    Jul 2014
    Location
    New Jersey
    Posts
    21
    Rep Power
    0

    Default Re: CompareTo not working

    Quote Originally Posted by jim829 View Post
    Move the sort call. You are sorting it after printing it out.

    Also, not certain why you are doing the following in your Print method. Creating new instances isn't necessary, just iterate
    thru the existing array and build the outText string using your existing instances (unless I am missing something else that you
    are trying to do).

    Java Code:
    {// print
             Genre g;
             g = new Genre("Dawn of the Dead", 123456, 10, 10, "Horror");
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
             g = new Genre("Walking Dead S1", 234567, 10, 20, "TV");
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
    
             g = new Genre("Tinkerbell", 345678, 10, 12, "Kids");
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
    
             g = new Genre("My Sisters Keeper", 456789, 10, 15, "Drama");
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
    
             g = new Genre("Star Wars", 567890, 5, 30, "Scifi");
    
             outText += ("\n Genre: " + g.getgenre() + " Has a restock fee:$" + g.getFee());
    
          }// end print
    Regards,
    Jim
    HI Jim,

    The sort worked!! Thank you SOOOOO Much!!

    The reason I am creating new instances for this print method is I can not figure out another way for each separate genre to print with its fee.
    Any suggestion? I figured out a way from the array, but the teacher wants it from the subclass so I am not sure this would count.

    Michelle
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    import java.util.Scanner;
    
    /**
     *
     * @author michelle
     */
    
    public class Inventoryprogram4
    {
     static GUI mainGUI = new GUI();
     static String outText = "";              
        /**
         * @param args the command line arguments
         */
    static int max = 5;
    static final Genre[] DVD = new Genre[max];
    
    //create array
    public static void main(String[] args)
    {    
        Scanner input = new Scanner(System.in);
        
        Genre g;
        
            DVD[0] = new Genre("Dawn of the Dead", 123456, 10, 10,"Horror");
            DVD[1] = new Genre("Walking Dead S1", 234567, 10, 20,"TV");
            DVD[2] = new Genre("Tinkerbell", 345678, 10, 12,"Kids");
            DVD[3] = new Genre("My Sisters Keeper", 456789, 10, 15,"Drama");
            DVD[4] = new Genre("Star Wars", 567890, 5, 30,"Scifi");
            Calculate(DVD);
            Genre.sortinv(DVD);
            print(DVD);
         
            
      mainGUI.ta.setText(outText);     
    }//end array
         //count and print
        static void print(Genre[] DVD)
        {      
            for (int i = 0; i < max; i++)
            {
    outText+=("\nName: " + DVD[i].Name + ", UPC: " + DVD[i].UPC + ", QTY: " + DVD[i].Stock 
            + "Price: $"+DVD[i].Price+" DVD Value: $" + DVD[i].Value +" Genre: " + DVD[i].genre);
      
    Genre g;
     g = new Genre ("",0 , 0, 0,"");
    outText+=("\n Genre: "+DVD[i].genre+" Has a restock fee:$"+DVD[i].Value*.05);
    
           }//end print
        }//end print array
        //calculate the value of DVD stock
        static void Calculate(Genre[] DVD)
        {
            int ttlvalue = 0;     
            for (int i = 0; i < max; i++)
            {
            ttlvalue += DVD[i].Value;
            }
     
    outText+=("Total Inventory Value is: $" + ttlvalue); 
        }//end calculate
    
    }//end InventoryProgram
    
    //create class
    class Product 
    {
        int Stock, UPC, Price,Value;
        String Name;
    
         
    public Product(String DVDName, int UPCNum, int StockQty, int Retail)
            {
            this.Name = DVDName;
            this.UPC = UPCNum;
            this.Stock = StockQty;
            this.Price = Retail;
            this.Value=Stock*Price;
                   
            }
         
    }//end product class
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    
    /**
     *
     * @author michelle
     */
    public class Genre extends Product implements Comparable<Genre>
    {
        public String genre;
        public int Fee;
        
         public Genre(String DVDName, int UPCNum, int StockQty, int Retail,String genre) 
        {
            super(DVDName, UPCNum, StockQty, Retail);
            this.genre=genre;
            this.Value=Retail*StockQty;
            this.Fee=(int) (Value*.05);        
        }   
    
        public void setgenre(String genre)
        {
            this.genre=genre;
        }
        public String getgenre()
        {
            return genre;
        }
        public int setValue(int StockQty, int Retail)
        {
            this.Value=StockQty*Retail;
            return StockQty*Retail;
        }
        public int getValue()
        {
            return Value;
        }
        public int setFee()
        {
           this.Fee= (int) (Value*.05);
            return (int) (Value*.05);
        }   
        public int getFee()
        {
            return Fee=(int) (Value*.05);
        }
         static void sortinv(Genre[] DVD) 
         {
              java.util.Arrays.sort(DVD);
         }
        @Override
         public int compareTo(Genre sortGenre) 
         {
              return this.genre.compareTo(sortGenre.genre);
         }
        @Override
        public String toString()
        {
            return String.format(" Genre:" + getgenre()+" Has a Restock Fee: "+getFee());
        }
    }
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    
    import javax.swing.*;
    import java.awt.FlowLayout;
    
    public class GUI extends JFrame
    {
    	
    	
    	JTextArea ta = new JTextArea(25,40);
    	
    	JScrollPane sp = new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    	
    	JPanel pane = new JPanel();
    	
    	public GUI() 
            {
    		super("DVD Inventory Program");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setBounds(100,100,500,500);
    		pane.add(sp);
    		add(pane);
    		setVisible(true);
    	}
    	
    }

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: CompareTo not working

    A couple of suggestions. When you initially created the Genre class and put them in the DVD array, you also set the fee in the constructor. So you can access the fee from the DVD array. Just iterate thru the array and tack the stuff onto outText.

    There's also an enhanced for-loop for arrays (and any class that implements the Iterable interface). This means that you could do this:

    Java Code:
    for (Genre g : DVD) {
         g.getgenre(); // etc
    }
    
    // instead of this:
    
    for (int i = 0; i < DVD.length; i++) {
        DVD[i].getgenre();  // etc
    }
    Regards,
    Jim
    Last edited by jim829; 07-18-2014 at 04:19 AM. Reason: clarified the for each syntax
    Mshelly likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Mshelly is offline Member
    Join Date
    Jul 2014
    Location
    New Jersey
    Posts
    21
    Rep Power
    0

    Default Re: CompareTo not working

    You are the BEST! Thank you so much for all your help. It works beautifully!!!

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    import java.util.Scanner;
    
    /**
     *
     * @author michelle
     */
    
    public class Inventoryprogram4
    {
     static GUI mainGUI = new GUI();
     static String outText = "";              
        /**
         * @param args the command line arguments
         */
    static int max = 5;
    static final Genre[] DVD = new Genre[max];
    
    //create array
    public static void main(String[] args)
    {    
        Scanner input = new Scanner(System.in);
        
        Genre g;
        
            DVD[0] = new Genre("Dawn of the Dead", 123456, 10, 10,"Horror");
            DVD[1] = new Genre("Walking Dead S1", 234567, 10, 20,"TV");
            DVD[2] = new Genre("Tinkerbell", 345678, 10, 12,"Kids");
            DVD[3] = new Genre("My Sisters Keeper", 456789, 10, 15,"Drama");
            DVD[4] = new Genre("Star Wars", 567890, 5, 30,"Scifi");
            Calculate(DVD);
            Genre.sortinv(DVD);
            print(DVD);
         
            
      mainGUI.ta.setText(outText);     
    }//end array
         //count and print
        static void print(Genre[] DVD)
        {      
            for (int i = 0; i < max; i++)
            {
    outText+=("\nName: " + DVD[i].Name + ", UPC: " + DVD[i].UPC + ", QTY: " + DVD[i].Stock 
            + "Price: $"+DVD[i].Price+" DVD Value: $" + DVD[i].Value +" Genre: " + DVD[i].genre);
            }
                for (Genre g : DVD) 
                {
                    g.getgenre();
                    g.getFee();
    outText+=("\n Genre: "+g.getgenre()+" Has a restock fee:$ "+g.getFee());
                }
           
        }//end print array
        //calculate the value of DVD stock
        static void Calculate(Genre[] DVD)
        {
            int ttlvalue = 0;     
            for (int i = 0; i < max; i++)
            {
            ttlvalue += DVD[i].Value;
            }
     
    outText+=("Total Inventory Value is: $" + ttlvalue); 
        }//end calculate
    
    }//end InventoryProgram
    
    //create class
    class Product 
    {
        double Stock, UPC, Price,Value;
        String Name;
    
         
    public Product(String DVDName, int UPCNum, int StockQty, int Retail)
            {
            this.Name = DVDName;
            this.UPC = UPCNum;
            this.Stock = StockQty;
            this.Price = Retail;
            this.Value=Stock*Price;
                   
            }
         
    }//end product class
    Java Code:
    package inventoryprogram4;
    
    /**
     *
     * @author michelle
     */
    public class Genre extends Product implements Comparable<Genre>
    {
        public String genre;
        public double Fee;
        
         public Genre(String DVDName, int UPCNum, int StockQty, int Retail,String genre) 
        {
            super(DVDName, UPCNum, StockQty, Retail);
            this.genre=genre;
            this.Value=Retail*StockQty;
            this.Fee=(double) (Value*.05);        
        }   
    
        public void setgenre(String genre)
        {
            this.genre=genre;
        }
        public String getgenre()
        {
            return genre;
        }
        public double setValue(int StockQty, int Retail)
        {
            this.Value=StockQty*Retail;
            return StockQty*Retail;
        }
        public int getValue()
        {
            return (int) Value;
        }
        public double setFee()
        {
           this.Fee= (double) (Value*.05);
            return (double) (Value*.05);
        }   
        public double getFee()
        {
            return Fee=(double) (Value*.05);
        }
         static void sortinv(Genre[] DVD) 
         {
              java.util.Arrays.sort(DVD);
         }
        @Override
         public int compareTo(Genre sortGenre) 
         {
              return this.genre.compareTo(sortGenre.genre);
         }
        @Override
        public String toString()
        {
            return String.format(" Genre:" + getgenre()+" Has a Restock Fee: "+getFee());
        }
    }
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package inventoryprogram4;
    
    import javax.swing.*;
    import java.awt.FlowLayout;
    
    public class GUI extends JFrame
    {
    	
    	
    	JTextArea ta = new JTextArea(25,40);
    	
    	JScrollPane sp = new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    	
    	JPanel pane = new JPanel();
    	
    	public GUI() 
            {
    		super("DVD Inventory Program");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setBounds(100,100,500,500);
    		pane.add(sp);
    		add(pane);
    		setVisible(true);
    	}
    	
    }
    My OUTPUT:
    Total Inventory Value is: $720
    Name: My Sisters Keeper, UPC: 456789.0, QTY: 10.0Price: $15.0 DVD Value: $150.0 Genre: Drama
    Name: Dawn of the Dead, UPC: 123456.0, QTY: 10.0Price: $10.0 DVD Value: $100.0 Genre: Horror
    Name: Tinkerbell, UPC: 345678.0, QTY: 10.0Price: $12.0 DVD Value: $120.0 Genre: Kids
    Name: Star Wars, UPC: 567890.0, QTY: 5.0Price: $30.0 DVD Value: $150.0 Genre: Scifi
    Name: Walking Dead S1, UPC: 234567.0, QTY: 10.0Price: $20.0 DVD Value: $200.0 Genre: TV
    Genre: Drama Has a restock fee:$ 7.5
    Genre: Horror Has a restock fee:$ 5.0
    Genre: Kids Has a restock fee:$ 6.0
    Genre: Scifi Has a restock fee:$ 7.5
    Genre: TV Has a restock fee:$ 10.0

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: CompareTo not working

    Glad it worked! You can delete these two statements from your loop:

    Java Code:
    g.getgenre();
    g.getFee();
    You not using the return value there. I showed that just as example of how you would reference
    the values.

    Regards,
    Jim
    Mshelly likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    Mshelly is offline Member
    Join Date
    Jul 2014
    Location
    New Jersey
    Posts
    21
    Rep Power
    0

Similar Threads

  1. compareTo method
    By Tabula Rasa in forum New To Java
    Replies: 4
    Last Post: 04-21-2011, 05:49 PM
  2. compareTo() problems...
    By falkon114 in forum New To Java
    Replies: 1
    Last Post: 01-28-2011, 10:08 PM
  3. using the compareTo method
    By anonymous445 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 04:53 AM
  4. need help wit this compareTo method
    By ShinTec in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 03:24 AM
  5. compareTo()
    By Tsiliadoros in forum Advanced Java
    Replies: 5
    Last Post: 10-03-2008, 02:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •