Membuat Program Ticket Machine
Source Code:
- Ticketing
/**
* Code for ticketing
*
* @author (Christine Amelia)
* @version (5-17/09/2018)
*/
import java.util.Scanner;
public class Ticketing
{
//menyimpan harga
private int harga;
//menghitung uang setelah ditambah atau dikurang.
private int balance;
//total.
private int total;
//menginputkan harga
public Ticketing(int HargaTiket)
{
harga = HargaTiket;
balance = 0;
total = 0;
}
//melihat harga
public int getharga()
{
return harga;
}
//melihat hasil setelah ditambah atau dikurang
public int getBalance()
{
return balance;
}
//menginsertkan uang sipembeli
public void insertMoney(int amount)
{
balance = balance + amount;
}
/**
* Print a ticket.
* Update the total collected and
* reduce the balance to zero.
*/
public void printTicket()
{
System.out.println("------Train Express------");
System.out.println("----------Ticket---------");
System.out.println("-------"+harga+ " Rupiah-------");
System.out.println("--------Safe Ride--------");
total = total + balance;
// mengembalikan jadi 0.
balance = 0;
}
}
- Main
/**
* Code for the main system of ticketing
*
* @author (Christine Amelia)
* @version (4-17/09/2018)
*/
import java.util.Scanner;
public class MainSystem
{
public static void main(String args[])
{
Scanner scan= new Scanner(System.in);
int cost,menu;
System.out.println("Insert the price of the ticket: \n");
cost=scan.nextInt();
Ticketing ticket=new Ticketing(cost);
System.out.println("1. Get Price");
System.out.println("2. Get Balance");
System.out.println("3. Insert Money");
System.out.println("4. Print Ticket");
System.out.println("5. Exit");
int x=1;
while(x == 1){
menu = scan.nextInt();
switch(menu)
{
case 1:
cost=ticket.getharga();
System.out.println(cost);
break;
case 2:
System.out.print(ticket.getBalance()+"\n");
break;
case 3:
int money=scan.nextInt();
ticket.insertMoney(money);
break;
case 4:
cost = ticket.getharga();
ticket.printTicket();
break;
case 5:
x=0;break;
}
}
}
}
Tidak ada komentar:
Posting Komentar