Problems with input/output and getters/setters
Hi Guys,
I was just wondering if you could help me with any suggestions in implementing input from a user and outputting it to the console, I have getters and setters that the user needs to be able to use as a template for the type of data they need to enter, here's some of the code I have written up:
This is some of the file that stores the variables,getters and setters..
Code :
// Instance Variables
// ==================
private int upc;
private String title;
private int issueNumber;
private String publisher;
private String gradeCode;
private double currentValue;
public String determineCondition;
public double calcSellingPrice;
// Main Method
public static void main(String[] args) {
// Create instance of Student
ComicBook comicbook = new ComicBook (687788599, "Aquaman", 12, "Marvel", "MT", 20.00);
comicbook.setUpc(539684363);
}
// Create the Constructors
public ComicBook() {
setUpc(596060547);
setTitle("Wolverine");
setIssueNumber(20);
setPublisher("Marvel Comics");
setGradeCode("FR");
setCurrentValue(50.00);
}
public ComicBook(int upc, String title, int issueNumber, String publisher, String gradeCode, double currentValue){
setUpc(upc);
setTitle(title);
setIssueNumber(issueNumber);
setPublisher("Marvel Comics");
setGradeCode("FR");
setCurrentValue(50.00);
}
// Getters and Setters
// Upc getters and setters
public int getUpc() {
return upc;
}
public void setUpc(int upc) {
this.upc = upc;
}
// Title getters and setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
This file needs to grab user input then display, the users need to be able to input into the getters and setters like they are creating a new item themself:
Code :
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); //Initialize the scanner
BufferedReader keyboard = null; // input stream
int numBooks = 0; //How many books are to be entered?
int whatNumBook = 0; //If this exceeds the value of numBooks, exit();
ComicBook c = new ComicBook(); //create new comic book
//Welcome message
System.out.println("Welcome to the Comic Book Manager!");
//Start running with request for no. of books
//Store value in numBooks for later use
System.out.print("How many books will you be entering today? ");
try {
numBooks = scanner.nextInt();
//Now wait for user to enter the number of books they are entering
System.out.print("Please enter what number book you would like to start with? ");
whatNumBook = scanner.nextInt();
//Once they are finished this will end the program
if (whatNumBook > numBooks){
exit();
}
System.out.println("Upc: " + c.getUpc());
System.out.println("Title: " + c.getTitle());
System.out.println("Issue Number: " + c.getIssueNumber());
System.out.println("Publisher: " + c.getPublisher());
System.out.println("Grade Code: " + c.getGradeCode());
System.out.println("Current Value: " + c.getCurrentValue());
}
finally {
if (whatNumBook != 0) {
System.out.println("Closing Comic Book Manager");
ComicBookManager.exit();
}else {
System.out.println("Comic Book Manager not open");
}
Hope that makes sense, any help would be appreciated
Re: Problems with input/output and getters/setters
Do you have a specific question? The getting help link in my signature might help you lay out the problems you may be having to increase your chances of receiving help.
Re: Problems with input/output and getters/setters
My main issue is I would like to know how users can input data through the console using the setters and getters that are stored in the first file?
So for example, I need to display "enter the data for: upc, title, publisher" and so on. Once they have entered that data I will need to display what they have entered later on.
Re: Problems with input/output and getters/setters
The best way to simplify your problem in this case would be creating two classes. One that will contain the main method and one that will contain all the members: - this will also contain the constructor. The problem you are asking if I understand it is how to fetch the information from the user then displaying them on the console: Well, after you have instantiated the object, just call the method that takes the parameter. That method should get the information and then you can use a output method to display it. You can combine the prompt method and the method you defined in the class definition. I hope this helps.
Re: Problems with input/output and getters/setters
Oh yes sorry I forgot to mention, the first piece of code does contain the main method, that's my first file, then the second piece of code i posted is the driver, my second file.
Re: Problems with input/output and getters/setters
I wrote a class that simplifies user input of primitive types. You can get it here. The really nice thing about this class is you can specify exactly what the user has to enter. If they enter incorrect data it will inform them what they did wrong and ask them to try again and you can customize the prompt and error message all with a single line of code.
Hope that helps.
Re: Problems with input/output and getters/setters
Thanks for your help guys, I think I've fixed it..
Code :
public static void main(String[] args) {
int numBooks = 0; //How many books are to be entered?
int whatNumBook = 0; //If this exceeds the value of numBooks, exit();
int num1 = 0;
Scanner scanner = new Scanner(System.in); //Initialize the scanner
ComicBook c = new ComicBook(); //create new comic book
//Welcome message
System.out.println("Welcome to the Comic Book Manager!");
//Start running with request for no. of books
//Store value in numBooks for later use
System.out.print("How many books will you be entering today? ");
try {
numBooks = scanner.nextInt();
//Now wait for user to enter the number of books they are entering
System.out.print("Please enter what number book you would like to start with? ");
whatNumBook = scanner.nextInt();
//Once they are finished this will end the program
if (whatNumBook > numBooks){
exit();
}
System.out.println("Please begin entering the data");
System.out.println("Upc: ");
num1 = scanner.nextInt();
System.out.println("Title: ");
scanner.next();
System.out.println("Issue Number: ");
scanner.nextInt();
System.out.println("Publisher: ");
scanner.next();
System.out.println("Grade Code: ");
scanner.next();
System.out.println("Current Value: ");
scanner.nextDouble();
}
finally {
if (whatNumBook != numBooks) {
System.out.println("Closing Comic Book Manager");
ComicBookManager.exit();
}else {
System.out.println("Comic Book Manager not open");
}
}
}
private static void exit() {
System.out.println("Shutting down program");
}
}