/* * Matric exam number */ package antelope; import javax.swing.JOptionPane; public class AntelopeUI { public static void main(String[] args) { String name; double price; int theIndex; AntelopeManager am = new AntelopeManager(); String allRecords = am.displayAll(); System.out.println(allRecords); name = JOptionPane.showInputDialog(null,"Find price." + "\n" + "Name of antelope."); price = am.getThePrice(name); System.out.println("The price of the antelope is " + price); name = JOptionPane.showInputDialog(null,"Edit price." + "\n" + "Name of antelope."); am.setThePrice(name); } } // end UI class ======================================================== /* * Matric number */ package antelope; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import javax.swing.JOptionPane; public class AntelopeManager { private Antelope[] antelope = new Antelope[200]; private int size = 0; public AntelopeManager() { try { Scanner scFile = new Scanner(new File("antelope.txt")); size = 0; while (scFile.hasNext()) { String line = scFile.nextLine(); Scanner scLine = new Scanner(line).useDelimiter(","); String name = scLine.next(); String colour = scLine.next(); double price = scLine.nextDouble(); boolean isProtected = scLine.nextBoolean(); antelope[size] = new Antelope(name, colour,price,isProtected); size++; } // end while } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null, "Error! File not found"); System.exit(0); } // end catch } // end constructor // Display all the records public String displayAll(){ String output = ""; for (int i = 0; i < size; i++){ output = output + (antelope[i].toString() + "\n"); } return output; } // end displayAll // Teaching only method to show "how to search" public int searchAntelope(String n){ String name = n; int theIndex = 0; for (int i = 0; i < size; i++){ if (antelope[i].getName().equals(name)){ theIndex = i; break; // No need to search further } } return theIndex; } // end searchAntelope // The the price of a specific antelope public double getThePrice(String n){ String name = n; double price = 0.0; int theIndex = 0; for (int i = 0; i < size; i++){ if (antelope[i].getName().equals(name)){ theIndex = i; price = antelope[i].getPrice(); break; // No need to search further } } // end loop return price; } // end getThePrice // Edit the price of a specific antelope public void setThePrice(String n){ String name = n; double price = 0.0; double newPrice = 0.0; int theIndex = 0; for (int i = 0; i < size; i++){ if (antelope[i].getName().equals(name)){ theIndex = i; String newPriceSt = JOptionPane.showInputDialog("Enter new price for " + name); newPrice = Double.parseDouble(newPriceSt); antelope[i].setPrice(newPrice); break; } } // end loop System.out.println("The price of the antelope " + name + " has been updated to " + newPrice); } // end setThePrice } // end class ========================================================== /* * Matric number */ package antelope; public class Antelope { private String name; private String country; private String colour; private double price; private boolean isProtected; private String details; // Constructor method public Antelope(String n, String c, double p, boolean pr) { name = n; int space = name.indexOf(" "); String country = name.substring(space + 1); if (country.equals("ZA")) country = "South Africa"; else if (country.equals("NM")) country = "Nambia"; else if (country.equals("ZM")) country = "Zambia"; else if (country.equals("BO")) country = "Botswana"; else if (country.equals("MZ")) country = "Mozambique"; else { System.out.println("Error"); System.exit(0); } colour = c; price = p; isProtected = pr; } public String getName(){ return name; } public double getPrice() { return price; } public void setPrice(double p){ price = p; } public String toString(){ details = name + "\t" + country + "\t" + colour + "\t" + price + "\t" + isProtected; return details; } } ========================================== impala ZA,brown,5000,false impala BO,brown,5000,false impala MZ,brown,7000,false eland ZA,grey,35000,false eland NM,grey,55000,true sable ZA,black,65000,true sable BO,grey,75000,true sable ZM,black,85000,true duiker ZA,blown,1000,false duiker MZ,brown,2000,true duiker ZM,brown,3000,false gemsbok NM,white,50000,true ====================================