Music Player and Auction
Ditugas kali ini, saya akan membuat sebuah program yang mempresentasikan music player dan auction dalam java.
A. Music Player
Didalam Music Player, saya memiliki 2 class , yaitu:
1.Music Organizer
2.Music Player
Source code saya sebagai berikut:
1. MusicOrganizer
2. MusicPlayer
Hasil Output Program saya sebagai berikut :
B. Auction
Untuk program pelalangan yang saya buat, ada beberapa class yaitu:
1. Auction
2. Person
3. Lot
4. Bid
Source code saya sebagai berikut :
1. Auction
2. Person
3. Lot
4.Bid
Output program sebagai berikut :
A. Music Player
Didalam Music Player, saya memiliki 2 class , yaitu:
1.Music Organizer
2.Music Player
Source code saya sebagai berikut:
1. MusicOrganizer
/**
*
*
* @author (Krisna Badru W)
* @version (14 Oktober 2018)
*/
import java.util.ArrayList;
public class MusicOrganizer
{
private ArrayList<String> files;
private MusicPlayer player;
public MusicOrganizer()
{
files = new ArrayList<String>();
player = new MusicPlayer();
}
public void tambahFile(String namafile)
{
files.add(namafile);
}
// input jumlah file pada koleksi
public int jumlahFile()
{
return files.size();
}
// list file pada koleksi
public void listFile(int index)
{
if(index >= 0 && index < files.size())
{
String namafile = files.get(index);
System.out.println("Music ke-" + index + ": " +namafile);
}
}
// hapus file dari koleksi
public void hapusFile(int index)
{
if(index >= 0 && index < files.size())
{
files.remove(index);
}
}
public void startPlaying(int index)
{
String namafile = files.get(index);
player.startPlaying(namafile);
}
public void stopPlaying()
{
player.stopPlaying();
}
}
2. MusicPlayer
/**
* Write a description of class MusicPlayer here.
*
* @author (Krisna Badru W)
* @version (14 Oktober 2018)
*/
public class MusicPlayer
{
private String music;
public MusicPlayer()
{
music = null;
}
public void startPlaying(String namafile)
{
music = namafile;
System.out.println(music+ " This Music Now Playing");
}
public void stopPlaying()
{
System.out.println("This Music Now Stop Playing.");
}
}
Hasil Output Program saya sebagai berikut :
B. Auction
Untuk program pelalangan yang saya buat, ada beberapa class yaitu:
1. Auction
2. Person
3. Lot
4. Bid
Source code saya sebagai berikut :
1. Auction
import java.util.ArrayList;
/**
* class Auction
*
* @author (Krisna Badru W)
* @version (14 Oktober 2018)
*/
public class Auction
{
private ArrayList<Lot> lots;
private int LotNumber;
//Membuat Auction baru
public Auction()
{
lots = new ArrayList<Lot>();
LotNumber = 1;
}
//Menambahkan barang baru
public void enterLot(String description)
{
lots.add(new Lot(LotNumber, description));
LotNumber++;
}
//Menampilkan barang yang dilelang
public void showLots()
{
for(Lot lot : lots) {
System.out.println(lot.detail());
}
}
//Melakukan tawaran
public void MakeBid(int CurrentlotNumber, Person bidder, long value)
{
Lot selectedLot = getLot(CurrentlotNumber);
if(selectedLot != null)
{
boolean succes = selectedLot.bidFor(new Bid(bidder, value));
if(succes)
{
System.out.println("The bid for lot number " +CurrentlotNumber+ " was successful.");
}
else
{
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number: " +CurrentlotNumber+ " already has a bid of: " +highestBid.getBid());
}
}
}
public Lot getLot(int CurrentlotNumber)
{
if((CurrentlotNumber >= 1) && (CurrentlotNumber < LotNumber))
{
Lot selectedLot = lots.get(CurrentlotNumber - 1);
if(selectedLot.getId() != CurrentlotNumber)
{
System.out.println("Internal error: Lot number " +selectedLot.getId()+ " was returned instead of " +CurrentlotNumber);
selectedLot = null;
}
return selectedLot;
}
else
{
System.out.println("Lot number : " + CurrentlotNumber + " does not exist.");
return null;
}
}
}
2. Person
/**
* class Person
*
* @author (Krisna Badru W)
* @version (14 Oktober 2018)
*/
public class Person
{
private final String name;
public Person(String newName)
{
this.name = newName;
}
public String getName()
{
return name;
}
}
3. Lot
/**
* Class Lot
*
* @author (Krisna Badru W)
* @version (14 Oktober 2018)
*/
public class Lot
{
private final int Id;
private String description;
private Bid highestBid;
public Lot(int number, String description)
{
this.Id = number;
this.description = description;
}
public boolean bidFor(Bid bid)
{
if((highestBid ==null) || (bid.getBid() > highestBid.getBid()))
{
highestBid = bid;
return true;
}
else
{
return false;
}
}
public String detail()
{
String details = Id+ ": " +description;
if(highestBid != null)
{
details += " Bid: " +highestBid.getBid();
}
else
{
details += " (No Bid)";
}
return details;
}
public int getId()
{
return Id;
}
public String getDescription()
{
return description;
}
public Bid getHighestBid()
{
return highestBid;
}
}
4.Bid
/**
* class Bid
*
* @author (Krisna Badru W)
* @version (14 Oktober 2018)
*/
public class Bid
{
private final Person bidder;
private final long value;
public Bid(Person bidder, long value)
{
this.bidder = bidder;
this.value = value;
}
public Person getBidder()
{
return bidder;
}
public long getBid()
{
return value;
}
}
Output program sebagai berikut :
Komentar
Posting Komentar