You are on page 1of 9

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using System.Globalization;

namespace projekKos
{
class datakamar:setting
{
//constructor
//adl void atau function yg otomatis jalan
//saat kelas diinstance

public datakamar()
{
BukaKoneksi();
}

public void submenu()


{
//menu sorot
//terbentuk dari array 1 dimensi
string[] mn = new string[5];
mn[0] = "TAMBAH DATA";
mn[1] = "EDIT DATA";
mn[2] = "TAMPIL DATA";
mn[3] = "HAPUS DATA";
mn[4] = "MENU UTAMA";

Console.CursorVisible = false;

for (int i = 0; i < mn.Length; i++)


{
Console.SetCursorPosition(3, 10 + (2 * i));
Console.Write(mn[i]);
}

////berikan sorot di menu paling atas


Console.SetCursorPosition(3, 10);
Console.BackgroundColor = ConsoleColor.Red;
Console.Write(mn[0]);
Console.SetCursorPosition(3, 10);

//kendalikan sorot dengan menekan panah atas/bawah


ConsoleKeyInfo tombol;
int idx1 = 0;
do
{
tombol = Console.ReadKey();

if (tombol.Key == ConsoleKey.UpArrow) //panah Atas


{
//index lama
//netralkan sorot pada posisi index lama
Console.SetCursorPosition(3, 10 + (idx1 * 2));
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(mn[idx1]);

//merubah index
if (idx1 == 0)
{
idx1 = 4;
}
else
{
idx1 = idx1 - 1;
}

//sdh index baru


//berikan sorot pada posisi index baru
Console.SetCursorPosition(3, 10 + (idx1 * 2));
Console.BackgroundColor = ConsoleColor.Red;
Console.Write(mn[idx1]);
Console.SetCursorPosition(3, 10 + (idx1 * 2));
}
else if (tombol.Key == ConsoleKey.DownArrow) //panah Bawah
{
//index lama
//netralkan sorot pada posisi index lama
Console.SetCursorPosition(3, 10 + (idx1 * 2));
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(mn[idx1]);

//merubah index
if (idx1 < 4)
{
idx1 = idx1 + 1;
}
else
{
idx1 = 0;
}

//sdh index baru


//berikan sorot pada posisi index baru
Console.SetCursorPosition(3, 10 + (idx1 * 2));
Console.BackgroundColor = ConsoleColor.Red;
Console.Write(mn[idx1]);
Console.SetCursorPosition(3, 10 + (idx1 * 2));
}
else if (tombol.Key == ConsoleKey.Enter)
{
//deteksi posisi index
switch (idx1)
{
case 0: //tambah data kamar
//bersihkan area konten
BersihArea(21, 4, 85, 25);
TeksTengah("TAMBAH DATA KAMAR BARU", 21, 85, 5,
ConsoleColor.Red);
//TeksTengah("======================", 21, 85, 6,
ConsoleColor.Red);

Label(30, 7, "No.Kamar :",ConsoleColor.Black,


ConsoleColor.White);
Label(30, 9, "Nm.Kamar :", ConsoleColor.Black,
ConsoleColor.White);
Label(30, 11, "Fasilitas :", ConsoleColor.Black,
ConsoleColor.White);
Label(30, 14, "Ukuran :", ConsoleColor.Black,
ConsoleColor.White);
Label(30, 16, "Harga :", ConsoleColor.Black,
ConsoleColor.White);

string no_kamar = TextBox(42, 7, 5, ConsoleColor.Red,


ConsoleColor.White);
string nm_kamar = TextBox(42, 9, 30, ConsoleColor.Red,
ConsoleColor.White);
string fasilitas1 = TextBox(42, 11, 30, ConsoleColor.Red,
ConsoleColor.White);
string fasilitas2 = TextBox(42, 12, 30, ConsoleColor.Red,
ConsoleColor.White);
string ukuran = TextBox(42, 14, 25, ConsoleColor.Red,
ConsoleColor.White);
string harga = TextBox(42, 16, 15, ConsoleColor.Red,
ConsoleColor.White);

Console.SetCursorPosition(42, 18);
string tanya = "Apakah data ingin disimpan (Y/N) : ";
Console.Write(tanya);
string jwb = TextBox(42 + tanya.Length, 18, 2,
ConsoleColor.Red, ConsoleColor.White);

if (jwb == "Y" || jwb == "y")


{
//simpan data baru
string sqltext = "INSERT INTO [tabel kamar]
(no_kamar,nm_kamar,fasilitas,ukuran,harga) VALUES (@1,@2,@3,@4,@5)";
OleDbCommand cmd = new OleDbCommand(sqltext, conn);
cmd.Parameters.Add("@1", OleDbType.Integer).Value =
no_kamar.ToUpper().Trim();
cmd.Parameters.Add("@2", OleDbType.VarChar).Value =
nm_kamar.ToUpper().Trim();
cmd.Parameters.Add("@3", OleDbType.VarChar).Value =
CultureInfo.CurrentCulture.TextInfo.ToTitleCase(fasilitas1.Trim()) +

fasilitas2.ToUpper().Trim();
cmd.Parameters.Add("@4", OleDbType.VarChar).Value =
ukuran.ToUpper().Trim();
cmd.Parameters.Add("@5", OleDbType.Currency).Value =
harga;

int hsl = cmd.ExecuteNonQuery();


if (hsl > 0)
{
Console.SetCursorPosition(42, 20);
Console.Write(CultureInfo.CurrentCulture.TextInfo.ToTitleCase("data berhasil
tersimpan"));
}
else
{
Console.SetCursorPosition(42, 20);
Console.Write("Data Tidak Tersimpan");
}
}
else
{
Console.SetCursorPosition(42, 20);
Console.Write("Data Tidak Tersimpan");
}
break;

case 1:
//edit data
//pencarian data yang ingin diedit
//bersihkan area konten
BersihArea(21, 4, 85, 25);
TeksTengah("EDIT DATA KAMAR BARU", 21, 85, 5,
ConsoleColor.Red);

Label(30, 7, "Ketik Nm.Kamar yg ingin diedit :",


ConsoleColor.Black, ConsoleColor.White);
string cr_nmkamar = TextBox(63, 7, 15, ConsoleColor.Red,
ConsoleColor.White);

string sqltext_cr = "SELECT * FROM [tabel kamar] WHERE


nm_kamar = '" + cr_nmkamar + "'";

DataTable dtkamar = new DataTable();


dtkamar = bacaTabel(sqltext_cr);

if (dtkamar.Rows.Count > 0)
{ //data ketemu

Label(30, 9, "DATA KAMAR KETEMU :", ConsoleColor.Black,


ConsoleColor.White);
Label(30, 10, "-------------------", ConsoleColor.Black,
ConsoleColor.White);
for (int i = 0; i < dtkamar.Rows.Count; i++)
{
Console.SetCursorPosition(30, 11);
Console.WriteLine("Nomor kamar : " +
dtkamar.Rows[i][0]);
Console.SetCursorPosition(30, 12);
Console.WriteLine("Nama kamar : " +
dtkamar.Rows[i][1]);
Console.SetCursorPosition(30, 13);
Console.WriteLine("Fasilitas : " +
dtkamar.Rows[i][2].ToString().Substring(0, 40));
Console.SetCursorPosition(30, 14);
Console.WriteLine(" " +
dtkamar.Rows[i][2].ToString().Substring(40, dtkamar.Rows[i][2].ToString().Length - 40));
Console.SetCursorPosition(30, 15);
Console.WriteLine("Ukuran : " +
dtkamar.Rows[i][3]);
Console.SetCursorPosition(30, 16);
Console.WriteLine("Harga : " +
dtkamar.Rows[i][4]);
Console.SetCursorPosition(30, 17);
Console.WriteLine("Status : " +
dtkamar.Rows[i][5]);
}

Console.SetCursorPosition(30, 24);
tanya = "Apakah data ingin diEdit (Y/N) : ";
Console.Write(tanya);
jwb = TextBox(30 + tanya.Length, 24, 2, ConsoleColor.Red,
ConsoleColor.White);

if (jwb == "Y" || jwb == "y")


{
//bersihkan area konten
BersihArea(21, 4, 85, 25);
TeksTengah("EDIT DATA KAMAR BARU", 21, 85, 5,
ConsoleColor.Red);
//TeksTengah("======================", 21, 85, 6,
ConsoleColor.Red);

Label(30, 7, "No.Kamar : " + dtkamar.Rows[0][0],


ConsoleColor.Black, ConsoleColor.White);
Label(30, 9, "Nm.Kamar :", ConsoleColor.Black,
ConsoleColor.White);
Label(30, 11, "Fasilitas :", ConsoleColor.Black,
ConsoleColor.White);
Label(30, 14, "Ukuran :", ConsoleColor.Black,
ConsoleColor.White);
Label(30, 16, "Harga :", ConsoleColor.Black,
ConsoleColor.White);

no_kamar = dtkamar.Rows[0][0].ToString();
nm_kamar = TextBox(42, 9, 30, ConsoleColor.Red,
ConsoleColor.White);
fasilitas1 = TextBox(42, 11, 30, ConsoleColor.Red,
ConsoleColor.White);
fasilitas2 = TextBox(42, 12, 30, ConsoleColor.Red,
ConsoleColor.White);
ukuran = TextBox(42, 14, 25, ConsoleColor.Red,
ConsoleColor.White);
harga = TextBox(42, 16, 15, ConsoleColor.Red,
ConsoleColor.White);

Console.SetCursorPosition(42, 18);
tanya = "Apakah data ingin diSimpan (Y/N) : ";
Console.Write(tanya);
jwb = TextBox(42 + tanya.Length, 18, 2,
ConsoleColor.Red, ConsoleColor.White);

if (jwb == "Y" || jwb == "y")


{
//simpan data edit
string sqltext = "UPDATE [tabel kamar] SET
nm_kamar=@2, fasilitas=@3, ukuran=@4, harga=@5 WHERE no_kamar=" + int.Parse(no_kamar);
OleDbCommand cmd = new OleDbCommand(sqltext,
conn);
cmd.Parameters.Add("@2", OleDbType.VarChar).Value
= nm_kamar.ToUpper().Trim();
cmd.Parameters.Add("@3", OleDbType.VarChar).Value
= CultureInfo.CurrentCulture.TextInfo.ToTitleCase(fasilitas1.Trim()) +

fasilitas2.ToUpper().Trim();
cmd.Parameters.Add("@4", OleDbType.VarChar).Value
= ukuran.ToUpper().Trim();
cmd.Parameters.Add("@5",
OleDbType.Currency).Value = harga;

int hsl = cmd.ExecuteNonQuery();


if (hsl > 0)
{
Console.SetCursorPosition(42, 20);

Console.Write(CultureInfo.CurrentCulture.TextInfo.ToTitleCase("data berhasil
tersimpan"));
}
else
{
Console.SetCursorPosition(42, 20);
Console.Write("Data Tidak Tersimpan");
}
}
else
{
//bersihkan area konten
BersihArea(21, 4, 85, 25);
}
}
else
{
//bersihkan area konten
BersihArea(21, 4, 85, 25);
}
}
else
{
Label(30, 9, "DATA KAMAR TIDAK KETEMU",
ConsoleColor.Black, ConsoleColor.White);
}
break;

case 2:
//tampil all data
//bersihkan area konten
BersihArea(21, 4, 85, 25);
TeksTengah("DATA KAMAR", 21, 85, 5, ConsoleColor.Red);

string sqltext1 = "SELECT * FROM [tabel kamar]";

DataTable dtkamar1 = new DataTable();


dtkamar1 = bacaTabel(sqltext1);
if (dtkamar1.Rows.Count > 0)
{
Console.ForegroundColor = ConsoleColor.White;

string l1, l2, l3, l4, l5, l6;


l1 = "No kamar";
l2 = "Nama kamar";
l3 = "Fasilitas";
l4 = "Ukuran";
l5 = "Harga";
l6 = "Status";
Console.SetCursorPosition(22, 8);
Console.WriteLine(l1);
Console.SetCursorPosition(22 + l1.Length + 2, 8);
Console.WriteLine(l2);
Console.SetCursorPosition(22 + l1.Length + l2.Length + 4,
8);
Console.WriteLine(l3);
Console.SetCursorPosition(22 + l1.Length + l2.Length +
l3.Length + 36, 8);
Console.WriteLine(l4);
Console.SetCursorPosition(22 + l1.Length + l2.Length +
l3.Length + l4.Length + 38, 8);
Console.WriteLine(l5);
Console.SetCursorPosition(22 + l1.Length + l2.Length +
l3.Length + l4.Length + l5.Length + 40, 8);
Console.WriteLine(l6);

Console.ForegroundColor = ConsoleColor.Yellow;
for (int i = 0; i < dtkamar1.Rows.Count; i++)
{
Console.SetCursorPosition(22, 8 + (i+1));
Console.WriteLine(dtkamar1.Rows[i][0]);

Console.SetCursorPosition(22 + l1.Length + 2, 8 +
(i+1));
Console.WriteLine(dtkamar1.Rows[i][1]);

Console.SetCursorPosition(22 + l1.Length + l2.Length


+ 4, 8 + (i+1));

Console.WriteLine(dtkamar1.Rows[i][2].ToString().Substring(0, 40));

Console.SetCursorPosition(22 + l1.Length + l2.Length


+ l3.Length + 36, 8 + (i+1));
Console.WriteLine(dtkamar1.Rows[i][3]);

Console.SetCursorPosition(22 + l1.Length + l2.Length


+ l3.Length + l4.Length + 38, 8 + (i+1));
Console.WriteLine(dtkamar1.Rows[i][4]);

Console.SetCursorPosition(22 + l1.Length + l2.Length


+ l3.Length + l4.Length + l5.Length + 40, 8 + (i+1));
Console.WriteLine(dtkamar1.Rows[i][5]);
}
}
else
{
Label(30, 9, "DATA KAMAR TIDAK KETEMU",
ConsoleColor.Black, ConsoleColor.White);
}

break;

case 3:
//delete data
//pencarian data yang ingin didelete
//bersihkan area konten
BersihArea(21, 4, 85, 25);
TeksTengah("HAPUS DATA KAMAR", 21, 85, 5, ConsoleColor.Red);

Label(30, 7, "Ketik Nm.Kamar yg ingin dihapus :",


ConsoleColor.Black, ConsoleColor.White);
cr_nmkamar = TextBox(63, 7, 15, ConsoleColor.Red,
ConsoleColor.White);

sqltext_cr = "SELECT * FROM [tabel kamar] WHERE nm_kamar = '"


+ cr_nmkamar + "'";

DataTable dtkamar2 = new DataTable();


dtkamar2 = bacaTabel(sqltext_cr);

if (dtkamar2.Rows.Count > 0)
{ //data ketemu

Label(30, 9, "DATA KAMAR KETEMU :", ConsoleColor.Black,


ConsoleColor.White);
Label(30, 10, "-------------------", ConsoleColor.Black,
ConsoleColor.White);
for (int i = 0; i < dtkamar2.Rows.Count; i++)
{
Console.SetCursorPosition(30, 11);
Console.WriteLine("Nomor kamar : " +
dtkamar2.Rows[i][0]);
Console.SetCursorPosition(30, 12);
Console.WriteLine("Nama kamar : " +
dtkamar2.Rows[i][1]);
Console.SetCursorPosition(30, 13);
Console.WriteLine("Fasilitas : " +
dtkamar2.Rows[i][2].ToString().Substring(0, 40));
Console.SetCursorPosition(30, 14);
Console.WriteLine(" " +
dtkamar2.Rows[i][2].ToString().Substring(40, dtkamar2.Rows[i][2].ToString().Length -
40));
Console.SetCursorPosition(30, 15);
Console.WriteLine("Ukuran : " +
dtkamar2.Rows[i][3]);
Console.SetCursorPosition(30, 16);
Console.WriteLine("Harga : " +
dtkamar2.Rows[i][4]);
Console.SetCursorPosition(30, 17);
Console.WriteLine("Status : " +
dtkamar2.Rows[i][5]);
}
Console.SetCursorPosition(30, 24);
tanya = "Apakah data ingin diHapus (Y/N) : ";
Console.Write(tanya);
jwb = TextBox(30 + tanya.Length, 24, 2, ConsoleColor.Red,
ConsoleColor.White);

if (jwb == "Y" || jwb == "y")


{
//simpan data hapus
no_kamar = dtkamar2.Rows[0][0].ToString();
string sqltext = "DELETE * FROM [tabel kamar] WHERE
no_kamar=" + int.Parse(no_kamar);
OleDbCommand cmd = new OleDbCommand(sqltext, conn);

int hsl = cmd.ExecuteNonQuery();


if (hsl > 0)
{
Console.SetCursorPosition(42, 20);

Console.Write(CultureInfo.CurrentCulture.TextInfo.ToTitleCase("data berhasil dihapus"));


}
else
{
Console.SetCursorPosition(42, 20);
Console.Write("Data Tidak Berhasil Dihapus");
}
}
else
{
//bersihkan area konten
BersihArea(21, 4, 85, 25);
}
}
else
{
Label(30, 9, "DATA KAMAR TIDAK KETEMU",
ConsoleColor.Black, ConsoleColor.White);
}
break;
}
}

}
while ((tombol.Key != ConsoleKey.Enter) || (idx1 < 4));

Console.SetCursorPosition(25, 25);
Console.Write("Anda keluar dari Sub Menu");

}
}

You might also like