I'm getting a bad operand types for operator <. first String. second string.
I get want the error means, so I'm assuming that I can't use this operator to sort my array of objects. Is there some other way for me to sort this array out. I need to sort the array by the Artist name.
Here's my new code
line 86 is the error
I get want the error means, so I'm assuming that I can't use this operator to sort my array of objects. Is there some other way for me to sort this array out. I need to sort the array by the Artist name.
Here's my new code
Code:
public class Product { //DECLARE VARIABLES private String number; //Item Number of product( used String as item // numbers sometimes contain alphabet characters) private String name; //Name of the product private int units; //How many of the product are in stock private double price; //How much one item costs private double value; //How much the stock is worth //CONSTRUCTOR public Product( String productName, String itemNumber, int instockUnits, double costPerUnit, double costOfStock ) { name = productName; number = itemNumber; units = instockUnits; price = costPerUnit; value = 0.00; } //METHODS OF THE CLASS //Set the item number public void setNumber( String itemNumber ) { number = itemNumber; } //End set item number //Get item number public String getNumber() { return number; } //End get item number //Set name public void setName( String productName ) { name = productName; } //End set name //Get name public String getName() { return name; } //End get name //set units public void setUnits( int instockUnits ) { units = instockUnits; } //end set units //Get units public int getUnits() { return units; } //End get units //Set price public void setPrice( double costPerUnit ) { price = costPerUnit; } //End set price //Get price public double getPrice() { return price; } //End get price //Set Value public double value( double price, int units ) { return value = price * units; } //end set value //String output of Product public String toString() { return "Product " + number + " Type " + name + ". Cost..." + price + ". Units..." + units + ". Value..." + (price * units); } }
Code:
public class CD extends Product { //Declare variable not inherited private String artist; //name of band or artist private final double restockFee = .05; //5% restocking fee //Constructor public CD( String productName, String itemNumber, int instockUnits, double costPerUnit, double costOfStock, String artistName ) { super( productName, itemNumber, instockUnits, costPerUnit, costOfStock ); artist = artistName; } //METHODS NOT INHERITED //Set artist name public void setArtist( String artistName ) { artist = artistName; } //End set artist name //Get artist name public String getArtist() { return artist; } //End get artist name //calculate new price with restocking fee public double price() { return ( super.getPrice() + ( super.getPrice() * restockFee ) ); } //Set Value public double value() { return ( price() * super.getUnits() ); } //end set value //String output of Product public String toString() { return String.format( "%s %s %s\n%s...%.2f\n\n", "Artist", getArtist(), super.toString(), "Value with Restocking Fee...", ( price() * super.getUnits() ) ); } }
Code:
import java.util.Scanner; //allows user input import java.util.Arrays; //allows us to add to the array import java.util.Comparator; public class InventoryDisplay { //Main Method public static void main( String args[] ) { //Create a product CD myCD = new CD( "0000", "Type", 0, 0.00, 0.00, "Band" ); //Create a Scanner Scanner input = new Scanner( System.in ); double total = 0.00; //Greeting System.out.print( "Welcome to Inventory Control\n" ); System.out.print( "This program displays inventory stock and value\n\n" ); //Get the length of the array System.out.printf( "%s%s", "Before we begin,", "how many CDs do you want to inventory? " ); int count = input.nextInt(); System.out.printf( "%s%s", "Thank you.", " Please fill in CD information.\n\n" ); //Create an array CD inventory[] = new CD[ count ]; //Using inventory.length as a counter, loop input and output for ( int counter = 0; counter < inventory.length; counter++ ) { String theName = "CD"; //Product Type is already determined as CD, so no input required myCD.setName( theName ); System.out.print( "What is the product number of the CD? "); String theNumber = input.next(); myCD.setNumber( theNumber ); System.out.println(); System.out.print( "How many of this CD is in stock? "); int theUnits = input.nextInt(); myCD.setUnits( theUnits ); System.out.println(); System.out.print( "How much does this CD cost? "); double thePrice = input.nextDouble(); myCD.setPrice( thePrice ); System.out.println(); System.out.print( "Who is the artist of the CD? "); String theArtist = input.next(); myCD.setArtist( theArtist ); System.out.println(); //Fills the array with the user input inventory[ counter ] = new CD( theName, theNumber, theUnits, thePrice, myCD.value( myCD.getPrice(), myCD.getUnits() ), theArtist ); //Totals the value of all the items /*This snippet of code taken Dream.In.Code http://www.dreamincode.net/forums/topic/251302-loop-not-incrementing-my-variable/page__p__1460738__fromsearch__1&#entry1460738 ChrisKellyDev Retrieved October 15th 2011*/ total += inventory[counter].value(); } System.out.println( "The restocking fee is 5%\n" ); for ( int a = 0; a <= inventory.length; a++ ) { for ( int b = 1; b <= inventory.length; b++ ) { if ( inventory[a].getArtist() < inventory[b].getArtist() ) { CD swapCD = new CD( "0000", "Type", 0, 0.00, 0.00, "Band" ); swapCD = inventory[a]; inventory[a] = inventory[b]; inventory[b] = swapCD; } } } System.out.println(Arrays.toString( inventory ) ); System.out.printf( "The total value of the inventory is %.2f\n\n", total ); //Closing System.out.print( "Thank you for using Inventory Control" ); } }
Comment