Option 1: Ask users to input Person Name, Ticket Type and Fine. You will keep as
ID: 3914543 • Letter: O
Question
Option 1: Ask users to input Person Name, Ticket Type and Fine. You will keep asking users until they press q key. Once user types Q, you will store all the details into the .txt file or any other file
Option2: Use the file that is generated from Option 1 and perform sorting by person name. You also need to prompt users if option 1 is not complete. You will then create another file with sorted person name.
Option3: Display file. You will read entire file from option 1 and display it on the screen. I would like to see everything as comma separated. You also need to use exception handling here.
Option 4: Ask users to enter a search keyword. You will then search the keyword in the file (option 1) and display success or failure message
Option5: Use the array concept and read the person name from the file. Just view the entire array on the output screen.
Option 6: Replace function. Ask users to replace ticket type and generate a new file with new ticket type. Please keep rest of the information same as it is.
Option 7: Delete all the files from hard drive
here is following code but option 2 and 7 doesnt work pls help
package zhao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class groupProject {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userChoice;
String name, type;
double fine;
boolean quit = false;
Person person = null;
List<Person> list = new ArrayList<Person>();
String fileName = ".txt";
String sorted = "E:\using.txt";
String newFile = "file.txt";
PrintWriter out = null;
PrintWriter sortedOut = null;
PrintWriter newWriter = null;
boolean infoExists = false;
try {
out = new PrintWriter(fileName);
sortedOut = new PrintWriter(sorted);
newWriter = new PrintWriter(newFile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("---------------------------------------------");
do {
System.out.println("1.Add ticket details");
System.out.println("2.Sort tickets ");
System.out.println("3.Display tickets");
System.out.println("4.Search tickets");
System.out.println("5.Show person names with tickets");
System.out.println("6.Modify ticket type");
System.out.println("7.Delete");
System.out.println("Enter your choice, 0 to quit: ");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
String choice = "y";
do {
System.out.println("Enter Person name :");
name = in.next();
System.out.println("Enter ticket type :");
type = in.next();
System.out.println("Enter fine amount :");
fine = in.nextDouble();
String temp = name + "," + type + "," + String.valueOf(fine);
out.println(temp);
infoExists = true;
System.out.println("Add more details y/q");
choice = in.next();
} while (choice.equals("y"));
out.close();
break;
case 2:
if (infoExists == true) {
System.out.println("Sorting files");
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
String[] input = line.split(",");
person = new Person(input[0], input[1], Double.parseDouble(input[2]));
list.add(person);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.sort(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.name.compareTo(o2.name);
}
});
for (int i = 0; i < list.size(); i++) {
String outLine = list.get(i).name + "," + list.get(i).ticketType + ","
+ String.valueOf(list.get(i).fine);
sortedOut.println(outLine);
}
} else {
System.out.println("Please add details using option 1");
}
break;
case 3:
System.out.println("Displaying records");
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
String[] input = line.split(",");
System.out.println(input[0] + " " + input[1] + " " + Double.parseDouble(input[2]));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 4:
System.out.println("Enter search keyword:");
String input1 = in.next();
Scanner scan;
try {
scan = new Scanner(new File(fileName));
while (scan.hasNext()) {
String line = scan.next();
if (line.contains(input1)) {
System.out.println("Success");
} else {
System.out.println("Failure");
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 5:
System.out.println("Person names");
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
String[] input = line.split(",");
System.out.println(input[0]);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 6:
System.out.println("Replacing ticket type");
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
String[] input = line.split(",");
System.out.println("Old ticket type is " + input[1]);
System.out.println("Enter new ticket type: ");
type = in.next();
String temp = input[0] + " " + input[1] + " " + Double.parseDouble(input[2]);
newWriter.println(temp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 7:
System.out.println("Clearing records");
String dir = "E:\\using.txt";
File files = new File(dir);
for (File file : files.listFiles())
if (!file.isDirectory())
file.delete();
break;
case 0:
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
in.close();
System.out.println("Bye!");
}
}
// Used while sorting person by name
class Person {
String name;
String ticketType;
double fine;
public Person(String name, String ticketType, double fine) {
this.name = name;
this.ticketType = ticketType;
this.fine = fine;
}
}
Explanation / Answer
Given below is the modified code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
package zhao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class groupProject {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userChoice;
String name, type;
double fine;
boolean quit = false;
Person person = null;
List<Person> list = new ArrayList<Person>();
String inputFile = "E:\input.txt";
String sortedFile = "E:\sorted.txt";
String replacedFile = "E: eplaced.txt";
boolean infoExists = false;
System.out.println("---------------------------------------------");
do {
System.out.println("1.Add ticket details");
System.out.println("2.Sort tickets ");
System.out.println("3.Display tickets");
System.out.println("4.Search tickets");
System.out.println("5.Show person names with tickets");
System.out.println("6.Modify ticket type");
System.out.println("7.Delete");
System.out.println("Enter your choice, 0 to quit: ");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
String choice = "y";
try (PrintWriter out = new PrintWriter(new File(inputFile))){
do {
System.out.println("Enter Person name :");
name = in.next();
System.out.println("Enter ticket type :");
type = in.next();
System.out.println("Enter fine amount :");
fine = in.nextDouble();
String temp = name + "," + type + "," + String.valueOf(fine);
out.println(temp);
System.out.println("Add more details y/q");
choice = in.next();
} while (choice.equals("y"));
infoExists = true;
System.out.println("User input saved to file " + inputFile);
}
catch(Exception e){
e.printStackTrace();
}
break;
case 2:
if (infoExists == true) {
System.out.println("Sorting files");
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] input = line.split(",");
person = new Person(input[0], input[1], Double.parseDouble(input[2]));
list.add(person);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
list.sort(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.name.compareTo(o2.name);
}
});
try(PrintWriter sortedOut = new PrintWriter(new File(sortedFile))){
for (int i = 0; i < list.size(); i++) {
String outLine = list.get(i).name + "," + list.get(i).ticketType + ","
+ String.valueOf(list.get(i).fine);
sortedOut.println(outLine);
System.out.println(outLine);
}
System.out.println("Sorted records written to " + sortedFile);
}
catch(Exception e){
e.printStackTrace();
}
} else {
System.out.println("Please add details using option 1");
}
break;
case 3:
if(!infoExists)
{
System.out.println("Please add details using option 1");
break;
}
System.out.println("Displaying records");
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 4:
if(!infoExists)
{
System.out.println("Please add details using option 1");
break;
}
System.out.println("Enter search keyword:");
String input1 = in.next();
Scanner scan;
boolean found = false;
try {
scan = new Scanner(new File(inputFile));
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (line.contains(input1)) {
System.out.println("Success");
found = true;
break;
}
}
if(!found)
System.out.println("Failure");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 5:
if(!infoExists)
{
System.out.println("Please add details using option 1");
break;
}
System.out.println("Person names");
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] input = line.split(",");
System.out.println(input[0]);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 6:
if(!infoExists)
{
System.out.println("Please add details using option 1");
break;
}
System.out.println("Replacing ticket type");
try (BufferedReader br = new BufferedReader(new FileReader(inputFile));
PrintWriter newWriter = new PrintWriter(new File(replacedFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] input = line.split(",");
System.out.println("Old ticket type for " + input[0] + " is " + input[1]);
System.out.println("Enter new ticket type: ");
type = in.next();
String temp = input[0] + "," + input[1] + "," + Double.parseDouble(input[2]);
newWriter.println(temp);
}
System.out.println("Modified records written to " + replacedFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 7:
System.out.println("Clearing records");
String[] filenames = {inputFile, sortedFile, replacedFile};
for(int i = 0; i < filenames.length; i++){
File f = new File(filenames[i]);
if(f.exists()){
f.delete();
System.out.println("Deleted file " + filenames[i]);
}
}
break;
case 0:
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
in.close();
System.out.println("Bye!");
}
}
// Used while sorting person by name
class Person {
String name;
String ticketType;
double fine;
public Person(String name, String ticketType, double fine) {
this.name = name;
this.ticketType = ticketType;
this.fine = fine;
}
}
output
---------------------------------------------
1.Add ticket details
2.Sort tickets
3.Display tickets
4.Search tickets
5.Show person names with tickets
6.Modify ticket type
7.Delete
Enter your choice, 0 to quit:
1
Enter Person name :
john
Enter ticket type :
t1
Enter fine amount :
3
Add more details y/q
y
Enter Person name :
bob
Enter ticket type :
t2
Enter fine amount :
4
Add more details y/q
y
Enter Person name :
alice
Enter ticket type :
t4
Enter fine amount :
9
Add more details y/q
q
User input saved to file input.txt
1.Add ticket details
2.Sort tickets
3.Display tickets
4.Search tickets
5.Show person names with tickets
6.Modify ticket type
7.Delete
Enter your choice, 0 to quit:
3
Displaying records
john,t1,3.0
bob,t2,4.0
alice,t4,9.0
1.Add ticket details
2.Sort tickets
3.Display tickets
4.Search tickets
5.Show person names with tickets
6.Modify ticket type
7.Delete
Enter your choice, 0 to quit:
2
Sorting files
alice,t4,9.0
bob,t2,4.0
john,t1,3.0
Sorted records written to sorted.txt
1.Add ticket details
2.Sort tickets
3.Display tickets
4.Search tickets
5.Show person names with tickets
6.Modify ticket type
7.Delete
Enter your choice, 0 to quit:
7
Clearing records
Deleted file input.txt
Deleted file sorted.txt
1.Add ticket details
2.Sort tickets
3.Display tickets
4.Search tickets
5.Show person names with tickets
6.Modify ticket type
7.Delete
Enter your choice, 0 to quit:
0
Bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.