Results 1 to 2 of 2
- 11-17-2012, 04:50 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 26
- Rep Power
- 0
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()); } }
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
- 11-19-2012, 11:46 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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
-
if-statement issue.
By qiTsuk in forum New To JavaReplies: 2Last Post: 10-11-2012, 04:14 PM -
IF statement issue
By AndreaRenee in forum New To JavaReplies: 15Last Post: 03-11-2012, 06:55 AM -
Solve the Error:null issue
By coopc in forum New To JavaReplies: 2Last Post: 04-12-2011, 07:18 PM -
How to solve this issue.
By yaso in forum EclipseReplies: 1Last Post: 11-07-2009, 08:28 AM -
help required to solve jasper report memory issue
By kiranrajan in forum Advanced JavaReplies: 0Last Post: 11-04-2009, 10:25 AM
Bookmarks