Reputation: 19
I'm having problems changing the ISBN number to the name of the title of the book
Starts of printing the ISBN, author, title and level of book left:
0201403765 Jan Skansholm Ada 95 from the Beginning 100
0202535665 M. Ben-Ari Software Engineering 25
034565976X Michael Feldman Program Construction 12
080539057X M.A. Weiss Data Structures 30
0805645782 Ken Arnold Java for Programmers 10
0905297568 A. Badone Chaos Theory 15
Prints out whats in transactions.txt
:
0201403765 -55
0201403765 2
0202535665 10
0202535665 -28
034565976X -7
080539057X -15
0905297568 13
0905297568 -5
So basically what I need to do is change the ISBN to the the title of the book if it matches, like this:
Java from the Beginning -55
Java from the Beginning 2
Software Engineering 10
Software Engineering -28
Program Construction -7
Data Structures -15
Chaos Theory 13
Chaos Theory -5
The problem im having is at 1 marked in the code below , really unsure how to check if the isbn matches and if so how to check what title matches the isbn and write it out , I think my problem is the arraylist ( should i make a third arraylist) or just make everything into arrays, any advice will do , cheers !!!! btw 1 is completely ludacrisly wrong....
import java.util.*;
import java.io.*;
class inventory{
static void intial(){
try{
RandomAccessFile in = new RandomAccessFile("books.dat","r");
ArrayList<String> list1=new ArrayList<String>();
String author ,title , isbn;
int level=0;
while(in.getFilePointer()<in.length()){
author = in.readUTF(); // author, at most 20 characters
title = in.readUTF(); // title, at most 40 characters
isbn = in.readUTF(); // ISBN
level = in.readInt(); // level, i.e. copies in stock (>=0)
//System.out.printf("%5d", isbn+author+title+level);
System.out.println(isbn+" "+author+" "+title+" "+level);
list1.add(title);
list1.add(isbn);
//list1.add(level);
}
in.close();
System.out.println(" ");
String isbn2;
int level2=0;
//try{
Scanner out = new Scanner(new File ("transactions.txt"));
ArrayList<String> list2=new ArrayList<String>();
while(out.hasNextLine()){
isbn2 = out.next();
level2 = out.nextInt();
System.out.println(isbn2 +" "+level2);
list2.add(isbn2);
//list2.add(level2);
}
out.close();
1) for (isbn: list1){
for(isbn2: list2){
if(isbn.contains(isbn2)){
System.out.println(title+" "+level);
}
}
}
}
catch(IOException f){
System.out.println("file error");
f.printStackTrace();
}
}
}
class BookShop{
public static void main(String[]args){
inventory x = new inventory();
x.intial();
Upvotes: 0
Views: 228
Reputation: 96394
I would create an object to hold each book, like this:
class Book {
public final String isbn;
public final String title;
public final String author;
public final int level;
public Book(String isbn, String title, String author, int level) {
this.isbn = isbn; this.title = title; this.author = author; this.level = level;
}
}
and populate the ArrayList with Book objects. That way everything for one Book is in one place, as opposed to needing parallel arrays.
(The way I wrote this class it is immutable, because you don't have any reason to update the objects once they're read in from the input file. If this was Python or Haskell we'd just use a tuple and do without the ceremony, but with Java we don't have that option.)
Upvotes: 3
Reputation: 4164
First create a Book object to store your book data.
public Book{
private String ISBN ="";
private String title="";
private String author="";
private int level;
public Book(String ISBN, String title,String author, int level){
this.ISBN=ISBN;
this.title=title;
this.author=author;
this.level=level;
}
public String getTitle(){
return title;
}
public String getISBN(){
return ISBN;
}
public String getAuthor(){
return author;
}
public int getLevel(){
return level;
}
}
Add the Book objects to the ArrayList.
ArrayList<Book> bkList = new ArrayList<Book>();
bkList.add(new Book('ISBN','Title','Author'));
Get Book data with the get() method.
Book tempBook;
for (int x=0;x<bkList.Size();x++){
tempBook=bkList.get(x);
System.out.println(tempBook.getTitle()+" "+tempBook.getLevel());
}
Upvotes: 3