Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java reading from user/seralizing

    I have 5 main classes. Account, CurrentAccount, JointAccount, Name and Date.
    What i have to do is read in from the user to make joint account, this is what im trying to do, in bold:

    Code:
    import java.util.*;
    import java.io.Serializable;
    
    public class Book implements Serializable{
       /**
    	 * 
    	 */
    	private static final long serialVersionUID = 3000078084253430895L;
    private String name;  		////////////////////////
       private String isbnNumber;	// Instance Variables //
       private String author;  		////////////////////////
       private String publishingCompany;
       private int libraryNumber;   
    	
    	private static int nextNumber=1000;	// static for unique number - starts off at 1
    	
    	// Default Constructor - set Instance Variables to null
    	public Book(){
    		name=isbnNumber=author=publishingCompany="";
    
    		// Set number to static nextNumber before incrementing nextNumber
     		libraryNumber=nextNumber++;
    	}
    	
    	// Initialisation Constructor
    	public Book(String name, String isbnNumber, String author, String publishingCompany){
    		this.name = name;
    		this.isbnNumber = isbnNumber;;
    		this.author = author;
    		this.publishingCompany = publishingCompany;      
    		// Set number to static nextNumber before incrementing nextNumber
     		libraryNumber=nextNumber++;
    	}
    
    	// read() - To read a book from the user	
       public void read(){
    	    Scanner kbString = new Scanner(System.in);		
    	   System.out.println("ENTER BOOK DETAILS ==>");
    	   System.out.print("NAME : ");name=kbString.nextLine();
    	   System.out.print("ISBN NUMBER : ");isbnNumber=kbString.nextLine();
    	   System.out.print("AUTHOR : ");author=kbString.nextLine();
    	   System.out.print("PUBLISHING COMPANY : ");publishingCompany=kbString.nextLine();
    	}
    
    	// get() and set() methods for each Instance Variable
    	public String getName(){
    	   return name;
    	}
    	public void setName(String name){
    	   this.name=name;
    	}
    	
    	public String getIsbnNumber(){
    	   return isbnNumber;
    	}	
    	public void setIsbnNumber(String isbnNumber){
    	   this.isbnNumber=isbnNumber;
    	}
    	
    	public String getAuthor(){
    	   return author;
    	}
    	public void setAuthor(String author){
    	   this.author=author;
    	}
    
    	public String getPublishingCompany(){
    	   return publishingCompany;
    	}
    	public void setPublishingCompany(String publishingCompany){
    	   this.publishingCompany=publishingCompany;
    	}
    
    	public int getLibraryNumber(){
    	   return libraryNumber;
    	}
       // SHOULD NOT HAVE A setLibraryNumber() METHOD
    	
    	// toString() method for displaying a Book objects
    	// Display Book object as 
    	//         "BOOK 100001:Clean Code by Robert Martin [ISBN 978-0132350884 PUBLISHER Prentice Hall]" 
     	@Override
    	public String toString(){
     		return("BOOK " + libraryNumber + ":" + name + " by " + author + " [ISBN " + isbnNumber + " PUBLISHER " + publishingCompany + "]");
      	}	
    
    	// equals() method for comparison of two Book objects
     	@Override
     	public boolean equals(Object o){
    		// First check if o is a null reference to prevent run-time exceptions
    		if(o == null) 
               return false;
    	
    		// Next check if o references the current object, if it does it must be equal
    		if (this == o)
    			return true;
             
    		// Next check if o is in fact an Book object
    		if(!(o instanceof Book))
    			return false;
    		else
    		{
    		    // If none of the above are true, cast o to a Book object so that
                // we can compare the object's instance variables and methods
    			Book other = (Book)o;
    			
    			return (this.libraryNumber == other.libraryNumber);
    		}
    	}	
    }
    and this is what i have, in bold.
    Code:
    /**
    
    */
    
    import java.io.Serializable;
    import java.util.Scanner;
    
    public class Joint extends Account implements Serializable{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 2053587399757756604L;
    	protected Name nameB;	// COMPOSITION - nameB is an object of class Name
    
      	// Default Constructor for Joint objects
     	// - Java will automatically call Account() constructor
    	public Joint(){
    		super(); // Can call super() but it will do it automatically
    		nameB = new Name();
    	}
    	
      	// Initialisation Constructor for Joint objects
    	// Provides a nameA, nameB, address, balance, date of opening & overdraft amount for Joint Account
    	// Called as follows:
    	// Joint j1 = new Joint("Mr","Joe","Bloggs","Mrs","Ann","Bloggs",
    	//                      "123 High Road",1000.00,12,6,2012);
    	public Joint(String t1, String fn1, String sn1,  
    				 String t2, String fn2, String sn2,     
    				 String address, double balance, 
    	             int day, int month, int year){
    		super(t1, fn1, sn1, address, balance, day, month, year);		
         	nameB = new Name(t2,fn2,sn2);
       }
    
    	// toString() method for displaying a Joint object
    	// Display Joint object as 
    	//         "ACCOUNT==>100001:Mr Joe Bloggs & Mrs Ann Bloggs,123 High Road,Letterkenny.	�100.00" 
    	@Override
    	public String toString(){
     		return("ACCOUNT==>"+ accountNo + ":" + name + " & " + nameB + "," + address + ".\t�" + balance);		
      	}
    	
    	// Don't need to override equals()
    	// Account equals is sufficient
    	
    	// get() and set() methods for each Instance Variable
      	public Name getNameB(){
    		return nameB;
    	}
      	public void setNameB(Name nameBIn){
    		nameB = nameBIn;
    	}
      	
      	//read
      	public void read(){
    	    Scanner kbString = new Scanner(System.in);		
    	    System.out.println("Enter Joint Account Details ==>");
    	    System.out.print("TITLE 1: ");t1=kbString.nextLine();
    	    System.out.print("FIRST NAME 1: ");String fn1=kbString.nextLine();
    	    System.out.print("SURNAME 1: ");String sn1=kbString.nextLine();
    
    	    System.out.print("TITLE 2: ");String t2=kbString.nextLine();
    	    System.out.print("FIRST NAME 2: ");String fn2=kbString.nextLine();
    	    System.out.print("SURNAME 2: ");String sn2=kbString.nextLine();
    	   
    	    System.out.print("Address : ");String addressIn=kbString.nextLine();
    	    System.out.print("Balance : ");double balanceIn=kbString.nextDouble();
    	    System.out.print("Day(number) : ");int day=kbString.nextInt();
    		System.out.print("Month(number) : ");int month=kbString.nextInt();
    		System.out.print("Year(number) : ");int year=kbString.nextInt();
    
    	}
      	
    }
    it doesnt work because of all of the variables are spread out across the classes and i cant figure out how to inherit them so it works.
    i know i haven't explained this well, but if you're willing to help let me know and ill try to explain it better.

    thanks,
    Last edited by vinyl-junkie; 11-14-2016 at 12:21 AM. Reason: altered thread title; moved thread to java forum

  2. #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,297
    Thanks
    213
    Thanked 2,676 Times in 2,652 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •