/** * Reads descriptions of a series of events from stdin. * Every event has a date. * Each event is a wedding, lecture, meeting, or dinner. * Computes the cost of each event using an event-specific formula. * Prints the sum of the costs and a description of the most * expensive event. * * Author: Don Chamberlin, March 2009 */ import java.util.*; import java.io.*; class Catering { public static void main(String[] args) { ArrayList events = new ArrayList(); String date = ""; Scanner scan = new Scanner(System.in); String eventType = scan.next(); while (!eventType.equals("end")) { if (eventType.equals("wedding")) { date = scan.next(); int nGuests = scan.nextInt(); int costPerGuest = scan.nextInt(); events.add(new Wedding(date, nGuests, costPerGuest)); } else if (eventType.equals("lecture")) { date = scan.next(); int lunchCost = scan.nextInt(); int speakerCost = scan.nextInt(); events.add(new Lecture(date, lunchCost, speakerCost)); } else if (eventType.equals("meeting")) { date = scan.next(); int lunchCost = scan.nextInt(); events.add(new Meeting(date, lunchCost)); } else if (eventType.equals("dinner")) { date = scan.next(); int foodCost = scan.nextInt(); int wineCost = scan.nextInt(); events.add(new Dinner(date, foodCost, wineCost)); } eventType = scan.next(); } // end of while-loop over inputs // Note that this part of the program does not make reference to // any particular event, so it does not change if new kinds of // events are added later. int totalCost = 0; int greatestCost = -1; Event mostExpensiveEvent = null; for (Event e: events) { int eCost = e.cost(); totalCost += eCost; if (eCost > greatestCost) { mostExpensiveEvent = e; greatestCost = eCost; } } System.out.println("Total cost = " + totalCost); System.out.println("Most expensive event is a " + mostExpensiveEvent); } // end of main() } // end of class Catering /* * Event is an abstract class that defines the cost() * and toString() methods but does not implement them. * Each subclass of Event provides implementations for * these two methods. */ abstract class Event { String date; Event(String date) { this.date = date; } abstract int cost(); public abstract String toString(); } class Wedding extends Event { int nGuests; int costPerGuest; Wedding(String date, int nGuests, int costPerGuest) { super(date); this.nGuests = nGuests; this.costPerGuest = costPerGuest; } int cost() { return nGuests * costPerGuest; } public String toString() { return "wedding on " + date; } } class Lecture extends Event { int lunchCost; int speakerCost; Lecture(String date, int lunchCost, int speakerCost) { super(date); this.lunchCost = lunchCost; this.speakerCost = speakerCost; } int cost() { return lunchCost + speakerCost; } public String toString() { return "lecture on " + date; } } class Meeting extends Lecture { int lunchCost; Meeting(String date, int lunchCost) { super(date, lunchCost, 0); } int cost() { return super.cost(); } public String toString() { return "meeting on " + date; } } class Dinner extends Event { int foodCost; int wineCost; Dinner(String date, int foodCost, int wineCost) { super(date); this.foodCost = foodCost; this.wineCost = wineCost; } int cost() { return foodCost + wineCost; } public String toString() { return "dinner on " + date; } }