Using the data array, create instances of books and store them in a one-dimensional array of Book, called bookArray. Using the quantity array, walk through the book array and calculate the total charge per book sold. Print in a dialog box the book title, the book isbn, and the total charge for each book. Then, print the grand total of all books sold.
So far I have done this, but I'm having issue with the JOptionPane and formatting.
Error
$ javac BookTest.java
BookTest.java:41: error: incompatible types: String cannot be converted to String[][]
dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
^
1 error
And the code:
import javax.swing.JOptionPane;
public class BookTest {
public static void main(String args[]) {
String dataArray [][]=
{{ "Fiction", "Abraham Lincoln Vampire Hunter", "Grahame-Smith", "Wiley", "NY", "978-0446563079", "13.99", "222"},
{"Fiction", "Frankenstein", "Shelley", "Prescott", "GA", "978-0486282114", "7.99", "321"},
{"NonFiction", "Life of Kennedy", "Jones", "Pearson", "MT", "758-29304566", "12.90", "biography"},
{"Fiction", "Dracula", "Stoker", "Addison", "CA", "978-0486411095","5.99", "145"},
{"Fiction", "Curse of the Wolfman", "Hageman", "Wesley", "MA", "B00381AKHG", "10.59", "876"},
{"NonFiction", "How to Pass Java", "Willis", "Wiley"," NY", "444-395869790", "1.99", "technology"},
{"Fiction", "The Mummy", "Rice", "Addision", "CA", "978-0345369949", "7.99", "954"},
{"NonFiction", "History of Texas", "Smith", "Prescott", "CA", "123-683947687", "9.75", "history"}
};
Book bookArray[] = new Book [8];
for (int i = 0; i < bookArray.length; i++) {
if (dataArray[i][0].equals("Fiction")) {
Publisher p = new Publisher(dataArray[i][3], dataArray[i][4]);
bookArray[i] = new Fiction(dataArray[i][1], dataArray[i][2], dataArray[i][5],
p, Double.parseDouble(dataArray[i][6]), dataArray[i][7]);
} else {
Publisher p = new Publisher(dataArray[i][3], dataArray[i][4]);
bookArray[i] = new NonFiction(dataArray[i][1], dataArray[i][2], dataArray[i][5],
p, Double.parseDouble(dataArray[i][6]), dataArray[i][7]);
}
}
for (Book b:bookArray) {
dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
JOptionPane.showMessageDialog(null, dataArray);
}
}
}
Don't use your dataArray to output things in the message box. Use a String variable.
for (Book b : bookArray) {
String bookOutput = String.format(" %s, %s ", b.getTitle(), b.getIsbn());
JOptionPane.showMessageDialog(null, bookOutput);
}
Even better, add a toString() to your Book class that returns String.format(" %s, %s ", getTitle(), getIsbn()) and then do your loop like this:
for (Book b : bookArray) {
JOptionPane.showMessageDialog(null, b.toString());
}
However, if you want to output the price for each book and then the total, your loop will look more like this:
double totalPrice = 0.0;
for (int i = 0; i < bookArray.length; i++) { //can't use foreach because of use of parallel arrays bookArray & quantityArray
Book b = bookArray[i];
double price = b.getPrice() * quantityArray[i];
totalPrice += price;
String bookStr = String.format(" %s, %s %s", b.getTitle(), b.getIsbn(),
DecimalFormat.getCurrencyInstance().format(price));
JOptionPane.showMessageDialog(null, bookStr);
}
String totalStr = String.format("Total Charge: %s",
DecimalFormat.getCurrencyInstance().format(totalPrice)));
JOptionPane.showMessageDialog(null, totalStr);
String.format returns a String as per the docs. You are trying to concatenate a String to a 2d array.
dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
In the above line you probably want to access a location from the 2d array, like dataArray[0][0] and then do the concatenation. Something like:
dataArray[0][0] +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
Related
Relatively new to Java, looking for a solution to a crucial part of a game I'm making for a class. The idea is to make a very simple stock market simulation game, but the problem is related to creating made-up company names. I have three arrays for the first, middle, and last names of companies. I'm trying to make some of these companies have one word names, others two, etc. So far I've used a random number generator and if/elif/else statement to simulate one, but I would like five of them and I would prefer a more efficient method of doing so. Here's the code:
import java.util.Random;
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) {
// The code all goes in here
String[] companyFirstNames = {"Alpine", "Bear", "Bull", "Cuckoo", "Delta", "Dragon", "Echo", "Fighter", "Giant", "H20", "Indo", "Jared", "Jason", "Kicker", "Lodge", "Little", "Manzo", "Mint", "Neighbour", "Nelson", "Ossuary", "Open", "Private", "Poor", "Quick", "Quiant", "Reach", "Rebel", "Space", "Spear", "Titus", "Trebble", "Underdog", "Upper", "Vital", "Vert", "White", "Whistle", "X's", "Yuri's", "Yogurt", "Zebra"};
String[] companySecondNames = {" Science", " Technology", " Arts", " Research", " Laboratory", " Lodging", " Woodworking", " Fashion", " Oil", " Trading", " Investing"};
String[] companyLastNames = {" Limited", " Co.", " Corp.", " Corporation", " Ltd", " Institute", " Association", " Federation", " Firm"};
//Three arrays of random company name "pieces"
Random randomNamer = new Random();
//Used for getting random names & ints
int randomOne = randomNamer.nextInt(companyFirstNames.length);
int randomTwo = randomNamer.nextInt(companySecondNames.length);
int randomThree = randomNamer.nextInt(companyLastNames.length);
//Three ints that are given random values associated to the arrays
int numberOne = randomNamer.nextInt(100);
//Getting a random 0-100 number
String bigNameCompany = companyFirstNames[randomOne] + companySecondNames[randomTwo] + companyLastNames[randomThree];
String midNameCompany = companyFirstNames[randomOne] + companyLastNames[randomThree];
String smallNameCompany = companyFirstNames[randomOne];
//The three types of company names possible to produce
String companyOne;
String companyTwo;
String companyThree;
String companyFour;
String companyFive;
//The five companies I want to name
if (numberOne <= 45) {
companyOne = bigNameCompany;
//The first company name is random and big
} else if (numberOne <= 85) {
companyOne = midNameCompany;
//Two word name
} else {
companyOne = smallNameCompany;
//One word name
}
System.out.println(companyOne);
//printing the name of the first company
//Can i get a loop to do this more efficiently for 5 companies?
}
}
I'd encourage you to play around with it. That's the fun of programming is solving little puzzles like this, and there are a bunch of ways to do something like this. Here is one to give you some ideas:
Random randomNamer = new Random();
String[] companyFirstNames = { "Alpine", "Bear", "Bull", "Cuckoo", "Delta", "Dragon", "Echo", "Fighter", "Giant", "H20", "Indo", "Jared", "Jason", "Kicker", "Lodge", "Little", "Manzo", "Mint", "Neighbour", "Nelson", "Ossuary", "Open", "Private", "Poor", "Quick", "Quiant", "Reach", "Rebel", "Space", "Spear", "Titus", "Trebble", "Underdog", "Upper", "Vital", "Vert", "White", "Whistle", "X's", "Yuri's", "Yogurt", "Zebra" };
String[] companySecondNames = { " Science", " Technology", " Arts", " Research", " Laboratory", " Lodging", " Woodworking", " Fashion", " Oil", " Trading", " Investing" };
String[] companyLastNames = { " Limited", " Co.", " Corp.", " Corporation", " Ltd", " Institute", " Association", " Federation", " Firm" };
for (int i = 0; i < 5; i++) {
int chance = randomNamer.nextInt(100);
int firstIdx = randomNamer.nextInt(companyFirstNames.length);
int secondIdx = randomNamer.nextInt(companySecondNames.length);
int lastIdx = randomNamer.nextInt(companyLastNames.length);
String name = null;
if (chance <= 45) {
name = companyFirstNames[firstIdx] + companySecondNames[secondIdx] + companyLastNames[lastIdx];
} else if (chance <= 85) {
name = companyFirstNames[firstIdx] + companyLastNames[lastIdx];
} else {
name = companyFirstNames[firstIdx];
}
System.out.println(name);
}
You can create a simple method for that (and you don't need white spaces in your name Strings):
private final String[] companyFirstNames = {"Alpine", "Bear", "Bull", "Cuckoo", "Delta", "Dragon", "Echo", "Fighter", "Giant", "H20", "Indo", "Jared", "Jason", "Kicker", "Lodge", "Little", "Manzo", "Mint", "Neighbour", "Nelson", "Ossuary", "Open", "Private", "Poor", "Quick", "Quiant", "Reach", "Rebel", "Space", "Spear", "Titus", "Trebble", "Underdog", "Upper", "Vital", "Vert", "White", "Whistle", "X's", "Yuri's", "Yogurt", "Zebra"};
private final String[] companySecondNames = {"Science", "Technology", "Arts", "Research", "Laboratory", "Lodging", "Woodworking", "Fashion", "Oil", "Trading", "Investing"};
private final String[] companyLastNames = {"Limited", "Co.", "Corp.", "Corporation", "Ltd", "Institute", "Association", "Federation", "Firm"};
private final Random randomNamer = new Random();
private String generateRandomName() {
StringBuilder sb = new StringBuilder();
int size = random.nextInt(100);
int first = random.nextInt(companyFirstNames.length);
sb.append(companyFirstNames[first]);
if (size <= 85) {
int second = random.nextInt(companySecondNames.length);
sb.append(' ').append(companySecondNames[second]);
}
if (size <= 45) {
int last = random.nextInt(companyLastNames.length);
sb.append(' ').append(companyLastNames[last]);
}
return sb.toString();
}
Then you can do:
private List<String> generateNames(int numberOfNames) {
return IntStream.range(0, numberOfNames).mapToObj(i ->
generateRandomName()).collect(Collectors.toList());
}
Calling the last method will allow you to create as many names as you want:
List<String> names = generateNames(5)
System.out.println(names);
Output:
[White Trading Firm, Open Research, Indo Oil, Fighter Technology, Cuckoo Oil Corporation]
Your solution would probably work, but you could reuse your Random to decide whether a company name should have one, two or three words to make it more compact. This example would always generate a company with at least one name, while there's an equal probability for second and third:
final Random rnd = new Random();
final String[] companies = new String[5];
for(int i = 0; i < companies.length; i++) {
final int idx = rnd.nextInt(100);
companies[i] = companyFirstNames[idx%companyFirstNames.length]
+ (rnd.nextBoolean() ? companySecondNames[idx%companySecondNames.length] : "")
+ (rnd.nextBoolean() ? companyLastNames[idx%companyLastNames.length] : "");
}
You can use something like rnd.nextInt(2) == 0 or rnd.nextInt(3) == 0 to play with the odds of a longer company name instead of nextBoolean().
Edit
Something that may be worth thinking about is uniqueness - is it a problem if you happen to generate the same company name more than once? Depending on your strategy you could keep track of either the number or name you generate (probably in a Set), and regenerate if it already exists.
I am tasked with creating 5 book objects and storing in an array, which I do no problem! I am able to print out the objects' variables using println no problem but when I want be able to move the variable around using printf I dont know how to call upon a specific variable of a certain element of the array. Please Help!
public class BookReport {
public static void main(String[] args){
Book[] TheBooks = new Book[5] ;
TheBooks[0] = new Book("Gone With The Wild", "Paul Mitchell", 1000) ;
TheBooks[1] = new Book("Harry Toilet", "Donald Trump", 100) ;
TheBooks[2] = new Book("Huckles Finn Berry", "SpiderMan", 500) ;
TheBooks[3] = new Book("The Bad Habbit", "Nose Picker", 700) ;
TheBooks[4] = new Book("Alien", "Mister Green", 600) ;
System.out.printf("%10s %20s %18s \n", "Book Title" , "Author", "Pages") ;
System.out.printf("%s \n", "----------------------------------------------------") ;
//This works but I cant justify the variables to align!!!
System.out.println(TheBooks[0]) ;
}
}
public class Book {
private String title = "" ;
private String author = "" ;
private int pages = 0 ;
public Book(String title, String author, int pages){
setTitle(title) ;
setAuthor(author) ;
setPages(pages) ;
}
public String getTitle(){
return title ;
}
public String getAuthor(){
return author ;
}
public int getPages(){
return pages ;
}
public void setTitle(String newTitle){
title = newTitle ;
}
public void setAuthor(String newAuthor){
author = newAuthor ;
}
public void setPages(int newPages){
pages = newPages ;
}
public String toString(){
return title + " " + author + " " + pages ;
}
public boolean equals(Book anotherBook){
return ((title.equals(anotherBook.title)) && (author.equals (anotherBook.author)) &&
(pages == anotherBook.pages)) ;
}
}
Just call the getters for the various fields you want to print:
System.out.printf("%10s %20s %18s \n", "Book Title" , "Author", "Pages");
System.out.printf("%s \n", "----------------------------------------------------") ;
for (Book b : TheBooks) {
System.out.printf("%10s %20s %18s \n", b.getTitle(), b.getAuthor(), b.getPages());
}
By the way, you named your array of books TheBooks, but it is Java convention to not name variables with capital letters as the first letter. So theBooks would work better here.
If you want to call specific variable of a certain element of the array.
you can use
java.util.Map
for example you can make Id for all object you want to store.
HashMap<String, Book> h = new HashMap<>();
Book book = new Book("Gone With The Wild", "Paul Mitchell", 1000);
h.put(book.getTitle(), book);
book = new Book("Harry Toilet", "Donald Trump", 100);
h.put(book.getTitle(), book);
book = new Book("Huckles Finn Berry", "SpiderMan", 500);
h.put(book.getTitle(), book);
book = new Book("The Bad Habbit", "Nose Picker", 700);
h.put(book.getTitle(), book);
book = new Book("Alien", "Mister Green", 600);
h.put(book.getTitle(), book);
// if you want to call it
Book b = h.get("exampletitle);
You should also be able to use String.format() in your toString method so that you have a reusable formatted version.
#Override
public String toString(){
return String.format("%10s %20s %18s \n", title, author, pages);
}
P.s. I think it would be good practice to include hashCode() for classes where you include an equals().
I'm having a little trouble with a fairly simple assignment, but couldn't find an answer here that seemed to work.
I need to have a 2D ArrayList that contains staff data, and then be able to run a separate function that allows the user to input a staff member's name, searches the ArrayList for that name, and if it exists, display the full row of data.
Here's what I've got so far:
The ArrayList
List<List<String>> staffArrayString = new ArrayList<>();
staffArrayString.add(Arrays.asList("Steven George", "12 York Road", "07123456678", "Permanent", "York", "27000/yr"));
staffArrayString.add(Arrays.asList("Rina Veyer", "20 Leeds Road", "08987987765", "Part Time", "Leeds", "10/hr"));
staffArrayString.add(Arrays.asList("Brian Lym", "13 Bradford Road", "07123234345", "Permanent", "Bradford", "27000/yr"));
The search function
public void staffNameSearch() {
System.out.println("Enter name of staff member:");
String staffName = in.next();
boolean found = false;
int row = 0;
for (int i = 0; i < staffArrayString.size(); i++) {
for (int j = 0; j < staffArrayString.get(i).size(); j++) {
if (staffArrayString.get(i).get(j).equals(staffName)) {
row = staffArrayString.get(i).size();
found = true;
}
}
}
if (found = true) {
System.out.print(staffArrayString.get(row) + " ");
}
}
I'm currently getting an output of 'Exception in thread "main" java.lang.IndexOutOfBoundsException' at the print line on the end there, but I can't for the life of me work out why. I'd appreciate any advice on this (especially if it's some obvious and stupid mistake on my part!).
The error is occuring because you are setting row to something unrelated to the row counter. When you discover the row (variable i) which has the name in the jth element, set row=i.
Be careful about if (found = true) - it is incorrect; prefer:
a) if (found)
b) if (found == true)
For efficiency, include && !found in the for loops so they exit as soon as you find something.
You can use for each loop for simpler.
System.out.println("Enter name of staff member:");
String staffName = in.next();
boolean found = false;
String[] foundArray;
for(String[] staffArray: staffArrayString){
for(String str : staffArray){
if(str.equals(staffName)){
foundArray = staffArray;
found = true;
break;
}
}
}
if (found == true) {
System.out.print(foundArray + " ");
}
You might be able to simplify the code a little bit by using a Map and a Staff class.
For example, the Staff class
public class Staff{
String name;
String address;
String id; // ?
boolean permanent; // Or a enum if there are more than 2 values
String city; // ?
int payrate;
boolean hourly;
#Override
public String toString(){ // Easily convert the class to a String
return String.format("%s %s %s %s %s $%d/%s",
name,
address,
id,
permanent ? "Permanent" : "Part-time",
city,
payrate,
hourly ? "hr" : "yr");
}
}
And for the code to read it
private Map<String, Staff> staffDirectory; // String is the name in lowercase
public void staffNameSearch() {
// Code
if(staffDirectory.containsKey(staffName)){ // Make sure staffName is lowercase
System.out.print(staffDirectory.get(staffName) + " ");
} else {
System.out.println("Name not found");
}
}
This way you can avoid using loops and get O(1) efficiency.
Hi I am having some problems with my array of objects. If I assign the length of the array (of objects) to 4 it does not allow me to choose option 3 (program keeps asking for the option when I choose 3. otherwise all options works fine), if I assign it to 5 it allows me but I get null pointer exception (because the array will have an extra object with no value assign to it - null). If I print out the array just after I called the constructor. It works fine
public static void main(String[] args)
{
int menuOption = 0;
int size = 4;
BookStore[] book = new BookStore[size];
//populate the object book for testing purpose
book[0] = new BookStore("Now", "Me", "Aussie", 2014, 123456, 25.50, 2);
book[1] = new BookStore("Today", "You", "UK", 1992, 023456, 20.50, 5);
book[2] = new BookStore("Current", "Me", "Aussie", 2005, 234567, 25.00, 4);
book[3] = new BookStore("Tomorrow", "You", "UK", 1909, 523456, 20.00, 15);
//print out the array here, changing the index one by one, I wont have any error
//E.g. book[0].displayDetails();
while(menuOption !=1)
{
displayMenu(); //display a plain menu
menuOption = readOption(); //read option from users keyboard
switch(menuOption)
{
..........
case 3:
for(BookStore b: book)
{
b.displayDetails();
//if i assign the length of array to 4 does not allow to choose the option 3
//if i assign the length of array to 5 null point exception
}
break;
........
}
..............
}
}
public class BookStore
{
private String title;
private String author;
private String publisher;
private int year;
private long isbn;
private double price;
private int quantity;
public BookStore(String bTitle, String bAuthor, String bPublisher, int bYear,
long bIsbn, double bPrice, int bQty)
{
title = bTitle;
author = bAuthor;
publisher = bPublisher;
year = bYear;
isbn = bIsbn;
price = bPrice;
quantity = bQty;
}
public void displayDetails()
{
System.out.println("Title: "+title);
System.out.println("Author: "+author);
System.out.println("Publisher: "+publisher);
System.out.println("Year: "+year);
System.out.println("Isbn: "+isbn);
System.out.println("Price: "+price);
System.out.println("Quantity: "+quantity);
System.out.println("Total Value: "+getTotalValue());
System.out.println("");
}
public double getTotalValue()
{
return (double)quantity * price;
}
.........
}
I suggest you modify BookStore, change your displayDetails() and Override toString() -
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Title: "+title);
sb.append("\nAuthor: "+author);
sb.append("\nPublisher: "+publisher);
sb.append("\nYear: "+year);
sb.append("\nIsbn: "+isbn);
sb.append("\nPrice: "+price);
sb.append("\nQuantity: "+quantity);
sb.append("\nTotal Value: "+getTotalValue());
return sb.toString();
}
Then you can use
System.out.println(Arrays.toString(book));
or just
for (Book b : book) {
System.out.println(b); // <-- calls toString()
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
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;
}
}
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.)