Advertisement
Crenox

TaxableItem Java Program

Apr 9th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. // Sammy Samkough
  2.  
  3. public interface Item
  4. {
  5. double purchasePrice();
  6. }
  7. -------------------------------------------------------------------------------------------------------------------------------
  8. // Sammy Samkough
  9.  
  10. public abstract class TaxableItem implements Item
  11. {
  12. private double taxRate;
  13.  
  14. public abstract double getListPrice();
  15.  
  16. public TaxableItem(double rate)
  17. {
  18. taxRate = rate;
  19. }
  20.  
  21. // Part A
  22. // returns the price of the item including the tax
  23. public double purchasePrice()
  24. {
  25. double purchasePrice = getListPrice() * (1 + taxRate);
  26.  
  27. return purchasePrice;
  28. }
  29.  
  30. /** Added the getTaxRate() Accessor Method */
  31. public double getTaxRate()
  32. {
  33. return taxRate;
  34. }
  35. }
  36. -------------------------------------------------------------------------------------------------------------------------------
  37. // Sammy Samkough
  38. // Part B - Create the Vehicle class which extends the TaxableItem class.
  39. // * A vehicle has two parts to its list price: a dealer cost and dealer markup.
  40. // * The list price of a vehicle is the sum of the dealer cost and the dealer markup.
  41. // * Your class should have a constructor that takes dealer cost, dealer markup, and the tax rate as parameters.
  42. // * Provide any instance variables needed and implement all necessary methods.
  43. // * Also provide a public method changeMarkup, which changes the dealer markup to the value of its parameter.
  44.  
  45.  
  46. public class Vehicle extends TaxableItem
  47. {
  48. /* Instance Variables not shown - read the comments for Part B above */
  49. private int dealerCost;
  50. private int dealerMarkup;
  51. private int taxRate;
  52.  
  53. /* Class Methods not shown - read the comments for Part B above */
  54. public Vehicle(double tRate)
  55. {
  56. super(tRate);
  57. dealerCost = 0;
  58. dealerMarkup = 0;
  59. }
  60.  
  61. public Vehicle(int dCost, int dMarkup, double tRate)
  62. {
  63. super(tRate);
  64. tRate = taxRate;
  65. dealerCost = dCost;
  66. dealerMarkup = dMarkup;
  67. }
  68.  
  69. public void changeMarkup(int cMarkup)
  70. {
  71. dealerMarkup = cMarkup;
  72. }
  73.  
  74. public double getListPrice()
  75. {
  76. return dealerCost + dealerMarkup;
  77. }
  78.  
  79. public double getPurchaseCost()
  80. {
  81. return getListPrice() + (1 + taxRate);
  82. }
  83.  
  84. /*** Not specifically asked for in question - but toString() should be added here to allow a client to print
  85. * Label and print out the Dealer Cost, Dealer Markup, List Price, Tax Rate and Purchase Price
  86. */
  87. public String toString()
  88. {
  89. String result = "";
  90.  
  91. result += "Dealer Cost: " + dealerCost + " Dealer Markup: " + dealerMarkup;
  92.  
  93. return result;
  94. }
  95. }
  96. -------------------------------------------------------------------------------------------------------------------------------
  97. // Sammy Samkough
  98.  
  99. import java.util.ArrayList;
  100.  
  101. public class VehicleRunner
  102. {
  103. public static void main(String[] args)
  104. {
  105. Vehicle one = new Vehicle(25000, 3200, .07);
  106. Vehicle two = new Vehicle(33000, 4300, .07);
  107. Vehicle three = new Vehicle(51000, 6000, .07);
  108.  
  109. ArrayList<Vehicle> cars = new ArrayList<Vehicle>();
  110.  
  111. cars.add(one);
  112. cars.add(two);
  113. cars.add(three);
  114.  
  115. System.out.println(cars);
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement