How to I load items into arrays inside of an object?
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Iterator;
import java.util.Scanner;
import java.io.*;
import java.util.StringTokenizer;
class LibItem{
private String id; // item id
private String title; // item title
public LibItem(String s, String t){
id = s;
title = t;
}
public String getID(){
return id;
}
public String getTitle(){
return title;
}
}
class PatronCopy{
// This class represents a record for every book copy borrowed by a patron
private int patron; // patron id
private int copy; // book's copy number
public PatronCopy(int x, int y){
patron = x;
copy = y;
}
public int getPatron(){
return patron;
}
public int getCopy(){
return copy;
}
}
class Book extends LibItem{
// This class represents a book and all its copies.
// book's authors
private List<String> authors = new ArrayList<String>();
// queue of copies on shelf
private Queue<Integer> onshelf = new LinkedList<Integer>();
// list of PatronCopy objects
private List<PatronCopy> onloan = new LinkedList<PatronCopy>();
// list of copies lost
private List<Integer> lost = new LinkedList<Integer>();
public Book(String s, String t){
super(s, t);
}
public void addAuthor(String s){
authors.add(s);
}
public int numAuthors(){
return authors.size();
}
public String getAuthor(int x){
if (x>0 && x<=authors.size()){
return authors.get(x-1);
}
return "";
}
public void addShelfCopy(int x){
if (x > 0)
onshelf.add(x);
}
public void addLost(int x){
if (x > 0)
lost.add(x);
}
public int checkOut(int patron){
if (onshelf.peek()!=null){
int c = onshelf.remove();
onloan.add(new PatronCopy(patron, c));
return c;
}
return -1;
}
public boolean checkIn(int c){
for (int i=0; i<onloan.size(); i++){
if (onloan.get(i).getCopy() == c){
onloan.remove(i);
onshelf.add(c);
return true;
}
}
return false;
}
public int numOnShelf(){
return onshelf.size();
}
public int numOnLoan(){
return onloan.size();
}
public int numLost(){
return lost.size();
}
public int getOnShelf(int x){
Iterator itr = onshelf.iterator();
for (int i=1; i<x; i++){
itr.next();
}
return (Integer)itr.next();
}
public String getOnLoan(int x){
ListIterator<PatronCopy> itr = onloan.listIterator();
for (int i=1; i<x; i++){
itr.next();
}
PatronCopy p = itr.next();
String s = Integer.toString(p.getCopy()) + " ";
s = s + Integer.toString(p.getPatron());
return s;
}
public int getLost(int x){
ListIterator<Integer> itr = lost.listIterator();
for (int i=1; i<x; i++){
itr.next();
}
return itr.next();
}
}
class Magazine extends LibItem{
// This class represents a copy of magazine.
// Volume number of the magazine
private int volume;
// Magazine status is 0 - on shelf -1 missing
private int status;
public Magazine(String d, String t, int v, int s){
super(d, t);
volume = v;
status = s;
}
public void setLost(){
status = -1;
}
public void setFound(){
status = 0;
}
public int getVolume(){
return volume;
}
public int getStatus(){
return status;
}
}
public class Homework4{
public static void loadItems(LinkedList<LibItem> libsys) throws IOException{
String id,title,authorLine,onshelf,lost,type,volume,status;
Scanner infile = new Scanner(new File("data.txt"));
while (infile.hasNext()){
type = infile.nextLine();
id= infile.nextLine();
title= infile.nextLine();
if(Integer.parseInt(type) ==1){
Book b1 = new Book(id, title);
authorLine = infile.nextLine();
onshelf = infile.nextLine();
lost = infile.nextLine();
StringTokenizer au = new StringTokenizer(authorLine);
while (au.hasMoreTokens()) {
b1.addAuthor(au.nextToken());
}
StringTokenizer onsh = new StringTokenizer(onshelf);
while (onsh.hasMoreTokens()) {
b1.addShelfCopy(Integer.parseInt(onsh.nextToken()));
}
StringTokenizer lt = new StringTokenizer(lost);
while (lt.hasMoreTokens()) {
if ((Integer.parseInt(lt.nextToken()))== -1){
return;
}
else{
b1.addLost(Integer.parseInt(lt.nextToken()));
}
}
libsys.add(new Book(id, title));
}
else if(Integer.parseInt(type) ==2){
volume=infile.nextLine();
status=infile.nextLine();
int newVolume =Integer.parseInt(volume);
int newStatus = Integer.parseInt(status);
libsys.add(new Magazine(id,title,newVolume,newStatus));
}
}
}
public static int menu(){
Scanner scan = new Scanner(System.in);
int choice;
System.out.println("Choose One");
System.out.println("1. Check Out a Book");
System.out.println("2. Check In a Book");
System.out.println("3. Search for Book by Title");
System.out.println("4. Search for Magazine by Title");
System.out.println("5. Patron Search");
System.out.println("6. Print Library Items");
choice = scan.nextInt();
while ((choice < 1) || (choice > 7)){
System.out.println("Invalid Selection - Try Again");
choice = scan.nextInt();
}
return choice;
}
public static void ProcessChoice(LinkedList<LibItem> libsys, int choice){
int itemid, amt;
Scanner scan = new Scanner(System.in);
switch (choice){
case 1: System.out.println("Check Out a Book");
System.out.println("Enter Book Id followed by Patron Id");
String BookId;
int patronId;
Book b;
/* b.getID();
b.checkOut(patronId);
/*
* prompt for book id and patron id
*/
break;
case 2: System.out.println("Check In a Book");
System.out.println("Enter Book Id followed by Patron Id");
/*
* prompt for book id and patron id
*/
break;
case 3: System.out.println("Search for a Book by Title");
System.out.println("Enter Book Id followed by Patron Id");
/*
* prompt for book title and search
*/
break;
case 4: System.out.println("Search for Magazine by Title");
System.out.println("Enter Magazine Title");
/*
* prompt for mag title and search
*/
break;
case 5: System.out.println("Patron Search");
System.out.println("Enter Patron Id");
b.getOnLoan(patronId);
break;
case 6: System.out.println("Print Library Items");
Magazine m1;
for(int i=0; i<libsys.size(); i++){
if(libsys.get(i) instanceof Book){
b=(Book)libsys.get(i);
System.out.println(b.getID()+" "+b.getTitle());
for(int a=0; a<b.numAuthors(); a++){
System.out.println(b.getAuthor(a));
}
for(int c=0; c<b.numOnLoan(); c++){
System.out.println(b.getOnLoan(c));
}
}
else {
m1 = (Magazine)libsys.get(i);
System.out.println(m1.getID()+" "+m1.getTitle()+
" "+m1.getVolume()+" "+m1.getStatus());
}
}
break;
}
}
public static void main(String args[]) throws IOException{
LinkedList<LibItem> libsys = new LinkedList <LibItem>();
loadItems(libsys);
int choice = menu();
ProcessChoice(libsys, choice);
}
}
I am having trouble with this program. I can't seem to load all the objects in? when i use case 6 of the process choice I only get the first object. Any and all advise would be helpful. thanks
Re: How to I load items into arrays inside of an object?
Please post your code in code tags.
Re: How to I load items into arrays inside of an object?
Try debugging your code by adding some printlns to show the values of variables as they change and the execution flow.
Re: How to I load items into arrays inside of an object?
Quote:
Originally Posted by
incubus898
I am having trouble with this program. I can't seem to load all the objects in?
You'll have to be a bit more specific than that - load all what objects in what?
You've posted a huge dump of unformatted code with no indication at all of where the code you're referring to is. I doubt anyone will be prepared to wade through it to find out.
Quote:
when i use case 6 of the process choice I only get the first object.
I have no idea what that means.
Quote:
Any and all advise would be helpful. thanks
Post your code formatted, inside CODE tags (see my sig), highlight the particular section you're having trouble with and explain exactly what's going wrong.
Preferably, post up an SSCCE that clearly demonstrates the problem.
Programming is an explanatory activity...
R. Harper