-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
144 lines (119 loc) · 3.77 KB
/
Library.java
File metadata and controls
144 lines (119 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**********************************************************
Author: Ethan Coomber
Middlebury College
Compilation: javac Library.java BST.java
Execution: java Library library.txt
Uses a binary search tree to keep track of a library. Books are
"checked in and out". The tree then deletes the node containing the book
or re-installs it.
***********************************************************/
import java.util.Scanner;
import java.io.File;
import java.util.Vector;
class Library{
static public int select(){
System.out.println("\nWelcome to the CSCI 201 Library.");
System.out.println("1: Check Availability");
System.out.println("2: Check out a book");
System.out.println("3: Return a book");
System.out.println("4: Print your checked out books");
System.out.println("5: Print catalog");
System.out.println("6: Number of items in collection");
System.out.println("7: Quit");
System.out.print("Choose and option: ");
Scanner sc = new Scanner(System.in);
String line = sc.next();
int option;
try{
option = Integer.parseInt(line);
if(option > 7 || option < 1){
throw new ArithmeticException("Selection is not in range.");
}
}
catch(Exception e){
System.out.println("Invalid Selection");
return select();
}
return option;
}
public static void main(String[] args){
File file = new File(args[0]);
Scanner sc;
try{
sc = new Scanner(file);
}
catch(Exception e){
return;
}
String line;
String[] info;
BST library = new BST();
while(sc.hasNext()){
line = sc.nextLine();
info = line.split(",");
Book bk = new Book(Double.parseDouble(info[0]),info[1],info[2]);
library.add(bk);
}
Vector<Book> books = new Vector<Book>();
Book book;
double ddn = 000;
int num = 0;
Scanner user = new Scanner(System.in);
int option = -1;
while(option != 7){
option = select();
switch(option){
case 1: // Check availability
System.out.print("Enter a call number: ");
ddn = user.nextDouble();
if(library.isAvailable(ddn)){
System.out.println("This title is available.");
}
else{
System.out.println("This title is not available.");
}
break;
case 2: // Check out a book
System.out.print("Enter a call number: ");
ddn = user.nextDouble();
if(library.isAvailable(ddn)){
book = library.remove(ddn);
if (book != null){
System.out.printf("Successfully checked out \"%s.\"\n", book.title);
}
books.add(book);
}
else{
System.out.println("This title is not available.");
}
break;
case 3: // Return a book
if (books.size() < 1){
System.out.println("You have no books checked out.");
break;
}
System.out.println("\nYou currently have the following books checked out.");
for(int ii = 0; ii < books.size(); ii++){
System.out.printf("%d: %s\n", ii, books.elementAt(ii));
}
System.out.print("Which book are you returning (0,1,2,3...): ");
num = user.nextInt();
book = books.elementAt(num);
books.remove(num);
library.add(book);
break;
case 4: //Print books currently checked out
for(Book b: books){
System.out.println(b);
}
break;
case 5: // Return the number of items in the collection
library.print();
break;
case 6: // Quit
System.out.printf("There are %d books in the library.\n", library.size());
break;
}
}
}
}