-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
95 lines (78 loc) · 2.92 KB
/
Main.java
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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import models.Movie;
import models.Store;
public class Main {
static Store store = new Store();
public static void main(String[] args) {
System.out.println("\n********************JAVA VIDEO STORE********************\n");
try {
loadMovies("movies.txt");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}finally {
System.out.println("Movies Loaded\n\n");
System.out.println(store);
manageMovies();
}
}
/**
* Name: manageMovies
* Inside the function:
* • 1. Starts a new instance of Scanner;
* • 2. In an infinite loop, the user can choose to a) purchase b) rent c) return d) exit.
* • case a: ask for the name and sell.
* • case b: ask for the name and rent.
* • case c: ask for the name and return.
* • 3. call close() from the Scanner object.
*/
public static void manageMovies() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("\nWould you like to \n\ta) purchase\n\tb) rent \n\tc) return");
String response = scan.nextLine();
if (!(response.equals("a") || response.equals("b") || response.equals("c"))) {
scan.close();
break;
}
System.out.print("Enter the name of the move: ");
String name = scan.nextLine();
if (store.getMovie(name) == null) {
System.out.println("\n\nThe input you provided is not valid. Please try again\n");
continue;
}
switch (response) {
case "a":
if (!(store.getMovie(name).isAvailable())) {
System.out.println("\n\n\n\nThe movie is not available for purchase. Please try again\n");
continue;
}
store.action(name, "sell"); break;
case "b": store.action(name, "rent"); break;
case "c": store.action(name, "return"); break;
}
System.out.println("\n\nUpdated Movies\n\n" + store);
}
}
/**
* Name: loadMovies
* @param fileName (String)
* @throws FileNotFoundException
*
* Inside the function:
* • 1. loads movies from <fileName>.txt.
* • 2. adds all movies to the store object's movie field.
* Hint: You will need to 'split' a String into three Strings.
*/
public static void loadMovies(String fileName) throws FileNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
Scanner scanFile = new Scanner(fis);
while (scanFile.hasNextLine()) {
String line = scanFile.nextLine();
String[] words = line.split("--");
store.addMovie(new Movie(words[0], words[1], Double.parseDouble(words[2])));
}
scanFile.close();
}
}