Enumerated Types - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Enumerated Types

Description:

Java Basics ENUMERATED TYPES Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features Before Enumerated Types ... – PowerPoint PPT presentation

Number of Views:159
Avg rating:3.0/5.0
Slides: 10
Provided by: Cynd66
Category:

less

Transcript and Presenter's Notes

Title: Enumerated Types


1
Enumerated Types
  • Java Basics

2
Enumerated Type Topics
  • Why use them?
  • Defining an enumerated type
  • Using an enumerated type
  • Advanced features

3
Before Enumerated Types
public class Appointment private int
whichDay private int scheduleLocation
public void scheduleAppointment()
System.out.println("Enter 1 for Monday, 2 for
Tuesday or 3 for Thursday ")
Scanner scan new Scanner(System.in)
whichDay scan.nextInt() System.out.println(
"Enter 1 for Golden, 2 for Boulder or 3
for Lafayette ") scheduleLocation
scan.nextInt() public void remindTuesdayPatient
s() if (whichDay 2)
System.out.println("Remember your
appointment!") public static void
main(String args) Appointment appointment
new Appointment() appointment.scheduleAppoi
ntment() appointment.remindTuesdayPatients()

4
Enumerated Types - defining
  • An enum type is a type whose fields consist of a
    fixed set of constants.
  • Define the type
  • public enum FilingStatus SINGLE,MARRIED
  • Declare a variable of this type
  • FilingStatus status

keyword to indicate this is enumerated type
name of the enum Similar to class, this is a new
data type!
typically public, so can access outside this
class
fixed set of possible values in
ALL_CAPS because they are like constants. In
braces similar to class definition, but
with special syntax for body.
enum name its a type!
name of variable
5
Enumerated Types - using
  • To specify a value, must include the name of the
    enumerated type
  • if (status FilingStatus.SINGLE) . . .
  • Can use in another class, if specify class name
    in which enum is declared
  • if (status TaxReturn.FilingStatus.SINGLE) . .
    .
  • An enumerated type variable can be null

variable of type FilingStatus
without this, SINGLE would not be recognized
(would look like a regular constant)
must be one of the options for FilingStatus
6
Enumerated Type - Example
  • public class Invoice
  • public enum Status PAID, UNPAID, WRITE_OFF
  • Status status
  • double balance
  • public Invoice(double balance)
  • this.balance balance
  • status Status.UNPAID
  • public void updateStatus(Status status)
  • this.status status
  • public String toString()
  • return "Invoice balance " balance "
    status " status

7
Example continued
  • public class InvoiceTracker
  • ArrayListltInvoicegt invoices
  • public void addInvoices()
  • invoices new ArrayListltInvoicegt()
  • Invoice inv new Invoice(300)
  • inv.updateStatus(Invoice.Status.PAID)
  • invoices.add(inv)
  • inv new Invoice(650)
  • inv.updateStatus(Invoice.Status.WRITE_OFF)
  • invoices.add(inv)
  • public void displayInvoices()
  • for (Invoice inv invoices)
  • System.out.println(inv)
  • public static void main(String args)
  • InvoiceTracker track new InvoiceTracker()
  • track.addInvoices()

8
Another Example
  • Enumerated types can be defined in their own file
  • Enumerated types can define methods, useful for
    converting to String, etc.
  • public enum DayOfWeek
  • MONDAY ("Monday"), TUESDAY ("Tuesday"),
    WEDNESDAY ("Wednesday"), THURSDAY ("Thursday"),
    FRIDAY ("Friday")
  • private String value
  • DayOfWeek (String aValue)
  • value aValue
  • public String toString()
  • return value

9
Revised Appointment
  • public class Appointment
  • //private int whichDay
  • private DayOfWeek whichDay
  • private int scheduleLocation
  • public void scheduleAppointment()
  • System.out.println("Enter day of week ")
  • Scanner scan new Scanner(System.in)
  • String day scan.next()
  • whichDay DayOfWeek.valueOf(day.toUpperCase())
  • public void remindTuesdayPatients()
  • if (whichDay DayOfWeek.TUESDAY)
  • System.out.println("Remember your
    appointment!")
  • public static void main(String args)
  • Appointment appointment new Appointment()
  • appointment.scheduleAppointment()
  • appointment.remindTuesdayPatients()
Write a Comment
User Comments (0)
About PowerShow.com