Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jenny931224 authored Dec 29, 2019
1 parent 0541227 commit e601e90
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions Session10/FileHandlingGameApp/bin/lk/ac/pdn/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0/The Last of Us/9.8/In a hostile, post-pandemic world, Joel and Ellie, brought together by desperate circumstances, must rely on each other to survive a brutal journey across what remains of the United States.
1/The Witcher 3 Wild Hunt/9.8/Geralt of Rivia, a monster hunter for hire, embarks on an epic journey to find his former apprentice, Ciri, before The Wild Hunt can capture her and bring about the destruction of the world.
2/Grand Theft Auto V/9.6/Three very different criminals team up for a series of heists in the corrupt city of Los Santos.
3/Uncharted 4 A Thief's End/9.6/Thrown back into the dangerous underworld he'd tried to leave behind, Nathan Drake must decide what he's willing to sacrifice to save the ones he loves.
4/Mass Effect 2/9.6/After being revived from death and having to join a pro-human organization without a choice, Commander Shepard must assemble a team, battle a new threat, and make tough choices in order to save the galaxy from total annihilation once more.
56 changes: 56 additions & 0 deletions Session10/FileHandlingGameApp/src/lk/ac/pdn/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package lk.ac.pdn;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Game
{
private int id;

public String getName()
{
return name;
}

private String name;
private double rating;
private String description;

public Game(int id, String name, double rating, String description)
{
this.id = id;
this.name = name;
this.rating = rating;
this.description = description;
}

public void display()
{
System.out.println("Game " + id + ":");
System.out.println("Name: " + this.name);
System.out.println("Rating: " + this.rating);
System.out.println("Description: " + this.description);
System.out.println("\n\n");
}

public void writeToFile()
{
File file = new File("C:\\Users\\Pramuka Weerasinghe\\eclipse-workspace\\FileHandlingGameApp" + this.name + ".txt");
try
{
file.createNewFile();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(this.name + "\n");
bufferedWriter.write(this.rating + "\n");
bufferedWriter.write(this.description + "\n");
bufferedWriter.flush();
bufferedWriter.close();
}
catch (IOException ex)
{
System.out.println(ex);
}
}
}
153 changes: 153 additions & 0 deletions Session10/FileHandlingGameApp/src/lk/ac/pdn/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package lk.ac.pdn;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main
{


public static void main(String[] args)
{
Game[] games = new Game[10];

displayMenu(games);
}

public static void read_file(Game[] games)
{
Game game = null;
int counter = 0;
try
{
BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\Final Year\\Lab sheets review Java\\lastyearsession10\\lab9_file_handling\\data.txt"));

while (true)
{
String line = bufferedReader.readLine();
if (line == null)
{
bufferedReader.close();
break;
}
games[counter++] = createGame(line);
}
}
catch (IOException ex)
{
System.out.println(ex);
}
}

private static Game createGame(String line)
{
StringTokenizer stringTokenizer = new StringTokenizer(line, "/");
int id = Integer.parseInt(stringTokenizer.nextToken());
String name = stringTokenizer.nextToken();
double rating = Double.parseDouble(stringTokenizer.nextToken());
String description = stringTokenizer.nextToken();

return new Game(id, name, rating, description);
}

private static void displayGames(Game[] games)
{
for(Game game: games)
{
if (game != null)
{
game.display();
}
else
{
break;
}
}
}

private static void displayMenu(Game[] games)
{
int choice = 0;
Scanner scanner = new Scanner(System.in);

while (choice != -1)
{
System.out.println("Game Manager");
System.out.println("\n1. Load games.");
System.out.println("2. Display all games.");
System.out.println("3. Search for game.");
System.out.println("4. Exit.");

System.out.println("\nEnter your choice: ");

choice = scanner.nextInt();

switch (choice)
{
case 1:
read_file(games);
System.out.println("Games loaded successfully.\n\n");
break;
case 2:
displayGames(games);
break;
case 3:
search(games);
break;
case 4:
choice = -1;
System.out.println("Thank you for using Game Manager.");
break;
default:
System.out.println("\n\n Invalid Choice \n\n");
break;
}
}
}

private static void search(Game[] games)
{
Scanner scanner = new Scanner(System.in);

System.out.println("Enter name of game to search for: ");
String searchGame = scanner.nextLine();
int choice;

for (Game game: games)
{
if (game != null)
{
if (searchGame.equalsIgnoreCase(game.getName()))
{
game.display();
while (true)
{
System.out.println("1. Write game to file.");
System.out.println("2. Go back to main menu.");
System.out.println("Enter choice: ");
choice = scanner.nextInt();

if (choice == 1)
{
game.writeToFile();
System.out.println("Game written to file.");
return;
}
else if (choice == 2)
{
return;
}
else
{
System.out.println("Invalid choice.");
}
}
}
}
}
System.out.println("Game not found.");
}
}
5 changes: 5 additions & 0 deletions Session10/FileHandlingGameApp/src/lk/ac/pdn/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0/The Last of Us/9.8/In a hostile, post-pandemic world, Joel and Ellie, brought together by desperate circumstances, must rely on each other to survive a brutal journey across what remains of the United States.
1/The Witcher 3 Wild Hunt/9.8/Geralt of Rivia, a monster hunter for hire, embarks on an epic journey to find his former apprentice, Ciri, before The Wild Hunt can capture her and bring about the destruction of the world.
2/Grand Theft Auto V/9.6/Three very different criminals team up for a series of heists in the corrupt city of Los Santos.
3/Uncharted 4 A Thief's End/9.6/Thrown back into the dangerous underworld he'd tried to leave behind, Nathan Drake must decide what he's willing to sacrifice to save the ones he loves.
4/Mass Effect 2/9.6/After being revived from death and having to join a pro-human organization without a choice, Commander Shepard must assemble a team, battle a new threat, and make tough choices in order to save the galaxy from total annihilation once more.

0 comments on commit e601e90

Please sign in to comment.