5 Replies - 1043 Views - Last Post: 30 November 2010 - 11:15 PM Rate Topic: -----

#1 jsc0290   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 10

Java Help-

Posted 30 November 2010 - 12:18 AM

hello. Im trying to write a program.. but I need help..

i) Now create a data file consisting of 5 customers. Each customer will have data representing all 5 data fields for the Person Class as well as the Customer Class combined.

ii) Create a driver (demo) program that will read all 5 customers into an Array of type Customer.

iii) Create static methods in the driver program to:

•Read-in the customer data from the external data file.
◦In order to capture the name field, address field, phone field, etc, to store in your Customer object, you will probably want to tokenize a given line of data from the input file.
•Print out all the customer data in the array.
•Sequentially search for a particular customer by customer ID number. If the customer ID is found, the corresponding data (name, address, and phone number) for that customer should be printed. Otherwise print a message indicating that the customer is not found. See the sample run below:

Would you like to search for a customer? (Y or N): Y

Please enter the customer ID you would like to find: 128-A430

The Customer Data corresponding to Customer Number 128-A430 is:

Name: Mickey Mouse

Address: 6 Disney Street

Telephone: 555-8976


Would you like to search for another customer? (Y or N): Y

Please enter the customer ID you would like to find: 12345657

Sorry, customer not found.


Would you like to search for another customer? (Y or N): N


import java.util.Scanner;
import java.io.*;

/**
*/

public class Customer
{
   public static void main(String[] args) throws IOException
   {  
	   
		String customerID;
		String input;
		String filename;
		char repeat;
                              
		Scanner keyboard = new Scanner(System.in);
		
		System.out.print("Enter the filename: ");
		filename = keyboard.nextLine();
		
		FileReader freader = new FileReader(filename);
		BufferedReader inputFile = new BufferedReader(freader);
		
		System.out.println("Please enter the customer ID you would like to find:");
		customerID = keyboard.nextLine();
		
		System.out.println("The Customer Data corresponding to Customer Number " + customerID  + " is:");
      System.out.println("Name:     " + "");
      System.out.println("Address:     " + "");
      System.out.println("Telephone:     " + "");
		
		System.out.println("Would you like to search for another customer? (Y or N):");
		input = keyboard.nextLine();
		repeat = input.charAt(0);
		
		while (repeat == 'Y' || repeat == 'y')
		{	
		System.out.println("Please enter the customer ID you would like to find:");
		customerID = keyboard.nextLine();
		
	   System.out.println("The Customer Data corresponding to Customer Number " + customerID  + " is:");
      System.out.println("Name:     " + "");
      System.out.println("Address:     " + "");
      System.out.println("Telephone:     " + "");
		
		System.out.println("Would you like to search for another customer? (Y or N):");
		input = keyboard.nextLine();
		repeat = input.charAt(0);
	   }
		
		inputFile.close();
		
		System.exit(0);
		   }
}


This post has been edited by jsc0290: 30 November 2010 - 12:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Help-

#2 masijade   User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Java Help-

Posted 30 November 2010 - 12:34 AM

Okay? And your question is?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_chaos386*


Reputation:

Re: Java Help-

Posted 30 November 2010 - 09:59 PM

well we could help but you have not told us what you are having trubble with but as luck would have it i got bord and have 2 minites to kill.
class Customer
/**
 * @(#)Customer.java
 *
 *
 * @author Chaos
 * @version 1.00 2010/12/1
 */


public class Customer {
	public String name="";
	public String address ="";
	public String phone ="";
	public String ID = "";

    public Customer() {
    }


}


Driver.java
/**
 * @(#)Driver.java
 *
 *
 * @author Chaos
 * @version 1.00 2010/12/1
 */

import java.util.Scanner;
import java.io.*;

public class Driver {
	private static int numberOfCustomer = 0;
    private static Customer[] customerList = new Customer[10];

	/**
	 * readInCustomer() : reads the txt file for data
	 *
	 * @pram String FileName
	 * @return void
	 */
	private static void readInCustomer(String file){
		FileReader freader;
		BufferedReader inputFile;
		try{
			freader = new FileReader(file);
			inputFile = new BufferedReader(freader);
			String strLine;
    		//Read File Line By Line
    		while ((strLine = inputFile.readLine()) != null)   {
      				customerList[numberOfCustomer] = new Customer();
      				customerList[numberOfCustomer].ID = strLine;
      				customerList[numberOfCustomer].name  = inputFile.readLine();
      				customerList[numberOfCustomer].address  = inputFile.readLine();
      				customerList[numberOfCustomer].phone  = inputFile.readLine();
      				numberOfCustomer++;
    		}
			inputFile.close();
		}catch(Exception e){
			System.out.println("Could not find file "+file+" System will now exit");
			System.exit(1);
		}

		return;
	}

