2021年5月3日星期一

Java Coding: Using arrays or arrayLists

I'm currently working on a final that has had me stuck on it for days. So basically I have to write a program that works as a catalog, it has 4 books, 4 movies, and 4 music(songs). I have the superclasses and subclasses done, Im just stuck on the test class. I don't know how to go about making the Array. I have the option to use either an Array or ArrayList. can anyone help? Here is the project:

FINAL PROJECT – You have been asked to create an application that makes it possible for users to browse a catalog of books, music and movies for a store. While the final plan is to create an online shopping system, the initial phase of this project will only focus on modeling the data and functionalities as various classes. You will also create an application that will make it possible for the user to search an item, and obtain itsinformation based on various search criteria. To start with modeling the classes, you are given a list of products labels that were directly obtained from the actual products. The data present in these labels is what should be visible to the user as they search the catalog items. You would have noticed that the catalog items are of the types – books , movies and music. While some of the attributes are common across all the items, some are unique to the item's category. Upon discussion with your peer group, you learned that implementing class inheritance might be good way to improve code-reuse. It was therefore, decided to have a super class called catalogItem that would encapsulate all the common attributes for the items. Each of the subclasses, - book, music and movie will the define the attributes that are unique to those items. Some of the attributes of the subclasses will contain a set of Strings that will need to be modeled as an array. You may also consider using an ArrayList whose size is elastic, when compared to using an array. For example, you don't need to specify the size of the ArrayList upfront and can keep adding a new element to it on the fly. If you really hate an array – you may model the authors as a long string ( eg : "author1 , author2, author3" ) to produce a quick working code. Your sub and super classes need all the getters and setters for the attributes. Additionally, you also need a print method to print all the attribute of the sub/super class. Your application needs to create a catalog – which is basically a collection of catalogItems. You may either have 3 separate lists – one for each category- book, movie or music. Or, you can use polymorphism and create one big list in which you will store the books, music and movie objects. Your first job in the main method is to use appropriate constructors to create objects and store them as an array (or better yet as an array list).Then, you will need to create a menu driven application that will have the following choices: 1)Show all details of all the items of a given category 2)Search based on title and print all the details of that title. 3)Search based on genre and display/print all items of that genre.You may optionally add many more types of search

Superclass: public class CatalogItem {

private String title, img, genre, format;  private int year, category;          //Default constructor      public CatalogItem() {          this.title="";          this.img="";          this.genre="";          this.format="";          this.year=0;          this.category=0;      }  //Overloaded constructor  public CatalogItem(String title, String img, String genre, String format, int year, int category) {      this.title=title;      this.img=img;      this.genre=genre;      this.format=format;      this.year=year;      this.category=category;  }    //getters  public String getTitle() {      return title;  }  public String getImg() {      return img;  }  public String getGenre() {      return genre;  }  public String getFormat() {      return format;  }  public int getCategory() {      return category;  }  public int getYear() {      return year;  }      //Setters    public void setTitle(String title) {      this.title = title;  }    public void setImg(String img) {      this.img = img;  }    public void setGenre(String genre) {      this.genre = genre;  }    public void setFormat(String format) {      this.format = format;  }  public void setCategory(int category) {      this.category = category;  }    public void setYear(int year) {      this.year = year;  }      public String ToString() {      return "Title: " + title + "Img: " + img + "Genre: " + genre + "Format: " + format + "Category: " + category;  }  

} Subclasses: public class Book extends CatalogItem {

private String publisher, author;  private int isbn;    //constructor  public Book() {      this.publisher= "";      this.author="";      this.isbn=0;        }    //overloaded constructor  public Book(String title, String img, String genre, String format, int year, int category, String publisher, String author, int isbn) {      super(title, img, genre, format, year, category);      this.publisher=publisher;      this.author=author;      this.isbn=isbn;  }    //getters  public String getPublisher() {      return publisher;  }  public String getAuthor() {      return author;  }  public int getIsbn() {      return isbn;  }    //setters  public void setPublisher(String publisher) {      this.publisher = publisher;  }    public void setAuthor(String author) {      this.author = author;  }    public void setIsbn(int isbn) {      this.isbn =isbn ;  }    public String toString() {      return "Book [isbn:" + isbn + ", author:" + author + ", publisher:" + publisher + "]";  }  

}

public class Movie extends CatalogItem {

private String director, writers, stars;      //constructors  public Movie() {      this.director="";      this.writers="";      this.stars="";  }    public Movie(String title, String img, String genre, String format, int year, int category, String director, String writers, String stars) {      super();      this.director=director;      this.writers=writers;      this.stars=stars;  }    //getters  public String getDirector() {      return director="";  }  public String getWriters() {      return writers="";  }  public String getStars() {      return stars="";  }    //setters  public void setDirector(String director ) {      this.director =director ;  }    public void setWriters(String writers) {      this.writers = writers;  }    public void setStars(String stars) {      this.stars = stars;  }    public String ToString() {      return "Director: " + director +"Writers: " + writers + " Stars: " + stars ;  }  

}

public class Music extends CatalogItem {

private String artist;    //constructors  public Music() {      this.artist="";  }  public Music(String title, String img, String genre, String format, int year, int category, String artist) {      super();      this.artist=artist;  }    //getters  public String getArtist() {      return artist ="";  }    //setters  public void setArtist(String artist) {      this.artist = artist;  }    public String ToString() {      return "Artist: " + artist;  }  
https://stackoverflow.com/questions/67378616/java-coding-using-arrays-or-arraylists May 04, 2021 at 11:58AM

没有评论:

发表评论