Help with subclasses and Methods

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 2
Reputation: JVStylez is an unknown quantity at this point 
Solved Threads: 0
JVStylez JVStylez is offline Offline
Newbie Poster

Help with subclasses and Methods

 
0
  #1
Oct 19th, 2007
Good evening all. I have what i think is a simple problem. I created this program to get info from the user and store it into an array. ( i am very new at this so if there is a better way please feel free to elaborate) . basically i am having problems with implementing my subclass. the program that i have is as follows:

(fyi.. the program is an inventory app for school using fudge . yummm)

main java file titled: mainfudge.java

  1. * This applicaion is an Inventory Program that can handle multiple items. Using an array
  2. to store the items the output displays the information one product at a time,
  3. including the item number, the name of the product, the number of units in stock, the
  4. price of each unit, and the value of the inventory of that product. In addition, the output sorts
  5. the arrays and display's the value of the entire inventory as you entered it. */
  6.  
  7.  
  8.  
  9. import java.util.Scanner; // scanner in
  10. import static java.lang.System.in; // cool api I found to reduce code
  11. import static java.lang.System.out; // cool api I found to reduce code
  12.  
  13. class mainfudge {
  14.  
  15. public static void main(String[] args) { // here we go!
  16. fudgei myfudgei = new fudgei (); // Create a "link" between java files
  17. Scanner myScanner = new Scanner(System.in);// create a Myscanner to obtain input in command window
  18.  
  19. double TempPrice; // Intilizations
  20. double TotalPrice=0;
  21. int Num = 0;
  22. int LoopNum = 1 ;
  23. int ArrayNum=0;
  24. int ALength = 0;
  25. String[] FstringArray = new String [100]; // array for name of fudge
  26. double [] FOnumArray = new double [100]; // array for fudge order num
  27. double [] FInvArray = new double [100]; // array for inventory amount
  28. double [] FpriceArray = new double [100]; // array for unit price
  29. boolean yesno;
  30. int comparetype;
  31. int userchoice;
  32.  
  33. out.println("Welcome to the Easy Fudge Inventory Application!\n" +
  34. "How many kinds of fudge will we be making today (max 100)?");
  35. LoopNum = myScanner.nextInt(); // get value for repeatagin and arraylength
  36. ALength = LoopNum ;
  37.  
  38. for ( int counter = 1; counter <= ALength; counter++ ){ //while loop to imput info
  39.  
  40. out.println("Please enter Type of Fudge: ");
  41. myfudgei.FudgeType = myScanner.next();
  42. FstringArray[ArrayNum] = myfudgei.FudgeType; // stores info into the array
  43.  
  44. out.print("Please enter the Order Number for '");
  45. out.print(myfudgei.FudgeType);
  46. out.println("':");
  47. myfudgei.FudgeOrderNum = myScanner.nextDouble();
  48. FOnumArray[ArrayNum] = myfudgei.FudgeOrderNum; // stores info into the array
  49.  
  50. out.println("Please enter the Total Number or Peices On Hand: ");
  51. myfudgei.FudgeInvCount = myScanner.nextDouble();
  52. FInvArray[ArrayNum] = myfudgei.FudgeInvCount; // stores info into the array
  53.  
  54. out.println("Please enter the Price for One Peice:");
  55. myfudgei.FPrice = myScanner.nextDouble();
  56. FpriceArray [ArrayNum] = myfudgei.FPrice; // stores info into the array
  57.  
  58. TempPrice = myfudgei.FPrice * myfudgei.FudgeInvCount; // get inventory price for this type
  59.  
  60. TotalPrice = TempPrice + TotalPrice ; // total inventory price adds the total
  61. //up as the program progresses.
  62.  
  63.  
  64. out.println("Thank you\n");
  65. ArrayNum++; // gives values to the array position
  66. }
  67.  
  68. out.println("\nWhat would you like to do now?");
  69. out.println("1. Display whats on hand again\n2. Edit Something\n3. Add Icing\n" +
  70. "4. Display Total Inventory Value\n5. Quit ");
  71. userchoice = myScanner.nextInt();
  72.  
  73. if (userchoice == 1){
  74.  
  75. while (LoopNum>0){ // sorts the results into sequential files anf places them on different lines
  76.  
  77. out.print("For the ");
  78. out.print(mainfudge.FstringArray[Num]);
  79. out.print(" fudge (order number ");
  80. out.print(mainfudge.FOnumArray[Num]);
  81. out.print("), we have ");
  82. out.print(mainfudge.FInvArray[Num] );
  83. out.print(" on hand. The price for each peice is ");
  84. out.print(mainfudge.FpriceArray[Num]);
  85. out.println(". ");
  86.  
  87.  
  88. LoopNum--; // while there us still a array value left this counts down
  89. Num++; // array value
  90. }
  91. }
  92. if (userchoice == 2){
  93. out.println("this function not yet availiable");
  94. }
  95. if (userchoice == 3){
  96. icedfudge.addicing(add5percent);
  97. }
  98. if (userchoice == 4){
  99. out.print(" Total value for all fudge: "); // total value of all fudge
  100. out.println(TotalPrice);
  101. }
  102. if (userchoice == 5){
  103. out.println("Thank you for using this app!");
  104. }
  105.  
  106.  
  107. }}
  108.  

a fudge object. (just to hold info)

  1. class fudgei {
  2.  
  3. String FudgeType; // type of fudge (cholate, peanutbutter, etc.)
  4. double FudgeOrderNum ; // order number for customers.
  5. double FudgeInvCount; // amount of peices of fudge on hand
  6. double FPrice ; // base price of the fudge

additional subclass(i think) in file entitled: icedfudge

  1.  
  2. class icedfudge extends fudgei
  3. {
  4. double fudgeInventoryCount; // amount of peices of fudge on hand
  5. double fudgePrice ; // base price of the fudge
  6. double add5percent;
  7. String typeF;
  8.  
  9. static int addicing() {
  10. get.fudgePrice;
  11. add5percent = fudgePrice*.05;
  12. return add5percent;
  13.  
  14. }
  15.  
  16.  
  17. }

okay.. if you read all of that i thank you.

the problem i am having is in the call to the subclass. at the end of my main method i have a menu that allows the user to select an additional option after filling the array (i know i could have used arraylist for this, i am still learning and prefer to keep things straightforward). the #3 option does not work because i have something screwed up. its SUPPOSED to call the method in the subclass that adds 5% to the fudge price and gives the new additional property of being "iced".

i know the code is incomplete in relation to giving the property to "fudge" but a bunp in the right direction will greatly help. (also if anyone knows someone whos brain i can pick over the phone or online (chat room) on a real time basis i have some pretty stupid questions to ask. )
again sorry for the length, any input helps!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: JVStylez is an unknown quantity at this point 
Solved Threads: 0
JVStylez JVStylez is offline Offline
Newbie Poster

better program

 
0
  #2
Oct 20th, 2007
I improved the program vastly (i think) but i am still having a problem with selection #3. it returns null?

after the input part it looks like this now
  1. while (userchoice !=0){
  2. out.println("\nWhat would you like to do now?");
  3. out.println("1. Display whats on hand \n2. Edit Something\n3. Add Icing\n" +
  4. "4. Display Total Inventory Value\n0 to Quit ");
  5. userchoice = myScanner.nextInt();
  6.  
  7. if (userchoice == 1){
  8.  
  9. while (LoopNum>0){ // sorts the results into sequential files anf places them on different lines
  10.  
  11. out.print("For the ");
  12. out.print(FstringArray[Num]);
  13. out.print(" fudge (order number ");
  14. out.print(FOnumArray[Num]);
  15. out.print("), we have ");
  16. out.print(FInvArray[Num] );
  17. out.print(" on hand. The price for each peice is ");
  18. out.print(FpriceArray[Num]);
  19. out.println(". ");
  20.  
  21.  
  22. LoopNum--; // while there us still a array value left this counts down
  23. Num++; // array value
  24. }
  25. }
  26.  
  27.  
  28. if (userchoice == 2){ // edit function
  29. out.println("this function not yet availiable");
  30. }
  31.  
  32.  
  33. if (userchoice == 3){ // Method to add icing.
  34. for ( int counter = 1; counter <= ALength; counter++ ){ //while loop to imput info
  35. out.printf("%s.%s", num2, FstringArray[num2]);
  36. num2++;
  37. }
  38. out.println("Choose Kind:");
  39. icingon = myScanner.nextInt();
  40.  
  41. out.printf("Adding icing to %s.", FstringArray[icingon]);
  42.  
  43. Wicing = FstringArray[icingon]; // gives values that can be manipulated without effecting the rest of the array
  44. Wicingnum = FpriceArray[icingon];// same
  45.  
  46. icedfudge.addicing(Wicing,Wicingnum); // call to add icing
  47.  
  48. FstringArray[icingon] = Wicing + "with icing"; // adds the "with icing" descriptor to the chosen type
  49. }
  50.  
  51. if (userchoice == 4){
  52. out.print(" Total value for all fudge: "); // total value of all fudge
  53. out.println(TotalPrice);
  54. }
  55. if (userchoice == 0){
  56. out.println("Thank you for using this app!");
  57. break;
  58. }
  59. }

the subclass has changed to
  1. import static java.lang.System.out; // cool api I found to reduce code
  2.  
  3. class icedfudge extends fudgei
  4. { static double fudgeInventoryCount; // amount of peices of fudge on hand
  5. static double fudgePrice ; // base price of the fudge
  6. static double add5percent;
  7.  
  8.  
  9. static void addicing(String Wicing, double Wicingnum) {
  10. fudgePrice = fudgePrice;
  11. add5percent = fudgePrice*.05;
  12. out.println(add5percent);
  13. }
  14.  
  15. }

again any help is .. um.. helpful. i know this was shortly after the first post.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Help with subclasses and Methods

 
0
  #3
Oct 22nd, 2007
Err, which part "returns null".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1514 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC