classes and arrays
I have created a invoice class that asks the user to enter different information about a part, I have also created a loop which goes through three times. the problems I am having are listed below i have been working at this for ages and just cant see what i need to do for it. the program and driver program are listed below also.
oCreate an object eg if num = 0 this will create an object in the first cell of the array:
inv [num] = new Invoice();
oUse the dialog boxes and setters to capture the data as before but include the invoice number ie 1 to 3 as shown below in the dialog box. eg
inv[num].setPartNumber(partNo);
昉rint the Invoice heading as before
昑hen use another For loop using the getter methods to display the values of each instance variable and the new method getInvoiceAmount to display the total of each line.
旳dd each line total and print the overall total.
?have the final dialog box show like below,
part description qty price total
nun01 nut 12 2 $24
bol01 bolt 12 3 $36
scw01 screw 24 1 $24
$84
/**
* This program demonstrates classes.
*
* @author (Simon Orazi)
* @version (11/05/07)
*/
publicclass Invoice
{
private String partNumber;
private String description;
privatedouble quantity;
privatedouble price;
public Invoice()
{
partNumber ="";
description ="";
quantity = 0;
price = 0;
}
/**
* The setPart method stores value in part field.
* @param partNumber the value to store in part.
*/
publicvoid setPartNumber(String piece)
{
partNumber = piece;
}
/**
* The setDescription method stores value in part field.
* @param desc the value to store in description.
*/
publicvoid setDescription(String desc)
{
description = desc;
}
/**
* The setQuantity method stores value in part field.
* @param quan the value to store in quantity.
*/
publicvoid setQuantity(double quan)
{
quantity = quan;
}
/**
* The setPrice method stores value in part field.
* @param amount the value to store in price.
*/
publicvoid setPrice(double amount)
{
price = amount;
}
/**
* The getPart method returns value.
* @return the value in the part field.
*/
public String getPartNumber()
{
return partNumber;
}
/**
* The getdescription method returns value.
* @return the value in the description field.
*/
public String getDescription()
{
return description;
}
/**
* The getQuantity method returns value.
* @return the value in the quantity field.
*/
publicdouble getQuantity()
{
return quantity;
}
/**
* The getPrice method returns value.
* @return the value in the price field.
*/
publicdouble getPrice()
{
return price;
}
publicdouble getTotal()
{
return quantity * price;
}
}
And now the Driver Program
import javax.swing.JOptionPane;
/**
* This program demonstrates classes.
*
* @author (Simon Orazi)
* @version (11/05/07)
*/
publicclass TestInvoice
{
publicstaticvoid main(String[] args)
{
String input;
double number;
double partTotal;
double invoiceTotal;
Invoice myInvoice =new Invoice();
for (int index = 0; index < 3; index++)
{
input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) +" Number");
myInvoice.setPartNumber(input);
input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) +" Description");
myInvoice.setDescription(input);
input = JOptionPane.showInputDialog("Please Enter Quantity of Part " + (index + 1) +" Required");
number = Double.parseDouble(input);
myInvoice.setQuantity(number);
input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) +" Price");
number = Double.parseDouble(input);
myInvoice.setPrice(number);
partTotal = myInvoice.getQuantity() * myInvoice.getPrice();
}
invoiceTotal =
JOptionPane.showMessageDialog(null,"Part " +"" +"Description" +"" +"Qty" +"" +"Price\n" +
" $" + myInvoice.getPartNumber() +" $" + myInvoice.getDescription() +" $" +
myInvoice.getQuantity() +" $" + myInvoice.getPrice() +"\n" +"$" + partTotal);
}
}
[7764 byte] By [
sorazia] at [2007-11-27 4:33:05]

> the problems I am having are listed below i have been working at this for ages where then? i only see the =, not what ur probs is
sorry mate,the problems are on my test program which this is not. I have only started with java and i am having a lot of trouble getting the arrays to fit in and also the for loop for the getters and displaying it all the way i have shown.any help would be greatly
> sorry mate,don't mate me, dude> the problems are on my test program which this is> not. I have only started with java and i am having a> lot of trouble getting the arrays to fit in exactly wot kind of trouble then?
I found one problem in your code
The variable partTotal should be initialized before use.
double partTotal = 0.0;
and
invoiceTotal =
JOptionPane.showMessageDialog(null, "Part " + "" + "Description" + "" + "Qty" + "" + "Price\n" +
" $" + myInvoice.getPartNumber() +" $" + myInvoice.getDescription() + " $" +
myInvoice.getQuantity() + " $" + myInvoice.getPrice() + "\n" + "$" + partTotal);
should be changed to
JOptionPane.showMessageDialog(null, "Part " + "" + "Description" + "" + "Qty" + "" + "Price\n" +
" $" + myInvoice.getPartNumber() +" $" + myInvoice.getDescription() + " $" +
myInvoice.getQuantity() + " $" + myInvoice.getPrice() + "\n" + "$" + partTotal);
Sorry Dude,
i just cant seem to grasp how to and why use arrays, my external lecturer won reply to any of my emails and i am finding it difficult to get through everything.
bassically what i am trying to do is to create a instatiable class and a driver program for an invoice, where the user is asked for part number, description, quantity and price then they are shown under the headings and including a total for each part and also for the total invoice. so far i have been able to create the for loop to repeat it three times but unable to get the array in this loop. also i dont know how i am supposed to get a for loop into the getters to repeat the showing of the answers.
> Sorry Dude,
don't dude me, mate
> i just cant seem to grasp how to and why use arrays,
...
> times but unable to get the array in this loop. also
> i dont know how i am supposed to get a for loop into
> the getters to repeat the showing of the answers.
last time: wot have u tried and wot exactly is the prob?
> my external lecturer won reply to any of my emails
i cant imagine y. maybe he thinks ur writin style is too formal and ur questions not vague enuf (append to me once).
teachers are usually unresponsive to kids who don't bother to do their own homework and don't pay attention in class.
If the only time they ever see you is 5 minutes before an assignment is due complaining about how it's too difficult and you're stuck they're not going to help you, get used to it.
jwenting
you know nothing about my situation so dont even try and get smart with me. i work full time and study full time externally and i also have a son to top it all off so i dont even get to go to any lectures and no i dont bother him 5 mins before an assignment is due so go try your **** with someone else.
OnBringeri apologise, i thought i was being specific with my problems being, that i do not understand how to get a for loop using the getter methods to display the values of each instance variables and also setting an array in the for loop for the setter methods.
Sorry to hear about your lack of time. However, I can't imagine that getting an attitude with the people who are trying to help you is going to be beneficial. By the way, lot's of us go to school full time while working full time (with kids), but that doesn't give us the right to be rude to one another. "You catch more flies with honey.."
I hope you find the answers your looking for.
Maybe if some of the forum members wouldn't automatically assume everyone is trying to do homework and/or the OP is an annoyance to their teacher, then problems might get solved nicely around here.
kal_025
i wasnt being rude, i just did not like that person assuming they knew me and knew what i was like, i do know that there are other people out there in the same situation like me and for those people they do understand the predicament i am in with availability to study and trying to rush study by where things get missed and possibly misunderstood.
> i wasnt being rudeSee those stars in your reply #8? That's a general indication that you were being rude, not to mention the tone that was obvious even in writing.
what is going on here, that word was cr_p. why am i the one being attacked here, all i did was defend myself against someone calling me lazy and that i am just using this forum to do my homework which i didnt appreciate as no one here knows anything about me.
http://www.catb.org/~esr/faqs/smart-questions.html
The point I'm trying to make (and probably others) is: Yes someone said something nasty about you but life is full of crap like that. The best thing for
you to do is forget them and continue trying to get the help you want. However, responding to what they said in even a slightly rude way will get you nothing but
further scorn and ridicule. Wipe the slate clean and try again.
Ok, now that we have that issue settled, let's see if we can't start over and get your coding problem solved. :-) The first question about your code is: what exactly are you having troubles with? We will be able to help better if we have specific questions to answer. The only thing that I see besides what AnanSmriti showed you is the following bit of code:
/**
* The setPart method stores value in part field.
* @param partNumber the value to store in part.
*/
public void setPartNumber(String piece)
{
partNumber = piece;
}
Where do you use "piece" in the rest of your code? Could this just be a small oversight causing major brain damage?
i have managed to fix all my problems, all that i have left to fix is my exception handling for the input for the 'quantity' and also 'price'. the main code has pretty much stayed the same but the driver code has changed substantially. please look at the exception handling within. when user inputs a string in either get quantity or get price the message show up but it does not repeat the previous dialog box but it just goes straight to the next box.
import javax.swing.JOptionPane;
/**
* This program demonstrates classes.
*
* @author (Simon Orazi)
* @version (11/05/07)
*/
public class TestInvoice
{
public static void main(String[] args)
{
String input;
double number;
double invoiceTotal = 0;
Invoice myInvoice[] = new Invoice[3];
for (int index = 0; index < 3; index++)
{
double num = 0;
myInvoice[index] = new Invoice();
input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Number");
myInvoice[index].setPartNumber(input);
input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Description");
myInvoice[index].setDescription(input);
do
{
input = JOptionPane.showInputDialog("Please Enter Quantity of Part " + (index + 1) + " Required");
try
{
number = Double.parseDouble(input);
myInvoice[index].setQuantity(number);
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "You must enter an integer");
}
}
while(input=="");
do
{
input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Price");
try
{
number = Double.parseDouble(input);
myInvoice[index].setPrice(number);
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "You must enter an integer");
}
}
while(input=="");
}
invoiceTotal = myInvoice[0].getTotal() + myInvoice[1].getTotal() + myInvoice[2].getTotal();
JOptionPane.showMessageDialog(null, "Part " + "Description " + "Qty " + "Price\n" +
" " + myInvoice[0].getPartNumber() + " " + myInvoice[0].getDescription() + " " +
myInvoice[0].getQuantity() + " " + myInvoice[0].getPrice() + "\n" + myInvoice[1].getPartNumber() + " " + myInvoice[1].getDescription() + " " +
myInvoice[1].getQuantity() + " " + myInvoice[1].getPrice() + "\n" + myInvoice[2].getPartNumber() + " " + myInvoice[2].getDescription() + " " +
myInvoice[2].getQuantity() + " " + myInvoice[2].getPrice() + "\n" + "$" + invoiceTotal);
}
}
Don't compare Strings with ==, use .equals(). In your error message you say you must enter an integer, but it's actually a decimal number they must enter.
what can i do if i dont want it to be a decimal number, just a double
> what can i do if i dont want it to be a decimal
> number, just a double
A double is a decimal number. Decimal just means that it has a decimal point in it. If you only want whole numbers, use ints. If you want to have decimal numbers too, then use doubles. My point was that you were telling the user they have to enter a whole number when they don't.
thanks thats fantastic.would i have to have use shown below or not.import java.text.DecimalFormat;
You only need to import the DecimalFormat class if you use it in your code. Do you use it in your code?
sorry, that was a very bad question.still having trouble with the exception handling, cant seem to get it right yet.