Im having trouble getting this code to display correctly.
this is what I get.
How can I get rid of the number before "Product Number"?
I know its having to reqrite my printf statement, but I have no idea how to rewrite it.
any help?
The total number of dvds is 5
0 Product Number = 0
DVD Name= Scream
# of Units = 5
Price Per Unit = $ 5.99
Total Inventory Value = $ 29.95
1 Product Number = 1
DVD Name= Scream 2
# of Units = 10
Price Per Unit = $ 6.99
Total Inventory Value = $ 69.90
2 Product Number = 2
DVD Name= Scream 3
# of Units = 10
Price Per Unit = $ 7.99
Total Inventory Value = $ 79.90
3 Product Number = 3
DVD Name= Scream 4
# of Units = 10
Price Per Unit = $ 8.99
Total Inventory Value = $ 89.90
4 Product Number = 4
DVD Name= Scream 5
# of Units = 10
Price Per Unit = $ 9.99
Total Inventory Value = $ 99.90
The total value of all the inventory is $369.55
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inventory1;
/**
*
* @author */
class Inventory1_2 {
private DVD[] dvds;
private int count;
Inventory1_2(){
dvds = new DVD[10];
count = 0 ;
}
public void add(DVD dvd){
dvds[count]= dvd;
++count;
sort();
}
public double totalvalue(){
double value = 0;
for (int i = 0; i < count;i++){
value=value + dvds[i].totalvalue();
}
return value;
}
public void sort(){
for (int index = 1; index < count; index++) {
DVD key = dvds[index];
int position = index;
while (position > 0 && key.getdvdname().compareTo(dvds [position-1].getdvdname())<0){
dvds[position]=dvds[position-1];
position--;
}
dvds[position]=key;
}
}
public void display(){
System.out.println("\nThe total number of dvds is "+ count);
for (int i=0;i< count;i++)
System.out.printf("\n%3d %s\n",i,dvds[i]);
System.out.printf("\nThe total value of all the inventory is $%.2f\n\n",totalvalue());
}
}
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inventory1;
/**
*
* @author */
public class Inventory2 {
public static void main(String args []){
Inventory1_2 invent = new Inventory1_2();
DVD d1 = new DVD (0,"Scream",5,5.99);
invent.add(d1);
DVD d2 = new DVD (1,"Scream 2",10,6.99);
invent.add(d2);
DVD d3 = new DVD (2,"Scream 3",10,7.99);
invent.add(d3);
DVD d4 = new DVD (3,"Scream 4",10,8.99);
invent.add(d4);
DVD d5 = new DVD (4,"Scream 5",10,9.99);
invent.add(d5);
//display the inventory information one by one
//System.out.println(d1);
//System.out.println();
//System.out.println(d2);
//System.out.println();
//System.out.println(d3);
//System.out.println();
//System.out.println(d4);
// System.out.println();
//System.out.println(d5);
invent.display();
}
}
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inventory1;
/**
*
* @author BurkhartR
*/
class DVD {
private int dvdprodnumber;
private String dvdname;
private int dvdunits;
private double dvdprice;
public DVD(int prodnumber, String name, int units,double price){
dvdprodnumber=prodnumber;
dvdname=name;
dvdunits=units;
dvdprice=price;
}//end of constructor
//set dvd productnumber
public void setDvdprodnumber(int prodnumber) {
dvdprodnumber=prodnumber;
}//end method
//return productnumber
public int getdvdprodnumber(){
return dvdprodnumber;
}//end method
//set the dvd name
public void setdvdname(String name){
dvdname=name;
}//end method
//get dvd name
public String getdvdname(){
return dvdname;
}//end method
//set dvd price
public void setdvdprice(double price) {
dvdprice=price;
}//end method
//get dvd price
public double getdvdprice(){
return dvdprice;
}//end method
public double totalvalue(){
return dvdprice*dvdunits;
}
// This is how the display will look
public String toString() {
return String.format("Product Number =%3d \nDVD Name= %-20s \n# of Units =%3d \nPrice Per Unit = $%6.2f \nTotal Inventory Value = $%7.2f",
dvdprodnumber, dvdname, dvdunits, dvdprice, totalvalue());
}
}