	/**
	 * addCustomer() adds a customer/person to the array if the
	 * array is to small then it expandes the array
	 *
	 * @pram Customer
	 * @return void
	 */
	private static void addCustomer(Customer customer){
		if(numberOfCustomer >= customerList.length-1){
			Customer[] temp = new Customer[customerList.length *2];
			for(int i=0;i<customerList.length;i++){
				if(customerList[i] != null){
					temp[i] = customerList[i];
				}
				customerList = temp; //replaces customerList with temp
			}
		}
		customerList[numberOfCustomer] = customer; //adds a new customer to the list
		System.out.println(customerList[numberOfCustomer].ID);
		numberOfCustomer++; //keeps trake of number of Customer
		return;
	}

	/*
	 * printCustomer(): prints out a singal customers detailes
	 * @pram Customer
	 * @return void
	 */
	private static void printCustomer(Customer customer){
		System.out.println("The Customer Data corresponding to Customer Number " + customer.ID  + " is:");
		System.out.println("Name:\t\t\t"+customer.name);
		System.out.println("Address:\t\t"+customer.address);
		System.out.println("Telephone:\t\t"+customer.phone);
		System.out.println();
		return;
	}

	/**
	 * printAll(): prints a list of all the customers from the file
	 *
	 * @pram void
	 * @return void
	 */
	private static void printAll(){

		System.out.println("All customers from data file "+numberOfCustomer);
		System.out.println("-------------------------------------------------");
		for(int i=0; i<numberOfCustomer; i++){
			if(customerList[i] != null){
				System.out.println("The Customer Data corresponding to Customer Number " + customerList[i].ID  + " is:");
				System.out.println("Name:\t\t\t"+customerList[i].name);
				System.out.println("Address:\t\t"+customerList[i].address);
				System.out.println("Telephone:\t\t"+customerList[i].phone);
			}
		}
		System.out.println("-------------------------------------------------");
		return;
	}

	private static void search(){
		boolean hasID = false;
		Scanner keyboard = new Scanner(System.in); //setup the scanner to read in from the keyboard

		System.out.println("Please enter the customer ID you would like to find:");
		String customerID = keyboard.nextLine();

		for(int i=0; i<numberOfCustomer; i++){
			if((customerList[i]!=null) && (customerID.equals(customerList[i].ID))){
				hasID = true;
				printCustomer(customerList[i]);
				i=customerList.length;
			}
		}

		if(!hasID){
			System.out.println("Sorry, customer not found.");
		}
		System.out.println("Would you like to search for another customer? (Y or N):");
		String input = keyboard.nextLine();
        char repeat = input.charAt(0);
         if(repeat == 'Y' || repeat == 'y'){search();} //if yes repeat serch
		return;
	}

    public static void main(String arg[]){
    	Scanner keyboard = new Scanner(System.in); //setup the scanner to read in from the keyboard
    	System.out.println("Enter the fileName: ");
    	readInCustomer(keyboard.nextLine());
    	search();
    	System.exit(0);
    }
}



test file
128-A430
Mickey Mouse
6 Disney Street
555-8976
168-A430
Min Mouse
6 Disney Street
555-8976
198-A430
cat in the hat
50 Disney Street
5543-8976
138-A430
green somthing
666 Disney Street
555-89543534
200-A430
Death bunny
50 Disney Street
5543-8976



hope it helps but as i said i realy did'ent have time to clean it up but it does work
Was This Post Helpful? 0

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Help-

Posted 30 November 2010 - 10:03 PM

Please respect rule #4 of this forum

Give your topic a descriptive title, NOT "Help" or "Plz Help Me" or "I'm a noob"

"Java help" is the most useless forum topic title that you can imagine for this forum and can apply to 99.9% of the posts

This post has been edited by pbl: 30 November 2010 - 10:04 PM

Was This Post Helpful? 1
  • +
  • -

#5 jsc0290   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 10

Re: Java Help-

Posted 30 November 2010 - 10:29 PM

Im sorry everyone..

How do I delete this question??

I will post a new one -

If not,, Please discard this question...

THank you...

This post has been edited by jsc0290: 30 November 2010 - 10:30 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12355
  • View blog
  • Posts: 45,464
  • Joined: 27-December 08

Re: Java Help-

Posted 30 November 2010 - 11:15 PM

You cannot delete this post. It's fine open. That way, others who may have the same problem down the road can benefit from this thread. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1