1 / 9

Enumerated Types

Java Basics. Enumerated Types. Enumerated Type Topics. Why use them? Defining an enumerated type Using an enumerated type Advanced features. Before Enumerated Types. public class Appointment { private int whichDay; private int scheduleLocation; public void scheduleAppointment() {

Download Presentation

Enumerated Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Basics Enumerated Types

  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; 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 – 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<Invoice> invoices; public void addInvoices() { invoices = new ArrayList<Invoice>(); 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(); } }

More Related