问题
I created this package, and the problem is when is runs it repeats the same catalog entries between bins. I think the problem is in the detailedInventory method. SHOULD BE: It should create a new random bin item (type, title, artist) for Bin B, not use the same element from Bin A. Other than this, the output is correct. SKU numbers are correctly generated and displayed.
**The values of i, when they correspond to the catalog, are objects. they are actually different types of physical media, such as DVDs or cassettes, with title, artist and SKU attributes.
***Although it does not seem to make sense, the data should be generated randomly. (This class is part of a larger concept/lesson to illustrate inheritance.)
Any help would be greatly appreciated!
Here is the output I see:
Bin A: DVD - Greatest Hits Volume 2 (Limp Bizkit), SKU 1234-0: 500 Cassette - The Best Of (Michael Jackson), SKU 1234-1: 25 DVD - Love Songs (Michael Jackson), SKU 1234-2: 7720
Bin B: DVD - Greatest Hits Volume 2 (Limp Bizkit), SKU 1234-3: 1000
The bin item under Bin B should not be the same as A.
public class testMusicMedia
{
public static ArrayList<MusicMedia> MakeMusicCatalog( int size )
{
String[] titles =
{
"Greatest Hits Volume 1", "Greatest Hits Volume 2",
"The Best Of", "Love Songs",
"The Early Years", "The Later Years"
};
String[] artists =
{
"Michael Jackson", "Eminem",
"The Beatles", "Shania Twain",
"Limp Bizkit"
};
ArrayList<MusicMedia> catalog = new ArrayList<MusicMedia>();
Random rn = new Random();
for ( int i = 0 ; i < size ; i++ )
{
MusicMedia m;
int mediatype = rn.nextInt( 3 );
String title = titles[ rn.nextInt( titles.length ) ];
String artist = artists[ rn.nextInt( artists.length ) ];
String sku = "1234-" + i;
if ( mediatype == 0 )
m = new CompactDisk( title, artist, sku );
else if ( mediatype == 1 )
m = new DigitalVideoDisk( title, artist, sku );
else
m = new CassetteTape( title, artist, sku );
catalog.add( m );
}
return catalog;
}
public static String lookupMedia( ArrayList<MusicMedia> catalog,
String sku )
{
for ( MusicMedia m : catalog )
{
if ( m.getSKU().equals( sku ))
return "SKU is in catalog";
}
return "SKU not in catalog";
}
public static String detailedInventory( ArrayList<MusicMedia> catalog, ArrayList<Bin> warehouse )
{
String s = "";
for ( Bin bn : warehouse ){
s += "Bin " + bn.getName() + ":\n";
for (int i = 0; i < bn.getContents().size(); i++){
s += catalog.get(i) + ", " + bn.getContents().get(i) + "\n";
}
}
s += "\n";
return s;
}
public static void main( String[] args )
{
ArrayList<MusicMedia> catalog = MakeMusicCatalog( 10 );
ArrayList<Bin> warehouse = new ArrayList<Bin>();
Bin a = new Bin( "A" );
Bin b = new Bin( "B" );
warehouse.add( a );
warehouse.add( b );
a.add( new BinItem( "1234-0", 500 ) );
a.add( new BinItem( "1234-1", 25 ) );
a.add( new BinItem( "1234-2", 7720 ) );
b.add( new BinItem( "1234-3", 1000 ) );
System.out.println( detailedInventory( catalog, warehouse ) );
}
}
I call the collection of all the items in a bin that share the same SKU a “bin item”. Each object of the Bin class (not shown) represents a bin in a pretend warehouse. A Bin object has two instance variables: a String that contains the bin name and an ArrayList that contains a BinItem for each of the SKUs stored in the bin. The toString method of the Bin class uses the toString method of the BinItem class. It generates lists that only involve SKUs and quantities.
This method should use a for-each loop to cycle thorugh all the bins in the warehouse. For each bin, s should be extended by the String "Bin " followed by the name of the bin, followed by a colon and \n to start new line. Then it should cycle through all the bin items in the current bin, for each one extending s by a new line of text that begins with the result of looking up the current bin item’s SKU in the input catalog, and continue with a comma followed by the String representation of the bin item.
The output should look something like this:
Bin A: CD - The Later Years (Shania Twain), SKU 1234-0: 500 Cassette - Greatest Hits Volume 2 (The Beatles), SKU 1234-1: 25 Cassette - Greatest Hits Volume 1 (Shania Twain), SKU 1234-2: 7720
Bin B: Cassette - Greatest Hits Volume 2 (Michael Jackson), SKU 1234-3: 1000
Full code:
public class MusicMedia
{
private String myTitle,
myArtist,
mySKU;
public MusicMedia( String title, String artist, String sku )
{
myTitle = title;
myArtist = artist;
mySKU = sku;
}
public String getTitle()
{
return myTitle;
}
public String getArtist()
{
return myArtist;
}
public String getMediaType()
{
return "Unknown";
}
public String getSKU()
{
return mySKU;
}
public String toString()
{
return " - " + getTitle() + " (" + getArtist() + ")";
}
}
public class Disk extends MusicMedia
{
/**
* Constructor for objects of class Disk
*/
public Disk( String title, String artist, String sku )
{
super(title, artist, sku);
}
public String getMediaType()
{
return "Disk";
}
public String toString()
{
return getMediaType() + super.toString();
}
}
I also have an identical CassetteTape class that also extends MusicMedia. Also two other subclasses of Disk, called CompactDisk and DigitalVideoDisk. These two are also almost identical to each other, so I have pasted the DVD class below.
public class DigitalVideoDisk extends Disk
{
/**
* Constructor for objects of class DigitalVideoDisk
*/
public DigitalVideoDisk( String title, String artist, String sku )
{
super(title, artist, sku);
}
public String getMediaType()
{
return "DVD";
}
}
public class BinItem
{
private String mySKU;
private int myQuantity;
public BinItem( String sku, int quantity )
{
mySKU = sku;
myQuantity = quantity;
}
public String getSKU()
{
return mySKU;
}
public int getQuantity()
{
return myQuantity;
}
public String toString()
{
return "SKU " + getSKU() + ": " + getQuantity();
}
}
public class Bin
{
private String myName; //bin name
private ArrayList<BinItem> myContents; //contains a binItem
public Bin( String name )
{
myName = name;
myContents = new ArrayList<BinItem>();
}
public String getName()
{
return myName;
}
public ArrayList<BinItem> getContents()
{
return myContents;
}
public void add( BinItem b )
{
myContents.add( b );
}
public String toString()
{
String s = "Bin " + myName + ":\n";
for ( BinItem b : myContents )
s += b + "\n";
return s;
}
}
回答1:
Ok, per your edit:
...the result of looking up the current bin item’s SKU in the input catalog...
You aren't doing this right now and it was the key point that we needed to understand what the program was supposed to do. Right now you're just retrieving an element from catalog
using i
, basically arbitrarily.
So the first thing you need to do is create a helper method that searches catalog
for a MusicMedia object of a certain SKU. You have a method very much like it called lookupMedia
so I've just modified it to this slightly different specification. This returns m
instead of a String value:
public static MusicMedia getMediaBySKU(
ArrayList<MusicMedia> catalog, String sku
) {
for ( MusicMedia m : catalog ) {
if ( m.getSKU().equals(sku) )
return m;
}
return null;
}
Now that you are able to retrieve an item based on the SKU you can modify the detailedInventory
loop to use it:
for ( Bin bn : warehouse ){
s += "Bin " + bn.getName() + ":\n";
for ( BinItem item : bn.getContents() ){
MusicMedia mm = getMediaBySKU(catalog, item.getSKU());
s += mm + ", " + item + "\n";
}
}
(Not sure how to get the SKU from the BinItem so I guessed but I hope you get the idea. If you don't have a method that returns the SKU already, you probably need to make one.)
来源:https://stackoverflow.com/questions/22277928/java-repeated-output-from-nested-for-loop