1. a public void method 'sortByName' that sorts an array of Customer objects by name
2. a public void method 'sortByPurchase' that sorts an array of Customer objects by purchase
3. a method named searchByName that searches an array A of Customer objects for a Customer object whose name is equal to a parameter string. If such an object is found in the array A, the method returns the index i of such that A[i] is the object found; if such an object cannot be found, the method returns -1.
The program reads data from a file named indata.txt to create Customer objects of an array and returns the array.
Here is the program
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class prog5 {
public static void main(String args[])
throws FileNotFoundException
{
Scanner kb=new Scanner(System.in);
Customer [] A =readArray(); //read data into objects in A
while(true)
{
System.out.println();
System.out.println();
/*here, show prompt messages and menu*/
System.out.println("Please select one of the follwing actions:");
System.out.println("q - Quit");
System.out.println("a - List the customer records sorted by customer name");
System.out.println("b - Enter a customer name to find the customer's record");
System.out.println("c - List the customer records sorted by purchase");
System.out.println("Please enter q, a, b, or c:");
String selection=kb.nextLine(); //read user's selection
if (selection.equals("")) continue; //break; //if selection is "", show menu again
switch (selection.charAt(0))
{
case 'a':
sortByName(A);
System.out.println("The customer list sorted by customer name:");
printArray(A);
System.out.println();
break;
case 'b':
sortByName(A);
System.out.println("Please enter a customer name:");
String n=kb.nextLine();
for(int i=n.length(); i < 16; i++) n=n+" ";
int k = searchByName(A, keyName);
if (k < 0)
System.out.println("Cannot find customer named "+n);
else
System.out.println("Customer found:\n"+A[k]);
System.out.println();
break;
case 'c':
//Arrays.sort(A, new CompareByPurchase());
sortByPurchase(A);
System.out.println("The customer list sorted by purchase:");
printArray(A);
System.out.println();
break;
case 'q':
System.out.println("\nThank you.");
return;
default: System.out.println("Incorrect selection.\n");
} //end switch
} //end while
} //end main();
//the following method uses the data from indata.txt
//to create Customer objects of an array
//and returns the array
private static Customer[] readArray()
throws FileNotFoundException
{
String name;
double purchase;
double rebate;
Scanner infile=new Scanner(new File("indata.txt")); //input file
int size=infile.nextInt(); //get number of lines
infile.nextLine(); //skips end of line
Customer A[]=new Customer[size];
for (int k=0; k<size; k++)
{
//read a name = 16 characters
infile.useDelimiter("");
name="";
for (int i=0; i<16; i++) name=name+infile.next();
infile.reset();
purchase=infile.nextDouble();
rebate=infile.nextDouble();
infile.nextLine(); //skip end of line
A[k]=new Customer(name, purchase, rebate); //create object for A[i]
} //end for loop
infile.close();
return A;
} //end readArray
//the method prints the Customer objects of the array A
//one customer record per line
private static void printArray(Customer [] A)
{
for (Customer x:A)
{
System.out.println(x);
}
} //end printArray
} //end class prog4
class Customer implements Comparable <Customer>
{
String name;
double purchase;
double rebate;
public Customer(String n, double p, double r)
{ name=n;
purchase=p;
rebate=r;
} //end Constructor
public Customer(String n)
{
name=n;
} //end Constructor
public String toString()
{
return String.format("%-20s%10.2f%10.2f", name, purchase, rebate);
}
public int compareTo(Customer a)
{
return name.compareTo(a.name);
}
public static void sortByName(Customer[] a)
{
int size = a.length;
int i;
int j;
int minindex;
Customer temp;
for (i =0; i<size -1; i ++)
{
minindex = i;
for(j=i+1; j<size; j++)
if(a[j].name.compareTo(a[minindex].name) < 0)
temp = a[i];
a[i] = a[minindex];
temp = a[minindex];
}
}
public static void sortByPurchase (Customer[] a)
{
int size = a.length;
int i, j, minindex;
Customer temp;
for(i = 0; i < size - 1; i++)
{
minindex = i;
for(j = i +1; j < size; j++)
if(a[j].purchase.compareTo(a[minindex].purchase) < 0)
temp = a[i]; a[i] = a[minindex]; temp = a[minindex];
}
}
public static int searchByName(Customer[] a, String keyName)
{
int lastIndex = a.length -1;
for (int i = 0; i <= lastIndex; i ++)
if (a[i].name.equals(keyName)) return i;
return -1;
}
} //end class Customer
I'm getting several errors, which I think are just syntax and order issues that I can't seem to figure out, I'm pretty sure I have something in the wrong place and it's a somewhat simple fix, but I need a little assistance.
Here are the errors I get when I compile.
> Executing: C:\Program Files (x86)\ConTEXT\ConExec.exe "C:\Program Files\Java\jdk1.6.0_23\bin\javac.exe" "C:\Users\Matthew\Desktop\CS121\prog5.java"
C:\Users\Matthew\Desktop\CS121\prog5.java:35: cannot find symbol
symbol : method sortByName(Customer[])
location: class prog5
sortByName(A);
^
C:\Users\Matthew\Desktop\CS121\prog5.java:42: cannot find symbol
symbol : method sortByName(Customer[])
location: class prog5
sortByName(A);
^
C:\Users\Matthew\Desktop\CS121\prog5.java:46: cannot find symbol
symbol : variable keyName
location: class prog5
int k = searchByName(A, keyName);
^
C:\Users\Matthew\Desktop\CS121\prog5.java:57: cannot find symbol
symbol : method sortByPurchase(Customer[])
location: class prog5
sortByPurchase(A);
^
C:\Users\Matthew\Desktop\CS121\prog5.java:185: double cannot be dereferenced
if(a[j].purchase.compareTo(a[minindex].purchase) < 0)
^
5 errors
> Execution finished.
Any help would be greatly appreciated.

New Topic/Question
Reply



MultiQuote



|