java Linked list problem
I am now struggling my assignment, the assignment require me to re-
implement the previous lab using a linked list;
I read the instruction, it seems to let me implement something like
the following diagram:
RecordList------>RecordNode------->RecordNode-------->RecordNode---
and each RecordNode includes a Customer information; in Customer
class:
I have the following methods and functions:
class Customer implements Comparable{ //
implements Comparator{
private int accountid;
private String name, address, dateofbirth, phoneNo, accType;
protected double balance;
protected double interestRate;
//protected double updatebal;
public Customer(){
accountid = 0;
name = address = dateofbirth = phoneNo = accType = null;
balance = interestRate = 0.0;
}
public Customer(int id, String n, String a, String d, String p,
String t, double b, double i){
accountid = id;
name = n;
address = a;
dateofbirth = d;
phoneNo = p;
accType = t;
balance = b;
interestRate = i;
}
public void setAccId(int accid){
accountid = accid;
}
public void setName(String n){
name = n;
}
public void setAddress(String add){
address = add;
}
public void setDOB(String dOfbirth){
dateofbirth = dOfbirth;
}
public void setPhone(String p){
phoneNo = p;
}
public void setBal(double bal){
balance = bal;
}
public void setAccType(String accType){
this.accType = accType;
}
public int getAccId(){
return accountid;
}
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public String getDOB(){
return dateofbirth;
}
public String getPhone(){
return phoneNo;
}
public double getBal(){
return balance;
}
public String getAccType(){
return accType;
}
// Interest Rate for fixed Account
public void setInterestRate(double iRate){
interestRate = iRate;
}
// Get method for interest rate
public double getInterestRate(){
return interestRate;
}
public void update(){
///return 0;
}
public int compareTo(Object o){
if(!(o instanceof Customer)){
throw new IllegalArgumentException("Err: non-Record type
object");
}
Customer c = (Customer)o;
if(accountid>c.accountid) return 1;
if(accountid<c.accountid) return -1;
return 0;
}
@Override
public String toString(){
return "Customer(" + getAccId() + "): " + getName();
}
@Override
public Customer clone(){
Customer r = new Customer(accountid,name,address,dateofbirth,
phoneNo,accType,balance,interestRate);
return r;
}
}
That means, by far, I have RecordList.java, RecordNode.java,
Customer.java, Main.java
In Main, I try to read data from file containg the customers
information(i.e. id,name,account balance,etc.)
One of my function is to check whether 2 account id is duplicated.
<---- I am stranding in this part, how can
I check the 2 record nodes containing the same id and remove it?.
the following code is the Readfile function in Main:
// I don't know how to use RecordList rList here?
static int readFile(File inputfile,RecordList rList){
try{
FileInputStream fis = new FileInputStream(inputfile);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream fileinput = new DataInputStream(bis);
BufferedReader br = new BufferedReader(new
InputStreamReader(fileinput));
String read;
int i = 1;
Customer temp = new Customer();
while((read=br.readLine())!=null){
if(read.contains("Account Id = ")){
String caid =
read.substring(read.lastIndexOf("=") + 2);
temp.setAccId(Integer.parseInt(caid));
}
else if(read.contains("Name = ")){
String cname =
read.substring(read.lastIndexOf("=") + 2);
if(cname.length()< )
temp.setName(cname.toUpperCase());
}
else if(read.contains("Address = ")){
String caddress =
read.substring(read.lastIndexOf("=") + 2);
if(caddress.length()<=80)
temp.setAddress(caddress);
}
else if(read.contains("DOB = ")){
String cdob =
read.substring(read.lastIndexOf("=") + 2);
if(cdob.length()<=10)
temp.setDOB(cdob);
}
else if(read.contains("Phone Number = ")){
String cphoneno =
read.substring(read.lastIndexOf("=") +2);
if(cphoneno.length()<=8)
temp.setPhone(cphoneno);
}
else if(read.contains("Account Balance = ")){
String caccbal =
read.substring(read.lastIndexOf("=")+2);
double accbal = Double.parseDouble(caccbal);
temp.setBal(accbal);
}
else if(read.contains("Account Type = ")){
String caactype =
read.substring(read.lastIndexOf("=") + 2);
if(caactype.length()<=8)
temp.setAccType(caactype);
if(temp.getAccType().equals("Saving")){
rList.insert(i,temp);
}
else if(temp.getAccType().equals("Checking")){
rList.insert(i,temp);
}
else if(temp.getAccType().equals("Fixed")){
read=br.readLine();
if(read.contains("Fixed Daily Interest =
")){
String cintrate =
read.substring(read.lastIndexOf("=")+2);
double intrate =
Double.parseDouble(cintrate);
temp.setInterestRate(intrate);
rList.insert(i,temp);
}
else System.out.println("Sorry, no daily
interest found!");
}
i++;
}
}
fileinput.close();
return i-1;
// i-1 returns the record successfully read;
}
catch (IOException ex){
System.err.println(ex);
}
return 0;
}
How can I implement a function checking 2 recordnodes is duplicated
here?
The following is RecordNode and RecordList code:
// RecordNode
public class RecordNode{
private Object item;
private RecordNode next;
public RecordNode(Object o){
o = item;
next = null;
}
public RecordNode(Object o, RecordNode newNext){
o = item;
next = newNext;
}
public void setItem(Object o){
item = o;
}
public Object getItem(){
return item;
}
public void setNext(RecordNode newNext){
next = newNext;
}
public RecordNode getNext(){
return next;
}
}
// RecordList
public class RecordList {
protected int numItem;
protected RecordNode head;
public RecordList(){
head = null;
numItem = 0;
}
public RecordList(RecordNode newNode){
head = newNode;
numItem ++;
}
public boolean isEmpty(){
return (numItem==0)? true:false;
}
public int getSize(){
return numItem;
}
public Object getItem(int index){
if(index>numItem) return null;
return find(index).getItem();
}
// find current node;
public RecordNode find(int index){
RecordNode cur = head;
if(index>numItem) return null;
for(int i = 1;i<index;i++)
cur = cur.getNext();
return cur;
}
public boolean insert(int at, Object newItem){
RecordNode pre,cur;
if(at < 1 || at > numItem+1) return false;
numItem++;
if(at == 1){
if(isEmpty()) head = new RecordNode(newItem);
else head = new RecordNode(newItem,head);
return true;
}
pre = find(at-1);
cur =pre.getNext();
pre.setNext(new RecordNode(newItem, cur));
return true;
}
}
// This following part is not yet done.
and inside the Customer class, there should be a transaction linked
list for recording the transaction of
each customer, something like:
Inside the Customer:
TransactionList--->TransactionNode------->TransactionNode--------
// This part I've not done yet.
I am sorry the question is too long.