100 likes | 235 Vues
This guide provides a comprehensive overview of enumerated types in Java, detailing their purpose, benefits, and defining techniques. Learn how to create and utilize enumerated types effectively. Your journey will include defining an enum, using it in classes like `Appointment` and `Invoice`, and exploring advanced features. Benefit from real-world examples that demonstrate how to manage statuses and appointments with ease while maximizing type safety and readability in your Java applications.
E N D
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() { 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(); } }
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
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
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; } }
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(); } }
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; } }
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(); } }