I have an assignment for college in which we must serialize a subclass called Current . we must ask a user to enter details for a new Current account so it can be serialized.
I have a class hierarchy built up with the Account class being the super class. It should be possible to add, view , list, edit and delete Current objects. We can't ask the user to add their account number as that is done via a static variable. eg: **accountNo** = **nextAvailableNumber++**
;
I have to take in the users name and dateOpened details and I have already written a Name class but the Name class has a variable of type Name called name so i cant use it as a String in the subclass. It is giving me a mismatch exception, cannot convert String to Name and mismatch exception, cannot convert from int to Date for dateOpened .
My question then is. If I have to take user input. should i be using the name variable of Type Name in the Name class to take input and should i be doing the same with the **dateOpened**
variable of type Date
? Should i be casting. Here is my code, its a menu based system that allows a user to set up an account.
// Name class with name variable of type Name
&& Date
variable **dateOpened**
of type Date.
package ie.lyit.bank;
public abstract class Account
{
protected Name name; // COMPOSITION - Account HAS-A name (object of class Name)
protected String address;
protected double balance;
protected Date dateOpened; // COMPOSITION - Account HAS-A dateOpened (object of class Date)
protected int accountNo;
private static int nextUniqueNumber = 1; // Next available unique Account number
// Default Constructor - set Instance Variables to null
public Account()
{
name = new Name();
address= new String(); // OR address = ""; OR address = null;
balance = 0.0;
dateOpened = new Date();
// Set accountNo to static nextUniqueNumber & increment it for next accountNo
accountNo = nextUniqueNumber++;
}
// Current class with read() method
// Read method to read in the account details from the user
public void read()
{
Scanner keyboardIn = new Scanner(System.in);
System.out.println("ENTER ACCOUNT DETAILS ==>");
System.out.print("ACCOUNT NAME : ");
name = keyboardIn.nextLine();
System.out.print("ACCOUNT ADDRESS : ");
address = keyboardIn.nextLine();
System.out.print("ACCOUNT BALANCE : ");
balance = keyboardIn.nextDouble();
System.out.print("ACCOUNT DATE OPENED : ");
dateOpened = keyboardIn.nextInt();
}
}
You dont need to use Name type, You can simply declare it as String and access it as class variable.
If you have a derived class say xyz who needs to access name variable of superclass say Account.
You can access it like Account.Name saying you have defined it as below.
class Account
{
protected String Name;
}
class xyz extends Account
{
// here u can access using Account.Name;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.