import java.io.*; /** * This Class demonstrates a console version of BiscuitWindow * * @author (your name) * @version (a version number or a date) */ public class BiscuitConsole { // Filename to load and save records from private static final String FILENAME = "c:\\biscuits.txt"; private static final String EOF = "XXXX"; // Array to hold biscuit collection private static final int MAX = 10; private Biscuit2[] biscuits = new Biscuit2[MAX]; // pointers into the biscuit collection int lastBiscuit = -1; int current = -1; public static void main(String[] args) { new BiscuitConsole(); } /** * Constructor for objects of class BiscuitConsole */ public BiscuitConsole() { // Get the cookies from the data file readFromFile(); String c; do { c = getCommand(); doCommand(c); }while (!c.equals("q")); } private String getCommand() { String commandList = ("adnpflhq"); // add, del, next, prev, first, last, help, quit String c = input("Please input a command (h for help):"); c = c.trim().substring(0,1).toLowerCase(); //check c against list of valid commands: if (commandList.indexOf(c) == -1) { return "h"; } else { return c; } } private void doCommand(String c) { char ch = c.charAt(0); if (ch == 'a') { output("add biscuit to array code required"); } else if (ch == 'd') { output("delete code needed"); } else if (ch == 'q') { output("bye then"); System.exit(0); } else { output("some other code needed..."); } } /** * load the array contents from text file */ private void readFromFile() { try { lastBiscuit = -1; SimpleReader reader = new SimpleReader(FILENAME); // read first line of file, if file is empty should be EOF String next = reader.readLine(); while (!next.equals(EOF)) { lastBiscuit = lastBiscuit + 1; biscuits[lastBiscuit] = new Biscuit2(next); next = reader.readLine(); } // show the last one, check for none if (lastBiscuit >=0) { current = lastBiscuit; showBiscuit(biscuits[current]); output("File opened with " + (lastBiscuit + 1) + " records"); } else { output("The file is empty"); } } catch(IOException io) { output("Error reading from file"); } } private void showBiscuit(Biscuit2 b ) { output("Biscuit description"); output("==================="); output("Shape: " + b.getShape()); output("Taste: " + b.getTaste()); output("Price: " + b.getPrice()); output("Unit: " + b.getUnit()); output("My favourite: " + b.isMyFavourite()); output(""); } /** * IBIO methods, (c) International Baccalaureate 2003 * Computer Science Subject Guide, Appendix 2. */ static void output(String info) { System.out.println(info); } static void output(double info) { System.out.println(info); } static void output(int info) { System.out.println(info); } static int inputInt(String Prompt) { int result=0; try{result=Integer.parseInt(input(Prompt).trim());} catch (Exception e){result = 0;} return result; } static double inputDouble(String Prompt) { double result=0; try{result=Double.valueOf(input(Prompt).trim()).doubleValue();} catch (Exception e){result = 0;} return result; } static String input(String prompt) { String inputLine = ""; System.out.print(prompt); try { java.io.InputStreamReader sys = new java.io.InputStreamReader(System.in); java.io.BufferedReader inBuffer = new java.io.BufferedReader(sys); inputLine = inBuffer.readLine(); } catch (Exception e) { String err = e.toString(); System.out.println(err); } return inputLine; } static String input() { return input(""); } }