|
Help with else statments please
Hey Guys, (first post!)
I have an assignment on a Library class i think i'm almost there except for the fact I have four constructors for four methorods and only two are required.
I have been fiddling around with if else for the last week with little luck below are my objectives and the program (libraryitem) and tester (libraryitemtest).
I am still getting confused between constructors and methods i have read a few online books and asked my teacher also for help but i still don't get it.
The code below works, I tried to use if else but made a mess of it and it was a little too confusing so i have left it out.
Below is are the requirements and code:
a) Attributes title, author, media, status,cost . Choose Appropriate data types.
b) Code a constructor to handle 5 inputs.
c) Code a constructor to handle title and author only, Otherwise default values.
d) Code methods to set media. status and cost.
e) Code a method to display author and title of all overdue items.
f) Code a method to display all data of an object.
-----------------------------------------------------------------
public class libraryitem {
String title;
String author;
String media;
String status;
Double cost;
libraryitem (String tit, String aut, String med, String sta, Double cos) {
title=tit;
author=aut;
media=med;
status=sta;
cost=cos;
}
libraryitem (String tit, String aut) {
title=tit;
author=aut;
}
libraryitem (String med, String sta, Double cos) {
media=med;
status=sta;
cost=cos;
}
libraryitem (String med, String sta, String tit) {
media=med;
status=sta;
title=tit;
}
public String geteverycat() {
return title+ ", " + author + ", "+media+ ", " +status+ ", "+cost;
}
public String gettitleauthonly() {
return title+ ", " +author;
}
public String getmedstacosonly() {
return media+ ", " +status+ ", "+cost;
}
public String getmedstatitonly() {
return media+ ", " +status+ ", "+title;
}
}
-----------------------------------------------------------------
public class libraryitemtest {
public static void main ( String [] args){
libraryitem libraryitemtestone = new libraryitem ("Java Programming","Vikki","Book","Onloan",35.95);
libraryitem libraryitemtesttwo = new libraryitem ("Visual Basic Programming","Bob Down");
libraryitem libraryitemtestthree = new libraryitem ("DVD","Overdue",23.45);
libraryitem libraryitemtestfour = new libraryitem ("Magazine","OnShelf","C++ Tips");
String allcat;
String titaut;
String medstacos;
String medstatit;
allcat=libraryitemtestone.geteverycat();
titaut=libraryitemtesttwo.gettitleauthonly();
medstacos=libraryitemtestthree.getmedstacosonly();
medstatit=libraryitemtestfour.getmedstatitonly();
System.out.println("TITLE AUTHOR MEDIA STATUS AND COST = "+allcat);
System.out.println("TITLE AUTHOR = "+titaut);
System.out.println("MEDIA STATUS AND COST = "+medstacos);
System.out.println("MEDIA STATUS TITLE = "+medstatit);
}
}
|