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!! :)
38 4165
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.
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?
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
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!
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? -
-
//Inventory2.java
-
-
-
import java.util.*;
-
class Product implements Comparable
-
-
{
-
private String name; // class variable that stores the item name
-
private long number; // class variable that stores the item number
-
private long stockQuantity; // class variable that stores the quantity in stock
-
private double price; // class variable that stores the item price
-
-
public Product() // Constructor for the Product class
-
{
-
name = "";
-
number = 0L;
-
stockQuantity = 0L;
-
price = 0.0;
-
}
-
public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
-
{
-
this.name = name;
-
this.number = number;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
-
public void setItemName(String name) // Method to set the item name
-
{
-
this.name = name;
-
}
-
public String getItemName() // Method to get the item name
-
{
-
return name;
-
}
-
-
-
public void setItemNumber(long number) // Method to set the item number
-
{
-
this.number = number;
-
}
-
public long getItemNumber() // Method to get the item number
-
{
-
return number;
-
}
-
-
-
public void setStockQuantity(long quantity) // Method to set the quantity in stock
-
{
-
stockQuantity = quantity;
-
}
-
public long getStockQuantity() // Method to get the quantity in stock
-
{
-
return stockQuantity;
-
}
-
-
-
public void setItemPrice(double price) // Method to set the item price
-
{
-
this.price = price;
-
}
-
public double getItemPrice() // Method to get the item price
-
{
-
return price;
-
}
-
-
-
public double calculateInventoryValue() // Method to calculate the value of the inventory
-
{
-
return price * stockQuantity;
-
}
-
public int compareTo (Object o)
-
{
-
-
-
Product p = (Product)o;
-
return name.compareTo(p.getItemName());
-
}
-
-
public String toString()
-
{
-
return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
-
}
-
-
}//end class Product
-
-
public class Inventory2
-
{
-
// main methods begins execution of java application
-
public static void main( String args[])
-
{
-
-
Product[] supplies = new Product[3];
-
-
Product p1 = new Product("DVD1", 1, 11, 10.99);
-
Product p2 = new Product("DVD2", 2, 22, 20.99);
-
Product p3 = new Product("DVD3", 3, 33, 30.99);
-
-
supplies[0] = p1;
-
supplies[1] = p2;
-
supplies[2] = p3;
-
-
double total = 0.0;
-
-
for(int i= 0; i < 3;i++)
-
{
-
total = total + supplies[i].calculateInventoryValue();
-
}
-
-
System.out.println("Total Value is: $"+total);
-
-
Arrays.sort(supplies);
-
-
for(Product p: supplies)
-
{
-
System.out.println(p);
-
System.out.println();
-
}
-
-
} // end main method
-
}//end class Inventory2
-
-
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: -
-
//Inventory2.java
-
-
-
import java.util.*;
-
class Product implements Comparable
-
-
{
-
private String name; // class variable that stores the item name
-
private long number; // class variable that stores the item number
-
private long stockQuantity; // class variable that stores the quantity in stock
-
private double price; // class variable that stores the item price
-
-
public Product() // Constructor for the Product class
-
{
-
name = "";
-
number = 0L;
-
stockQuantity = 0L;
-
price = 0.0;
-
}
-
public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
-
{
-
this.name = name;
-
this.number = number;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
-
public void setItemName(String name) // Method to set the item name
-
{
-
this.name = name;
-
}
-
public String getItemName() // Method to get the item name
-
{
-
return name;
-
}
-
-
-
public void setItemNumber(long number) // Method to set the item number
-
{
-
this.number = number;
-
}
-
public long getItemNumber() // Method to get the item number
-
{
-
return number;
-
}
-
-
-
public void setStockQuantity(long quantity) // Method to set the quantity in stock
-
{
-
stockQuantity = quantity;
-
}
-
public long getStockQuantity() // Method to get the quantity in stock
-
{
-
return stockQuantity;
-
}
-
-
-
public void setItemPrice(double price) // Method to set the item price
-
{
-
this.price = price;
-
}
-
public double getItemPrice() // Method to get the item price
-
{
-
return price;
-
}
-
-
-
public double calculateInventoryValue() // Method to calculate the value of the inventory
-
{
-
return price * stockQuantity;
-
}
-
public int compareTo (Object o)
-
{
-
-
-
Product p = (Product)o;
-
return name.compareTo(p.getItemName());
-
}
-
-
public String toString()
-
{
-
return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
-
}
-
-
}//end class Product
-
-
public class Inventory2
-
{
-
// main methods begins execution of java application
-
public static void main( String args[])
-
{
-
-
Product[] supplies = new Product[3];
-
-
Product p1 = new Product("cDVD3", 1, 11, 10.99);
-
Product p2 = new Product("aDVD2", 2, 22, 20.99);
-
Product p3 = new Product("bDVD1", 3, 33, 30.99);
-
-
supplies[0] = p1;
-
supplies[1] = p2;
-
supplies[2] = p3;
-
-
// product temp[] supplies = new product[1];
-
-
double total = 0.0;
-
-
for(int i= 0; i < 3;i++)
-
{
-
total = total + supplies[i].calculateInventoryValue();
-
}
-
-
-
-
for(Product p: supplies)
-
{
-
System.out.println(p);
-
System.out.println();
-
}
-
-
Arrays.sort(supplies);
-
// sorting the array using Bubble Sort
-
// for(int j = 0; j < supplies.length - 1; j++)
-
// {
-
//
-
// for(int k = 0; k < supplies.length - 1; k++)
-
// {
-
//
-
// if(supplies[k].getItemName().compareToIgnoreCase(supplies[k+1].getItemName()) > 0)
-
// {
-
//
-
// temp[0] = supplies[k];
-
// supplies[k] = supplies[k+1];
-
// supplies[k+1] = temp[0];
-
//
-
// }//end if
-
//
-
// }//end for loop
-
//
-
// }//end for loop
-
-
-
-
-
-
for(Product p: supplies)
-
{
-
System.out.println(p);
-
System.out.println();
-
}
-
-
-
System.out.println("Total Value is: $"+total);
-
-
-
} // end main method
-
}//end class Inventory2
-
-
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....
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): -
swapped = false;
-
do
-
{
-
swapped = false;
-
for(int i=0; i<supplies.length; i++)
-
{
-
if(supplies[i].getItemName().compareToIgnoreCase( supplies[i+1].getItemName()) >0)
-
{
-
temp[0] = supplies[k];
-
supplies[k] = supplies[k+1];
-
supplies[k+1] = temp[0];
-
swapped = true;
-
}//end if
-
}//end for loop
-
} while(swapped == false);
-
-
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: -
//Inventory2.java
-
-
-
import java.util.*;
-
class Product implements Comparable
-
-
{
-
private String name; // class variable that stores the item name
-
private long number; // class variable that stores the item number
-
private long stockQuantity; // class variable that stores the quantity in stock
-
private double price; // class variable that stores the item price
-
-
public Product() // Constructor for the Product class
-
{
-
name = "";
-
number = 0L;
-
stockQuantity = 0L;
-
price = 0.0;
-
}
-
public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
-
{
-
this.name = name;
-
this.number = number;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
-
public void setItemName(String name) // Method to set the item name
-
{
-
this.name = name;
-
}
-
public String getItemName() // Method to get the item name
-
{
-
return name;
-
}
-
-
-
public void setItemNumber(long number) // Method to set the item number
-
{
-
this.number = number;
-
}
-
public long getItemNumber() // Method to get the item number
-
{
-
return number;
-
}
-
-
-
public void setStockQuantity(long quantity) // Method to set the quantity in stock
-
{
-
stockQuantity = quantity;
-
}
-
public long getStockQuantity() // Method to get the quantity in stock
-
{
-
return stockQuantity;
-
}
-
-
-
public void setItemPrice(double price) // Method to set the item price
-
{
-
this.price = price;
-
}
-
public double getItemPrice() // Method to get the item price
-
{
-
return price;
-
}
-
-
-
public double calculateInventoryValue() // Method to calculate the value of the inventory
-
{
-
return price * stockQuantity;
-
}
-
public int compareTo (Object o)
-
{
-
-
-
Product p = (Product)o;
-
return name.compareTo(p.getItemName());
-
}
-
-
public String toString()
-
{
-
return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
-
}
-
-
}//end class Product
-
-
public class Inventory2
-
{
-
// main methods begins execution of java application
-
public static void main( String args[])
-
{
-
-
Product[] supplies = new Product[3];
-
-
Product p1 = new Product("cDVD3", 1, 11, 10.99);
-
Product p2 = new Product("aDVD2", 2, 22, 20.99);
-
Product p3 = new Product("bDVD1", 3, 33, 30.99);
-
-
supplies[0] = p1;
-
supplies[1] = p2;
-
supplies[2] = p3;
-
-
// product temp[] supplies = new product[1];
-
-
double total = 0.0;
-
-
for(int i= 0; i < 3;i++)
-
{
-
total = total + supplies[i].calculateInventoryValue();
-
}
-
-
-
-
for(Product p: supplies)
-
{
-
System.out.println(p);
-
System.out.println();
-
}
-
//OLD CODE-------------------------------------
-
//Arrays.sort(supplies);
-
// sorting the array using Bubble Sort
-
// for(int j = 0; j < supplies.length - 1; j++)
-
// {
-
//
-
// for(int k = 0; k < supplies.length - 1; k++)
-
// {
-
//
-
// if(supplies[k].getItemName().compareToIgnoreCase(supplies[k+1].getItemName()) > 0)
-
// {
-
//
-
// temp[0] = supplies[k];
-
// supplies[k] = supplies[k+1];
-
// supplies[k+1] = temp[0];
-
//
-
// }//end if
-
//
-
// }//end for loop
-
//
-
// }//end for loop
-
//END OLD CODE------------------------------------
-
-
swapped = false;
-
do
-
{
-
swapped = false;
-
for(int i=0; i<supplies.length; i++)
-
{
-
if(supplies[i].getItemName().compareToIgnoreCase( supplies[i+1].getItemName()) >0)
-
{
-
temp[0] = supplies[k];
-
supplies[k] = supplies[k+1];
-
supplies[k+1] = temp[0];
-
swapped = true;
-
}//end if
-
}//end for loop
-
} while(swapped == false);
-
-
-
-
-
-
for(Product p: supplies)
-
{
-
System.out.println(p);
-
System.out.println();
-
}
-
-
-
System.out.println("Total Value is: $"+total);
-
-
-
} // end main method
-
}//end class Inventory2
-
-
Do the sorting in a seperate method. Here is an example program that you should go through and make sure you understand before proceeding - import java.util.*;
-
class Church {
-
private String name;
-
private String pastor;
-
public Church(String name, String pastor) {
-
this.name = name;
-
this.pastor = pastor;
-
}
-
public String getPastor() {
-
return pastor;
-
}
-
public String getName() {
-
return name;
-
}
-
public void setPastor(String pastor) {
-
this.pastor = pastor;
-
}
-
public String toString() {
-
return getName() + " is Pastored by "+getPastor();
-
}
-
public int compareByPastor(Church c) {
-
int x = pastor.compareTo(c.getPastor());
-
return x;
-
}
-
public int compareByName(Church c) {
-
int x = name.compareTo(c.getName());
-
return x;
-
}
-
}
-
-
class Churches {
-
private final List<Church> churches;
-
-
public Churches() {
-
churches = new ArrayList<Church>();
-
}
-
public void addWithoutSorting(Church c) {
-
churches.add(c);
-
}
-
-
//You could always add using this method
-
public void addWithSorting(Church c) {
-
-
}
-
public void display() {
-
for(int j = 0; j < churches.size(); j++) {
-
System.out.print(churches.get(j).toString());
-
System.out.println("");
-
}
-
}
-
public List<Church> getChurches() {
-
return churches;
-
}
-
public void sortBy(String s) {
-
for (int i = 1; i < churches.size(); i++) {
-
int j;
-
Church val = churches.get(i);
-
for (j = i-1; j > -1; j--) {
-
Church temp = churches.get(j);
-
if(s.equals("Pastor")) {
-
if (temp.compareByPastor(val) <= 0) {
-
break;
-
}
-
}
-
else if(s.equals("Name")) {
-
if (temp.compareByName(val) <= 0) {
-
break;
-
}
-
}
-
churches.set(j+1, temp);
-
}
-
churches.set(j+1, val);
-
}
-
}
-
-
public static void main(String[] args) {
-
Churches baptists = new Churches();
-
baptists.addWithoutSorting(new Church("Pac", "Pastor G"));
-
baptists.addWithoutSorting(new Church("New Life", "Tudor"));
-
baptists.addWithoutSorting(new Church("My Church", "r035198x"));
-
baptists.addWithoutSorting(new Church("AFM", "Cathy"));
-
System.out.println("**********************Before Sorting***********************");
-
baptists.display();
-
baptists.sortBy("Pastor");
-
System.out.println("**********************After sorting by Pastor**************");
-
baptists.display();
-
baptists.sortBy("Name");
-
System.out.println("**********************After sorting by Name****************");
-
baptists.display();
-
-
}
-
-
}
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.......
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?
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 - import java.util.*;
-
class Church {
-
private String name;
-
private String pastor;
-
public Church(String name, String pastor) {
-
this.name = name;
-
this.pastor = pastor;
-
}
-
public String getPastor() {
-
return pastor;
-
}
-
public String getName() {
-
return name;
-
}
-
public void setPastor(String pastor) {
-
this.pastor = pastor;
-
}
-
public String toString() {
-
return getName() + " is Pastored by "+getPastor();
-
}
-
public int compareByPastor(Church c) {
-
int x = pastor.compareTo(c.getPastor());
-
return x;
-
}
-
public int compareByName(Church c) {
-
int x = name.compareTo(c.getName());
-
return x;
-
}
-
}
-
-
class Churches2 {
-
private final List churches;
-
-
public Churches2() {
-
churches = new ArrayList<Church>();
-
}
-
public void addWithoutSorting(Church c) {
-
churches.add(c);
-
}
-
-
//You could always add using this method
-
public void addWithSorting(Church c) {
-
-
}
-
public void display() {
-
for(int j = 0; j < churches.size(); j++) {
-
System.out.print(churches.get(j).toString());
-
System.out.println("");
-
}
-
}
-
public List getChurches() {
-
return churches;
-
}
-
public void sortBy(String s) {
-
for (int i = 1; i < churches.size(); i++) {
-
int j;
-
Church val = (Church)churches.get(i);
-
for (j = i-1; j > -1; j--) {
-
Church temp = (Church)churches.get(j);
-
if(s.equals("Pastor")) {
-
if (temp.compareByPastor(val) <= 0) {
-
break;
-
}
-
}
-
else if(s.equals("Name")) {
-
if (temp.compareByName(val) <= 0) {
-
break;
-
}
-
}
-
churches.set(j+1, temp);
-
}
-
churches.set(j+1, val);
-
}
-
}
-
-
public static void main(String[] args) {
-
Churches baptists = new Churches();
-
baptists.addWithoutSorting(new Church("Pac", "Pastor G"));
-
baptists.addWithoutSorting(new Church("New Life", "Tudor"));
-
baptists.addWithoutSorting(new Church("My Church", "r035198x"));
-
baptists.addWithoutSorting(new Church("AFM", "Cathy"));
-
System.out.println("**********************Before Sorting***********************");
-
baptists.display();
-
baptists.sortBy("Pastor");
-
System.out.println("**********************After sorting by Pastor**************");
-
baptists.display();
-
baptists.sortBy("Name");
-
System.out.println("**********************After sorting by Name****************");
-
baptists.display();
-
-
}
-
-
}
You should use at least jdk1.5 these days
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.
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.
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: -
// Product class
-
-
-
public class Product {
-
-
private String title;
-
private double item;
-
private double stock;
-
private double price;
-
-
// Constructor to initialize the product information.
-
public Product(String title, double item, double stock,
-
double price) {
-
setTitle(title);
-
setItem(item);
-
setStock(stock);
-
setPrice(price);
-
-
}
-
-
// Method to get the title
-
public String getTitle() {
-
return title;
-
}
-
-
// Method to get the item number
-
public double getItem() {
-
return item;
-
}
-
-
//Method to get the stock
-
public double getStock() {
-
return stock;
-
}
-
-
//Method to get the price
-
public double getPrice() {
-
return price;
-
-
-
-
}
-
-
// Method to set the title name.
-
public void setTitle(String title) {
-
this.title = title;
-
}
-
-
// Method to set the item number.
-
public void setItem(double item) {
-
this.item = item;
-
}
-
-
// Method to set the stock available.
-
public void setStock(double stock) {
-
this.stock = stock;
-
}
-
-
//Method to set the price.
-
public void setPrice(double price) {
-
this.price = price;
-
-
}
-
// Method to compute the value of inventory.
-
public double getInvValue() {
-
return this.stock *this.price;
-
}
-
-
}
-
-
-
// Inventory program
-
// Main application
-
-
-
-
public class Inventory
-
{
-
public static void main (String args [])
-
{
-
-
System.out.println ("Inventory of DVD Movies:\n");
-
-
String title = "Beerfest";
-
double item = 1;
-
double stock = 15;
-
double price = 22.50;
-
double value;
-
-
Product product = new Product (title, item, stock, price);
-
-
System.out.printf( "%s%18s\n", "DVD Title:", product.getTitle() );
-
-
System.out.printf( "%s%16s\n", "Item #:", product.getItem() );
-
-
System.out.printf( "%s%8s\n", "Number in Stock:" , product.getStock() );
-
-
System.out.printf( "%s%18s\n", "Price:", product.getPrice() );
-
-
-
-
System.out.printf( "%s%9s\n", "Inventory Value:", product.getInvValue() );
-
-
}
-
}
-
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?
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.
Ahh, Sorry! I forgot to post the code.
Here it is: -
//Inventory2.java
-
-
-
import java.util.*;
-
class Product implements Comparable
-
-
{
-
private String name; // class variable that stores the item name
-
private long number; // class variable that stores the item number
-
private long stockQuantity; // class variable that stores the quantity in stock
-
private double price; // class variable that stores the item price
-
-
public Product() // Constructor for the Product class
-
{
-
name = "";
-
number = 0L;
-
stockQuantity = 0L;
-
price = 0.0;
-
}
-
public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
-
{
-
this.name = name;
-
this.number = number;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
-
public void setItemName(String name) // Method to set the item name
-
{
-
this.name = name;
-
}
-
public String getItemName() // Method to get the item name
-
{
-
return name;
-
}
-
-
-
public void setItemNumber(long number) // Method to set the item number
-
{
-
this.number = number;
-
}
-
public long getItemNumber() // Method to get the item number
-
{
-
return number;
-
}
-
-
-
public void setStockQuantity(long quantity) // Method to set the quantity in stock
-
{
-
stockQuantity = quantity;
-
}
-
public long getStockQuantity() // Method to get the quantity in stock
-
{
-
return stockQuantity;
-
}
-
-
-
public void setItemPrice(double price) // Method to set the item price
-
{
-
this.price = price;
-
}
-
public double getItemPrice() // Method to get the item price
-
{
-
return price;
-
}
-
-
-
public double calculateInventoryValue() // Method to calculate the value of the inventory
-
{
-
return price * stockQuantity;
-
}
-
public int compareTo (Object o)
-
{
-
-
-
Product p = (Product)o;
-
return name.compareTo(p.getItemName());
-
}
-
-
public String toString()
-
{
-
return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
-
}
-
-
-
}//end class Product
-
-
public class Inventory2
-
{
-
// main methods begins execution of java application
-
public static void main( String args[])
-
{
-
-
Product[] supplies = new Product[3];
-
-
Product p1 = new Product("cDVD3", 1, 11, 10.99);
-
Product p2 = new Product("aDVD2", 2, 22, 20.99);
-
Product p3 = new Product("bDVD1", 3, 33, 30.99);
-
-
supplies[0] = p1;
-
supplies[1] = p2;
-
supplies[2] = p3;
-
-
// product temp[] supplies = new product[1];
-
-
-
-
//OLD CODE-------------------------------------
-
//Arrays.sort(supplies);
-
//
-
//
-
public void sortByName() {
-
}
-
public String toString() {
-
String s = "";
-
for(Product p : supplies) {
-
s = s + p.toString();
-
s = s + "\n\n";
-
}
-
return s;
-
}
-
public void addProduct(Product p1) {
-
Product[] p = supplies;
-
Product[] temp = new Product[p.length + 1];
-
for(int i = 0; i < p.length; i++) {
-
temp[i] = p[i];
-
}
-
temp[(temp.length - 1)] = p1;
-
supplies = temp;
-
}
-
-
//
-
//
-
-
-
//END OLD CODE------------------------------------
-
-
-
//sorting the array using Bubble Sort
-
double total = 0.0;
-
-
for(int i= 0; i < 3;i++)
-
{
-
total = total + supplies[i].calculateInventoryValue();
-
}
-
-
-
-
for(Product p: supplies)
-
{
-
System.out.println(p);
-
System.out.println();
-
}
-
-
-
-
for(Product p: supplies)
-
{
-
System.out.println(p);
-
System.out.println();
-
}
-
-
-
System.out.println("Total Value is: $"+total);
-
-
-
} // end main method
-
}//end class Inventory2
-
-
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
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.
- //Inventory2.java
-
-
-
import java.util.*;
-
class Product implements Comparable {
-
private String name; // class variable that stores the item name
-
private long number; // class variable that stores the item number
-
private long stockQuantity; // class variable that stores the quantity in stock
-
private double price; // class variable that stores the item price
-
-
public Product() {
-
name = "";
-
number = 0L;
-
stockQuantity = 0L;
-
price = 0.0;
-
}
-
-
public Product(String name, long number, long stockQuantity, double price) {
-
this.name = name;
-
this.number = number;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
-
public void setItemName(String name) {
-
this.name = name;
-
}
-
-
public String getItemName() {
-
return name;
-
}
-
-
public void setItemNumber(long number) {
-
this.number = number;
-
}
-
-
public long getItemNumber() {
-
return number;
-
}
-
-
public void setStockQuantity(long quantity) {
-
stockQuantity = quantity;
-
}
-
-
public long getStockQuantity() {
-
return stockQuantity;
-
}
-
-
public void setItemPrice(double price) {
-
this.price = price;
-
}
-
-
public double getItemPrice() {
-
return price;
-
}
-
-
public double calculateInventoryValue() {
-
return price * stockQuantity;
-
}
-
-
public int compareTo (Object o) {
-
Product p = null;
-
try {
-
p = (Product)o;
-
}
-
catch(ClassCastException cE) {
-
cE.printStackTrace();
-
}
-
return name.compareTo(p.getItemName());
-
}
-
-
public String toString() {
-
return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
-
}
-
}
-
-
public class Inventory2 {
-
Product[] supplies;
-
-
public static void main(String[] args) {
-
Inventory2 inventory = new Inventory2();
-
inventory.addProduct(new Product("cDVD3", 1, 11, 10.99));
-
inventory.addProduct(new Product("aDVD2", 2, 22, 20.99));
-
inventory.addProduct(new Product("bDVD1", 3, 33, 30.99));
-
-
double total = inventory.calculateTotalInventory();
-
-
System.out.println("Total Value is: $"+total);
-
-
System.out.println("*****Before Sorting******");
-
inventory.showInventory();
-
inventory.sortByName();
-
System.out.println("*****After Sorting By Name******");
-
inventory.showInventory();
-
-
}
-
-
public void sortByName() {
-
for (int i = 1; i < supplies.length; i++) {
-
int j;
-
Product val = supplies[i];
-
for (j = i-1; j > -1; j--) {
-
Product temp = supplies[j];
-
if (temp.compareTo(val) <= 0) {
-
break;
-
}
-
supplies[j+1] = temp;
-
}
-
supplies[j+1] = val;
-
}
-
}
-
-
//creates a String representation of the array of products
-
public String toString() {
-
String s = "";
-
for(Product p : supplies) {
-
s = s + p.toString();
-
s = s + "\n\n";
-
}
-
return s;
-
}
-
-
//Using an array so adding an item requires us to increase the size of the array first
-
public void addProduct(Product p1) {
-
if(supplies == null) {
-
supplies = new Product[0];
-
}
-
Product[] p = supplies; //Copy all products into p first
-
Product[] temp = new Product[p.length + 1]; //create bigger array
-
for(int i = 0; i < p.length; i++) {
-
temp[i] = p[i];
-
}
-
temp[(temp.length - 1)] = p1; //add the new product at the last position
-
supplies = temp;
-
}
-
-
//sorting the array using Bubble Sort
-
public double calculateTotalInventory () {
-
double total = 0.0;
-
for(int i = 0; i < supplies.length;i++) {
-
total = total + supplies[i].calculateInventoryValue();
-
}
-
return total;
-
}
-
-
public void showInventory () {
-
System.out.println(toString()); //call our toString method
-
}
-
}
You really should ask about anything at all that you are unsure about here.
Ok, I understand the beginning of the code but here is where I start to get confused: - 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?
Ok, I understand the beginning of the code but here is where I start to get confused: - 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?
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.
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! :)
Here is some code I'm trying to understand: -
//Inventory3
-
-
-
// the DVD Class
-
-
class DVD extends Inventory {
-
-
String genre; // Genre of the DVD
-
double restockingFee; // percentage that is added to the base price as a restocking fee
-
//( actual inventory value will be the base inventory value plus
-
// the base inventory value times this amount ) (value + (value * restockFee))
-
-
public DVD(String genre, double restockingFee, String Item_Number, String Item_Name, int Items_in_Stock,
-
double Item_Price) {
-
super(Item_Number, Item_Name, Items_in_Stock, Item_Price);
-
this.genre = genre;
-
this.restockingFee = restockingFee;
-
-
-
// TODO Auto-generated constructor stub
-
}
-
-
//returns the value of the inventory, plus the restocking fee
-
public double getInventoryValue() {
-
// TODO Auto-generated method stub
-
return super.getInventoryValue() + (super.getInventoryValue() * restockingFee);
-
}
-
-
public String toString()
-
{
-
StringBuffer sb = new StringBuffer("Genre \t").append(genre).append("\n");
-
sb.append(super.toString());
-
-
return sb.toString();
-
}
-
-
}
-
-
//
-
//
-
//
-
-
//The Inventory class
-
-
public class Inventory {
-
-
String productnumber;
-
-
String name;
-
-
int numberofunits;
-
-
double priceperunit;
-
-
// Create a new instance of Inventory
-
// main constructor for the class
-
public Inventory(String Item_Number, String Item_Name, int Items_in_Stock,
-
double Item_Price) {
-
productnumber = Item_Number;
-
name = Item_Name;
-
numberofunits = Items_in_Stock;
-
priceperunit = Item_Price;
-
}
-
-
public void setItemName(String Item_Name)
-
// sets the items name
-
{
-
name = Item_Name;
-
}
-
-
public void setItemNumber(String Item_Number) { // Sets the Product =Number
-
// for the item
-
-
productnumber = Item_Number;
-
}
-
-
public void setItemsInStock(int Items_in_Stock) { // sets the =number of
-
// units in stock
-
-
numberofunits = Items_in_Stock;
-
}
-
-
public void setItemPrice(double Item_Price) { // sets the price of =the
-
// item
-
priceperunit = Item_Price;
-
}
-
-
public String getItemName() { // returns the Product Name of this item
-
return name;
-
}
-
-
public String getItemNumber() { // returns the Product Number of the =item
-
-
return productnumber;
-
}
-
-
public int getItemsInStock() { // returns how many units are in stock
-
return numberofunits;
-
}
-
-
public double getItemPrice() { // returns the price of the item
-
return priceperunit;
-
}
-
-
public double getInventoryValue() { // returns the total value of =the stock
-
// for this item
-
return priceperunit * numberofunits;
-
}
-
-
public static double getTotalValueOfAllInventory(Inventory [] inv)
-
{
-
double tot = 0.0;
-
-
for(int i = 0; i < inv.length; i++)
-
{
-
tot += inv[i].getInventoryValue();
-
}
-
return tot;
-
-
}
-
-
-
-
public String toString()
-
{
-
StringBuffer sb = new StringBuffer();
-
-
sb.append("DVD Title: \t").append(name).append("\n");
-
sb.append("Item #: \t").append(productnumber).append("\n");
-
sb.append("Number in stock:\t").append(numberofunits).append("\n");
-
sb.append("Price: \t").append(String.format("$%.2f%n", priceperunit));
-
sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryValue()));
-
-
return sb.toString();
-
}
-
-
-
}
-
-
//
-
//
-
//
-
-
//
-
//
-
//The InvTest Class
-
class InvTest
-
-
{
-
-
public static void main(String args[])
-
-
{
-
-
double restockFee = 0.05;
-
// initialize a new inventory object
-
DVD[] inventory = new DVD[4];
-
-
inventory[0] = new DVD("Action" ,restockFee, "00623","aDVD1" , 10, 20.00);
-
inventory[1] = new DVD("Action" ,restockFee, "00564","dDVD2" , 10, 12.00);
-
inventory[2] = new DVD("Comedy",restockFee, "00222","cDVD3", 10, 15.00);
-
inventory[3] = new DVD("Family",restockFee,"00153","bDVD4" , 10, 18.00);
-
-
-
DVD temp[] = new DVD[1];
-
-
// sorting the array using Bubble Sort
-
for(int j = 0; j < inventory.length - 1; j++)
-
{
-
-
for(int k = 0; k < inventory.length - 1; k++)
-
{
-
-
if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
-
{
-
-
temp[0] = inventory[k];
-
inventory[k] = inventory[k+1];
-
inventory[k+1] = temp[0];
-
-
}//end if
-
-
}//end for loop
-
-
}//end for loop
-
-
-
// print the inventory information
-
for(int j = 0; j < inventory.length; j++)
-
{
-
System.out.println(inventory[j].toString());
-
}
-
-
-
System.out.printf("Total value of all inventory = $%.2f" , Inventory.getTotalValueOfAllInventory(inventory));
-
return;
-
-
}
-
}
-
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?
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?
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.
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.
Here is some code I'm trying to understand: -
//Inventory3
-
-
-
// the DVD Class
-
-
class DVD extends Inventory {
-
-
String genre; // Genre of the DVD
-
double restockingFee; // percentage that is added to the base price as a restocking fee
-
//( actual inventory value will be the base inventory value plus
-
// the base inventory value times this amount ) (value + (value * restockFee))
-
-
public DVD(String genre, double restockingFee, String Item_Number, String Item_Name, int Items_in_Stock,
-
double Item_Price) {
-
super(Item_Number, Item_Name, Items_in_Stock, Item_Price);
-
this.genre = genre;
-
this.restockingFee = restockingFee;
-
-
-
// TODO Auto-generated constructor stub
-
}
-
-
//returns the value of the inventory, plus the restocking fee
-
public double getInventoryValue() {
-
// TODO Auto-generated method stub
-
return super.getInventoryValue() + (super.getInventoryValue() * restockingFee);
-
}
-
-
public String toString()
-
{
-
StringBuffer sb = new StringBuffer("Genre \t").append(genre).append("\n");
-
sb.append(super.toString());
-
-
return sb.toString();
-
}
-
-
}
-
-
//
-
//
-
//
-
-
//The Inventory class
-
-
public class Inventory {
-
-
String productnumber;
-
-
String name;
-
-
int numberofunits;
-
-
double priceperunit;
-
-
// Create a new instance of Inventory
-
// main constructor for the class
-
public Inventory(String Item_Number, String Item_Name, int Items_in_Stock,
-
double Item_Price) {
-
productnumber = Item_Number;
-
name = Item_Name;
-
numberofunits = Items_in_Stock;
-
priceperunit = Item_Price;
-
}
-
-
public void setItemName(String Item_Name)
-
// sets the items name
-
{
-
name = Item_Name;
-
}
-
-
public void setItemNumber(String Item_Number) { // Sets the Product =Number
-
// for the item
-
-
productnumber = Item_Number;
-
}
-
-
public void setItemsInStock(int Items_in_Stock) { // sets the =number of
-
// units in stock
-
-
numberofunits = Items_in_Stock;
-
}
-
-
public void setItemPrice(double Item_Price) { // sets the price of =the
-
// item
-
priceperunit = Item_Price;
-
}
-
-
public String getItemName() { // returns the Product Name of this item
-
return name;
-
}
-
-
public String getItemNumber() { // returns the Product Number of the =item
-
-
return productnumber;
-
}
-
-
public int getItemsInStock() { // returns how many units are in stock
-
return numberofunits;
-
}
-
-
public double getItemPrice() { // returns the price of the item
-
return priceperunit;
-
}
-
-
public double getInventoryValue() { // returns the total value of =the stock
-
// for this item
-
return priceperunit * numberofunits;
-
}
-
-
public static double getTotalValueOfAllInventory(Inventory [] inv)
-
{
-
double tot = 0.0;
-
-
for(int i = 0; i < inv.length; i++)
-
{
-
tot += inv[i].getInventoryValue();
-
}
-
return tot;
-
-
}
-
-
-
-
public String toString()
-
{
-
StringBuffer sb = new StringBuffer();
-
-
sb.append("DVD Title: \t").append(name).append("\n");
-
sb.append("Item #: \t").append(productnumber).append("\n");
-
sb.append("Number in stock:\t").append(numberofunits).append("\n");
-
sb.append("Price: \t").append(String.format("$%.2f%n", priceperunit));
-
sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryValue()));
-
-
return sb.toString();
-
}
-
-
-
}
-
-
//
-
//
-
//
-
-
//
-
//
-
//The InvTest Class
-
class InvTest
-
-
{
-
-
public static void main(String args[])
-
-
{
-
-
double restockFee = 0.05;
-
// initialize a new inventory object
-
DVD[] inventory = new DVD[4];
-
-
inventory[0] = new DVD("Action" ,restockFee, "00623","aDVD1" , 10, 20.00);
-
inventory[1] = new DVD("Action" ,restockFee, "00564","dDVD2" , 10, 12.00);
-
inventory[2] = new DVD("Comedy",restockFee, "00222","cDVD3", 10, 15.00);
-
inventory[3] = new DVD("Family",restockFee,"00153","bDVD4" , 10, 18.00);
-
-
-
DVD temp[] = new DVD[1];
-
-
// sorting the array using Bubble Sort
-
for(int j = 0; j < inventory.length - 1; j++)
-
{
-
-
for(int k = 0; k < inventory.length - 1; k++)
-
{
-
-
if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
-
{
-
-
temp[0] = inventory[k];
-
inventory[k] = inventory[k+1];
-
inventory[k+1] = temp[0];
-
-
}//end if
-
-
}//end for loop
-
-
}//end for loop
-
-
-
// print the inventory information
-
for(int j = 0; j < inventory.length; j++)
-
{
-
System.out.println(inventory[j].toString());
-
}
-
-
-
System.out.printf("Total value of all inventory = $%.2f" , Inventory.getTotalValueOfAllInventory(inventory));
-
return;
-
-
}
-
}
-
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.
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: -
-
-
class DVD extends Product
-
{
-
-
String genre;
-
double restockingFee;
-
-
-
public DVD(String genre, double restockingFee, int item, String title, double stockQuantity,
-
double price)
-
{
-
super(title,item, stockQuantity, price);
-
this.genre = genre;
-
this.restockingFee = restockingFee;
-
-
// TODO Auto-generated constructor stub
-
}
-
-
//returns the value of the inventory, plus the restocking fee
-
public double getInventoryValue()
-
{
-
// TODO Auto-generated method stub
-
return super.getItemPrice() + restockingFee;
-
}
-
-
public String toString()
-
{
-
StringBuffer sb = new StringBuffer("Genre \t").append(genre).append("\n");
-
sb.append(super.toString());
-
-
return sb.toString();
-
}
-
-
}
-
-
//Inventory2.java
-
-
-
-
class Product implements Comparable
-
{
-
private String title; // class variable that stores the item name
-
private int item; // class variable that stores the item number
-
private double stockQuantity; // class variable that stores the quantity in stock
-
private double price; // class variable that stores the item price
-
-
public Product()
-
{
-
title = "";
-
item = 0;
-
stockQuantity = 0;
-
price = 0.0;
-
}
-
-
public Product(String title, int item, double stockQuantity, double price)
-
{
-
this.title = title;
-
this.item = item;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
-
public void setTitle(String title)
-
{
-
this.title = title;
-
}
-
-
public String getTitle()
-
{
-
return title;
-
}
-
-
public void setItem(int item)
-
{
-
this.item = item;
-
}
-
-
public int getItem()
-
{
-
return item;
-
}
-
-
public void setStockQuantity(double quantity)
-
{
-
stockQuantity = quantity;
-
}
-
-
public double getStockQuantity()
-
{
-
return stockQuantity;
-
}
-
-
public void setItemPrice(double price)
-
{
-
this.price = price;
-
}
-
-
public double getItemPrice()
-
{
-
return price;
-
}
-
-
public double calculateInventoryValue()
-
{
-
return price * stockQuantity;
-
}
-
-
public int compareTo(Object o)
-
{
-
Product p = null;
-
try
-
{
-
p = (Product) o;
-
}
-
catch (ClassCastException cE)
-
{
-
cE.printStackTrace();
-
}
-
return title.compareTo(p.getTitle());
-
}
-
-
public String toString()
-
{
-
return "Genre" +genre+"\nDVD Title: " + title + "\nItem #: " + item + "\nPrice: $" + price + "\nQuantity: "
-
+ stockQuantity + "\nValue: $" + calculateInventoryValue();
-
}
-
}
-
-
public class Inventory2
-
{
-
Product[] supplies;
-
-
public static void main(String[] args)
-
{
-
Inventory2 inventory = new Inventory2();
-
inventory.addProduct(new Product("Beerfest", 1, 15, 22.95));
-
inventory.addProduct(new Product("Illustionist", 2, 25, 25.95));
-
inventory.addProduct(new Product("Employee of the Month", 3, 33, 20.95));
-
-
System.out.println("Inventory of DVD Movies:\n\n");
-
-
-
System.out.println();
-
inventory.showInventory();
-
inventory.sortByName();
-
System.out.println();
-
inventory.showInventory();
-
-
double total = inventory.calculateTotalInventory();
-
System.out.println("Total Value is: $" + total);
-
-
-
}
-
-
public void sortByName()
-
{
-
for (int i = 1; i < supplies.length; i++)
-
{
-
int j;
-
Product val = supplies[i];
-
for (j = i - 1; j > -1; j--)
-
{
-
Product temp = supplies[j];
-
if (temp.compareTo(val) <= 0)
-
{
-
break;
-
}
-
supplies[j + 1] = temp;
-
}
-
supplies[j + 1] = val;
-
}
-
}
-
-
//creates a String representation of the array of products
-
public String toString()
-
{
-
String s = "";
-
for (Product p : supplies)
-
{
-
s = s + p.toString();
-
s = s + "\n\n";
-
}
-
return s;
-
}
-
-
//Using an array so adding an item requires us to increase the size of the array first
-
public void addProduct(Product p1)
-
{
-
if (supplies == null)
-
{
-
supplies = new Product[0];
-
}
-
Product[] p = supplies; //Copy all products into p first
-
Product[] temp = new Product[p.length + 1]; //create bigger array
-
for (int i = 0; i < p.length; i++)
-
{
-
temp[i] = p[i];
-
}
-
temp[(temp.length - 1)] = p1; //add the new product at the last position
-
supplies = temp;
-
}
-
-
//sorting the array using Bubble Sort
-
public double calculateTotalInventory()
-
{
-
double total = 0.0;
-
for (int i = 0; i < supplies.length; i++)
-
{
-
total = total + supplies[i].calculateInventoryValue();
-
}
-
return total;
-
}
-
-
public void showInventory()
-
{
-
System.out.println(toString()); //call our toString method
-
}
-
}
-
- class DVD extends Product
-
{
-
-
String genre;
-
double restockingFee;
-
-
-
public DVD(String title, int item, double stockQuantity, double price, String genre)
-
{
-
super(title,item, stockQuantity, price);
-
this.genre = genre;
-
this.restockingFee = 0.05;
-
-
// TODO Auto-generated constructor stub
-
}
-
-
//returns the value of the inventory, plus the restocking fee
-
public double getInventoryValue()
-
{
-
// TODO Auto-generated method stub
-
return super.getItemPrice() + restockingFee;
-
}
-
-
public String toString()
-
{
-
//this is not efficient
-
//StringBuffer sb = new StringBuffer("Genre \t").append(genre).append("\n");
-
//sb.append(super.toString());
-
-
return super.toString() + "\nGenre \t" + genre;
-
}
-
-
}
-
-
//Inventory2.java
-
-
-
-
class Product implements Comparable
-
{
-
private String title; // class variable that stores the item name
-
private int item; // class variable that stores the item number
-
private double stockQuantity; // class variable that stores the quantity in stock
-
private double price; // class variable that stores the item price
-
-
public Product()
-
{
-
title = "";
-
item = 0;
-
stockQuantity = 0;
-
price = 0.0;
-
}
-
-
public Product(String title, int item, double stockQuantity, double price)
-
{
-
this.title = title;
-
this.item = item;
-
this.stockQuantity = stockQuantity;
-
this.price = price;
-
}
-
-
public void setTitle(String title)
-
{
-
this.title = title;
-
}
-
-
public String getTitle()
-
{
-
return title;
-
}
-
-
public void setItem(int item)
-
{
-
this.item = item;
-
}
-
-
public int getItem()
-
{
-
return item;
-
}
-
-
public void setStockQuantity(double quantity)
-
{
-
stockQuantity = quantity;
-
}
-
-
public double getStockQuantity()
-
{
-
return stockQuantity;
-
}
-
-
public void setItemPrice(double price)
-
{
-
this.price = price;
-
}
-
-
public double getItemPrice()
-
{
-
return price;
-
}
-
-
public double calculateInventoryValue()
-
{
-
return price * stockQuantity;
-
}
-
-
public int compareTo(Object o)
-
{
-
Product p = null;
-
try
-
{
-
p = (Product) o;
-
}
-
catch (ClassCastException cE)
-
{
-
cE.printStackTrace();
-
}
-
return title.compareTo(p.getTitle());
-
}
-
-
public String toString()
-
{
-
return "DVD Title: " + title + "\nItem #: " + item + "\nPrice: $" + price + "\nQuantity: "
-
+ stockQuantity + "\nValue: $" + calculateInventoryValue();
-
}
-
}
-
-
public class Inventory2
-
{
-
Product[] supplies;
-
-
public static void main(String[] args)
-
{
-
Inventory2 inventory = new Inventory2();
-
inventory.addProduct(new DVD("Beerfest", 1, 15, 22.95, "Action"));
-
inventory.addProduct(new DVD("Illustionist", 2, 25, 25.95, "Comedy"));
-
inventory.addProduct(new DVD("Employee of the Month", 3, 33, 20.95, "Comedy"));
-
-
System.out.println("Inventory of DVD Movies:\n\n");
-
-
-
System.out.println();
-
inventory.showInventory();
-
inventory.sortByName();
-
System.out.println();
-
inventory.showInventory();
-
-
double total = inventory.calculateTotalInventory();
-
System.out.println("Total Value is: $" + total);
-
-
-
}
-
-
public void sortByName()
-
{
-
for (int i = 1; i < supplies.length; i++)
-
{
-
int j;
-
Product val = supplies[i];
-
for (j = i - 1; j > -1; j--)
-
{
-
Product temp = supplies[j];
-
if (temp.compareTo(val) <= 0)
-
{
-
break;
-
}
-
supplies[j + 1] = temp;
-
}
-
supplies[j + 1] = val;
-
}
-
}
-
-
//creates a String representation of the array of products
-
public String toString()
-
{
-
String s = "";
-
for (Product p : supplies)
-
{
-
s = s + p.toString();
-
s = s + "\n\n";
-
}
-
return s;
-
}
-
-
//Using an array so adding an item requires us to increase the size of the array first
-
public void addProduct(Product p1)
-
{
-
if (supplies == null)
-
{
-
supplies = new Product[0];
-
}
-
Product[] p = supplies; //Copy all products into p first
-
Product[] temp = new Product[p.length + 1]; //create bigger array
-
for (int i = 0; i < p.length; i++)
-
{
-
temp[i] = p[i];
-
}
-
temp[(temp.length - 1)] = p1; //add the new product at the last position
-
supplies = temp;
-
}
-
-
//sorting the array using Bubble Sort
-
public double calculateTotalInventory()
-
{
-
double total = 0.0;
-
for (int i = 0; i < supplies.length; i++)
-
{
-
total = total + supplies[i].calculateInventoryValue();
-
}
-
return total;
-
}
-
-
public void showInventory()
-
{
-
System.out.println(toString()); //call our toString method
-
}
-
}
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
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.
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.
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |