import java.io.InputStream;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.io.*;
public class RestaurantesBD10 {
///=========================================================================///
/// ///
/// Variaveis ///
/// ///
///=========================================================================///
static String i = "i";
static String a = "a";
static String r = "r";
static String m = "m";
static String s = "s";
static String letraOpcoes = "";
static final int MAX = 1000;
static Restaurante [] bd = new Restaurante [MAX];
static int pos = 0;
///=========================================================================///
/// ///
/// Main ///
/// ///
///=========================================================================///
public static void main(String[] opcoes) {
lerFicheiro();
while (!letraOpcoes.equals("s") && !letraOpcoes.equals("S")) {
showMenu();
letraOpcoes = getTexto (" ");
executarOpcao ();
}
}
///////////////////////////////////////////////////////////////////////////////
///=========================================================================///
/// ///
/// Auxiliares ///
/// ///
///=========================================================================///
static Restaurante novoRestaurante(String nome,String preco, String pratos){
Restaurante d = new Restaurante();
d.nome= nome;
d.preco= preco;
d.pratos= pratos;
return d;
}
static String getMsgInserir(){
return " i : Inserir Restaurante ";
}
static String getMsgAlterar(){
return " a : Alterar Restaurante ";
}
static String getMsgAlterarNome(){
return " an : Alterar Nome Restaurante ";
}
static String getMsgAlterarPreco(){
return " ap : Alterar Preco Restaurante ";
}
static String getMsgalterarPratos(){
return " apr: Alterar Pratos Restaurante ";
}
static String getMsgRemover(){
return " r : Remover Restaurante ";
}
static String getMsgListar(){
return " m : Listar Restaurante ";
}
static String getMsgProcurar(){
return " P : Procurar Restaurante ";
}
static String getMsgGuardar(){
return " g : Guardar Restaurante";
}
static String getMsgSair(){
return " s : Sair";
}
static void escreverFicheiro (String dados){
FileWriter writer = null;
try {
writer = new FileWriter("RestaurantesBD10.txt");
//False - para criar um ficheiro novo
//True - para adicionar a um ficheiro
writer.write(dados);
//writer.write("Ola");
//writer.write("\n\r");
writer.write(System.lineSeparator());
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
writer.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void lerFicheiro() {
// The name of the file to open.
String fileName = "RestaurantesBD10.txt";
try {
Scanner scanner = new Scanner(new File(fileName));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(!line.trim().isEmpty())
{
String[] dados = line.split("#");
bd[pos] = novoRestaurante (dados[0], dados[1], dados[2]);
pos=pos+1;
}
}
}
catch(FileNotFoundException ex) {
ex.printStackTrace();
}
}
///-------------------------------------------------------------------------///
/// Mostrar Menu ///
///-------------------------------------------------------------------------///
static void showMenu() {
System.out.println();
System.out.println(" -------------------------------------------");
System.out.println(" Restaurante ");
System.out.println(" -------------------------------------------");
System.out.println(getMsgInserir());
System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(getMsgAlterar());
System.out.println(getMsgAlterarNome());
System.out.println(getMsgAlterarPreco());
System.out.println(getMsgalterarPratos());
System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(getMsgRemover());
System.out.println(getMsgListar());
System.out.println(getMsgProcurar());
System.out.println(getMsgGuardar());
System.out.println(" <<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>");
System.out.println(getMsgSair());
System.out.println();
System.out.print(" Escolha uma opcao:");
}
static void executarOpcao() {
switch (letraOpcoes.toLowerCase()){
case "i":
case "-i":
processaInserirRestaurante();
break;
case "a":
case "-a":
processaAlterarRestaurante();
break;
case "an":
case "-an":
processaAlterarNomeRestaurante();
break;
case "ap":
case "-ap":
processaAlterarPrecoRestaurante();
break;
case "apr":
case "-apr":
processaalterarPratosRestaurante();
break;
case "r":
case "-r":
processaRemoverRestaurante();
break;
case "m":
case "-m":
processaListarRestaurante();
break;
case "p":
case "-p":
processaProcurarRestaurante();
break;
case "g":
case "-g":
processaGuardarRstaurante();
break;
case "s":
case "-s":
processaGuardarRstaurante();
break;
default:
break;
}
}
///-------------------------------------------------------------------------///
/// Executar a Mensagem ///
///-------------------------------------------------------------------------///
static String getTexto (String msg){
System.out.print(msg + " ");
InputStream teclado = System.in;
Scanner scanner = new Scanner(teclado);
String nome = scanner.nextLine();
return nome;
}
static int getNumero (String msg){
System.out.print(msg + " ");
InputStream teclado = System.in;
Scanner scanner = new Scanner(teclado);
int numero = scanner.nextInt();
return numero;
}
static void processaInserirRestaurante( ){
Restaurante d = new Restaurante();
d.nome = getTexto ("Diga qual e o nome do restaurante: ");
d.preco = getTexto ("Inserir o preco: ");
d.pratos = getTexto ("Inserir O nome do prato: ");
bd [pos] = d;
pos = pos + 1;
}
static void processaAlterarRestaurante( ){
String nome = getTexto("O nome do Restaurante a alterar: ");
String alterarPreco = getTexto("O novo preco a alterar: ");
String alterarPratoss = getTexto("O novo prato a alterar: ");
int i=0;
while (i<pos){
if(nome.equals(bd[i].nome))
{
bd[i].preco = alterarPreco;
bd[i].pratos = alterarPratoss;
System.out.println("O nome do restaurante e " + bd[i].nome + ", o preco e " + bd[i].preco + " e o nome do prato e " + bd[i].pratos);
}
i++;
}
}
static void processaAlterarNomeRestaurante( ){
String nome = getTexto("O nome do Restaurante a alterar: ");
String alterarNome = getTexto("O novo do Restaurante a alterar: ");
int i=0;
while (i<pos){
if(nome.equals(bd[i].nome))
{
bd[i].nome = alterarNome;
System.out.println("O nome do restaurante e " + bd[i].nome + ", o preco e " + bd[i].preco + " e o nome do prato e " + bd[i].pratos);
}
i++;
}
}
static void processaAlterarPrecoRestaurante( ){
String nome = getTexto("O nome do Restaurante para alterar o preco: ");
String alterarPreco = getTexto("O novo preco a alterar: ");
int i=0;
while (i<pos){
if(nome.equals(bd[i].nome))
{
bd[i].preco = alterarPreco;
System.out.println("O nome do restaurante e " + bd[i].nome + ", o preco e " + bd[i].preco + " e o nome do prato e " + bd[i].pratos);
}
i++;
}
}
static void processaalterarPratosRestaurante( ){
String nome = getTexto("O nome do Restaurante para alterar o prato: ");
String alterarPratos = getTexto("O novo prato a alterar: ");
int i=0;
while (i<pos){
if(nome.equals(bd[i].nome))
{
bd[i].pratos = alterarPratos;
System.out.println("O nome do restaurante e " + bd[i].nome + ", o preco e " + bd[i].preco + " e o nome do prato e " + bd[i].pratos);
}
i++;
}
}
static void processaGuardarRstaurante( ){
FileWriter writer = null;
try {
writer = new FileWriter("RestaurantesBD10.txt", false);
//False - para criar um ficheiro novo
//True - para adicionar a um ficheiro
int i=0;
while (i<pos){
writer.write(bd[i].nome + "#" + bd[i].preco + "#" + bd[i].pratos);
writer.write(System.lineSeparator());
i++;
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
writer.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
static void processaRemoverRestaurante(){
String nome = getTexto("Nome do Restaurante a Remover: ");
int i=0;
while (i<pos){
if(nome.equals(bd[i].nome)){
int j=pos-1;
while (j<i){
bd[j]=bd[j-1];
j=j-1;
}
pos--;
System.out.println("Restaurante Removido!!");
}
i++;
}
}
static void processaListarRestaurante( ){
int
i=0;
while (i<pos){
System.out.println("O nome do restaurante e " + bd[i].nome + ", o preco e " + bd[i].preco + " e o nome do prato e " + bd[i].pratos);
//i=i+1;
i++;
}
}
static void processaProcurarRestaurante( ){
String nome = getTexto("Procurar Restaurante: ");
int
i=0;
while (i<pos){
if(nome.equals(bd[i].nome))
{
System.out.println("O nome do restaurante e " + bd[i].nome + ", o preco e " + bd[i].preco + " e o nome do prato e " + bd[i].pratos);
}
//i=i+1;
i++;
}
}
///=========================================================================///
/// ///
/// Class ///
/// ///
///=========================================================================///
static class Restaurante{
String nome = " ";
String preco = " ";
String pratos = " ";
}
}