Hallo Java-Forum!
Ich soll im Zuge einer Hochschulaufgabe aus vorgegebenen Klassen einen Ticketautomaten programiieren. Nun habe ich das Problem, dass die Aufgabenstellung für mich sehr kryptisch formuliert ist, was mir Probleme dabei bereitet in die Aufgabe reinzukommen.
gegeben sind folgende Klassen:
hab dann über die debug hilfe in eclipse diese Methoden in der klasse Ticketmachine erstellt wie es in der Aufgabenstellung vorgegeben wird.
Hoffe hier gibt es jemanden der mir etwas Starthilfe geben kann
Ich soll im Zuge einer Hochschulaufgabe aus vorgegebenen Klassen einen Ticketautomaten programiieren. Nun habe ich das Problem, dass die Aufgabenstellung für mich sehr kryptisch formuliert ist, was mir Probleme dabei bereitet in die Aufgabe reinzukommen.
gegeben sind folgende Klassen:
Java:
package edu.cs.hm.swe1;
import javax.swing.*;
import edu.cs.hm.swe1.ticketmachine.TicketMachine;
import edu.cs.hm.swe1.ticketmachine.TicketMachineGUI;
/**
* Simple GUI for the first version of the TicketMachine.
*
*
*
*/
public class Main extends JFrame {
private static final long serialVersionUID = -7793153788995243551L;
/**
* main Method to start up the UI.
* @param args One argument is required: ticket cost as forwarded to Constructor of class TicketMachine
* @throws Exception
*/
public static void main(String[] args) throws Exception {
int price = HelperClass.requestUserInput("Was ist der Preis für ein Ticket?");
if(price <= 0) {
throw new RuntimeException("Error: you must provide a ticket price grater than zero!");
}
TicketMachineGUI ticketMachineGUI = new TicketMachineGUI(new TicketMachine(price));
new TicketMachineGUI(new TicketMachine(price));
}
}
Java:
package edu.cs.hm.swe1;
import javax.swing.JOptionPane;
/**
* Contains helper methods such as wait.
*
*
*
*/
public class HelperClass {
/**
* Request some user information and transform it into an int-value.
*
* @param question Question the user is asked
* @return The user's reply, transformed into an int-value.
*/
public static int requestUserInput(String question) {
// Request user to input information about smiley
String answer = JOptionPane.showInputDialog(question);
// Transform textual answer into int-value
int result = Integer.parseInt(answer);
// Return result to caller
return result;
}
/**
* Print error message in separate window
*
* @param message Error message
*/
public static void printErrorMessage(String message) {
JOptionPane.showMessageDialog(null, message, "Fehlermeldung", JOptionPane.ERROR_MESSAGE);
}
/**
* Wait for a specified number of milliseconds before finishing.
* This provides an easy way to specify a small delay which can be
* used when producing animations.
* @param milliseconds the number
*/
public static void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (Exception e)
{
// ignoring exception at the moment
}
}
}
Java:
package edu.cs.hm.swe1.ticketmachine;
import java.awt.*;
import java.awt.event.*;
import javax.sound.sampled.*;
import javax.swing.*;
/**
* Simple GUI for the first version of the TicketMachine.
*
*
*
*/
public class TicketMachineGUI extends JFrame {
static final long serialVersionUID = 7571949288624872816L;
public static final String SOUND_FILE_DROPPED_COIN = "coin.wav";
private final TicketMachine ticketMachine;
private JPanel mainPanel;
/**
* All UI-Element we use.
*/
private final JButton insert1CtCoinButton = new JButton("Insert 1Ct Coin");
private final JButton insert2CtCoinButton = new JButton("Insert 2Ct Coin");
private final JButton insert5CtCoinButton = new JButton("Insert 5Ct Coin");
private final JButton insert10CtCoinButton = new JButton("Insert 10Ct Coin");
private final JButton refundButton = new JButton("Refund");
private final JButton produceTicketButton = new JButton("Get Ticket");
private final JLabel balance = new JLabel("Balance: 0");
private final JLabel internalStorageArea = new JLabel("Internal Storage", SwingConstants.CENTER);
private JLabel fill1CtArea = new JLabel("1 Ct Coins: ", SwingConstants.CENTER);
private JLabel fill2CtArea = new JLabel("2 Ct Coins: ", SwingConstants.CENTER);
private JLabel fill5CtArea = new JLabel("5 Ct Coins: ", SwingConstants.CENTER);
private JLabel fill10CtArea = new JLabel("10 Ct Coins: ", SwingConstants.CENTER);
private final JLabel emptyLabel1 = new JLabel();
private final JLabel emptyLabel2 = new JLabel();
public TicketMachineGUI(TicketMachine ticketMachine) {
this.ticketMachine = ticketMachine;
add(createPanel());
pack();
setVisible(true);
setTitle("Ticket Machine");
}
/**
* Creates the Master-Panel.
* @return
*/
private Component createPanel() {
mainPanel = new JPanel(new GridLayout(1, 2));
updateCoinStorageFillings();
mainPanel.add(createControlPanel());
// mainPanel.add(new InternalPanel());
balance.setHorizontalAlignment(SwingConstants.CENTER);
return mainPanel;
}
/**
* Creates the Panel with buttons and the display.
* @return
*/
private Component createControlPanel() {
JPanel controlPanel = new JPanel(new GridLayout(7, 2));
controlPanel.add(balance);
controlPanel.add(internalStorageArea);
controlPanel.add(insert1CtCoinButton);
controlPanel.add(fill1CtArea);
controlPanel.add(insert2CtCoinButton);
controlPanel.add(fill2CtArea);
controlPanel.add(insert5CtCoinButton);
controlPanel.add(fill5CtArea);
controlPanel.add(insert10CtCoinButton);
controlPanel.add(fill10CtArea);
controlPanel.add(refundButton);
controlPanel.add(emptyLabel1);
controlPanel.add(produceTicketButton);
controlPanel.add(emptyLabel2);
insert1CtCoinButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if(!ticketMachine.insert1Ct()) {
playSound();
}
else {
fill1CtArea.setText("1 Ct Coins: " + ticketMachine.getFill1Ct());
balance.setText("Balance: " + ticketMachine.getBalance() + "");
mainPanel.repaint();
}
}});
insert2CtCoinButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if(!ticketMachine.insert2Ct()) {
playSound();
}
else {
fill2CtArea.setText("2 Ct Coins: " + ticketMachine.getFill2Ct());
balance.setText("Balance: " + ticketMachine.getBalance() + "");
mainPanel.repaint();
}
}});
insert5CtCoinButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if(!ticketMachine.insert5Ct()) {
playSound();
}
else {
fill5CtArea.setText("5 Ct Coins: " + ticketMachine.getFill5Ct());
balance.setText("Balance: " + ticketMachine.getBalance() + "");
mainPanel.repaint();
}
}});
insert10CtCoinButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if(!ticketMachine.insert10Ct()) {
playSound();
}
else {
fill10CtArea.setText("10 Ct Coins: " + ticketMachine.getFill10Ct());
balance.setText("Balance: " + ticketMachine.getBalance() + "");
mainPanel.repaint();
}
}});
refundButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if(ticketMachine.refundBalance() > 0 ) {
playSound();
updateCoinStorageFillings();
balance.setText("Balance: " + ticketMachine.getBalance() + "");
mainPanel.repaint();
}
}});
produceTicketButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
showTicket(ticketMachine.printTicket());
updateCoinStorageFillings();
balance.setText("Balance: " + ticketMachine.getBalance() + "");
mainPanel.repaint();
}});
return controlPanel;
}
private void updateCoinStorageFillings() {
fill1CtArea.setText("1 Ct Coins: " + ticketMachine.getFill1Ct());
fill2CtArea.setText("2 Ct Coins: " + ticketMachine.getFill2Ct());
fill5CtArea.setText("5 Ct Coins:: " + ticketMachine.getFill5Ct());
fill10CtArea.setText("10 Ct Coins: " + ticketMachine.getFill10Ct());
}
/**
* Displays a ticket in its own JFrame. That frame can be closed by clicking Escape.
* @param ticket The text to show on the ticket (i.e. on the frame).
*/
private void showTicket(String ticket) {
final JFrame ticketFrame = new JFrame("Ticket");
JLabel ticketText = new JLabel("<html><body>" + ticket.replaceAll("\n", "<br>") + "</body></html>");
ticketFrame.add(ticketText);
ticketFrame.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
ticketFrame.setVisible(false);
ticketFrame.dispose();
}
}
});
ticketFrame.pack();
ticketFrame.setVisible(true);
}
/*
* Play the coin-dropped-sound
*/
private synchronized void playSound() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
this.getClass().getResourceAsStream(SOUND_FILE_DROPPED_COIN));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
//silently catch all exceptions to avoid confusion.
//throw new RuntimeException(e);
}
}
}
hab dann über die debug hilfe in eclipse diese Methoden in der klasse Ticketmachine erstellt wie es in der Aufgabenstellung vorgegeben wird.
Java:
package edu.cs.hm.swe1.ticketmachine;
public class TicketMachine {
private int ticketprice;
private int balance;
private int fill1ct;
private int fill2ct;
private int fill5ct;
private int fill10ct;
public TicketMachine(int price) {
ticketprice = price;
}
public boolean insert1Ct() {
// TODO Auto-generated method stub
return false;
}
public String getFill1Ct() {
// TODO Auto-generated method stub
return null;
}
public String getBalance() {
// TODO Auto-generated method stub
return null;
}
public boolean insert2Ct() {
// TODO Auto-generated method stub
return false;
}
public String getFill2Ct() {
// TODO Auto-generated method stub
return null;
}
public boolean insert5Ct() {
// TODO Auto-generated method stub
return false;
}
public String getFill5Ct() {
// TODO Auto-generated method stub
return null;
}
public boolean insert10Ct() {
// TODO Auto-generated method stub
return false;
}
public String getFill10Ct() {
// TODO Auto-generated method stub
return null;
}
public int refundBalance() {
// TODO Auto-generated method stub
return 0;
}
public String printTicket() {
// TODO Auto-generated method stub
return null;
}
}
Hoffe hier gibt es jemanden der mir etwas Starthilfe geben kann