Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package retailpricecalc;
  7.  
  8. /**
  9.      Month Class
  10.  */
  11.  
  12. class Month
  13. {
  14.     private int monthNumber; // January = 1, ... , December = 12
  15.  
  16.     public Month()
  17.     {
  18.     monthNumber = 1; // January
  19.     }
  20.  
  21.     public Month(int num)
  22.     {
  23.     if((num < 1)||(num>12))
  24.         monthNumber = 1; // Jan
  25.     else
  26.         monthNumber = num;
  27.     }
  28.  
  29.     public Month(String month)
  30.     {
  31.     String lowerMonth = month.toLowerCase();
  32.     if(lowerMonth.equals("january")) monthNumber = 1;
  33.     if(lowerMonth.equals("february")) monthNumber = 2;
  34.     if(lowerMonth.equals("march")) monthNumber = 3;
  35.     if(lowerMonth.equals("april")) monthNumber = 4;
  36.     if(lowerMonth.equals("may")) monthNumber = 5;
  37.     if(lowerMonth.equals("june")) monthNumber = 6;
  38.     if(lowerMonth.equals("july")) monthNumber = 7;
  39.     if(lowerMonth.equals("august")) monthNumber = 8;
  40.     if(lowerMonth.equals("september")) monthNumber = 9;
  41.     if(lowerMonth.equals("october")) monthNumber = 10;
  42.     if(lowerMonth.equals("november")) monthNumber = 11;
  43.     if(lowerMonth.equals("december")) monthNumber = 12;
  44.     }
  45.  
  46.     public void setMonthNumber(int num)
  47.     {
  48.     if((num < 1)||(num>12))
  49.         monthNumber = 1; // Jan
  50.     else
  51.         monthNumber = num;
  52.     }
  53.  
  54.     public int getMonthNumber()
  55.     {
  56.     return monthNumber;
  57.     }
  58.  
  59.     public String getMonthName()
  60.     {
  61.     switch(monthNumber) {
  62.         case 1: return "January";
  63.         case 2: return "February";
  64.         case 3: return "March";
  65.         case 4: return "April";
  66.         case 5: return "May";
  67.         case 6: return "June";
  68.         case 7: return "July";
  69.         case 8: return "August";
  70.         case 9: return "September";
  71.         case 10: return "October";
  72.         case 11: return "November";
  73.         case 12: return "December";
  74.     }
  75.     return "January"; // Default, incase nothing matches in the switch (which should never happen)
  76.     }
  77.  
  78.     @Override
  79.     public String toString()
  80.     {
  81.     return getMonthName();
  82.     }
  83.  
  84.     public Boolean equals(Month obj)
  85.     {
  86.     if(this.getMonthNumber()==obj.getMonthNumber())
  87.         return true;
  88.     else
  89.         return false;
  90.     }
  91.  
  92.     public Boolean greaterThan(Month obj)
  93.     {
  94.     if(this.getMonthNumber() > obj.getMonthNumber())
  95.         return true;
  96.     else
  97.         return false;
  98.     }
  99.  
  100.     public Boolean lessThan(Month obj)
  101.     {
  102.     if(this.getMonthNumber() < obj.getMonthNumber())
  103.         return true;
  104.     else
  105.         return false;
  106.     }
  107. }
  108. /**
  109.      Month Class Demo Program
  110.  */
  111.  
  112. class MonthDemo
  113. {
  114.         public static void demo1()
  115.         {
  116.                 // Use the default constructor.
  117.                 Month m = new Month();
  118.                 System.out.println("Month " + m.getMonthNumber() +
  119.                                    " is " + m);
  120.                 // Set the month number to the values 0 through 12
  121.                 // (0 is invalid), and display the resulting month name.
  122.                 for (int i = 0; i <= 12; i++)
  123.                 {
  124.                         m.setMonthNumber(i);
  125.                         System.out.println("Month " + m.getMonthNumber() +
  126.                                    " is " + m);
  127.                 }
  128.         }
  129.  
  130.         public static void demo2()
  131.         {
  132.                 // Use the 2nd constructor to create two objects.
  133.                 Month m1 = new Month(10);
  134.                 Month m2 = new Month(5);
  135.                 System.out.println("Month " + m1.getMonthNumber() +
  136.                                    " is " + m1);
  137.                 System.out.println("Month " + m2.getMonthNumber() +
  138.                                    " is " + m2);
  139.  
  140.                 // Test for equality.
  141.                 if (m1.equals(m2))
  142.                         System.out.println(m1 + " and " + m2 + " are equal.");
  143.                 else
  144.                         System.out.println(m1 + " and " + m2 + " are NOT equal.");
  145.  
  146.                 // Is m1 greater than m2?
  147.                 if (m1.greaterThan(m2))
  148.                         System.out.println(m1 + " is greater than " + m2);
  149.                 else
  150.                         System.out.println(m1 + " is NOT greater than " + m2);
  151.  
  152.                 // Is m1 less than m2?
  153.                 if (m1.lessThan(m2))
  154.                         System.out.println(m1 + " is less than " + m2);
  155.                 else
  156.                         System.out.println(m1 + " is NOT less than " + m2);
  157.         }
  158.  
  159.         public static void demo3()
  160.         {
  161.                 // Use the 3rd constructor to create three objects.
  162.                 Month m1 = new Month("March");
  163.                 Month m2 = new Month("december");
  164.                 Month m3 = new Month("Bad Month");
  165.                 System.out.println("Month " + m1.getMonthNumber() +
  166.                                    " is " + m1);
  167.                 System.out.println("Month " + m2.getMonthNumber() +
  168.                                    " is " + m2);
  169.                 System.out.println("Month " + m3.getMonthNumber() +
  170.                                    " is " + m3);
  171.         }
  172.  
  173.         public static void main(String[] args) {
  174.             System.out.println("DEMO 1: setMonthNumber test");
  175.             demo1();
  176.             System.out.println();
  177.             System.out.println("DEMO 2: Constructors and comparisons");
  178.             demo2();
  179.             System.out.println();
  180.             System.out.println("DEMO 3: Construction from month names");
  181.             demo3();
  182.         }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement