Define a class named Payment that contains a member variable of type double that
ID: 3633845 • Letter: D
Question
Define a class named Payment that contains a member variable of type double that stores the amount of the payment and appropriate accessor and mutator methods. Also create a method named paymentDetails that outputs an English sentence that describes the amount of the payment.Next define a class named CashPayment that is derived from Payment. This class should redefine the paymentDetails method to indicate that the payment is in cash. Include appropriate constructor(s).
Define a class named CreditCardPayment that is derived from Payment. This class should contain member variables for the name on the card, expiration date, and credit card number. Include appropriate constructor(s). Finally, redefine the paymentDetails method to include all credit card information in the printout.
Create a main method that creates at least two CashPayment and two CreditCardPayment objects with different values and calls paymentDetails for each.
In the CodeMate exercise, you should provide complete implementations for the CashPayment and CreditCardPayment classes.
Explanation / Answer
Payment.java public class Payment { private double getPlace(String number) { switch (number.length()) { case 1: return DefinePlace.UNITS; case 2: return DefinePlace.TENS; case 3: return DefinePlace.HUNDREDS; case 4: return DefinePlace.THOUSANDS; case 5: return DefinePlace.TENTHOUSANDS; case 6: return DefinePlace.LAKHS; case 7: return DefinePlace.TENLAKHS; case 8: return DefinePlace.CRORES; case 9: return DefinePlace.TENCRORES; }// switch return 0.0; }// getPlace private String getWord(int number) { switch (number) { case 1: return "One"; case 2: return "Two"; case 3: return "Three"; case 4: return "Four"; case 5: return "Five"; case 6: return "Six"; case 7: return "Seven"; case 8: return "Eight"; case 9: return "Nine"; case 0: return "Zero"; case 10: return "Ten"; case 11: return "Eleven"; case 12: return "Tweleve"; case 13: return "Thirteen"; case 14: return "Forteen"; case 15: return "Fifteen"; case 16: return "Sixteen"; case 17: return "Seventeen"; case 18: return "Eighteen"; case 19: return "Ninteen"; case 20: return "Twenty"; case 30: return "Thirty"; case 40: return "Forty"; case 50: return "Fifty"; case 60: return "Sixty"; case 70: return "Seventy"; case 80: return "Eighty"; case 90: return "Ninty"; case 100: return "Hundred"; } // switch return ""; } // getWord private String cleanNumber(String number) { String cleanedNumber = ""; cleanedNumber = number.replace('.', ' ').replaceAll(" ", ""); cleanedNumber = cleanedNumber.replace(',', ' ').replaceAll(" ", ""); if (cleanedNumber.startsWith("0")) cleanedNumber = cleanedNumber.replaceFirst("0", ""); return cleanedNumber; } // cleanNumber public String convertNumber(String number) { //number = cleanNumber(number); double num = 0.0; try { num = Double.parseDouble(number); } catch (Exception e) { return "Invalid Number Sent to Convert"; } // catch String returnValue = ""; while (num > 0) { number = "" + (int) num; double place = getPlace(number); if (place == DefinePlace.TENS || place == DefinePlace.TENTHOUSANDS || place == DefinePlace.TENLAKHS || place == DefinePlace.TENCRORES) { int subNum = Integer.parseInt(number.charAt(0) + "" + number.charAt(1)); if (subNum >= 21 && (subNum % 10) != 0) { returnValue += getWord(Integer.parseInt("" + number.charAt(0)) * 10) + " " + getWord(subNum % 10); } // if else { returnValue += getWord(subNum); }// else if (place == DefinePlace.TENS) { num = 0; }// if else if (place == DefinePlace.TENTHOUSANDS) { num -= subNum * DefinePlace.THOUSANDS; returnValue += " Thousands "; }// if else if (place == DefinePlace.TENLAKHS) { num -= subNum * DefinePlace.LAKHS; returnValue += " Lakhs "; }// if else if (place == DefinePlace.TENCRORES) { num -= subNum * DefinePlace.CRORES; returnValue += " Crores "; }// if }// if else { int subNum = Integer.parseInt("" + number.charAt(0)); returnValue += getWord(subNum); if (place == DefinePlace.UNITS) { num = 0; }// if else if (place == DefinePlace.HUNDREDS) { num -= subNum * DefinePlace.HUNDREDS; returnValue += " Hundred "; }// if else if (place == DefinePlace.THOUSANDS) { num -= subNum * DefinePlace.THOUSANDS; returnValue += " Thousand "; }// if else if (place == DefinePlace.LAKHS) { num -= subNum * DefinePlace.LAKHS; returnValue += " Lakh "; }// if else if (place == DefinePlace.CRORES) { num -= subNum * DefinePlace.CRORES; returnValue += " Crore "; }// if }// else }// while return returnValue; }// convert number protected static double amount; public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public Payment(double amount) { super(); this.amount = amount; } public String paymentDetails(double amount) { return convertNumber(String.valueOf(amount)); } } class DefinePlace { public static final double UNITS = 1; public static final double TENS = 10 * UNITS; public static final double HUNDREDS = 10 * TENS; public static final double THOUSANDS = 10 * HUNDREDS; public static final double TENTHOUSANDS = 10 * THOUSANDS; public static final double LAKHS = 10 * TENTHOUSANDS; public static final double TENLAKHS = 10 * LAKHS; public static final double CRORES = 10 * TENLAKHS; public static final double TENCRORES = 10 * CRORES; } // class ******************************************************************************************* CashPayment.java public class CashPayment extends Payment{ public CashPayment(){ super(amount); } public String paymentDetails(double amount){ return " PayMent Mode :Cash "+ "Amount : "+super.paymentDetails(amount); } public static void main(String a[]) { CashPayment payment = new CashPayment(); CashPayment payment2 = new CashPayment(); System.out.println(payment.paymentDetails(23456)); System.out.println(payment2.paymentDetails(435996)); } } ******************************************************************************************* CreditCardPayment.java import java.util.Date; public class CreditCardPayment extends Payment{ private String cardHolderName; public String getCardHolderName() { return cardHolderName; } public void setCardHolderName(String cardHolderName) { this.cardHolderName = cardHolderName; } public Date getExpiryDate() { return expiryDate; } public void setExpiryDate(Date expiryDate) { this.expiryDate = expiryDate; } public long getCreditCardNumber() { return creditCardNumber; } public void setCreditCardNumber(long creditCardNumber) { this.creditCardNumber = creditCardNumber; } private Date expiryDate; private long creditCardNumber; public CreditCardPayment(String cardHolderName,Date expiryDate, long creditCardNumber){ super(amount); this.cardHolderName = cardHolderName; this.creditCardNumber = creditCardNumber; this.expiryDate = expiryDate; } public String paymentDetails(double amount){ return "cardHolderName : "+cardHolderName+ " creditCardNumber :"+ creditCardNumber+ " expiryDate :"+ expiryDate+" "+ super.paymentDetails(amount); } public static void main(String a[]) { CreditCardPayment payment = new CreditCardPayment("sumitha",new Date(),12345678); CreditCardPayment payment2 = new CreditCardPayment("sudhakar",new Date(),445235678); System.out.println(payment.paymentDetails(1234)); System.out.println(payment2.paymentDetails(4356)); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.