the brief is to be able to assign a piece of an Artist from the available Artist array list. if the Artist doesnt exist, then you add the Artist to the array list then link it to the piece of Art, then print all pieces of art which belong to that Artist.

i know how to add to the art array pieces of art, same with artist

My teacher gave me these notes to help but im sturggling with the coding:
in Artist:
add Artist
in Inventory:
add new Art
read the title
read artist name
Artist who = search(artistname) -- not a clue how to implement this and beyond really
if(who ==null
add in who=new Artist
Art a = (art variables, who)

/**
* save() method writes data back to file
* this will write all the to strings for each piece in collection
* to the specified text file called from UserMenu in Menu class
* try catch block prevents bombing, as expecption is caught and handled
* @param fileName file to write to called in UserMenu
*/
public void save(String fileName)
{

/*
* adapted from Lynda's Contact code, made it handle my arrays
* and my art toString
* added if, to ask user to see if they want to save changes
* This allows for mistakes to not be added, and wont demage the laod method
* if a file has a blank field
*/
String save; //use with scanner

if(save.equals("yes")){
System.out.println("**method save writes data back to a file "+fileName);
try {
PrintWriter outfile = new PrintWriter(new OutputStreamWriter
(new FileOutputStream(fileName)));
outfile.println(collection.size());
for (Art a:collection) {
/*
* reason i have adapted this code as follows:
* if i chose to to a.tostring it would cause my load method to bomb
* This way saving each variable to a new line allows
* my load method to read the same file allowing me to save/load
* *
*/
outfile.println(a.getArtist()); //stores only this variable
outfile.println(a.getTitle());
outfile.println(a.getValue());
outfile.println(a.getFileName());
//can also use any other art method such as getValue
}
outfile.close();
}
catch (IOException e) {

/*
* adapted from Lynda's Contact code, made it handle my arrays
* and my artists toString
* added if, to ask user to see if they want to save changes
* This allows for mistakes to not be added, and wont demage the laod method
* if a file has a blank field
*/
String save; //use with scanner

System.out.println("Save changes to arists?");
save=scan.nextLine();

if(save.equals("yes")){
System.out.println("**method save writes data back to a file "+fileName);
try {
PrintWriter outfile = new PrintWriter(new OutputStreamWriter
(new FileOutputStream(fileName)));
outfile.println(artists.size());
for (Artist b:artists) {
/*
* reason i have adapted this code as follows:
* if i chose to to a.tostring it would cause my load method to bomb
* This way saving each variable to a new line allows
* my load method to read the same file allowing me to save/load
* *
*/
outfile.println(b.getName()); //stores only this variable
outfile.println(b.getAddress());
outfile.println(b.getEmail());
outfile.println(b.getTelephoneNumber());
//can also use any other art method such as getValue
}
outfile.close();
}
catch (IOException e) {

String n=infile.nextLine(); //check for next string in the file and it becomes name
String ad=infile.nextLine(); //check for next string in file it becomes owner
String em=infile.nextLine();
String tel=infile.nextLine();
Artist ar=new Artist(n, ad, em, tel); //created new art piece using these variables from file
artists.add(ar); //adds the new piece(a) based off file read and loaded
}

public void totalValue()
{
/*
*Below method taken from useful collections worksheet
*i have ammended the code to allow for use within my project
* added total as my own variable
* allowed it to serach my collections list
*
*/

private void averageValue()
{
/*
* set method to private so it cant be called before total value is calculated
* will be called after totalValue has been called so it has data to
* calculate and prevent errors.
*
* Colelctions.sort lists the elements in order of value prior to printing
*/
double averageValue=total/collection.size();
if(collection.size() !=0){ //sorts array list in price order
System.out.println("The average Value of the collection is £"+averageValue);
return;}
else{
//prevents calculations being done on an 'empty' arrayList<Art> collection
System.out.println("Collection contains 0 pieces, average is therefore £0");
return;
}
}

/**
* Enables a user to delete Search the collection for an Art piece.
* Searches the ArrayList<Art> collection: for "title"
* if it find this it will display the toString(a),
* return jumps it out the method so after it finds the piece so method is stopped
* @param title
*/
public void searchForPiece(String title) {
/*
* The below code has been adapted from my own worksheet 5/6 in CS122.
* the way in which i created this method was to use information provided by Lynda on searching arrays
* then created an equals and something to return accordingly
* *
*search for Art and print toString
*/
for (Art a:collection) {
if (title.equals(a.getTitle())) {
System.out.println(a.toString());

public void searchForArtist(String name) {
/*
* The below code has been adapted from my own worksheet 5/6 in CS122.
* the way in which i created this method was to use information provided by Lynda on searching arrays
* then created an equals and something to return accordingly
* *
*search for Art and print toString
*/
for (Artist b:artists) {
if (name.equals(b.getName())) {
System.out.println(b.toString());
return; //jump out of method
}
}
System.out.println("Piece not in collection, please try again");
}

/**
* Enables a user to delete Search the collection for an Art
* piece also prints recipt and removes piece if sold
* Searches the ArrayList<Art> collection: for "title"
* if it find this it will display the toString(a),
* return jumps it out the method so after it finds the piece so method is stopped
* @param title
*/
public void SellPieceAndRecipt(String title) {
/*
* The below code has been adapted from my own worksheet 5/6 in CS122.
* the way in which i created this method was to use information provided by Lynda on searching arrays
* then created an equals and something to return accordingly
* *
*search for Art and remove from array list whilst printing a "recipt"
*/
for (Art a:collection) {
if (title.equals(a.getTitle())) { //title = search param "String title"
/*
* creating the recipt, adding variables as needed some methods acces each other
* also since selling an Art piece removes it from array i used a scanner
* to confirm sale (removal) to avoid accidental deleted
* recipt will be printed before this is asked
* recipt all created by me using standard println and
* creating suitable variables and simple math for calcs.
* also if the piece is sold it will display a running total for this 'session'
*
*/

if (value==0) {
return " This piece is by " +artist+ " and is called " +title+
" and has no value";
}
else {
return " This piece is by " +artist+ " and is called " +title+
" and is worth £"+value;
}
}

/**
* method compareTo is used in sorting the inventory
* it compares the value of one piece of art to another
* returning a negative, 0, or positive value
* this is then used in 'ranking' the values
*/
public int compareTo(Art other)

return " Artist "+name+" and lives "+address+" and can be emailed at "
+email+" and telephoned on "+telno;
}
}

11-28-2012, 06:46 AM

Fubarable

Re: 7 hours stuck - Assigning Artist to piece of Art!

I can't say that I've read all of your code or understand, and a bit of what I did read doesn't compile, but having said that, some suggestions:

the method searchForWho(...) should not be a void method as then it would be fairly useless. And returning 1 wouldn't help either. Consider having it return the found Artist object if one is found, or null if one is not found.

The `return null;` statdment for the searchForWho() method should be present, but it should only be called if the desired artist is not found after searching through the entire loop. Knowing this, where do you think this statement should be placed within this method? (hint: not where you currently have it).

There's no need for your for-each loop where you search for the Artist inside of your addArt() method. The searchForWho(...) method already has this loop, and that's where it belongs. Instead simply call the searchForWho method there and use the Artist object that it returns, or else if it returns null, create your new Artist object there.

I'm not sure what your requirements are, but I like to keep things as clean as possible and separate out concerns that should be separate. For instance, if this were my project, I'd get all user-interface code out of my Inventory class. Instead Inventory would be for maintaining my collection of Art and nothing more. I'd have a separate UI class for my user interface, and my Inventory class would be coded in a flexible way such that the UI could be console or GUI, and either would work. Likewise it could work with File I/O as part of a file-handling class.

In the same light, then my addArt method would take a parameter, an Art object, and would have no System.out.println's (except perhaps temporarily for debugging purposes) and no Scanner code.

11-28-2012, 07:12 AM

monkeyjr97

Re: 7 hours stuck - Assigning Artist to piece of Art!

cheers for the reply, ive tried to understand what you have said.

Just so you know, she wants the addArt() and such to be in the inventory (not sure why)