· 4 years ago · Apr 19, 2021, 02:58 AM
1import java.io.BufferedReader;
2import java.io.FileReader;
3import java.util.ArrayList;
4import java.util.Scanner;
5
6/**
7 * COP 3530: Project 5 - Hash Tables
8 * <p> This project implements a Hash Table using a specified hashing function. This class contains the entry point of
9 * the program as well as the table instance. Furthermore, this class will maintain a list of all the states.</p>
10 *
11 * @author <Hayden Keeney>
12 * @version <4/18/2021>
13 */
14public class Project5 {
15 private HashTable myTable;
16 private ArrayList<String> lines;
17
18 /**
19 * This creates an instance of Project5 taking only a path from which a file should be read.
20 *
21 * @param path the path to the file from which we read.
22 */
23 public Project5(String path) {
24 try {
25 BufferedReader reader = new BufferedReader(new FileReader(path));
26
27 myTable = new HashTable();
28 lines = new ArrayList<>();
29
30 String line;
31 reader.readLine();
32 while ((line = reader.readLine()) != null) {
33 String[] inputs = line.split(",");
34 myTable.insert(inputs[0], Long.parseLong(inputs[4]), Long.parseLong(inputs[6]));
35 lines.add(line);
36 }
37 } catch (Exception ex) {
38 ex.printStackTrace();
39 }
40 }
41
42 /**
43 * This is the entry point for the program. It will request user input, create an instance of project 5, and act
44 * as the IO handler for most of the program functionality.
45 *
46 * @param args command line arguments (unused in this program)
47 */
48 public static void main(String[] args) {
49 try {
50 Scanner scanner = new Scanner(System.in);
51 System.out.println("Enter a file path: ");
52 String path = scanner.nextLine();
53
54 Project5 project5 = new Project5(path);
55
56 boolean quit = false;
57 while (!quit) {
58 int input;
59 do {
60 System.out.print("\t1. Print hash table\n\t2. Delete a state of a given name\n\t3. Insert a " +
61 "state of a given name\n\t4. Search and print a state and its DR for a give name\n\t" +
62 "5. Print numbers of empty cells and collisions\n\t6. Exit\n");
63 System.out.print("Enter a number between 1 and 6: ");
64
65 while (!scanner.hasNextInt()) {
66 System.out.print("Please enter an integer: ");
67 scanner.next();
68 }
69 input = scanner.nextInt();
70 } while (input < 1 || input > 6);
71
72 switch (input) {
73 case 1 -> project5.myTable.display();
74 case 2 -> {
75 scanner = new Scanner(System.in);
76 String userInput;
77 System.out.print("Enter a state's name: ");
78 while (!scanner.hasNextLine()) {
79 System.out.print("Try again: ");
80 scanner.next();
81 }
82 userInput = scanner.nextLine();
83
84 boolean handled = false;
85 for (String line : project5.lines) {
86 String[] inputs = line.split(",");
87 if (inputs[0].equals(userInput)) {
88 String name = inputs[0];
89 long population = Long.parseLong(inputs[4]), deaths = Long.parseLong(inputs[6]);
90 int hash = project5.myTable.find(name, population, deaths);
91 if (hash != -1) {
92 project5.myTable.delete(inputs[0], Long.parseLong(inputs[4]),
93 Long.parseLong(inputs[6]));
94 System.out.println("Deleted " + userInput + "!");
95
96 handled = true;
97 } else {
98 System.out.println("That state wasn't in the table.");
99 }
100 }
101 }
102 if (!handled) {
103 System.out.println(userInput + " is not a state.");
104 }
105 }
106 case 3 -> {
107 scanner = new Scanner(System.in);
108 String userInput;
109 System.out.print("Enter a state's name: ");
110 while (!scanner.hasNextLine()) {
111 System.out.print("Try again: ");
112 scanner.next();
113 }
114 userInput = scanner.nextLine();
115
116 boolean handled = false;
117 for (String line : project5.lines) {
118 String[] inputs = line.split(",");
119 if (inputs[0].equals(userInput)) {
120 String name = inputs[0];
121 long population = Long.parseLong(inputs[4]), deaths = Long.parseLong(inputs[6]);
122 int hash = project5.myTable.find(name, population, deaths);
123 if (hash == -1) {
124 project5.myTable.insert(inputs[0], Long.parseLong(inputs[4]),
125 Long.parseLong(inputs[6]));
126 System.out.println("Inserted " + userInput + "!");
127 } else {
128 System.out.println(userInput + " already exists in this table.");
129 }
130 handled = true;
131 }
132 }
133 if (!handled) {
134 System.out.println(userInput + " is not a state.");
135 }
136 }
137 case 4 -> {
138 scanner = new Scanner(System.in);
139 String userInput;
140 System.out.print("Enter a state's name: ");
141 while (!scanner.hasNextLine()) {
142 System.out.print("Try again: ");
143 scanner.next();
144 }
145 userInput = scanner.nextLine();
146
147 boolean handled = false;
148 for (String line : project5.lines) {
149 String[] inputs = line.split(",");
150 if (inputs[0].equals(userInput)) {
151 String name = inputs[0];
152 long population = Long.parseLong(inputs[4]), deaths = Long.parseLong(inputs[6]);
153 int hash = project5.myTable.find(name, population, deaths);
154 if (hash != -1) {
155 for (int i = 0; i < project5.myTable.hashTable.get(hash).size(); i++) {
156 if (project5.myTable.hashTable.get(hash).get(i).name.equals(name)) {
157 project5.myTable.hashTable.get(hash).get(i).printNode();
158 }
159 }
160 } else {
161 System.out.println("That state wasn't in the table.");
162 }
163 handled = true;
164 }
165 }
166 if (!handled) {
167 System.out.println(userInput + " is not a state.");
168 } }
169 case 5 -> project5.myTable.printEmptyAndCollisions();
170 default -> {
171 System.out.println("Have a nice day!");
172 quit = true;
173 }
174 }
175 System.out.println();
176 }
177 } catch (Exception ex) {
178 ex.printStackTrace();
179 }
180 }
181}
182