import javax.swing.*; /** * The MainMeal class is designed to test the two classes * MealCard and Student. The classes MealCard and Student * are to be written by my CSC 211 students. It is Homework 5, * for Fall quarter, 2005-06. * * @author G. Andrus * @version October 21, 2005 */ public class MainMeal { /** * A driver to test MealCard and Student classes */ public static void main(String[] args) { final double TEST_AMT1 = 63.66; final double TEST_AMT2 = 104.16; final int MAX_CARDS = 20; MealCard[] mc = new MealCard[MAX_CARDS]; boolean more; int yesno, count = 0; do { mc[count++] = createMealCard(); yesno = JOptionPane.showConfirmDialog(null, "Create another meal card?", "Confirmation", JOptionPane.YES_NO_OPTION); more = (yesno == JOptionPane.YES_OPTION); } while (more && count < MAX_CARDS); Student std1 = new Student("Smithson", "Ide", 40119); MealCard mc1 = new MealCard(std1, 250); System.out.println(mc1); System.out.println(); MealCard mc2 = new MealCard(); mc2.setStudent("Otis", "Danforth", 21963); System.out.println(mc2); System.out.println(); for (int j = 0; j < count; j++) { System.out.println("Transaction: BUY FOOD"); if (!mc[j].buyFood(TEST_AMT1)) System.out.println("You need at least " + (int)Math.round(TEST_AMT1 * 4) + " points,\n" + "your balance of " + mc[j].getBalance() + " points is insufficient."); else System.out.println("Transaction completed. " + "Remaining balance: " + mc[j].getBalance() + " points."); System.out.println( "Transaction: BUY " + (int)Math.round(TEST_AMT2 * 4) + " POINTS"); mc[j].buyPoints(TEST_AMT2); System.out.println(mc[j]); System.out.println(); } System.exit(0); } /** * Prompts the user for all necessary data to create a MealCard * object. Creates a MealCard object containing the data entered. * * @param * @return mc, a MealCard object */ public static MealCard createMealCard() { String name = JOptionPane.showInputDialog( null, "Enter your name (first last):"); String idStr = JOptionPane.showInputDialog( null, "Enter your five digit student ID number:"); String balStr = JOptionPane.showInputDialog( null, "Enter your starting food card balance (in points):"); int i = name.indexOf(" "); String fname = name.substring(0, i); String lname = name.substring(i+1); int sid = Integer.parseInt(idStr); int bal = Integer.parseInt(balStr); Student std = new Student(fname, lname, sid); MealCard mc = new MealCard(std, bal); return mc; } }