samedi 27 février 2016

Data will be in a two-dimensional data array as follows

Im having issue with below, java code. so far I've done this.

Write class, Publisher, with attributes name and state

public class Publisher

{
    private String name;
     private String state;

public Publisher()
{
    setName("");
    setState("");
}

public Publisher(String n, String s)
{
    setName(n);
    setState(s);
}

public void setName(String n)
{
    name = n; 
}

public void setState(String s)
{
    state = s;
}

public String getName()
{
    return name;
}

public String getState()
{
    return state;
}

public String toString()
{
    return ("Name:"+ name + "State:" + state);
}

}

Write Book class with attributes, title, author, isbn, publisher, and price. Book class has method calculateCharge that accepts the quantity sold and returns the total charge.

  public class Book 
{
private String title;
private String author;
private String isbn;
private Publisher publisher;
private double price;

public Book()
{
    setTitle("");
    setAuthor("");
    setIsbn("");
    setPublisher(new Publisher());  
    setPrice(0.0);
}

public Book(String t, String a, String i, Publisher Publisher, double m)  

{
    setTitle(t);
    setAuthor(a);
    setIsbn(i);
    setPublisher(publisher);
    setPrice(m);

 }

 public void setTitle(String t)
 {
    title = t;
 }

 public void setAuthor(String a)
 {
    author = a;
 }
 public void setIsbn(String i)
 {
    isbn = i;
 }
 public void setPublisher(Publisher publisher) 
 {
    this.publisher = publisher;
 }
 public void setPrice(double m)
 {
    price = m ;
 }

 public String getTitle()
 {
    return title;
  }
  public String getAuthor()
  {
    return author;
  }
  public String getIsbn()
  {
    return isbn;
  }

  public Publisher getPublisher()
  {
    return publisher;
  }
  public double getPrice()
  {
    return price;
  }

public double calculatecharge(int qtySold)
{
    double totalCharge; 
    totalCharge = qtySold * getPrice();
    return totalCharge; 
}

public String toString()
{
    return ( "Title :" + title + "Author :" + author + "ISBN :" + isbn + "Publisher :" + publisher + "Price :" + price);
 }

 }

And wrote the non-fiction and fiction class as follows.

public class Fiction extends Book  
{
private String fictionCode;

public Fiction()
{
    super(); 
    setFictionCode("");
}

public Fiction(String t, String a, String i, Publisher Publisher, double m)
{ 
    super(t,a,i,Publisher,m);   
    setFictionCode(fictionCode);

 }

 public void setFictionCode(String fc)

 {
    fictionCode = fc;
 }

 public String getFictionCode()

 {
    return fictionCode;
 }
 public String toString()

 {
    return super.toString();
 }
 }   

But I'm having issue with calling it in the booktest. Data will be in a two-dimensional data array as follows:

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"))

 {
  bookArray[i]= new Fiction(dataArray[i][1],dataArray[i][2],dataArray[i][3],      new Publisher (dataArray[i][4],
(dataArray[i][5]), new Book(double.parseDouble(dataArray[i][6]),dataArray[i][7])));
 }  
 if (dataArray [i][0].equals("NonFiction"))
  {     
    bookArray[i]= new NonFiction(dataArray[i][1],dataArray[i][2],dataArray[i][3], new Publisher (dataArray[i][4],dataArray[i][5],
new Book(double.parseDouble(dataArray[i][6]),dataArray[i][7])));
 }

 for (book b:bookArray)
 { 
    system.out.println(b.toString());
}
}
 }

Aucun commentaire:

Enregistrer un commentaire