Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to store data in library program?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying to make a library program where I need to create:- Methods:- addBook, issueBook, returnBook, showAvailableBooks Properties: Array to store the available books, and Array to store the issued books. I was able to make methods but I am not able to figure out that how to store data inside it using the given properties. Here is my program:-

Class:- Library



Class:- Main

 
Saloon Keeper
Posts: 7225
169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Firstly, as it is now, the library can only hold a single book, the title of which is stored in the variable "bookName". As soon as you add another book, the name of the book is overwritten. You seem to want to concatenate all book names in a single string (at least that' what lines 15 and 16 look like), but a single string really isn't the right data structure to use.

Since you'll want to hold several books, you need to use a different data structure, for example a List<String>. But then, you'll likely want to be able to hold several copies of any book, so a Map<String, Integer> would make even more sense. To initialize that, you'd enter the number of copies in the addBook method

Also, class names in Java are customarily uppercase. So the class should be called "Library", not "library", otherwise you'll confuse readers of your code.
 
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

Please break up those long lines; a line more than 80 keystrokes long is difficult to read on screen and over 120 keystrokes probably won't fit onto a sheet of paper, if you can reemember what paper is.
Only use the \n character if you have instructions about using CR and LF. Use printf() and %n instead. Note you may need an additional %n at the end of the format String.

Tim Moores wrote:. . . a single book, the title of which is stored in the variable "bookName". . . .

I think the answer to that problem is to create a Book class and put the information about the book in there.
You can also get rid of repeated “magic numbers” like this:-It might look longer but it is less error‑prone.
I don't like the condition for the while in line 7. What happens if you enter 8 or −1? Consider a do loop. Work out how you don't need to write the prompts twice; lines 5 and 18 look the same.
Don't use System.exit(); it is not going to do any harm in a simple program like this, but it is a vicious command and can stop a more complicated program with a task only partially done, and that can cause serious data corruption. There aree simpler ways to exit the program.


[edits]Minor formatting corrections and correct printlnprintf.
 
Saloon Keeper
Posts: 24825
172
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch (again)

Please break up those long lines; a line more than 80 keystrokes long is difficult to read on screen and over 120 keystrokes probably won't fit onto a sheet of paper, if you can reemember what paper is.



Now repeat that for Linus Torvalds' benefit.

Campbell Ritchie wrote:
Only use the \n character if you have instructions about using CR and LF. Use printf() and %n instead.

Hard-coding control characters can result in breaking Java's "write-once/run-anayware" abilities. "\n" is not an abstraction, it is a representation of one and only 1 Unicode codepoint: 0x0a. In the Microsoft world, end-of-line is 2 codepoints: "0x0a 0x0d". On an IBM mainframe, it's possible that there might not even be an explicit end-of-line character (the I/O hardware can delimit lines).

Campbell Ritchie wrote:
Don't use System.exit(); it is not going to do any harm in a simple program like this, but it is a vicious command and can stop a more complicated program with a task only partially done, and that can cause serious data corruption. There aree simpler ways to exit the program.



I'm not sure about the data corruption. As far as I can tell, System.exit() exits the VM's primary thread but child threads will continue to run until they shut themselves down or the entire VM is forcibly externally terminated. So I'm pretty sure that exit() by itself won't cause damage due to incomplete threads, at least.

Nevertheless, System.exit() is to be avoided. Java apps don't often set the return value the way that apps in languages like C do, but if memory serves, there's a way to set the exit code without actually calling exit() and it's the preferred approach.

Speaking of rude exits, my first encounter with a computer was with a desk-sized IBM mainframe (the 1130), which was a popular small scientific machine often used as a remote workstation controller. The machine was in a chemical plant that positively reeked. They made perfumes and artificial flavourings by fractional distillation of crude sulphate turpentine and supposedly were one of only 3 such plants in the world at the time - the other one being about 5 miles away and the third somewhere near Paris.

Although in theory this was a uni-tasking system, somehow they'd gimmicked it up to concurrently control a plant about 300 miles away while still being available for junior programmer wanna-be's working in Fortran IV. The accepted way to terminate such programs was a "CALL EXIT" statement, but they cautioned us NOT to use it. Doing so would shut down the remote plant as well. Actually, it would probably have shut down the entire OS, since the 1130 Disk Monitor system was about as sophisticated as CP/M. I have an emulator for the 1130. Ought to try it.

Here's a useless fun fact: Disk drives often were not standard equipment on mainframes made before 1970, so it was not uncommon to do almost everything via punched cards. To compile a program, you sometimes had to feed 2 separate decks into the machine, One containing the compiler (since there was no disk to store it on) and the other containing the application source. You'd either compile-and-go or the compiler would punch out an executable object deck. To avoid confusion, IBM made different types of punched cards. Pink cards were pre-ruled for the unique statement formatting options of Fortran source, Blue cards for COBOL, yellow or white for assembler. That way you could tell what part of the pile of cards you were processing was source code and what part was compiler!
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's see what the API documentation says. It calls Runtime.exit(), and that runs all shutdown hooks. It doesn't say what happens to other threads, and in the current (single‑threaded) application that is a non‑issue. Then it calls halt(). If there is any reading or writing going on in a non‑shutdown‑hook thread, the behaviour of that thread is unspecified and it doesn't say whether that task completes or not.
 
Tim Holloway
Saloon Keeper
Posts: 24825
172
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Let's see what the API documentation says. It calls Runtime.exit(), and that runs all shutdown hooks. It doesn't say what happens to other threads, and in the current (single‑threaded) application that is a non‑issue. Then it calls halt(). If there is any reading or writing going on in a non‑shutdown‑hook thread, the behaviour of that thread is unspecified and it doesn't say whether that task completes or not.



The problem here is that we don't actually know what the shutdown hooks do.

But the reason why I suspect that System.exit() may not terminate child threads is that if you shut down Tomcat, for example, Tomcat doesn't really shut down until all of its child threads have terminated. Otherwise you have to force it (Ctrl-C/sigkill). Tomcat often has this problem because people spawn threads in their webapps and don't provide any way for the application to terminate on shutdown. Which is bad design on their part, but some people have to learn from mistakes.

I'm concerned about child threads, because offhand I can't think of anything that would be corrupted by System.exit() if there were no other application threads (the system threads, such as GC I figure can handle themselves).

And, of course, if the exit hooks don't/can't abnormally terminate child threads, that brings me back to my original contention.
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does Tomcat run on the JVM?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic