473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Need help with Class Project!

OK, so I'm very new to Java programming and I've been able to squeek by so far, but I'm completely stuck on this assignment. Here is the assignment that is due this week -
• Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.

• Create a method to calculate the value of the entire inventory.

• Create another method to sort the array items by the name of the product. DON'T use array.sort.

Compile and run the Java program.

And here is my code from last week -

Expand|Select|Wrap|Line Numbers
  1. //Inventory Program Part 1
  2.  
  3.  
  4. class Inventory
  5.  
  6. {
  7.     private String Name; //stores DVD name
  8.     private long itemNumber; //stores item number
  9.     private long stockQuantity; //stores quanity in stock
  10.     private double dvdPrice; //stores DVD price
  11.     public Inventory ()
  12.  
  13.     {
  14.         Name = "";
  15.         itemNumber = 0L;
  16.         stockQuantity = 0L;
  17.         dvdPrice = 0.0;
  18.         }
  19.            public Inventory (String Name, long itemNumber, long stockQuantity, double dvdPrice)
  20.               {
  21.                  this.Name = Name;
  22.                  this.itemNumber = itemNumber;
  23.                  this.stockQuantity = stockQuantity;
  24.                  this.dvdPrice = dvdPrice;
  25.            }
  26.  
  27.  
  28.           public void setItemName(String Name)  //Method to set and get the item name
  29.            {
  30.               this.Name = Name;
  31.            }
  32.            public String getItemName()
  33.            {
  34.               return Name;
  35.            }
  36.  
  37.  
  38.            public void setItemNumber(long itemNumber)  //Method to set and get the item number
  39.            {
  40.               itemNumber = itemNumber;
  41.            }
  42.            public long getItemNumber()
  43.            {
  44.               return itemNumber;
  45.            }
  46.  
  47.  
  48.            public void setStockQuantity(long quantity)  //Method to set and get the quantity in stock
  49.            {
  50.               stockQuantity = stockQuantity;
  51.            }
  52.            public long getStockQuantity()
  53.            {
  54.               return stockQuantity;
  55.            }
  56.  
  57.  
  58.            public void setItemPrice(double dvdPrice)  //Method to set and get the item price
  59.            {
  60.               this.dvdPrice = dvdPrice;
  61.            }
  62.            public double getItemPrice()
  63.            {
  64.               return dvdPrice;
  65.            }
  66.  
  67.  
  68.            public double calculateInventoryValue()  //Method to calculate the value of the in stock inventory
  69.            {
  70.               return dvdPrice * stockQuantity;
  71.            }
  72.  
  73.         }//end class Inventory
  74.  
  75.  
  76.         public class Inventory1
  77.  
  78.  
  79.         {
  80.  
  81.            public static void main( String args[])
  82.            {
  83.  
  84.             Inventory p = new Inventory("24: Season 4", 1, 10, 24.99); //Lists 1st DVD inventory information
  85.             System.out.println();
  86.             System.out.print( "Inventory of DVD Movies: " ); //display title
  87.             System.out.printf("\n\nDVD Title:         %s\n",p.getItemName()); //display DVD title
  88.             System.out.printf("Item Number:       %s\n",p.getItemNumber()); //display item number
  89.             System.out.printf("Quantity in Stock: %s\n",p.getStockQuantity()); //display quantity in stock
  90.             System.out.printf("Item Price:        $%.2f\n",p.getItemPrice()); //display DVD price
  91.             System.out.printf("Inventory Value:   $%.2f\n",p.calculateInventoryValue()); //display total value of inventory for item
  92.             System.out.println();
  93.             System.out.println();
  94.  
  95.            }
  96. }//end class Inventory1
Can somebody please help me get to the next step!! Many thanks in advance!! :)
Jan 24 '07 #1
38 4165
r035198x
13,262 8TB
OK, so I'm very new to Java programming and I've been able to squeek by so far, but I'm completely stuck on this assignment. Here is the assignment that is due this week -
• Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.

• Create a method to calculate the value of the entire inventory.

• Create another method to sort the array items by the name of the product. DON'T use array.sort.

Compile and run the Java program.

And here is my code from last week -

//Inventory Program Part 1


class Inventory

{
private String Name; //stores DVD name
private long itemNumber; //stores item number
private long stockQuantity; //stores quanity in stock
private double dvdPrice; //stores DVD price
public Inventory ()

{
Name = "";
itemNumber = 0L;
stockQuantity = 0L;
dvdPrice = 0.0;
}
public Inventory (String Name, long itemNumber, long stockQuantity, double dvdPrice)
{
this.Name = Name;
this.itemNumber = itemNumber;
this.stockQuantity = stockQuantity;
this.dvdPrice = dvdPrice;
}


public void setItemName(String Name) //Method to set and get the item name
{
this.Name = Name;
}
public String getItemName()
{
return Name;
}


public void setItemNumber(long itemNumber) //Method to set and get the item number
{
itemNumber = itemNumber;
}
public long getItemNumber()
{
return itemNumber;
}


public void setStockQuantity(long quantity) //Method to set and get the quantity in stock
{
stockQuantity = stockQuantity;
}
public long getStockQuantity()
{
return stockQuantity;
}


public void setItemPrice(double dvdPrice) //Method to set and get the item price
{
this.dvdPrice = dvdPrice;
}
public double getItemPrice()
{
return dvdPrice;
}


public double calculateInventoryValue() //Method to calculate the value of the in stock inventory
{
return dvdPrice * stockQuantity;
}

}//end class Inventory


public class Inventory1


{

public static void main( String args[])
{

Inventory p = new Inventory("24: Season 4", 1, 10, 24.99); //Lists 1st DVD inventory information
System.out.println();
System.out.print( "Inventory of DVD Movies: " ); //display title
System.out.printf("\n\nDVD Title: %s\n",p.getItemName()); //display DVD title
System.out.printf("Item Number: %s\n",p.getItemNumber()); //display item number
System.out.printf("Quantity in Stock: %s\n",p.getStockQuantity()); //display quantity in stock
System.out.printf("Item Price: $%.2f\n",p.getItemPrice()); //display DVD price
System.out.printf("Inventory Value: $%.2f\n",p.calculateInventoryValue()); //display total value of inventory for item
System.out.println();
System.out.println();

}
}//end class Inventory1


Can somebody please help me get to the next step!! Many thanks in advance!! :)
Hi next time you post code please remember to use code tags. Now for your problem please read this and post if you still need any help.
Jan 24 '07 #2
Hi next time you post code please remember to use code tags. Now for your problem please read this and post if you still need any help.

Yes, I read through that whole post and it did not help much as it's somewhat jumbled. That is the same project I'm working on though. Their part3 is close to what I need, but not quite there.

Can you please explain code tags?
Jan 24 '07 #3
r035198x
13,262 8TB
Yes, I read through that whole post and it did not help much as it's somewhat jumbled. That is the same project I'm working on though. Their part3 is close to what I need, but not quite there.

Can you please explain code tags?
I've just added them to your post there. Every time you make a post and want to include code then just make sure to wrap the code around code tags
Jan 24 '07 #4
I've just added them to your post there. Every time you make a post and want to include code then just make sure to wrap the code around code tags
Thanks, I just read the code tags rule to the right! Shows you how observant I am!
Jan 24 '07 #5
Thanks, I just read the code tags rule to the right! Shows you how observant I am!
OK, I've been playing around with some of the code and I'm making progress. Here's what I have now. I would like to display the total amount at the bottom instead of the top and display the list as is then display as sorted. I also can't use the array.sort so I need to sort another way.

I would also like to display the DVD's in a uniform line rather than 1 space after the field:. For example:
DVD Name:___DVD1
Item:________1
Cost:________10.99
And is there a way to change the field names on the printout? I was able to more easily do this in part1.

Any ideas?


Expand|Select|Wrap|Line Numbers
  1.  
  2. //Inventory2.java
  3.  
  4.  
  5. import java.util.*;
  6. class Product implements Comparable
  7.  
  8. {
  9.    private String name;     // class variable that stores the item name
  10.    private long number;      // class variable that stores the item number
  11.    private long stockQuantity;   // class variable that stores the quantity in stock
  12.    private double price;      // class variable that stores the item price
  13.  
  14.    public Product() // Constructor for the Product class
  15.    {
  16.       name = "";
  17.       number = 0L;
  18.       stockQuantity = 0L;
  19.       price = 0.0;
  20.    }
  21.    public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
  22.       {
  23.       this.name = name;
  24.       this.number = number;
  25.       this.stockQuantity = stockQuantity;
  26.       this.price = price;
  27.          }
  28.  
  29.    public void setItemName(String name)  // Method to set the item name
  30.    {
  31.       this.name = name;
  32.    }
  33.    public String getItemName()  // Method to get the item name
  34.    {
  35.       return name;
  36.    }
  37.  
  38.  
  39.    public void setItemNumber(long number)  // Method to set the item number
  40.    {
  41.       this.number = number;
  42.    }
  43.    public long getItemNumber()  // Method to get the item number
  44.    {
  45.       return number;
  46.    }
  47.  
  48.  
  49.    public void setStockQuantity(long quantity)  // Method to set the quantity in stock
  50.    {
  51.       stockQuantity = quantity;
  52.    }
  53.    public long getStockQuantity()  // Method to get the quantity in stock
  54.    {
  55.       return stockQuantity;
  56.    }
  57.  
  58.  
  59.    public void setItemPrice(double price)  // Method to set the item price
  60.    {
  61.       this.price = price;
  62.    }
  63.    public double getItemPrice()  // Method to get the item price
  64.    {
  65.       return price;
  66.    }
  67.  
  68.  
  69.    public double calculateInventoryValue()  // Method to calculate the value of the inventory
  70.    {
  71.       return price * stockQuantity;
  72.    }
  73.    public int compareTo (Object o)
  74.    {
  75.  
  76.  
  77.    Product p = (Product)o;
  78.       return name.compareTo(p.getItemName());
  79.    }
  80.  
  81.    public String toString()
  82.    {
  83.       return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
  84.    }
  85.  
  86. }//end class Product
  87.  
  88. public class Inventory2
  89. {
  90.    // main methods begins execution of java application
  91.    public static void main( String args[])
  92.    {
  93.  
  94.    Product[] supplies = new Product[3];
  95.  
  96.    Product p1 = new Product("DVD1", 1, 11, 10.99);
  97.    Product p2 = new Product("DVD2", 2, 22, 20.99);
  98.    Product p3 = new Product("DVD3", 3, 33, 30.99);
  99.  
  100.    supplies[0] = p1;
  101.    supplies[1] = p2;
  102.    supplies[2] = p3;
  103.  
  104.    double total = 0.0;
  105.  
  106.    for(int i= 0; i < 3;i++)
  107.    {
  108.    total = total + supplies[i].calculateInventoryValue();
  109.    }
  110.  
  111.   System.out.println("Total Value is: $"+total);
  112.  
  113.   Arrays.sort(supplies);
  114.  
  115.     for(Product p: supplies)
  116.     {
  117.     System.out.println(p);
  118.     System.out.println();
  119.     }
  120.  
  121.    } // end main method
  122. }//end class Inventory2
  123.  
  124.  
