Need help regarding Linked List

Discussion in 'Java' started by aevylainreds, Mar 11, 2007.

  1. aevylainreds

    aevylainreds New Member

    Joined:
    Mar 11, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I'm a beginner who just spent ages working on the following code.. but need help on re-implementing the following using a linked list, i.e. no array is allowed for customer records but you still can use arrays for names, address, etc.. Hopefully I've inserted enough comments..

    Help very much appreciated!! Thanks! =]

    -----------------------------------------------------------------------------------------------
    Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Bank
    {
    /* Private variables declared so that the data is only accessible to its own
    class, but not to any other class, thus preventing other classes from
    referring to the data directly */
    private static Customer[] customerList = new Customer[30];
    //Array of 30 objects created for storing information of each customer
    private static int noOfCustomers;
    //Integer used to store number of customers in customerList
    
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    menu();
    }
    
    public static void menu()
    {
    char choice;
    String filename;
    int custId,counter=0;
    double interestRate;
    Scanner sc = new Scanner(System.in);
    
    do
    {
    //Displaying of Program Menu for user to choose
    System.out.println("ABC Bank Customer Management System Menu");
    System.out.println("========================================");
    System.out.println("(1) Input Data from File");
    System.out.println("(2) Display Data");
    System.out.println("(3) Output Data to File");
    System.out.println("(4) Delete Record");
    System.out.println("(5) Update Record");
    System.out.println("(Q) Quit");
    System.out.println();
    System.out.print("Enter your choice: ");
    String input = sc.next();
    System.out.println();
    choice = input.charAt(0);
    //switch statement used to assign each 'selection' to its 'operation'
    switch(choice)
    {
    case '1': int noOfRecords;
    System.out.print("Enter file name: ");
    sc.nextLine();
    filename = sc.nextLine();
    System.out.println();
    noOfRecords = readFile(filename);
    System.out.println(+noOfRecords+" records read.");
    break;
    
    case '2': displayRecords();
    break;
    
    case '3': writeFile();
    break;
    
    case '4': System.out.print("Enter account ID to be deleted: ");
    sc.nextLine();
    custId = sc.nextInt();
    deleteRecord(custId);
    break;
    
    case '5': if(counter==0)
    {
    System.out.print("Enter current interest rate for saving account: ");
    sc.nextLine();
    interestRate = sc.nextDouble();
    update(interestRate);
    counter++;
    }
    else
    System.out.println("Error: Accounts have been updated for the month.");
    break;
    }System.out.println();
    }while(choice!='Q' && choice!='q');
    }
    
    /* The method readFile() loads the customer list of a Bank from a specified
    text file fileName into customerList to be stored as array of Customer
    objects in customerList in ascending alphabetical order according to the
    customer names */
    
    public static int readFile(String fileName)
    {
    int custId,i=0;
    String custName,custAddress,custBirthdate,custPhone,custAccType;
    double custBalance,curRate;
    boolean d;
    /* Try block to enclose statements that might throw an exception, followed by
    the catch block to handle the exception */
    try
    {
    Scanner sc = new Scanner(new File(fileName));
    while(sc.hasNext())
    {
    /* sc.next() gets rid of "Account", "Id" and "=" */
    sc.next();sc.next();sc.next();
    custId = sc.nextInt();
    
    d=checkDuplicate(custId);
    /* checkDuplicate() is a method created to locate duplicating ids in array */
    if(d==true)
    {
    /* A return value of true indicates duplicating record and the sc.nextLine()
    will get rid of all the following lines to read the next customer's record */
    sc.nextLine();sc.nextLine();sc.nextLine();
    sc.nextLine();sc.nextLine();sc.nextLine();
    continue;
    }
    /* A return value of false indicates no duplicating record and the following
    lines containing the information of that customer's record is being read
    in */
    if(d==false)
    {
    /* sc.next() gets rid of "Name" and "=" and name is changed to upper case*/
    sc.next();sc.next();
    custName = sc.nextLine().toUpperCase();
    /* sc.nextLine get rids of the following lines to read the next customer's
    record if length of name is more than 20 characters*/
    if(custName.length()>21)
    {
    System.out.println("Name of custId "+custId+" is more than 20 characters");
    System.out.println();
    sc.nextLine();sc.nextLine();sc.nextLine();sc.nextLine();
    continue;
    }
    /* sc.next() gets rid of "Address" and "=" */
    sc.next();sc.next();
    custAddress = sc.nextLine();
    /* sc.nextLine get rids of the following lines to read the next customer's
    record if length of address is more than 80 characters*/
    if(custAddress.length()>81)
    {
    System.out.println("Address of custId "+custId+" is more than 80 characters");
    System.out.println();
    sc.nextLine();sc.nextLine();sc.nextLine();sc.nextLine();
    continue;
    }
    /* sc.next() gets rid of "DOB" and "=" */
    sc.next();sc.next();
    custBirthdate = sc.nextLine();
    /* sc.nextLine get rids of the following lines to read the next customer's
    record if length of date of birth is more than 10 characters*/
    if(custBirthdate.length()>11)
    {
    System.out.println("D.O.B of custId "+custId+" is more than 10 characters");
    System.out.println();
    sc.nextLine();sc.nextLine();sc.nextLine();sc.nextLine();
    continue;
    }
    /* sc.next() gets rid of "Phone", "Number" and "=" */
    sc.next();sc.next();sc.next();
    custPhone = sc.nextLine();
    /* sc.nextLine get rids of the following lines to read the next customer's
    record if length of phone number is more than 8 characters*/
    if(custPhone.length()>9)
    {
    System.out.println("Phone no. of custId "+custId+" is more than 8 characters");
    System.out.println();
    sc.nextLine();sc.nextLine();sc.nextLine();sc.nextLine();
    continue;
    }
    /* sc.next() gets rid of "Account", "Balance" and "=" */
    sc.next();sc.next();sc.next();
    custBalance = sc.nextDouble();
    /* sc.next() gets rid of "Account", "Type" and "=" */
    sc.next();sc.next();sc.next();
    custAccType = sc.next();
    if(custAccType.equals("Saving"))
    {
    customerList[noOfCustomers] = new Account1(custId,custName,custAddress,custBirthdate,custPhone,custBalance,custAccType);
    sc.nextLine();
    noOfCustomers++;
    i++;
    }
    else if(custAccType.equals("Checking"))
    {
    customerList[noOfCustomers] = new Account2(custId,custName,custAddress,custBirthdate,custPhone,custBalance,custAccType);
    sc.nextLine();
    noOfCustomers++;
    i++;
    }
    else if(custAccType.equals("Fixed"))
    {
    sc.next();sc.next();sc.next();sc.next();
    curRate = sc.nextDouble();
    Account3 temp = new Account3(custId,custName,custAddress,custBirthdate,custPhone,custBalance,custAccType,curRate);
    customerList[noOfCustomers]=temp;
    sc.nextLine();
    noOfCustomers++;
    i++;
    }
    else
    System.out.println("Account type not defined.");
    
    
    if(noOfCustomers==30)
    {
    System.out.println("The customer list has reached its maximum limit of 30 records!");
    System.out.println();
    return noOfCustomers;
    }
    }
    }
    }
    //Exceptions to be caught
    catch (FileNotFoundException e)
    {
    System.out.println("Error opening file");
    System.exit(0);
    }
    catch (IOException e)
    {
    System.out.println("IO error!");
    System.exit(0);
    }
    /* Bubblesort method used to sort the array in ascending alphabetical order
    according to customer's name */
    bubbleSort(customerList);
    return i;
    }
    
    /* The method displayRecords() displays the data of the customer records on
    screen */
    
    public static void displayRecords()
    {
    int k;
    /* Displaying text using the printf() method */
    for(k=0;k<noOfCustomers;k++)
    {
    System.out.printf("Name = %s\n", customerList[k].getName());
    System.out.printf("Account Balance = %.2f\n", customerList[k].getBalance());
    System.out.printf("Account Id = %d\n", customerList[k].getId());
    System.out.printf("Address = %s\n", customerList[k].getAddress());
    System.out.printf("DOB = %s\n", customerList[k].getBirthdate());
    System.out.printf("Phone Number = %s\n", customerList[k].getPhone());
    String type = customerList[k].getAccType();
    System.out.println("Account Type = " +type);
    if(type.equals("Fixed"))
    System.out.println("Fixed daily interest = "+((Account3)customerList[k]).getFixed());
    System.out.println();
    }
    }
    
    /* The method writeFile() saves the content from customerList into a
    specified text file. Data is printed on the screen at the same time */
    
    public static void writeFile()
    {
    /* Try block to enclose statements that might throw an exception, followed by
    the catch block to handle the exception */
    try
    {
    int i;
    int n=0;
    //PrintWriter class used to write contents of studentList to specified file
    FileWriter fwStream = new FileWriter("newCustomers.txt");
    BufferedWriter bwStream = new BufferedWriter(fwStream);
    PrintWriter pwStream = new PrintWriter(bwStream);
    
    for(i=0;i<noOfCustomers;i++)
    {
    pwStream.println("Account Id = "+customerList.getId());
    pwStream.println("Name = "+customerList.getName());
    pwStream.println("Address = "+customerList.getAddress());
    pwStream.println("DOB = "+customerList.getBirthdate());
    pwStream.println("Phone Number = "+customerList.getPhone());
    pwStream.printf("Account Balance = %.2f\n", customerList.getBalance());
    pwStream.println("Account Type = "+customerList.getAccType());
    if(customerList.getAccType().equals("Fixed"))
    pwStream.println("Fixed Daily Interest = "+((Account3)customerList).getFixed());
    pwStream.println();
    n++;
    }
    //Closure of stream
    pwStream.close();
    System.out.println(+n+" records written.");
    }
    catch(IOException e)
    {
    System.out.println("IO error!");
    System.exit(0);
    }
    }
    
    //Deletes specified record from list
    public static void deleteRecord(int id)
    {
    int i;
    
    i=locate(id);
    
    if(i==200)
    {
    //checking if account to be deleted does not exist
    System.out.println("Error: no account with the id of "+id+" found!");
    }
    //if account exists
    else
    {
    while(i><noOfCustomers)
    {
    customerList = customerList[i+1];
    i++;
    }
    System.out.println("Account Id: "+id+" has been deleted");
    --noOfCustomers;
    }
    }
    
    //Updates the accounts
    public static void update(double interest)
    {
    int i,j,k;
    double custBalance,addition=0;
    
    for(i=0;i<noOfCustomers;i++)
    {
    if(customerList instanceof Account1)
    {
    for(j=0;j<30;j++)
    {
    addition=customerList.getBalance()*interest;
    custBalance=customerList.getBalance()+addition;
    customerList.setBalance(custBalance);
    }
    }
    else if(customerList instanceof Account2)
    continue;
    else if(customerList instanceof Account3)
    {
    for(j=0;j<30;j++)
    {
    addition=customerList.getBalance()*((Account3)customerList).getFixed();
    custBalance=customerList.getBalance()+addition;
    customerList.setBalance(custBalance);
    }
    }
    else
    System.out.println("Account type not defined");
    }
    System.out.println("The updated balances are: \n");
    
    for(k=0;k<noOfCustomers;k++)
    {
    System.out.printf("Name = %s\n", customerList[k].getName());
    System.out.printf("Account Balance = %.2f\n", customerList[k].getBalance());
    System.out.println();
    }
    }
    
    /* ================== Additional methods ==================== */
    
    /* Bubblesort method to sort the customerList in ascending alphabetical
    order according to customer's name */
    
    public static void bubbleSort(Customer[] x)
    {
    int pass, index;
    Customer tempValue;
    
    for(pass=0; pass><noOfCustomers-1; pass++)
    {
    for(index=0; index><noOfCustomers-1; index++)
    {
    if(customerList[index].getName().compareToIgnoreCase(customerList[index+1].getName()) > 0)
    {
    tempValue = x[index];
    x[index] = x[index+1];
    x[index+1]= tempValue;
    }
    }
    }
    }
    
    /* Method used to check for duplicated ids in array */
    public static boolean checkDuplicate(int id)
    {
    int i;
    for(i=0;i<noOfCustomers;i++)
    {
    if(id == customerList.getId())
    {
    System.out.println("Account Id = "+id+" already exists");
    System.out.println();
    return true;
    }
    }return false;
    }
    
    /* Method to seach for account id in array */
    public static int locate(int id)
    {
    int j;
    
    for(j=0;j<noOfCustomers;j++)
    {
    if(customerList[j].getId()==id)
    {
    return j;
    }
    }
    j=200;
    return j;
    }
    }
    -----------------------------------------------------------------------------------
    Code:
    import java.util.Scanner;
    public class Customer
    {
    /* The following private variables are declared so that the data is only
    accessible to its own class,but not to any other class, thus preventing
    other classes from referring to the data directly */
    
    protected int id;
    protected String name,address,birthdate,phone,accType;
    protected double balance;
    
    // Null constructor of Customer
    public Customer()
    {
    id = 0;
    name = null;
    address = null;
    birthdate = null;
    phone = null;
    balance = 0;
    accType = null;
    }
    
    /* The following statements with the keyword this activates the Customer
    (int id, String name String address, String birthdate, String phone, double
    balance) constructor that has six parameters of account id, name, address,
    date of birth, phone number, account balance and assign the values of the
    parameters to the instance variables of the object */
    public Customer(int id, String name, String address, String birthdate, String phone, double balance, String accType)
    {
    //this is the object reference that stores the receiver object
    this.id = id;
    this.name = name;
    this.address = address;
    this.birthdate = birthdate;
    this.phone = phone;
    this.balance = balance;
    this.accType = accType;
    }
    
    /* The following get methods getId(), getName(), getAddress(), getBirthdate(),
    getPhone(), getBalance() return the values of the corresponding instance
    properties */
    public int getId()
    {
    return id;
    }
    
    public String getName()
    {
    return name;
    }
    
    public String getAddress()
    {
    return address;
    }
    
    public String getBirthdate()
    {
    return birthdate;
    }
    
    public String getPhone()
    {
    return phone;
    }
    
    public double getBalance()
    {
    return balance;
    }
    
    public String getAccType()
    {
    return accType;
    }
    
    
    /* The following set methods setId(), setName(), setAddress(), setBirthdate(),
    setPhone and setBalance() set the values of the corresponding instance
    properties */
    public void setId (int custId)
    {
    id = custId;
    }
    
    public void setName(String custName)
    {
    name = custName;
    }
    
    public void setAddress (String custAddress)
    {
    address = custAddress;
    }
    
    public void setBirthdate (String custBirthdate)
    {
    birthdate = custBirthdate;
    }
    
    public void setPhone (String custPhone)
    {
    phone = custPhone;
    }
    
    public void setBalance (double custBalance)
    {
    balance = custBalance;
    }
    
    public void setAccType (String custAccType)
    {
    accType = custAccType;
    }
    }
    --------------------------------------------------------------------------------------
    Code:
    class Account1 extends Customer
    {
    public Account1(int id, String name, String address, String birthdate, String phone, double balance, String accType)
    {
    super(id,name,address,birthdate,phone,balance,accType);
    this.id = id;
    this.name = name;
    this.address = address;
    this.birthdate = birthdate;
    this.phone = phone;
    this.balance = balance;
    this.accType = accType;
    }
    }
    -------------------------------------------------------------------------------------------
    Code:
    class Account2 extends Customer
    {
    public Account2(int id, String name, String address, String birthdate, String phone, double balance, String accType)
    {
    super(id,name,address,birthdate,phone,balance,accType);
    this.id = id;
    this.name = name;
    this.address = address;
    this.birthdate = birthdate;
    this.phone = phone;
    this.balance = balance;
    this.accType = accType;
    }
    }
    ----------------------------------------------------------------------------------
    Code:
    class Account3 extends Customer
    {
    protected double fixed=0;
    
    public Account3(int id, String name, String address, String birthdate, String phone, double balance, String accType, double fixed)
    {
    super(id,name,address,birthdate,phone,balance,accType);
    this.id = id;
    this.name = name;
    this.address = address;
    this.birthdate = birthdate;
    this.phone = phone;
    this.balance = balance;
    this.accType = accType;
    this.fixed = fixed;
    }
    public double getFixed()
    {
    return fixed;
    }
    }
    -------------------------------------------------------------------------------------

    Example of a customers.txt

    Account Id = 123
    Name = Matt Damon
    Address = 465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
    DOB = 10-10-1970
    Phone Number = 790-3233
    Account Balance = 405600.00
    Account Type = Fixed
    Fixed Daily Interest = 0.05

    Account Id = 126
    Name = Ben Affleck
    Address = 200 Hunting Street, Singapore 784563
    DOB = 25-10-1968
    Phone Number = 432-4579
    Account Balance = 530045.00
    Account Type = Saving

    Account Id = 65
    Name = Salma Hayek
    Address = 45 Mexican Boulevard, Hotel California, Singapore 467822
    DOB = 06-04-73
    Phone Number = 790-0000
    Account Balance = 2345.00
    Account Type = Checking

    Account Id = 78
    Name = Phua Chu Kang
    Address = 50 PCK Avenue, Singapore 639798
    DOB = 11-08-64
    Phone Number = 345-6780
    Account Balance = 0.00
    Account Type = Checking

    Account Id = 234
    Name = Zoe Tay
    Address = 100 Blue Eyed St, Singapore 456872
    DOB = 15-02-68
    Phone Number = 456-1234
    Account Balance = 600.00
    Account Type = Saving>
     
    Last edited by a moderator: Mar 11, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice