Results 1 to 2 of 2
  1. #1
    behedwin is offline Member
    Join Date
    Oct 2012
    Posts
    26
    Rep Power
    0

    Default how to solve my issue? if statement in my program....

    Hello
    I need some help with my program.
    Tho i find it hard to just ask a specific question since im not sure what i need to solve it.
    I guess ppl dont want me to zip down all my java files and upload it here, not many will trust me and download it.
    So ill copy paste the code i think is relevant here and hope for the best :)

    I have several classes.
    MainLibraryProgram is the main class
    Library
    Reader
    MediaItem
    LoanStatus
    MediaCollection
    ReaderCollection
    and a few more.

    MAINLIBRARYPROGRAM

    Java Code:
    	private void makeLoan() {
    
    		int mediaID = readInt("Media ID: ");
    		MediaItem mediaItem = library.searchMediaItem(mediaID);
    
    		int readerID = readInt("Reader ID ");
    		Reader reader = library.searchReader(readerID);
    
    		Date date1 = new Date();
    		Date date2 = new Date(date1.getYear(), date1.getMonth(), date1.getDay()+14);
    		LoanStatus loanStatus = mediaItem.getLoanStatus();
    		
    		loanStatus.makeLoan(date1, date2, reader);
    		System.out.println("User " + reader.getName() + " have loaned " + mediaItem.getName());
    		System.out.println(reader.getName() + "Reader has a total of " + reader.getLoans().size() + " loans.");
    
    	}
    
    	private void printLoanCollection() {
    		System.out.println("--------------------");
    		for (LoanStatus a : library.getLoans()) {
    			System.out.println(a.toString());
    		}
    
    	}
    LIBRARY

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class Library {
    	private ReaderCollection readerCollection = new ReaderCollection();
    	private MediaCollection mediaCollection = new MediaCollection();
    	List<LoanStatus> allLoans = new ArrayList<LoanStatus>();
    
    
    	public void addReader(Reader reader) {
    		readerCollection.addReader(reader);
    
    	}
    
    	public void deleteReader(int readerID) {
    		readerCollection.deleteReader(readerID);
    	}
    
    	public List<Reader> getReaders() {
    		return readerCollection.getReaders();
    	}
    
    	public void addMediaItem(MediaItem mediaItem) {
    		mediaCollection.addMediaItem(mediaItem);
    
    	}
    
    	public void deleteMediaItem(int mediaID) {
    		mediaCollection.deleteMediaItem(mediaID);
    	}
    
    	public List<MediaItem> getMediaItems() {
    		return mediaCollection.getMediaItems();
    	}
    
    	public MediaItem searchMediaItem(int mediaID) {
    		return mediaCollection.searchMediaItem(mediaID);
    	}
    
    	public Reader searchReader(int readerID) {
    		return readerCollection.searchReader(readerID);
    
    	}
    
    	public List<LoanStatus> getLoans() {
    		for (Reader reader : readerCollection.getReaders()) {
    			allLoans.addAll(reader.getLoans());
    		}
    		return allLoans;
    	}
    
    
    }

    READER

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class Reader {
    	private String name;
    	private int readerID;
    	private int birthYear;
    	private Address address;
    	private PhoneNo phoneNo;
    	private Email email;
    
    	private List<LoanStatus> loans = new ArrayList<LoanStatus>();
    
    	public Reader(String name, int readerID, int birthYear, Address address,
    			PhoneNo phoneNo, Email email) {
    		this.name = name;
    		this.readerID = readerID;
    		this.birthYear = birthYear;
    		this.address = address;
    		this.phoneNo = phoneNo;
    		this.email = email;
    
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public int getReaderID() {
    		return readerID;
    
    	}
    
    	public int getbirthYear() {
    		return birthYear;
    	}
    
    	public Address getAddress() {
    		return address;
    	}
    
    	public PhoneNo getPhoneNo() {
    		return phoneNo;
    	}
    
    	public Email getEmail() {
    		return email;
    	}
    
    	public String toString() {
    		return "Name: " + name + " Reader ID: " + readerID + " Birth Year"
    				+ birthYear + " Address: " + address + " Phone Number: "
    				+ phoneNo + " Email Address: " + email;
    
    	}
    
    	public void makeLoan(LoanStatus loan) {
    		loans.add(loan);
    
    	}
    
    	public List<LoanStatus> getLoans() {
    		return new ArrayList(loans);
    
    	}
    	
    
    }

    MEDIAITEM

    Java Code:
    public class MediaItem {
    	protected String name;
    	protected int mediaID;
    	protected int publishYear;
    	protected LoanStatus loan;
    
    	public MediaItem(String name, int mediaID, int publishYear) {
    		this.name = name;
    		this.mediaID = mediaID;
    		this.publishYear = publishYear;
    		loan = new LoanStatus(this);
    
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public int getMediaID() {
    		return mediaID;
    
    	}
    	public int getPublishYear(){
    		return publishYear;
    	}
    	public LoanStatus getLoanStatus(){
    		return loan;
    	}
    	
    	public String toString() {
    		return name + mediaID + publishYear;
    
    	}
    
    }

    LOANSTATUS

    Java Code:
    import java.util.Date;
    
    public class LoanStatus {
    
    	private MediaItem mediaItem;
    	private Date startDate;
    	private Date endDate;
    	private Reader r;
    
    	public LoanStatus(MediaItem mediaItem) {
    		this.mediaItem = mediaItem;
    
    	}
    
    	public void makeLoan(Date startDate, Date endDate, Reader r) {
    		this.startDate = startDate;
    		this.endDate = endDate;
    		this.r = r;
    		r.makeLoan(this);
    
    	}
    
    	public boolean checkLoan() {
    		return r != null;
    	}
    
    
    	public String toString() {
    		return "\n" + "Item: " + mediaItem + " \n" + "Loan Was Made: "
    				+ startDate + " \n" + "Item is to be returned: " + endDate
    				+ " \n" + "Loan was made by: " + r;
    	}
    
    }

    And now finaly to my question.

    I am making a loan in MainLibraryProgram.
    I want to make some kind of "if" statement around that.
    If searchMediaItem wont return a mediaItem that is in the system it will not crash the program, but genereate a message instead.
    same for searchReader

    i dont know where to put this if statement
    and how to type it....

    if (searchReader returns nothing/no readerID && searchMediaItem returns nothing/no mediaID)
    //do this
    else
    //do this

    Any ideas or did i just ask something really stupid :p

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,935
    Rep Power
    19

    Default Re: how to solve my issue? if statement in my program....

    After getting the search objects then check if either are null.
    If they are then display a relevant message, else do the code you currently have.

    This assumes that your collection objects return null for an invalid id.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. if-statement issue.
    By qiTsuk in forum New To Java
    Replies: 2
    Last Post: 10-11-2012, 03:14 PM
  2. IF statement issue
    By AndreaRenee in forum New To Java
    Replies: 15
    Last Post: 03-11-2012, 05:55 AM
  3. Solve the Error:null issue
    By coopc in forum New To Java
    Replies: 2
    Last Post: 04-12-2011, 06:18 PM
  4. How to solve this issue.
    By yaso in forum Eclipse
    Replies: 1
    Last Post: 11-07-2009, 07:28 AM
  5. help required to solve jasper report memory issue
    By kiranrajan in forum Advanced Java
    Replies: 0
    Last Post: 11-04-2009, 09:25 AM

Posting Permissions

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