Rancangan Class dan Penjelasan:
- TempelKartu
- Class ini berfungsi sebagai perantara antara user dan database.
- TotalAkun
- Class ini berfungsi sebagai pemroses data untuk single user.
- GTODatabase
- Class ini berfungsi sebagai pemanggil proses yang ada di TotalAkun.
- Keypad
- Class ini berfungsi sebagai pengatur segala inputan.
- Layar
- Class ini berfungsi sebagai penampil berbagai hasil yang di outputkan oleh system.
- GTO
- Class ini adalah pengatur utama dalam penerapan sistem GTO.
- RunProgram
- Class ini sebagai pemicu program utama.
Rancangan Output:
Output yang dihasilkan dari program ini adalah:- Jika saldo>=Harga e-toll:
- nama perusahaan
- tanggal transaksi
- harga e-toll
- sisa saldo e-toll
- Jika saldo<Harga e-toll:
- Output string "Saldo tidak mencukupi"
Codingan:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Write a description of class GTO here. | |
* | |
* @author (your name) | |
* @version (a version number or a date) | |
*/ | |
import java.util.Date; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
public class GTO | |
{ | |
// instance variables - replace the example below with your own | |
private boolean userSession; | |
private int currentUserId; | |
private TempelKartu tempelKartu; | |
private Layar layar; | |
private Keypad keypad; | |
private String namaPerusahaan; | |
private String tanggalNow; | |
private int moneyGTO; | |
/** | |
* Constructor for objects of class GTO | |
*/ | |
public GTO() | |
{ | |
// initialise instance variables | |
userSession = false; | |
currentUserId = 0; | |
tempelKartu = new TempelKartu(); | |
layar = new Layar(); | |
keypad = new Keypad(); | |
namaPerusahaan = "TRANS MARGA JATENG"; | |
moneyGTO = 7500; | |
} | |
public void proses() | |
{ | |
while(!userSession){ | |
autentifikasi(); | |
} | |
prosesTransaksi(); | |
userSession = false; | |
currentUserId = 0; | |
} | |
public void autentifikasi(){ | |
layar.pesan("Masukan UserId: "); | |
int kartuUserID = keypad.getInput(); | |
userSession = tempelKartu.changeGTOStatus(kartuUserID); | |
if(userSession){ | |
currentUserId = kartuUserID; | |
} | |
} | |
public void prosesTransaksi(){ | |
int saldo = tempelKartu.getMoneyCard(currentUserId); | |
if (saldo>=moneyGTO){ | |
tempelKartu.callBayar(currentUserId,moneyGTO); | |
printStruk(); | |
} | |
else{ | |
layar.pesanEnter("Saldo Tidak Mencukupi"); | |
} | |
} | |
public void printStruk(){ | |
DateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy H:mm:ss"); | |
Date date = new Date(); | |
String timeStamp = formatDate.format(date); | |
int sisaUang = tempelKartu.getMoneyCard(currentUserId); | |
layar.pesanEnter(namaPerusahaan); | |
layar.pesanEnter(timeStamp); | |
layar.pesan("Harga: "); | |
layar.pesanUang(moneyGTO); | |
layar.pesan("\nSisa Saldo: "); | |
layar.pesanUang(sisaUang); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Write a description of class GTODatabase here. | |
* | |
* @author (your name) | |
* @version (a version number or a date) | |
*/ | |
public class GTODatabase | |
{ | |
// instance variables - replace the example below with your own | |
private TotalAkun[] akunUser; | |
/** | |
* Constructor for objects of class GTODatabase | |
*/ | |
public GTODatabase() | |
{ | |
// initialise instance variables | |
akunUser = new TotalAkun[2]; | |
akunUser[0] = new TotalAkun(123456, 2000, "Orang B"); | |
akunUser[1] = new TotalAkun(248350, 100000, "Aguel Satria Wijaya"); | |
} | |
/** | |
* An example of a method - replace this comment with your own | |
* | |
* @param y a sample parameter for a method | |
* @return the sum of x and y | |
*/ | |
public boolean autentifikasi(int idKartu) | |
{ | |
for(TotalAkun akunEcer: akunUser){ | |
if (akunEcer.getId() == idKartu) return true; | |
} | |
return false; | |
} | |
private TotalAkun getID(int akunID){ | |
// loop through accounts searching for matching account number | |
for(TotalAkun akunEcer : akunUser){ | |
// return current account if match found | |
if(akunEcer.getId() == akunID) return akunEcer; | |
} | |
return null; // if no matching account was found, return null | |
} | |
public void bayarTol(int userId, int amount){ | |
getID(userId).bayarTol(amount); | |
} | |
public int getMoney(int userId){ | |
return getID(userId).getMoney(); | |
} | |
public String getNama(int userId){ | |
return getID(userId).getNama(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Keypad{ | |
private Scanner input; | |
public Keypad(){ | |
input = new Scanner(System.in); | |
} | |
public int getInput(){ | |
return input.nextInt(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Write a description of class Struk here. | |
* | |
* @author (your name) | |
* @version (a version number or a date) | |
*/ | |
public class Layar | |
{ | |
/** | |
* An example of a method - replace this comment with your own | |
* | |
* @param y a sample parameter for a method | |
* @return the sum of x and y | |
*/ | |
public void pesan(String message){ | |
System.out.print(message); | |
} | |
public void pesanEnter(String message){ | |
System.out.println(message); | |
} | |
public void pesanUang(int amount){ | |
System.out.printf("Rp. %d",amount); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Write a description of class runProgram here. | |
* | |
* @author (your name) | |
* @version (a version number or a date) | |
*/ | |
public class RunProgram | |
{ | |
public static void main(String[] args) | |
{ | |
GTO playGTO = new GTO(); | |
playGTO.proses(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Write a description of class TempelKartu here. | |
* | |
* @author (your name) | |
* @version (a version number or a date) | |
*/ | |
public class TempelKartu | |
{ | |
// instance variables - replace the example below with your own | |
private GTODatabase gtoDatabase; | |
public TempelKartu() | |
{ | |
// initialise instance variables | |
gtoDatabase = new GTODatabase(); | |
} | |
/** | |
* An example of a method - replace this comment with your own | |
* | |
* @param y a sample parameter for a method | |
* @return the sum of x and y | |
*/ | |
public boolean changeGTOStatus(int userID) | |
{ | |
boolean status = gtoDatabase.autentifikasi(userID); | |
return status; | |
} | |
public void callBayar(int userId, int amount){ | |
gtoDatabase.bayarTol(userId,amount); | |
} | |
public int getMoneyCard(int userId) | |
{ | |
return gtoDatabase.getMoney(userId); | |
} | |
public String getNamaCard(int userId){ | |
return gtoDatabase.getNama(userId); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Write a description of class TotalAkun here. | |
* | |
* @author (your name) | |
* @version (a version number or a date) | |
*/ | |
public class TotalAkun | |
{ | |
private int userID; | |
private int money; | |
private String nama; | |
public TotalAkun(int userId, int moneyId, String namaId){ | |
userID = userId; | |
money = moneyId; | |
nama = namaId; | |
} | |
public int getMoney(){ | |
return money; | |
} | |
public void saveMoney(int amount){ | |
money = amount; | |
} | |
public String getNama(){ | |
return nama; | |
} | |
public int getId(){ | |
return userID; | |
} | |
public void bayarTol(int nominal){ | |
money = money - nominal; | |
} | |
} |
Tidak ada komentar:
Posting Komentar