In: Computer Science
Step 1:
Create a new Java project in NetBeans called “Wedding1”
Step 2:
Use appropriate data types to store the following information:
Step 3:
Add static methods to:
Step 4:
Add functionality to:
Note: please use basic exception handling (i.e. a try block) when attempting to access a file.
Step 5:
Add logic to your main() method that uses all of the methods you’ve created in order to prompt the user for values, store their responses, save those values to a file, and display the contents of the file.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Wedding.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Wedding {
// scanner for reading user input from keyboard
static Scanner keyboard = new Scanner(System.in);
// reads and returns the bride's name
static String getBrideName() {
System.out.print("Enter bride's name: ");
String bride = keyboard.nextLine();
return bride;
}
// reads and returns the groom's name
static String getGroomName() {
System.out.print("Enter groom's name: ");
String groom = keyboard.nextLine();
return groom;
}
// reads and returns the number of guests
static int getNumGuests() {
System.out.print("Enter number of guests: ");
int guests = Integer.parseInt(keyboard.nextLine());
return guests;
}
// reads and returns the square footage
static double getSquareFootage() {
System.out.print("Enter square footage of the location: ");
double sqft = Double.parseDouble(keyboard.nextLine());
return sqft;
}
// reads and returns the list of songs in DJ's playlist
static ArrayList<String> getPlayList() {
ArrayList<String> playList = new ArrayList<String>();
System.out.println("Enter songs in DJ's playlist in separate lines. "
+ "Type 'done' to finish");
String input = keyboard.nextLine();
// looping until user enter 'done'
while (!input.equalsIgnoreCase("done")) {
// adding to playlist
playList.add(input);
// reading next song name
input = keyboard.nextLine();
}
return playList;
}
// calculates and returns the number of guests per square foot using number
// of guests and sqft
static double getNumGuestsPerSquareFoot(int guests, double sqft) {
double guestsPerSqFt = (double) guests / sqft;
return guestsPerSqFt;
}
// writes all data to a file with given name
static void saveData(String filename, String bride, String groom,
int numGuests, double sqft, ArrayList<String> playList,
double numGuestsPerSqFt) throws FileNotFoundException {
// opening file in write mode
PrintWriter writer = new PrintWriter(new File(filename));
// writing all data
writer.println("Couple: " + bride + " & " + groom);
writer.println("Number of guests: " + numGuests);
// displaying square footage with 1 digit precision
writer.printf("Square footage of location: %.1f\n", sqft);
writer.println("Playlist: ");
// writing all songs
for (String song : playList) {
writer.println("\t" + song);
}
// displaying number of guests per sqft with 2 digits precision
writer.printf("Number of guests per square footage: %.2f\n",
numGuestsPerSqFt);
// closing file, saving changes
writer.close();
}
// loads a file and display the contents on screen
static void loadAndPrintData(String filename) throws FileNotFoundException {
Scanner scanner = new Scanner(new File(filename));
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
scanner.close();
}
public static void main(String[] args) {
// reading values in order
String brideName = getBrideName();
String groomName = getGroomName();
int numGuests = getNumGuests();
double squareFootage = getSquareFootage();
ArrayList<String> playList = getPlayList();
// calculating numGuestsPerSquareFoot
double numGuestsPerSquareFoot = getNumGuestsPerSquareFoot(numGuests,
squareFootage);
// opening try block, saving data to file, and loading from file,
// displaying contents
try {
saveData("wedding.txt", brideName, groomName, numGuests,
squareFootage, playList, numGuestsPerSquareFoot);
loadAndPrintData("wedding.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
/*OUTPUT*/
Enter bride's name: Lyla
Enter groom's name: John
Enter number of guests: 50
Enter square footage of the location: 2000
Enter songs in DJ's playlist in separate lines. Type 'done' to finish
Faded
Hello
Coldplay
Shape of You
done
Couple: Lyla & John
Number of guests: 50
Square footage of location: 2000.0
Playlist:
Faded
Hello
Coldplay
Shape of You
Number of guests per square footage: 0.03