Jan 24 '07 #6
OK, I'm making some progress. I still need to sort without using array.sort. I tried a bubble sort which I kept in there, but it wouldn't work for me. And I would still like to know how to allign the names better.
Here is what I just came up with:

Expand|Select|Wrap|Line Numbers
  1.  
  2. //Inventory2.java
  3.  
  4.  
  5. import java.util.*;
  6. class Product implements Comparable
  7.  
  8. {
  9.    private String name;     // class variable that stores the item name
  10.    private long number;      // class variable that stores the item number
  11.    private long stockQuantity;   // class variable that stores the quantity in stock
  12.    private double price;      // class variable that stores the item price
  13.  
  14.    public Product() // Constructor for the Product class
  15.    {
  16.       name = "";
  17.       number = 0L;
  18.       stockQuantity = 0L;
  19.       price = 0.0;
  20.    }
  21.    public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
  22.       {
  23.       this.name = name;
  24.       this.number = number;
  25.       this.stockQuantity = stockQuantity;
  26.       this.price = price;
  27.          }
  28.  
  29.    public void setItemName(String name)  // Method to set the item name
  30.    {
  31.       this.name = name;
  32.    }
  33.    public String getItemName()  // Method to get the item name
  34.    {
  35.       return name;
  36.    }
  37.  
  38.  
  39.    public void setItemNumber(long number)  // Method to set the item number
  40.    {
  41.       this.number = number;
  42.    }
  43.    public long getItemNumber()  // Method to get the item number
  44.    {
  45.       return number;
  46.    }
  47.  
  48.  
  49.    public void setStockQuantity(long quantity)  // Method to set the quantity in stock
  50.    {
  51.       stockQuantity = quantity;
  52.    }
  53.    public long getStockQuantity()  // Method to get the quantity in stock
  54.    {
  55.       return stockQuantity;
  56.    }
  57.  
  58.  
  59.    public void setItemPrice(double price)  // Method to set the item price
  60.    {
  61.       this.price = price;
  62.    }
  63.    public double getItemPrice()  // Method to get the item price
  64.    {
  65.       return price;
  66.    }
  67.  
  68.  
  69.    public double calculateInventoryValue()  // Method to calculate the value of the inventory
  70.    {
  71.       return price * stockQuantity;
  72.    }
  73.    public int compareTo (Object o)
  74.    {
  75.  
  76.  
  77.    Product p = (Product)o;
  78.       return name.compareTo(p.getItemName());
  79.    }
  80.  
  81.    public String toString()
  82.    {
  83.       return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
  84.    }
  85.  
  86. }//end class Product
  87.  
  88. public class Inventory2
  89. {
  90.    // main methods begins execution of java application
  91.    public static void main( String args[])
  92.    {
  93.  
  94.    Product[] supplies = new Product[3];
  95.  
  96.    Product p1 = new Product("cDVD3", 1, 11, 10.99);
  97.    Product p2 = new Product("aDVD2", 2, 22, 20.99);
  98.    Product p3 = new Product("bDVD1", 3, 33, 30.99);
  99.  
  100.    supplies[0] = p1;
  101.    supplies[1] = p2;
  102.    supplies[2] = p3;
  103.  
  104.  //  product temp[] supplies = new product[1];
  105.  
  106.    double total = 0.0;
  107.  
  108.    for(int i= 0; i < 3;i++)
  109.    {
  110.    total = total + supplies[i].calculateInventoryValue();
  111.    }
  112.  
  113.  
  114.  
  115.       for(Product p: supplies)
  116.       {
  117.       System.out.println(p);
  118.       System.out.println();
  119.     }
  120.  
  121.  Arrays.sort(supplies);
  122. //        sorting the array using Bubble Sort
  123.     //    for(int j = 0; j < supplies.length - 1; j++)
  124.     //    {
  125. //
  126.     //        for(int k = 0; k < supplies.length - 1; k++)
  127.     //        {
  128. //
  129.     //            if(supplies[k].getItemName().compareToIgnoreCase(supplies[k+1].getItemName()) > 0)
  130.     //            {
  131. //
  132.     //                temp[0] = supplies[k];
  133.     //                supplies[k] = supplies[k+1];
  134.     //                supplies[k+1] = temp[0];
  135. //
  136.     //            }//end if
  137. //
  138.     //        }//end for loop
  139. //
  140.     //    }//end for loop
  141.  
  142.  
  143.  
  144.  
  145.  
  146.     for(Product p: supplies)
  147.     {
  148.     System.out.println(p);
  149.     System.out.println();
  150.     }
  151.  
  152.  
  153.    System.out.println("Total Value is: $"+total);
  154.  
  155.  
  156.    } // end main method
  157. }//end class Inventory2
  158.  
  159.  
Jan 24 '07 #7
DeMan
1,806 1GB
Are you posting this because the sort doesn't work?
Does it alter the order at all?

Not sure why you have 2 loops around the sort....
Jan 24 '07 #8
DeMan
1,806 1GB
You will need two loops (my mistake) but in your code one of them does nothing.
Try Something like this (which I think is cliose to yours, so you were on the right track):

Expand|Select|Wrap|Line Numbers
  1. swapped = false;
  2. do
  3. {
  4.     swapped = false;
  5.     for(int i=0; i<supplies.length; i++)
  6.     {
  7.       if(supplies[i].getItemName().compareToIgnoreCase( supplies[i+1].getItemName()) >0)
  8.       {
  9.         temp[0] = supplies[k];
  10.         supplies[k] = supplies[k+1];
  11.         supplies[k+1] = temp[0];
  12.         swapped = true;
  13.       }//end if
  14.     }//end for loop
  15. } while(swapped == false);
  16.  
  17.  
Jan 24 '07 #9
Yes, the array.sort does work fine, but for some reason we are not allowed to use that method to sort.

I get a bunch of errors when I insert your code. Here's what I get:
C:\Inventory2.java:142: cannot find symbol
symbol : variable swapped
location: class Inventory2
swapped = false;
^
C:\Inventory2.java:145: cannot find symbol
symbol : variable swapped
location: class Inventory2
swapped = false;
^
C:\Inventory2.java:150: cannot find symbol
symbol : variable temp
location: class Inventory2
temp[0] = supplies[k];
^
C:\Inventory2.java:150: cannot find symbol
symbol : variable k
location: class Inventory2
temp[0] = supplies[k];
^
C:\Inventory2.java:151: cannot find symbol
symbol : variable k
location: class Inventory2
supplies[k] = supplies[k+1];
^
C:\Inventory2.java:151: cannot find symbol
symbol : variable k
location: class Inventory2
supplies[k] = supplies[k+1];
^
C:\Inventory2.java:151: incompatible types
found : <nulltype>
required: int
supplies[k] = supplies[k+1];
^
C:\Inventory2.java:152: cannot find symbol
symbol : variable k
location: class Inventory2
supplies[k+1] = temp[0];
^
C:\Inventory2.java:152: incompatible types
found : <nulltype>
required: int
supplies[k+1] = temp[0];
^
C:\Inventory2.java:152: cannot find symbol
symbol : variable temp
location: class Inventory2
supplies[k+1] = temp[0];
^
C:\Inventory2.java:153: cannot find symbol
symbol : variable swapped
location: class Inventory2
swapped = true;
^
C:\Inventory2.java:156: cannot find symbol
symbol : variable swapped
location: class Inventory2
} while(swapped == false);
^
12 errors

Tool completed with exit code 1

And here's the code I was using if you want to compile it yourself:

Expand|Select|Wrap|Line Numbers
  1. //Inventory2.java
  2.  
  3.  
  4. import java.util.*;
  5. class Product implements Comparable
  6.  
  7. {
  8.    private String name;     // class variable that stores the item name
  9.    private long number;      // class variable that stores the item number
  10.    private long stockQuantity;   // class variable that stores the quantity in stock
  11.    private double price;      // class variable that stores the item price
  12.  
  13.    public Product() // Constructor for the Product class
  14.    {
  15.       name = "";
  16.       number = 0L;
  17.       stockQuantity = 0L;
  18.       price = 0.0;
  19.    }
  20.    public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
  21.       {
  22.       this.name = name;
  23.       this.number = number;
  24.       this.stockQuantity = stockQuantity;
  25.       this.price = price;
  26.          }
  27.  
  28.    public void setItemName(String name)  // Method to set the item name
  29.    {
  30.       this.name = name;
  31.    }
  32.    public String getItemName()  // Method to get the item name
  33.    {
  34.       return name;
  35.    }
  36.  
  37.  
  38.    public void setItemNumber(long number)  // Method to set the item number
  39.    {
  40.       this.number = number;
  41.    }
  42.    public long getItemNumber()  // Method to get the item number
  43.    {
  44.       return number;
  45.    }
  46.  
  47.  
  48.    public void setStockQuantity(long quantity)  // Method to set the quantity in stock
  49.    {
  50.       stockQuantity = quantity;
  51.    }
  52.    public long getStockQuantity()  // Method to get the quantity in stock
  53.    {
  54.       return stockQuantity;
  55.    }
  56.  
  57.  
  58.    public void setItemPrice(double price)  // Method to set the item price
  59.    {
  60.       this.price = price;
  61.    }
  62.    public double getItemPrice()  // Method to get the item price
  63.    {
  64.       return price;
  65.    }
  66.  
  67.  
  68.    public double calculateInventoryValue()  // Method to calculate the value of the inventory
  69.    {
  70.       return price * stockQuantity;
  71.    }
  72.    public int compareTo (Object o)
  73.    {
  74.  
  75.  
  76.    Product p = (Product)o;
  77.       return name.compareTo(p.getItemName());
  78.    }
  79.  
  80.    public String toString()
  81.    {
  82.       return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
  83.    }
  84.  
  85. }//end class Product
  86.  
  87. public class Inventory2
  88. {
  89.    // main methods begins execution of java application
  90.    public static void main( String args[])
  91.    {
  92.  
  93.    Product[] supplies = new Product[3];
  94.  
  95.    Product p1 = new Product("cDVD3", 1, 11, 10.99);
  96.    Product p2 = new Product("aDVD2", 2, 22, 20.99);
  97.    Product p3 = new Product("bDVD1", 3, 33, 30.99);
  98.  
  99.    supplies[0] = p1;
  100.    supplies[1] = p2;
  101.    supplies[2] = p3;
  102.  
  103.  //  product temp[] supplies = new product[1];
  104.  
  105.    double total = 0.0;
  106.  
  107.    for(int i= 0; i < 3;i++)
  108.    {
  109.    total = total + supplies[i].calculateInventoryValue();
  110.    }
  111.  
  112.  
  113.  
  114.       for(Product p: supplies)
  115.       {
  116.       System.out.println(p);
  117.       System.out.println();
  118.     }
  119. //OLD CODE-------------------------------------
  120.  //Arrays.sort(supplies);
  121. //        sorting the array using Bubble Sort
  122.     //    for(int j = 0; j < supplies.length - 1; j++)
  123.     //    {
  124. //
  125.     //        for(int k = 0; k < supplies.length - 1; k++)
  126.     //        {
  127. //
  128.     //            if(supplies[k].getItemName().compareToIgnoreCase(supplies[k+1].getItemName()) > 0)
  129.     //            {
  130. //
  131.     //                temp[0] = supplies[k];
  132.     //                supplies[k] = supplies[k+1];
  133.     //                supplies[k+1] = temp[0];
  134. //
  135.     //            }//end if
  136. //
  137.     //        }//end for loop
  138. //
  139.     //    }//end for loop
  140. //END OLD CODE------------------------------------
  141.  
  142.     swapped = false;
  143.     do
  144.     {
  145.         swapped = false;
  146.         for(int i=0; i<supplies.length; i++)
  147.         {
  148.           if(supplies[i].getItemName().compareToIgnoreCase( supplies[i+1].getItemName()) >0)
  149.           {
  150.             temp[0] = supplies[k];
  151.             supplies[k] = supplies[k+1];
  152.             supplies[k+1] = temp[0];
  153.             swapped = true;
  154.           }//end if
  155.         }//end for loop
  156. } while(swapped == false);
  157.  
  158.  
  159.  
  160.  
  161.  
  162.     for(Product p: supplies)
  163.     {
  164.     System.out.println(p);
  165.     System.out.println();
  166.     }
  167.  
  168.  
  169.    System.out.println("Total Value is: $"+total);
  170.  
  171.  
  172.    } // end main method
  173. }//end class Inventory2
  174.  
  175.  
Jan 24 '07 #10
r035198x
13,262 8TB
Do the sorting in a seperate method. Here is an example program that you should go through and make sure you understand before proceeding

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. class Church {
  3.     private String name;
  4.     private String pastor;
  5.     public Church(String name, String pastor) {
  6.         this.name = name;
  7.         this.pastor = pastor;
  8.     }
  9.     public String getPastor() {
  10.         return pastor;
  11.     }
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public void setPastor(String pastor) {
  16.         this.pastor = pastor;
  17.     }
  18.     public String toString() {
  19.         return getName() + " is Pastored by "+getPastor();
  20.     }
  21.     public int compareByPastor(Church c) {
  22.         int x = pastor.compareTo(c.getPastor());
  23.         return x;
  24.     }
  25.     public int compareByName(Church c) {
  26.         int x = name.compareTo(c.getName());
  27.         return x;
  28.     }
  29. }
  30.  
  31. class Churches {
  32.     private final List<Church> churches;
  33.  
  34.     public Churches() {
  35.         churches = new ArrayList<Church>();
  36.     }
  37.     public void addWithoutSorting(Church c) {
  38.         churches.add(c);
  39.     }
  40.  
  41.     //You could always add using this method
  42.     public void addWithSorting(Church c) {
  43.  
  44.     }
  45.     public void display() {
  46.         for(int j = 0; j < churches.size(); j++) {
  47.             System.out.print(churches.get(j).toString());
  48.             System.out.println("");
  49.         }
  50.    }
  51.    public List<Church> getChurches() {
  52.        return churches;
  53.    }
  54.    public void sortBy(String s) {
  55.        for (int i = 1; i < churches.size(); i++) {
  56.            int j;
  57.            Church val = churches.get(i);
  58.            for (j = i-1; j > -1; j--) {
  59.                Church temp = churches.get(j);
  60.                if(s.equals("Pastor")) {
  61.                    if (temp.compareByPastor(val) <= 0) {
  62.                        break;
  63.                    }
  64.                }
  65.                else if(s.equals("Name")) {
  66.                    if (temp.compareByName(val) <= 0) {
  67.                           break;
  68.                    }
  69.                }
  70.                churches.set(j+1, temp);
  71.             }
  72.             churches.set(j+1, val);
  73.        }
  74.      }
  75.  
  76.     public static void main(String[] args) {
  77.         Churches baptists = new Churches();
  78.         baptists.addWithoutSorting(new Church("Pac", "Pastor G"));
  79.         baptists.addWithoutSorting(new Church("New Life", "Tudor"));
  80.         baptists.addWithoutSorting(new Church("My Church", "r035198x"));
  81.         baptists.addWithoutSorting(new Church("AFM", "Cathy"));
  82.         System.out.println("**********************Before Sorting***********************");
  83.         baptists.display();
  84.         baptists.sortBy("Pastor");
  85.         System.out.println("**********************After sorting by Pastor**************");
  86.         baptists.display();
  87.         baptists.sortBy("Name");
  88.         System.out.println("**********************After sorting by Name****************");
  89.         baptists.display();
  90.  
  91.     }
  92.  
  93.   }
Jan 25 '07 #11
DeMan
1,806 1GB
Yes, the array.sort does work fine, but for some reason we are not allowed to use that method to sort.

I get a bunch of errors when I insert your code. Here's what I get:
C:\Inventory2.java:142: cannot find symbol
symbol : variable swapped
location: class Inventory2
swapped = false;
My code isn't meant to fit directly into your code, it was an ide of the logic flow that you need..... in particular 'swapped' hasn't been declared which is why the program can't find it - I hope to have described the method so that you can implement it.......
Jan 25 '07 #12
That doesn't really help me much! I'm not sure I understand the example though and the code doesn't compile so I can't see how the program works. I'm also not sure why my original sort doesn't work. What's wrong with the code?
Jan 25 '07 #13
r035198x
13,262 8TB
That doesn't really help me much! I'm not sure I understand the example though and the code doesn't compile so I can't see how the program works. I'm also not sure why my original sort doesn't work. What's wrong with the code?
If you make an effort to understand the program I posted then you will be able to get your sort to work easily yourself. What errors are you getting with it?
Which version of jdk are you using?

If you are using a version earlier than 1.5 then use this

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. class Church {
  3.     private String name;
  4.     private String pastor;
  5.     public Church(String name, String pastor) {
  6.         this.name = name;
  7.         this.pastor = pastor;
  8.     }
  9.     public String getPastor() {
  10.         return pastor;
  11.     }
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public void setPastor(String pastor) {
  16.         this.pastor = pastor;
  17.     }
  18.     public String toString() {
  19.         return getName() + " is Pastored by "+getPastor();
  20.     }
  21.     public int compareByPastor(Church c) {
  22.         int x = pastor.compareTo(c.getPastor());
  23.         return x;
  24.     }
  25.     public int compareByName(Church c) {
  26.         int x = name.compareTo(c.getName());
  27.         return x;
  28.     }
  29. }
  30.  
  31. class Churches2 {
  32.     private final List churches;
  33.  
  34.     public Churches2() {
  35.         churches = new ArrayList<Church>();
  36.     }
  37.     public void addWithoutSorting(Church c) {
  38.         churches.add(c);
  39.     }
  40.  
  41.     //You could always add using this method
  42.     public void addWithSorting(Church c) {
  43.  
  44.     }
  45.     public void display() {
  46.         for(int j = 0; j < churches.size(); j++) {
  47.             System.out.print(churches.get(j).toString());
  48.             System.out.println("");
  49.         }
  50.    }
  51.    public List getChurches() {
  52.        return churches;
  53.    }
  54.    public void sortBy(String s) {
  55.        for (int i = 1; i < churches.size(); i++) {
  56.            int j;
  57.            Church val = (Church)churches.get(i);
  58.            for (j = i-1; j > -1; j--) {
  59.                Church temp = (Church)churches.get(j);
  60.                if(s.equals("Pastor")) {
  61.                    if (temp.compareByPastor(val) <= 0) {
  62.                        break;
  63.                    }
  64.                }
  65.                else if(s.equals("Name")) {
  66.                    if (temp.compareByName(val) <= 0) {
  67.                           break;
  68.                    }
  69.                }
  70.                churches.set(j+1, temp);
  71.             }
  72.             churches.set(j+1, val);
  73.        }
  74.      }
  75.  
  76.     public static void main(String[] args) {
  77.         Churches baptists = new Churches();
  78.         baptists.addWithoutSorting(new Church("Pac", "Pastor G"));
  79.         baptists.addWithoutSorting(new Church("New Life", "Tudor"));
  80.         baptists.addWithoutSorting(new Church("My Church", "r035198x"));
  81.         baptists.addWithoutSorting(new Church("AFM", "Cathy"));
  82.         System.out.println("**********************Before Sorting***********************");
  83.         baptists.display();
  84.         baptists.sortBy("Pastor");
  85.         System.out.println("**********************After sorting by Pastor**************");
  86.         baptists.display();
  87.         baptists.sortBy("Name");
  88.         System.out.println("**********************After sorting by Name****************");
  89.         baptists.display();
  90.  
  91.     }
  92.  
  93.   } 
You should use at least jdk1.5 these days
Jan 25 '07 #14
I am using 1.5 and I just realized what I did wrong (duh). I saved the file as Church.java instead of Churches.java. It still compiled, but I got a run error when it ran. Churches run fine and I see how you are sorting it. I will try to apply this to my program and will post on here if I can't get it.

I'm sure I'll have more question with the later assignments so I will post on here in the future if that's OK. Or I can create a new post. Whatever is best.
Jan 25 '07 #15
r035198x
13,262 8TB
I am using 1.5 and I just realized what I did wrong (duh). I saved the file as Church.java instead of Churches.java. It still compiled, but I got a run error when it ran. Churches run fine and I see how you are sorting it. I will try to apply this to my program and will post on here if I can't get it.

I'm sure I'll have more question with the later assignments so I will post on here in the future if that's OK. Or I can create a new post. Whatever is best.
Just keep posting here as long as it's the same assignment so that those answering do not answer out of context. Make sure you really understand every line in that example (if you don't post here) because that really forms the basis of most programs you are going to write.
Jan 25 '07 #16
Hello all, I have that same exact assingment to do as well, and I am having a rough time to. I've read through many posts about this assignment, but I am just not really comprehending how to set up the array and how to sort. I will try to take a better look at it to see if I can understand it better. Here is my code I have so far by the way:

Expand|Select|Wrap|Line Numbers
  1. // Product class
  2.  
  3.  
  4. public class Product {
  5.  
  6.     private String title;
  7.     private double item;
  8.     private double stock;
  9.     private double price;
  10.  
  11.     // Constructor to initialize the product information.
  12.     public Product(String title, double item, double stock,
  13.             double price) {
  14.         setTitle(title);
  15.         setItem(item);
  16.         setStock(stock);
  17.         setPrice(price);
  18.  
  19.     }
  20.  
  21.     // Method to get the title
  22.     public String getTitle() {
  23.         return title;
  24.     }
  25.  
  26.     // Method to get the item number
  27.     public double getItem() {
  28.         return item;
  29.     }
  30.  
  31.     //Method to get the stock
  32.     public double getStock() {
  33.         return stock;
  34.     }
  35.  
  36.     //Method to get the price
  37.     public double getPrice() {
  38.         return price;
  39.  
  40.  
  41.  
  42.     }
  43.  
  44.     // Method to set the title name.
  45.     public void setTitle(String title) {
  46.         this.title = title;
  47.     }
  48.  
  49.     // Method to set the item number.
  50.     public void setItem(double item) {
  51.         this.item = item;
  52.     }
  53.  
  54.     // Method to set the stock available.
  55.     public void setStock(double stock) {
  56.         this.stock = stock;
  57.     }
  58.  
  59.     //Method to set the price.
  60.     public void setPrice(double price) {
  61.         this.price = price;
  62.  
  63.     }
  64.     // Method to compute the value of inventory.
  65.     public double getInvValue() {
  66.         return this.stock *this.price;
  67.     }
  68.  
  69. }
  70.  
  71.  
  72. // Inventory program
  73. // Main application
  74.  
  75.  
  76.  
  77. public class Inventory
  78. {
  79.     public static void main (String args [])
  80.     {
  81.  
  82.     System.out.println ("Inventory of DVD Movies:\n");
  83.  
  84.         String title = "Beerfest";
  85.         double item = 1;
  86.         double stock = 15;
  87.         double price = 22.50;
  88.         double value;
  89.  
  90.     Product product = new Product (title, item, stock, price);
  91.  
  92.     System.out.printf( "%s%18s\n", "DVD Title:", product.getTitle() );
  93.  
  94.     System.out.printf( "%s%16s\n", "Item #:", product.getItem() );
  95.  
  96.     System.out.printf( "%s%8s\n", "Number in Stock:" , product.getStock() );
  97.  
  98.     System.out.printf( "%s%18s\n", "Price:", product.getPrice() );
  99.  
  100.  
  101.  
  102.     System.out.printf( "%s%9s\n", "Inventory Value:", product.getInvValue() );
  103.  
  104.     }
  105. }
  106.  
Jan 25 '07 #17
OK, so I went through and I think I got the sort figured in, but now I can't compile my program. The error I get is:

