Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package retailpricecalc;
- /**
- Month Class
- */
- class Month
- {
- private int monthNumber; // January = 1, ... , December = 12
- public Month()
- {
- monthNumber = 1; // January
- }
- public Month(int num)
- {
- if((num < 1)||(num>12))
- monthNumber = 1; // Jan
- else
- monthNumber = num;
- }
- public Month(String month)
- {
- String lowerMonth = month.toLowerCase();
- if(lowerMonth.equals("january")) monthNumber = 1;
- if(lowerMonth.equals("february")) monthNumber = 2;
- if(lowerMonth.equals("march")) monthNumber = 3;
- if(lowerMonth.equals("april")) monthNumber = 4;
- if(lowerMonth.equals("may")) monthNumber = 5;
- if(lowerMonth.equals("june")) monthNumber = 6;
- if(lowerMonth.equals("july")) monthNumber = 7;
- if(lowerMonth.equals("august")) monthNumber = 8;
- if(lowerMonth.equals("september")) monthNumber = 9;
- if(lowerMonth.equals("october")) monthNumber = 10;
- if(lowerMonth.equals("november")) monthNumber = 11;
- if(lowerMonth.equals("december")) monthNumber = 12;
- }
- public void setMonthNumber(int num)
- {
- if((num < 1)||(num>12))
- monthNumber = 1; // Jan
- else
- monthNumber = num;
- }
- public int getMonthNumber()
- {
- return monthNumber;
- }
- public String getMonthName()
- {
- switch(monthNumber) {
- case 1: return "January";
- case 2: return "February";
- case 3: return "March";
- case 4: return "April";
- case 5: return "May";
- case 6: return "June";
- case 7: return "July";
- case 8: return "August";
- case 9: return "September";
- case 10: return "October";
- case 11: return "November";
- case 12: return "December";
- }
- return "January"; // Default, incase nothing matches in the switch (which should never happen)
- }
- @Override
- public String toString()
- {
- return getMonthName();
- }
- public Boolean equals(Month obj)
- {
- if(this.getMonthNumber()==obj.getMonthNumber())
- return true;
- else
- return false;
- }
- public Boolean greaterThan(Month obj)
- {
- if(this.getMonthNumber() > obj.getMonthNumber())
- return true;
- else
- return false;
- }
- public Boolean lessThan(Month obj)
- {
- if(this.getMonthNumber() < obj.getMonthNumber())
- return true;
- else
- return false;
- }
- }
- /**
- Month Class Demo Program
- */
- class MonthDemo
- {
- public static void demo1()
- {
- // Use the default constructor.
- Month m = new Month();
- System.out.println("Month " + m.getMonthNumber() +
- " is " + m);
- // Set the month number to the values 0 through 12
- // (0 is invalid), and display the resulting month name.
- for (int i = 0; i <= 12; i++)
- {
- m.setMonthNumber(i);
- System.out.println("Month " + m.getMonthNumber() +
- " is " + m);
- }
- }
- public static void demo2()
- {
- // Use the 2nd constructor to create two objects.
- Month m1 = new Month(10);
- Month m2 = new Month(5);
- System.out.println("Month " + m1.getMonthNumber() +
- " is " + m1);
- System.out.println("Month " + m2.getMonthNumber() +
- " is " + m2);
- // Test for equality.
- if (m1.equals(m2))
- System.out.println(m1 + " and " + m2 + " are equal.");
- else
- System.out.println(m1 + " and " + m2 + " are NOT equal.");
- // Is m1 greater than m2?
- if (m1.greaterThan(m2))
- System.out.println(m1 + " is greater than " + m2);
- else
- System.out.println(m1 + " is NOT greater than " + m2);
- // Is m1 less than m2?
- if (m1.lessThan(m2))
- System.out.println(m1 + " is less than " + m2);
- else
- System.out.println(m1 + " is NOT less than " + m2);
- }
- public static void demo3()
- {
- // Use the 3rd constructor to create three objects.
- Month m1 = new Month("March");
- Month m2 = new Month("december");
- Month m3 = new Month("Bad Month");
- System.out.println("Month " + m1.getMonthNumber() +
- " is " + m1);
- System.out.println("Month " + m2.getMonthNumber() +
- " is " + m2);
- System.out.println("Month " + m3.getMonthNumber() +
- " is " + m3);
- }
- public static void main(String[] args) {
- System.out.println("DEMO 1: setMonthNumber test");
- demo1();
- System.out.println();
- System.out.println("DEMO 2: Constructors and comparisons");
- demo2();
- System.out.println();
- System.out.println("DEMO 3: Construction from month names");
- demo3();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement