Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENUMERATED TYPES Java Basics. Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features.

Similar presentations


Presentation on theme: "ENUMERATED TYPES Java Basics. Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features."— Presentation transcript:

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 remindTuesdayPatients() { if (whichDay == 2) System.out.println("Remember your appointment!"); } public static void main(String[] args) { Appointment appointment = new Appointment(); appointment.scheduleAppointment(); 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; typically public, so can access outside this class keyword to indicate this is enumerated type name of the enum Similar to class, this is a new data type! 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 – it’s 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 { ArrayList invoices; public void addInvoices() { invoices = new ArrayList (); 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(); track.displayInvoices(); }

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(); }


Download ppt "ENUMERATED TYPES Java Basics. Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features."

Similar presentations


Ads by Google