C:\Inventory2.java:112: illegal start of expression
public void sortByName() {
^
1 error

Tool completed with exit code 1

Why can't I start my expression like that? In the Churches program you did and that compiles fine. What am I missing?
Jan 25 '07 #18
Ganon11
3,652 Expert 2GB
OK, so I went through and I think I got the sort figured in, but now I can't compile my program. The error I get is:

C:\Inventory2.java:112: illegal start of expression
public void sortByName() {
^
1 error

Tool completed with exit code 1

Why can't I start my expression like that? In the Churches program you did and that compiles fine. What am I missing?
That sounds to me like there is something wrong with the previous line of code. Perhaps you forgot a semicolon, or left a set of curly braces {} unclosed.
Jan 25 '07 #19
Ahh, Sorry! I forgot to post the code.

Here it is:
Expand|Select|Wrap|Line Numbers
  1. //Inventory2.java
  2.  
  3.  
  4. import java.util.*;
  5. class Product implements Comparable
  6.  
  7. {
  8.    private String name;     // class variable that stores the item name
  9.    private long number;      // class variable that stores the item number
  10.    private long stockQuantity;   // class variable that stores the quantity in stock
  11.    private double price;      // class variable that stores the item price
  12.  
  13.    public Product() // Constructor for the Product class
  14.    {
  15.       name = "";
  16.       number = 0L;
  17.       stockQuantity = 0L;
  18.       price = 0.0;
  19.    }
  20.    public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
  21.       {
  22.       this.name = name;
  23.       this.number = number;
  24.       this.stockQuantity = stockQuantity;
  25.       this.price = price;
  26.          }
  27.  
  28.    public void setItemName(String name)  // Method to set the item name
  29.    {
  30.       this.name = name;
  31.    }
  32.    public String getItemName()  // Method to get the item name
  33.    {
  34.       return name;
  35.    }
  36.  
  37.  
  38.    public void setItemNumber(long number)  // Method to set the item number
  39.    {
  40.       this.number = number;
  41.    }
  42.    public long getItemNumber()  // Method to get the item number
  43.    {
  44.       return number;
  45.    }
  46.  
  47.  
  48.    public void setStockQuantity(long quantity)  // Method to set the quantity in stock
  49.    {
  50.       stockQuantity = quantity;
  51.    }
  52.    public long getStockQuantity()  // Method to get the quantity in stock
  53.    {
  54.       return stockQuantity;
  55.    }
  56.  
  57.  
  58.    public void setItemPrice(double price)  // Method to set the item price
  59.    {
  60.       this.price = price;
  61.    }
  62.    public double getItemPrice()  // Method to get the item price
  63.    {
  64.       return price;
  65.    }
  66.  
  67.  
  68.    public double calculateInventoryValue()  // Method to calculate the value of the inventory
  69.    {
  70.       return price * stockQuantity;
  71.    }
  72.    public int compareTo (Object o)
  73.    {
  74.  
  75.  
  76.    Product p = (Product)o;
  77.       return name.compareTo(p.getItemName());
  78.    }
  79.  
  80.    public String toString()
  81.    {
  82.       return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
  83.    }
  84.  
  85.  
  86. }//end class Product
  87.  
  88. public class Inventory2
  89. {
  90.    // main methods begins execution of java application
  91.    public static void main( String args[])
  92.    {
  93.  
  94.    Product[] supplies = new Product[3];
  95.  
  96.    Product p1 = new Product("cDVD3", 1, 11, 10.99);
  97.    Product p2 = new Product("aDVD2", 2, 22, 20.99);
  98.    Product p3 = new Product("bDVD1", 3, 33, 30.99);
  99.  
  100.    supplies[0] = p1;
  101.    supplies[1] = p2;
  102.    supplies[2] = p3;
  103.  
  104.  //  product temp[] supplies = new product[1];
  105.  
  106.  
  107.  
  108. //OLD CODE-------------------------------------
  109.  //Arrays.sort(supplies);
  110. //
  111. //
  112.  public void sortByName() {
  113.  }
  114.  public String toString() {
  115.   String s = "";
  116.   for(Product p : supplies) {
  117.    s = s + p.toString();
  118.    s = s + "\n\n";
  119.   }
  120.   return s;
  121.  }
  122.  public void addProduct(Product p1) {
  123.   Product[] p = supplies;
  124.   Product[] temp = new Product[p.length + 1];
  125.   for(int i = 0; i < p.length; i++) {
  126.    temp[i] = p[i];
  127.   }
  128.   temp[(temp.length - 1)] = p1;
  129.   supplies = temp;
  130.  }
  131.  
  132. //
  133. //
  134.  
  135.  
  136. //END OLD CODE------------------------------------
  137.  
  138.  
  139.     //sorting the array using Bubble Sort
  140.    double total = 0.0;
  141.  
  142.    for(int i= 0; i < 3;i++)
  143.    {
  144.    total = total + supplies[i].calculateInventoryValue();
  145.    }
  146.  
  147.  
  148.  
  149.       for(Product p: supplies)
  150.       {
  151.       System.out.println(p);
  152.       System.out.println();
  153.     }
  154.  
  155.  
  156.  
  157.     for(Product p: supplies)
  158.     {
  159.     System.out.println(p);
  160.     System.out.println();
  161.     }
  162.  
  163.  
  164.    System.out.println("Total Value is: $"+total);
  165.  
  166.  
  167.    } // end main method
  168. }//end class Inventory2
  169.  
  170.  
Jan 25 '07 #20
Were you able to get the sorting down yet Jennifer? I just don't understand the concept of how to code in a sort. I have looked at numerous examples, but it just doesnt' seem to click
Jan 26 '07 #21
A little bit. The code from earlier in this post was a good example. I am stuck though as to why I can't my current code to compile.
Jan 26 '07 #22
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. //Inventory2.java
  2.  
  3.  
  4. import java.util.*;
  5. class Product implements Comparable {
  6.     private String name;     // class variable that stores the item name
  7.     private long number;      // class variable that stores the item number
  8.     private long stockQuantity;   // class variable that stores the quantity in stock
  9.     private double price;      // class variable that stores the item price
  10.  
  11.     public Product() {
  12.         name = "";
  13.         number = 0L;
  14.         stockQuantity = 0L;
  15.         price = 0.0;
  16.     }
  17.  
  18.     public Product(String name, long number, long stockQuantity, double price) {
  19.         this.name = name;
  20.         this.number = number;
  21.         this.stockQuantity = stockQuantity;
  22.         this.price = price;
  23.        }
  24.  
  25.        public void setItemName(String name) {
  26.         this.name = name;
  27.     }
  28.  
  29.     public String getItemName() {
  30.         return name;
  31.     }
  32.  
  33.     public void setItemNumber(long number) {
  34.         this.number = number;
  35.     }
  36.  
  37.     public long getItemNumber() {
  38.         return number;
  39.     }
  40.  
  41.     public void setStockQuantity(long quantity) {
  42.         stockQuantity = quantity;
  43.     }
  44.  
  45.     public long getStockQuantity() {
  46.         return stockQuantity;
  47.     }
  48.  
  49.     public void setItemPrice(double price) {
  50.         this.price = price;
  51.     }
  52.  
  53.     public double getItemPrice() {
  54.         return price;
  55.     }
  56.  
  57.     public double calculateInventoryValue() {
  58.         return price * stockQuantity;
  59.     }
  60.  
  61.     public int compareTo (Object o) {
  62.         Product p = null;
  63.         try {
  64.             p = (Product)o;
  65.         }
  66.         catch(ClassCastException cE) {
  67.             cE.printStackTrace();
  68.         }
  69.         return name.compareTo(p.getItemName());
  70.     }
  71.  
  72.     public String toString() {
  73.         return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
  74.     }
  75. }
  76.  
  77. public class Inventory2 {
  78.     Product[] supplies;
  79.  
  80.     public static void main(String[] args) {
  81.         Inventory2 inventory = new Inventory2();
  82.         inventory.addProduct(new Product("cDVD3", 1, 11, 10.99));
  83.         inventory.addProduct(new Product("aDVD2", 2, 22, 20.99));
  84.         inventory.addProduct(new Product("bDVD1", 3, 33, 30.99));
  85.  
  86.         double total = inventory.calculateTotalInventory();
  87.  
  88.         System.out.println("Total Value is: $"+total);
  89.  
  90.         System.out.println("*****Before Sorting******");
  91.         inventory.showInventory();
  92.         inventory.sortByName();
  93.         System.out.println("*****After Sorting By Name******");
  94.         inventory.showInventory();
  95.  
  96.     }
  97.  
  98.     public void sortByName() {
  99.         for (int i = 1; i < supplies.length; i++) {
  100.             int j;
  101.             Product val = supplies[i];
  102.             for (j = i-1; j > -1; j--) {
  103.                 Product temp = supplies[j];
  104.                 if (temp.compareTo(val) <= 0) {
  105.                     break;
  106.                 }
  107.                 supplies[j+1] = temp;
  108.             }
  109.             supplies[j+1] = val;
  110.         }
  111.     }
  112.  
  113.     //creates a String representation of the array of products
  114.      public String toString() {
  115.         String s = "";
  116.           for(Product p : supplies) {
  117.                s = s + p.toString();
  118.                s = s + "\n\n";
  119.           }
  120.           return s;
  121.     }
  122.  
  123.     //Using an array so adding an item requires us to increase the size of the array first
  124.     public void addProduct(Product p1) {
  125.         if(supplies == null) {
  126.             supplies = new Product[0];
  127.         }
  128.         Product[] p = supplies; //Copy all products into p first
  129.            Product[] temp = new Product[p.length + 1]; //create bigger array
  130.          for(int i = 0; i < p.length; i++) {
  131.             temp[i] = p[i];
  132.            }
  133.            temp[(temp.length - 1)] = p1; //add the new product at the last position
  134.          supplies = temp;
  135.     }
  136.  
  137.     //sorting the array using Bubble Sort
  138.     public double calculateTotalInventory () {
  139.         double total = 0.0;
  140.            for(int i = 0; i < supplies.length;i++) {
  141.             total = total + supplies[i].calculateInventoryValue();
  142.         }
  143.         return total;
  144.     }
  145.  
  146.     public void showInventory () {
  147.         System.out.println(toString()); //call our toString method
  148.     }
  149. }
You really should ask about anything at all that you are unsure about here.
Jan 26 '07 #23
Ok, I understand the beginning of the code but here is where I start to get confused:
Expand|Select|Wrap|Line Numbers
  1. public int compareTo (Object o) {
Then I get confused when calling the sort in the main app. I guess everything just seems confusing because our text doesn't cover any of this at all. I will try to read the code thourghouly today to see if I can make any sense of it. Thanks for all the great help, you really know your stuff. By the way, how long did it take you to become good with Java?
Jan 26 '07 #24
r035198x
13,262 8TB
Ok, I understand the beginning of the code but here is where I start to get confused:
Expand|Select|Wrap|Line Numbers
  1. public int compareTo (Object o) {
Then I get confused when calling the sort in the main app. I guess everything just seems confusing because our text doesn't cover any of this at all. I will try to read the code thourghouly today to see if I can make any sense of it. Thanks for all the great help, you really know your stuff. By the way, how long did it take you to become good with Java?
I'm not yet good. I've known Java for about 2,5 yrs now.

Now for that compareTo line:

When we have integers or any other primitive types, comparing then is pretty straight forward. Just if a == b or if a > b will do just fine.
Now we have products of the Product class which is made up of name, code, quantity e.t.c. So what do we mean when we say if(a == b) where a and b are products?
Do we mean if they have the same name or the same code or both? That is the point of the compareTo method. It tells us how to compare our products. In the churches example I put two compareTo methods for comparing the objects differently.

Now we can compare the products using the compareTo method. This helps us in sorting the products because then we need to know if a product comes before or after a given product. Do you understand this?
Jan 26 '07 #25
Ok, I understand what you are saying, and I understand the reason for, but I just don't understand the actual code. It's hard to keep all the code straight, then on top of that make sure it compiles. Thanks again for the replies, I'm sure I will have more questions for this Sunday's assignment. The part about adding a subclass, then adding the restocking fee.
Jan 26 '07 #26
Thanks r035198x! That makes a lot more sense now and I see what I did wrong with the sort. Now I have to modify the program again.
CheckPoint: Inventory Program Part 3

Resources Required

Chapters 9 & 10 in Java: How to Program

• Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.

• Modify the output to display this additional feature you have chosen and the restocking fee.

Compile and run the Java program.

I'm not sure where I can add the subclass. I know I could easily add another line of data in the array to include DVD rating for example, but it sounds like that's not what they want. And the 5% should be added by taking the price of the DVD and multiplying by.05 to get the restock fee. This I should be able to add to the current array, correct?

Really, thank you so much for all your help - I really appreciate it! :)
Jan 27 '07 #27
Here is some code I'm trying to understand:
Expand|Select|Wrap|Line Numbers
  1. //Inventory3
  2.  
  3.  
  4. // the DVD Class
  5.  
  6. class DVD extends Inventory {
  7.  
  8.     String genre;                    // Genre of the DVD
  9.     double restockingFee;            // percentage that is added to the base price as a restocking fee
  10.                                     //( actual inventory value will be the base inventory value plus
  11.                                     // the base inventory value times this amount ) (value + (value * restockFee))
  12.  
  13.     public DVD(String genre, double restockingFee, String Item_Number, String Item_Name, int Items_in_Stock,
  14.             double Item_Price) {
  15.         super(Item_Number, Item_Name, Items_in_Stock, Item_Price);
  16.         this.genre = genre;
  17.         this.restockingFee = restockingFee;
  18.  
  19.  
  20.         // TODO Auto-generated constructor stub
  21.     }
  22.  
  23.     //returns the value of the inventory, plus the restocking fee
  24.     public double getInventoryValue() {
  25.         // TODO Auto-generated method stub
  26.         return  super.getInventoryValue() + (super.getInventoryValue() * restockingFee);
  27.     }
  28.  
  29.     public String toString()
  30.     {
  31.         StringBuffer sb = new StringBuffer("Genre      \t").append(genre).append("\n");
  32.         sb.append(super.toString());
  33.  
  34.         return sb.toString();
  35.     }
  36.  
  37. }
  38.  
  39. //
  40. //
  41. //
  42.  
  43. //The Inventory class
  44.  
  45. public class Inventory {
  46.  
  47.     String productnumber;
  48.  
  49.     String name;
  50.  
  51.     int numberofunits;
  52.  
  53.     double priceperunit;
  54.  
  55.     // Create a new instance of Inventory
  56.     // main constructor for the class
  57.     public Inventory(String Item_Number, String Item_Name, int Items_in_Stock,
  58.             double Item_Price) {
  59.         productnumber = Item_Number;
  60.         name = Item_Name;
  61.         numberofunits = Items_in_Stock;
  62.         priceperunit = Item_Price;
  63.     }
  64.  
  65.     public void setItemName(String Item_Name)
  66.     // sets the items name
  67.     {
  68.         name = Item_Name;
  69.     }
  70.  
  71.     public void setItemNumber(String Item_Number) { // Sets the Product =Number
  72.                                                     // for the item
  73.  
  74.         productnumber = Item_Number;
  75.     }
  76.  
  77.     public void setItemsInStock(int Items_in_Stock) { // sets the =number of
  78.                                                         // units in stock
  79.  
  80.         numberofunits = Items_in_Stock;
  81.     }
  82.  
  83.     public void setItemPrice(double Item_Price) { // sets the price of =the
  84.                                                     // item
  85.         priceperunit = Item_Price;
  86.     }
  87.  
  88.     public String getItemName() { // returns the Product Name of this item
  89.         return name;
  90.     }
  91.  
  92.     public String getItemNumber() { // returns the Product Number of the =item
  93.  
  94.         return productnumber;
  95.     }
  96.  
  97.     public int getItemsInStock() { // returns how many units are in stock
  98.         return numberofunits;
  99.     }
  100.  
  101.     public double getItemPrice() { // returns the price of the item
  102.         return priceperunit;
  103.     }
  104.  
  105.     public double getInventoryValue() { // returns the total value of =the stock
  106.                                         // for this item
  107.         return priceperunit * numberofunits;
  108.     }
  109.  
  110.     public static double getTotalValueOfAllInventory(Inventory [] inv)
  111.     {
  112.         double tot = 0.0;
  113.  
  114.         for(int i = 0; i < inv.length; i++)
  115.         {
  116.             tot += inv[i].getInventoryValue();
  117.         }
  118.         return tot;
  119.  
  120.     }
  121.  
  122.  
  123.  
  124.     public String toString()
  125.     {
  126.         StringBuffer sb = new StringBuffer();
  127.  
  128.         sb.append("DVD Title:      \t").append(name).append("\n");
  129.         sb.append("Item #:           \t").append(productnumber).append("\n");
  130.         sb.append("Number in stock:\t").append(numberofunits).append("\n");
  131.         sb.append("Price:          \t").append(String.format("$%.2f%n", priceperunit));
  132.         sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryValue()));
  133.  
  134.         return sb.toString();
  135.     }
  136.  
  137.  
  138. }
  139.  
  140. //
  141. //
  142. //
  143.  
  144. //
  145. //
  146. //The InvTest Class
  147. class InvTest
  148.  
  149. {
  150.  
  151.     public static void main(String args[])
  152.  
  153.     {
  154.  
  155.         double restockFee = 0.05;
  156.         // initialize a new inventory object
  157.         DVD[] inventory = new DVD[4];
  158.  
  159.         inventory[0] = new DVD("Action" ,restockFee, "00623","aDVD1" , 10, 20.00);
  160.         inventory[1] = new DVD("Action" ,restockFee, "00564","dDVD2" , 10,  12.00);
  161.         inventory[2] = new DVD("Comedy",restockFee, "00222","cDVD3", 10,  15.00);
  162.         inventory[3] = new DVD("Family",restockFee,"00153","bDVD4" , 10, 18.00);
  163.  
  164.  
  165.         DVD temp[] = new DVD[1];
  166.  
  167. //        sorting the array using Bubble Sort
  168.         for(int j = 0; j < inventory.length - 1; j++)
  169.         {
  170.  
  171.             for(int k = 0; k < inventory.length - 1; k++)
  172.             {
  173.  
  174.                 if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
  175.                 {
  176.  
  177.                     temp[0] = inventory[k];
  178.                     inventory[k] = inventory[k+1];
  179.                     inventory[k+1] = temp[0];
  180.  
  181.                 }//end if
  182.  
  183.             }//end for loop
  184.  
  185.         }//end for loop
  186.  
  187.  
  188.         // print the inventory information
  189.         for(int j = 0; j < inventory.length; j++)
  190.         {
  191.             System.out.println(inventory[j].toString());
  192.         }
  193.  
  194.  
  195.         System.out.printf("Total value of all inventory = $%.2f" , Inventory.getTotalValueOfAllInventory(inventory));
  196.         return;
  197.  
  198.     }
  199. }
  200.  
It works when the 3 classes - DVD, Inventory, InvTest are in separate java files and declared as public, but it doesn't work when I try to combine them and only declare Inventory as public. And it only works when I run the InvTest.java application. It doesn't work if I try to run all 3 separate. I wanted to combine them as I think they should be all in 1 java file to compile and run. Does that make sense?
Jan 27 '07 #28
Ahh, I did it again! I just answered my own question. If I declare the InvTest as public and save it as InvTest.java it works. OK, let me analyze and apply it to the program I'm working on.

But can anyone answer why it can't be Inventory.java if the Inventory class is declared as public?
Jan 27 '07 #29
I believe the reason is because you can only have one main class, which is why you declare it public static void main. Let's keep this thread going Jennifer, I'm sure we will really need help for the next assignments, especially with the GUI part of the program.
Jan 27 '07 #30
Man, I just can't figure out how to get the subclass to work. I tried to follow your code, but I can't implement it into my code.
Jan 28 '07 #31
r035198x
13,262 8TB
Here is some code I'm trying to understand:
Expand|Select|Wrap|Line Numbers
  1. //Inventory3
  2.  
  3.  
  4. // the DVD Class
  5.  
  6. class DVD extends Inventory {
  7.  
  8.     String genre;                    // Genre of the DVD
  9.     double restockingFee;            // percentage that is added to the base price as a restocking fee
  10.                                     //( actual inventory value will be the base inventory value plus
  11.                                     // the base inventory value times this amount ) (value + (value * restockFee))
  12.  
  13.     public DVD(String genre, double restockingFee, String Item_Number, String Item_Name, int Items_in_Stock,
  14.             double Item_Price) {
  15.         super(Item_Number, Item_Name, Items_in_Stock, Item_Price);
  16.         this.genre = genre;
  17.         this.restockingFee = restockingFee;
  18.  
  19.  
  20.         // TODO Auto-generated constructor stub
  21.     }
  22.  
  23.     //returns the value of the inventory, plus the restocking fee
  24.     public double getInventoryValue() {
  25.         // TODO Auto-generated method stub
  26.         return  super.getInventoryValue() + (super.getInventoryValue() * restockingFee);
  27.     }
  28.  
  29.     public String toString()
  30.     {
  31.         StringBuffer sb = new StringBuffer("Genre      \t").append(genre).append("\n");
  32.         sb.append(super.toString());
  33.  
  34.         return sb.toString();
  35.     }
  36.  
  37. }
  38.  
  39. //
  40. //
  41. //
  42.  
  43. //The Inventory class
  44.  
  45. public class Inventory {
  46.  
  47.     String productnumber;
  48.  
  49.     String name;
  50.  
  51.     int numberofunits;
  52.  
  53.     double priceperunit;
  54.  
  55.     // Create a new instance of Inventory
  56.     // main constructor for the class
  57.     public Inventory(String Item_Number, String Item_Name, int Items_in_Stock,
  58.             double Item_Price) {
  59.         productnumber = Item_Number;
  60.         name = Item_Name;
  61.         numberofunits = Items_in_Stock;
  62.         priceperunit = Item_Price;
  63.     }
  64.  
  65.     public void setItemName(String Item_Name)
  66.     // sets the items name
  67.     {
  68.         name = Item_Name;
  69.     }
  70.  
  71.     public void setItemNumber(String Item_Number) { // Sets the Product =Number
  72.                                                     // for the item
  73.  
  74.         productnumber = Item_Number;
  75.     }
  76.  
  77.     public void setItemsInStock(int Items_in_Stock) { // sets the =number of
  78.                                                         // units in stock
  79.  
  80.         numberofunits = Items_in_Stock;
  81.     }
  82.  
  83.     public void setItemPrice(double Item_Price) { // sets the price of =the
  84.                                                     // item
  85.         priceperunit = Item_Price;
  86.     }
  87.  
  88.     public String getItemName() { // returns the Product Name of this item
  89.         return name;
  90.     }
  91.  
  92.     public String getItemNumber() { // returns the Product Number of the =item
  93.  
  94.         return productnumber;
  95.     }
  96.  
  97.     public int getItemsInStock() { // returns how many units are in stock
  98.         return numberofunits;
  99.     }
  100.  
  101.     public double getItemPrice() { // returns the price of the item
  102.         return priceperunit;
  103.     }
  104.  
  105.     public double getInventoryValue() { // returns the total value of =the stock
  106.                                         // for this item
  107.         return priceperunit * numberofunits;
  108.     }
  109.  
  110.     public static double getTotalValueOfAllInventory(Inventory [] inv)
  111.     {
  112.         double tot = 0.0;
  113.  
  114.         for(int i = 0; i < inv.length; i++)
  115.         {
  116.             tot += inv[i].getInventoryValue();
  117.         }
  118.         return tot;
  119.  
  120.     }
  121.  
  122.  
  123.  
  124.     public String toString()
  125.     {
  126.         StringBuffer sb = new StringBuffer();
  127.  
  128.         sb.append("DVD Title:      \t").append(name).append("\n");
  129.         sb.append("Item #:           \t").append(productnumber).append("\n");
  130.         sb.append("Number in stock:\t").append(numberofunits).append("\n");
  131.         sb.append("Price:          \t").append(String.format("$%.2f%n", priceperunit));
  132.         sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryValue()));
  133.  
  134.         return sb.toString();
  135.     }
  136.  
  137.  
  138. }
  139.  
  140. //
  141. //
  142. //
  143.  
  144. //
  145. //
  146. //The InvTest Class
  147. class InvTest
  148.  
  149. {
  150.  
  151.     public static void main(String args[])
  152.  
  153.     {
  154.  
  155.         double restockFee = 0.05;
  156.         // initialize a new inventory object
  157.         DVD[] inventory = new DVD[4];
  158.  
  159.         inventory[0] = new DVD("Action" ,restockFee, "00623","aDVD1" , 10, 20.00);
  160.         inventory[1] = new DVD("Action" ,restockFee, "00564","dDVD2" , 10,  12.00);
  161.         inventory[2] = new DVD("Comedy",restockFee, "00222","cDVD3", 10,  15.00);
  162.         inventory[3] = new DVD("Family",restockFee,"00153","bDVD4" , 10, 18.00);
  163.  
  164.  
  165.         DVD temp[] = new DVD[1];
  166.  
  167. //        sorting the array using Bubble Sort
  168.         for(int j = 0; j < inventory.length - 1; j++)
  169.         {
  170.  
  171.             for(int k = 0; k < inventory.length - 1; k++)
  172.             {
  173.  
  174.                 if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
  175.                 {
  176.  
  177.                     temp[0] = inventory[k];
  178.                     inventory[k] = inventory[k+1];
  179.                     inventory[k+1] = temp[0];
  180.  
  181.                 }//end if
  182.  
  183.             }//end for loop
  184.  
  185.         }//end for loop
  186.  
  187.  
  188.         // print the inventory information
  189.         for(int j = 0; j < inventory.length; j++)
  190.         {
  191.             System.out.println(inventory[j].toString());
  192.         }
  193.  
  194.  
  195.         System.out.printf("Total value of all inventory = $%.2f" , Inventory.getTotalValueOfAllInventory(inventory));
  196.         return;
  197.  
  198.     }
  199. }
  200.  
It works when the 3 classes - DVD, Inventory, InvTest are in separate java files and declared as public, but it doesn't work when I try to combine them and only declare Inventory as public. And it only works when I run the InvTest.java application. It doesn't work if I try to run all 3 separate. I wanted to combine them as I think they should be all in 1 java file to compile and run. Does that make sense?
Guys sorry I'd been away. Not sure for long I'll be in. About compiling and running classes,
You are right only one public class is allowed per java file.
When running the programs you can only run a class that has the main method.
If the class uses other classes for compilation, make sure you compile those classes first.
Jan 29 '07 #32
Hey, no problem at all. I really want to thank you r035198x, you have been a tremendous help. I think I almost have my code right, I just need to figure out how to get it to show the genre and the restocking fee in the main app. Here is what I have if you want to take a look:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. class DVD extends Product
  4. {
  5.  
  6.    String genre;
  7.    double restockingFee;
  8.  
  9.  
  10.    public DVD(String genre, double restockingFee, int item, String title, double stockQuantity,
  11.               double price)
  12.    {
  13.       super(title,item,  stockQuantity, price);
  14.       this.genre = genre;
  15.       this.restockingFee = restockingFee;
  16.  
  17.       // TODO Auto-generated constructor stub
  18.    }
  19.  
  20.    //returns the value of the inventory, plus the restocking fee
  21.    public double getInventoryValue()
  22.    {
  23.       // TODO Auto-generated method stub
  24.       return super.getItemPrice() + restockingFee;
  25.    }
  26.  
  27.    public String toString()
  28.    {
  29.       StringBuffer sb = new StringBuffer("Genre      \t").append(genre).append("\n");
  30.       sb.append(super.toString());
  31.  
  32.       return sb.toString();
  33.    }
  34.  
  35. }
  36.  
  37. //Inventory2.java
  38.  
  39.  
  40.  
  41. class Product implements Comparable
  42. {
  43.    private String title;   // class variable that stores the item name
  44.    private int item;     // class variable that stores the item number
  45.    private double stockQuantity;   // class variable that stores the quantity in stock
  46.    private double price;     // class variable that stores the item price
  47.  
  48.    public Product()
  49.    {
  50.       title = "";
  51.       item = 0;
  52.       stockQuantity = 0;
  53.       price = 0.0;
  54.    }
  55.  
  56.    public Product(String title, int item, double stockQuantity, double price)
  57.    {
  58.       this.title = title;
  59.       this.item = item;
  60.       this.stockQuantity = stockQuantity;
  61.       this.price = price;
  62.    }
  63.  
  64.    public void setTitle(String title)
  65.    {
  66.       this.title = title;
  67.    }
  68.  
  69.    public String getTitle()
  70.    {
  71.       return title;
  72.    }
  73.  
  74.    public void setItem(int item)
  75.    {
  76.       this.item = item;
  77.    }
  78.  
  79.    public int getItem()
  80.    {
  81.       return item;
  82.    }
  83.  
  84.    public void setStockQuantity(double quantity)
  85.    {
  86.       stockQuantity = quantity;
  87.    }
  88.  
  89.    public double getStockQuantity()
  90.    {
  91.       return stockQuantity;
  92.    }
  93.  
  94.    public void setItemPrice(double price)
  95.    {
  96.       this.price = price;
  97.    }
  98.  
  99.    public double getItemPrice()
  100.    {
  101.       return price;
  102.    }
  103.  
  104.    public double calculateInventoryValue()
  105.    {
  106.       return price * stockQuantity;
  107.    }
  108.  
  109.    public int compareTo(Object o)
  110.    {
  111.       Product p = null;
  112.       try
  113.       {
  114.          p = (Product) o;
  115.       }
  116.       catch (ClassCastException cE)
  117.       {
  118.          cE.printStackTrace();
  119.       }
  120.       return title.compareTo(p.getTitle());
  121.    }
  122.  
  123.    public String toString()
  124.    {
  125.       return "Genre" +genre+"\nDVD Title: " + title + "\nItem #: " + item + "\nPrice: $" + price + "\nQuantity: "
  126.       + stockQuantity + "\nValue: $" + calculateInventoryValue();
  127.    }
  128. }
  129.  
  130. public class Inventory2
  131. {
  132.    Product[] supplies;
  133.  
  134.    public static void main(String[] args)
  135.    {
  136.       Inventory2 inventory = new Inventory2();
  137.       inventory.addProduct(new Product("Beerfest", 1, 15, 22.95));
  138.       inventory.addProduct(new Product("Illustionist", 2, 25, 25.95));
  139.       inventory.addProduct(new Product("Employee of the Month", 3, 33, 20.95));
  140.  
  141.       System.out.println("Inventory of DVD Movies:\n\n");
  142.  
  143.  
  144.       System.out.println();
  145.       inventory.showInventory();
  146.       inventory.sortByName();
  147.       System.out.println();
  148.       inventory.showInventory();
  149.  
  150.       double total = inventory.calculateTotalInventory();
  151.       System.out.println("Total Value is: $" + total);
  152.  
  153.  
  154.    }
  155.  
  156.    public void sortByName()
  157.    {
  158.       for (int i = 1; i < supplies.length; i++)
  159.       {
  160.          int j;
  161.          Product val = supplies[i];
  162.          for (j = i - 1; j > -1; j--)
  163.          {
  164.             Product temp = supplies[j];
  165.             if (temp.compareTo(val) <= 0)
  166.             {
  167.                break;
  168.             }
  169.             supplies[j + 1] = temp;
  170.          }
  171.          supplies[j + 1] = val;
  172.       }
  173.    }
  174.  
  175.    //creates a String representation of the array of products
  176.    public String toString()
  177.    {
  178.       String s = "";
  179.       for (Product p : supplies)
  180.       {
  181.          s = s + p.toString();
  182.          s = s + "\n\n";
  183.       }
  184.       return s;
  185.    }
  186.  
  187.    //Using an array so adding an item requires us to increase the size of the array first
  188.    public void addProduct(Product p1)
  189.    {
  190.       if (supplies == null)
  191.       {
  192.          supplies = new Product[0];
  193.       }
  194.       Product[] p = supplies; //Copy all products into p first
  195.       Product[] temp = new Product[p.length + 1]; //create bigger array
  196.       for (int i = 0; i < p.length; i++)
  197.       {
  198.          temp[i] = p[i];
  199.       }
  200.       temp[(temp.length - 1)] = p1; //add the new product at the last position
  201.       supplies = temp;
  202.    }
  203.  
  204.    //sorting the array using Bubble Sort
  205.    public double calculateTotalInventory()
  206.    {
  207.       double total = 0.0;
  208.       for (int i = 0; i < supplies.length; i++)
  209.       {
  210.          total = total + supplies[i].calculateInventoryValue();
  211.       }
  212.       return total;
  213.    }
  214.  
  215.    public void showInventory()
  216.    {
  217.       System.out.println(toString()); //call our toString method
  218.    }
  219. }
  220.  
Jan 29 '07 #33
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. class DVD extends Product
  2. {
  3.  
  4.    String genre;
  5.    double restockingFee;
  6.  
  7.  
  8.    public DVD(String title, int item, double stockQuantity, double price, String genre)
  9.    {
  10.       super(title,item,  stockQuantity, price);
  11.       this.genre = genre;
  12.       this.restockingFee = 0.05;
  13.  
  14.       // TODO Auto-generated constructor stub
  15.    }
  16.  
  17.    //returns the value of the inventory, plus the restocking fee
  18.    public double getInventoryValue()
  19.    {
  20.       // TODO Auto-generated method stub
  21.       return super.getItemPrice() + restockingFee;
  22.    }
  23.  
  24.    public String toString()
  25.    {
  26.       //this is not efficient
  27.       //StringBuffer sb = new StringBuffer("Genre      \t").append(genre).append("\n");
  28.       //sb.append(super.toString());
  29.  
  30.       return super.toString() + "\nGenre      \t" + genre;
  31.    }
  32.  
  33. }
  34.  
  35. //Inventory2.java
  36.  
  37.  
  38.  
  39. class Product implements Comparable
  40. {
  41.    private String title;   // class variable that stores the item name
  42.    private int item;     // class variable that stores the item number
  43.    private double stockQuantity;   // class variable that stores the quantity in stock
  44.    private double price;     // class variable that stores the item price
  45.  
  46.    public Product()
  47.    {
  48.       title = "";
  49.       item = 0;
  50.       stockQuantity = 0;
  51.       price = 0.0;
  52.    }
  53.  
  54.    public Product(String title, int item, double stockQuantity, double price)
  55.    {
  56.       this.title = title;
  57.       this.item = item;
  58.       this.stockQuantity = stockQuantity;
  59.       this.price = price;
  60.    }
  61.  
  62.    public void setTitle(String title)
  63.    {
  64.       this.title = title;
  65.    }
  66.  
  67.    public String getTitle()
  68.    {
  69.       return title;
  70.    }
  71.  
  72.    public void setItem(int item)
  73.    {
  74.       this.item = item;
  75.    }
  76.  
  77.    public int getItem()
  78.    {
  79.       return item;
  80.    }
  81.  
  82.    public void setStockQuantity(double quantity)
  83.    {
  84.       stockQuantity = quantity;
  85.    }
  86.  
  87.    public double getStockQuantity()
  88.    {
  89.       return stockQuantity;
  90.    }
  91.  
  92.    public void setItemPrice(double price)
  93.    {
  94.       this.price = price;
  95.    }
  96.  
  97.    public double getItemPrice()
  98.    {
  99.       return price;
  100.    }
  101.  
  102.    public double calculateInventoryValue()
  103.    {
  104.       return price * stockQuantity;
  105.    }
  106.  
  107.    public int compareTo(Object o)
  108.    {
  109.       Product p = null;
  110.       try
  111.       {
  112.          p = (Product) o;
  113.       }
  114.       catch (ClassCastException cE)
  115.       {
  116.          cE.printStackTrace();
  117.       }
  118.       return title.compareTo(p.getTitle());
  119.    }
  120.  
  121.    public String toString()
  122.    {
  123.       return "DVD Title: " + title + "\nItem #: " + item + "\nPrice: $" + price + "\nQuantity: "
  124.       + stockQuantity + "\nValue: $" + calculateInventoryValue();
  125.    }
  126. }
  127.  
  128. public class Inventory2
  129. {
  130.    Product[] supplies;
  131.  
  132.    public static void main(String[] args)
  133.    {
  134.       Inventory2 inventory = new Inventory2();
  135.       inventory.addProduct(new DVD("Beerfest", 1, 15, 22.95, "Action"));
  136.       inventory.addProduct(new DVD("Illustionist", 2, 25, 25.95, "Comedy"));
  137.       inventory.addProduct(new DVD("Employee of the Month", 3, 33, 20.95, "Comedy"));
  138.  
  139.       System.out.println("Inventory of DVD Movies:\n\n");
  140.  
  141.  
  142.       System.out.println();
  143.       inventory.showInventory();
  144.       inventory.sortByName();
  145.       System.out.println();
  146.       inventory.showInventory();
  147.  
  148.       double total = inventory.calculateTotalInventory();
  149.       System.out.println("Total Value is: $" + total);
  150.  
  151.  
  152.    }
  153.  
  154.    public void sortByName()
  155.    {
  156.       for (int i = 1; i < supplies.length; i++)
  157.       {
  158.          int j;
  159.          Product val = supplies[i];
  160.          for (j = i - 1; j > -1; j--)
  161.          {
  162.             Product temp = supplies[j];
  163.             if (temp.compareTo(val) <= 0)
  164.             {
  165.                break;
  166.             }
  167.             supplies[j + 1] = temp;
  168.          }
  169.          supplies[j + 1] = val;
  170.       }
  171.    }
  172.  
  173.    //creates a String representation of the array of products
  174.    public String toString()
  175.    {
  176.       String s = "";
  177.       for (Product p : supplies)
  178.       {
  179.          s = s + p.toString();
  180.          s = s + "\n\n";
  181.       }
  182.       return s;
  183.    }
  184.  
  185.    //Using an array so adding an item requires us to increase the size of the array first
  186.    public void addProduct(Product p1)
  187.    {
  188.       if (supplies == null)
  189.       {
  190.          supplies = new Product[0];
  191.       }
  192.       Product[] p = supplies; //Copy all products into p first
  193.       Product[] temp = new Product[p.length + 1]; //create bigger array
  194.       for (int i = 0; i < p.length; i++)
  195.       {
  196.          temp[i] = p[i];
  197.       }
  198.       temp[(temp.length - 1)] = p1; //add the new product at the last position
  199.       supplies = temp;
  200.    }
  201.  
  202.    //sorting the array using Bubble Sort
  203.    public double calculateTotalInventory()
  204.    {
  205.       double total = 0.0;
  206.       for (int i = 0; i < supplies.length; i++)
  207.       {
  208.          total = total + supplies[i].calculateInventoryValue();
  209.       }
  210.       return total;
  211.    }
  212.  
  213.    public void showInventory()
  214.    {
  215.       System.out.println(toString()); //call our toString method
  216.    }
  217. }
