reset password
Author Message
dbravoru
Posts: 60
Posted 21:04 Apr 08, 2014 |

I'm currently working on the Computer class of my program, but I'm having trouble getting the total cost.

This is my thought process:

I have all the required classes, and a Computer Driver with the main method that includes all of my array lists.

The problem I face now is getting the individual price from each item.

For example,

I tried to call the method getPrice() using Case.getPrice(); however, getPrice is a non-static method and I was unable to do so.

The getPrice() method must be called through an object, but which object do I call it through? Doesn't the constructor in the Case class already take care of that for me? 

HeroVega
Posts: 8
Posted 21:29 Apr 08, 2014 |

Inside of your Computer class, I'm assuming you have a constructor which takes in the objects from the other classes and assigns them to the data fields, for example: 

Computer(Case towerCase, blah blah blah){
   this.towerCase = towerCase;
   etc
   etc
}

That's how it should look if you followed the UML.

Now as for calling the getPrice method you need to use the data fields, not the class itself. You wouldn't call Case.getPrice() because the Case class by itself is just a template. You would have to call this.towerCase.getPrice(); because THAT is the SPECIFIC instance of the Case class that you need to find the price of.

Hopefully that helped, and you can figure out the process for the rest of the data fields. (Hint: they're pretty much the same, the key is to make sure you can pass the objects into the Computer constructor to begin with)

kknaur
Posts: 540
Posted 22:13 Apr 08, 2014 |

Thank you for the good reply, you actually beat me to it ^_^.  Everything the above poster said is correct.  Think of the assignment like this.  I have given you all of these puzzle pieces and you need to figure out how to fit them all together. 

You have all of these individual classes for the parts (Case, Monitor, etc) and you create specific instances of them based on the specs I have given you.  Now you use your menu to decide which of the three options for each type of part that the user wants.  When you know which specific instance of the part the user wants for each, then you need to fit all of those parts together into a "Computer".  In your Computer class, as the previous poster said, you can just use each of the member variables to access the public methods of their respective classes.  Since each part has a getPrice() method...well its fairly straightforward to find the total price, and then generate a sales receipt.

Keenan Knaur

dbravoru
Posts: 60
Posted 23:48 Apr 08, 2014 |

Thank you for all the help, it is greatly appreciated! 

I've run into another problem I'm having; how do I pass on the parts of the computer to the Computer class?

For example,

I created a computer object in the ComputerDriver class. 

I hope this doesn't give off too much information to people that haven't quite gotten to this part yet, but the code I wrote is:

Computer computer = new Computer(null, null, null, null, null, null);

How do I pass on arguments to the Computer object after figuring out what the user wants?

Last edited by dbravoru at 23:51 Apr 08, 2014.
dbravoru
Posts: 60
Posted 23:50 Apr 08, 2014 |

This computer object was created at the beginning of the driver; maybe I should create it near the end of the driver so that I already have the arguments I need to create it?

I'll try it.