The genre should be printed for DVDs not for products.
Remember all DVDs are products but not all products are DVDs therefore not all products have genre
Jan 29 '07 #34
I was able to get what I posted earlier to work with some tweaking. I did make a few mistakes like I didn't know how to add the calculation for the 5% so I manually added it in the array. The printout looked right, but the program had more than I needed I guess. Not a big deal as it only cost me a few points.

Now, on to the fun stuff - GUI! I haven't a clue where to start so it might be a day or so until I get something posted on here.
Jan 29 '07 #35
r035198x
13,262 8TB
I was able to get what I posted earlier to work with some tweaking. I did make a few mistakes like I didn't know how to add the calculation for the 5% so I manually added it in the array. The printout looked right, but the program had more than I needed I guess. Not a big deal as it only cost me a few points.

Now, on to the fun stuff - GUI! I haven't a clue where to start so it might be a day or so until I get something posted on here.
Yes GUI can be fun. Just post anytime you get stuck.
Jan 29 '07 #36
Man, how can we ever repay you r035198x. You have really made this Java class a little easier to accomplish. I feel bad for asking, but how do I get the main app to show the restocking fee? The program should look liek this:

DVD Title
Item #
Price
Quantity
Restocking Fee
Value
Genre

I just can't figure out how to get that in there. Thank you so much again.
Jan 29 '07 #37
Ok, I am now to add a GUI to this assignment. I have followed other posts with this assignment, but I can't get the Inventory code to compile. I get like 80 errors. Is there two classes to compile? Or is there something wrong with the version of Java that I have.
Feb 4 '07 #38
r035198x
13,262 8TB
Ok, I am now to add a GUI to this assignment. I have followed other posts with this assignment, but I can't get the Inventory code to compile. I get like 80 errors. Is there two classes to compile? Or is there something wrong with the version of Java that I have.
Not likely to be anything to do with your Java version just post the code that is giving errors and we'll see if we can help.
Feb 5 '07 #39

Sign in to post your reply or Sign up for a free account.

Similar topics

48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
5
by: Benne Smith | last post by:
Hi, I have three enviroments; a development, a testing and a production enviroment. I'm making a big application (.exe), which uses alot of different webservices. I don't use the webservices...
5
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function...
2
by: Charles | last post by:
I need to find a way to share information between two classes, one is an employee class and the other is a custom error class that inherits from ApplicationException. These two classes are part of...
7
by: moondaddy | last post by:
I want to create a public enum that can be used throughout a project. I created an enum like this in a module: Public Enum ParentType Project = 0 Stage = 1 VIP = 2 Func = 3 Equipment = 4...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
8
by: Brett Romero | last post by:
I have this situation: myEXE <needs< DerivedClass <which needs< BaseClass Meaning, myEXE is using a type defined in DerivedClass, which inherits from BaseClass. I include a reference to...
1
by: lxxslay3rxxl | last post by:
Let's say I haf 3 project that I wan to list, each project should have 1 value assign to it, but if i remove 1 of the value for 1 of the project, and i tried to list all 3 projects, I get this error:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.