參考了別人的代碼。略作修改,已經(jīng)很簡(jiǎn)單了:
創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供泰安企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站制作、成都做網(wǎng)站、H5建站、小程序制作等業(yè)務(wù)。10年已為泰安眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
InfoATM.java:
public?class?InfoATM?{
double?money?=?0;
public?InfoATM(double?cash)?{
super();
this.money?=?cash;
}
//?存款的方法
public?void?save(double?count)?{
money?+=?count;
}
//?取款的方法
public?void?draw(double?count)?{
money?-=?count;
}
public?double?getMoney()?{
return?money;
}
public?void?setMoney(double?money)?{
this.money?=?money;
}
}
TestATM.java:
import?java.awt.BorderLayout;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
public?class?TestATM?extends?JFrame?{
private?static?final?long?serialVersionUID?=?2531222181184935595L;
//?主面板pnBasic是用來(lái)裝pnDate和標(biāo)簽文字的。
private?JPanel?pnBasic;
//?添加到主面板中的中間?pnDate面板是為了裝表單的。
private?JPanel?pnDate;
//?添加到主面板中的北邊?pnLabel面板是為了裝歡迎詞的
private?JPanel?pnLabel;
InfoATM?atm?=?new?InfoATM(0);
public?TestATM()?{
pnBasic?=?new?JPanel();
//?主面板pnBasic是用來(lái)裝pnDate和標(biāo)簽文字的。
pnDate?=?new?JPanel(new?GridLayout(2,?2));
//?pnDate面板是為了裝表單的。
pnLabel?=?new?JPanel();
JLabel?top?=?new?JLabel("歡迎來(lái)到中國(guó)銀行!");
pnLabel.add(top);
//?先將數(shù)值添加在一個(gè)容器中并設(shè)置其在容器的右邊,在將容器添加在網(wǎng)格的第一格
JPanel?jp1?=?new?JPanel();
JLabel?number?=?new?JLabel("數(shù)值:");
final?JTextField?box?=?new?JTextField(5);
jp1.add(number);
jp1.add(box);
JPanel?jp2?=?new?JPanel();
JButton?create?=?new?JButton("新建銀行賬戶(hù)");
jp2.add(create);
JButton?take?=?new?JButton("取款");
JButton?in?=?new?JButton("存款");
pnDate.add(jp1);
pnDate.add(jp2);
pnDate.add(take);
pnDate.add(in);
//?加一句下面的就好了
JPanel?jpS?=?new?JPanel();
final?JLabel?total?=?new?JLabel("您現(xiàn)在的賬戶(hù)余額是:0?元");
jpS.add(total);
pnBasic.setLayout(new?BorderLayout());
pnBasic.add(pnLabel,?BorderLayout.NORTH);
pnBasic.add(pnDate,?BorderLayout.CENTER);
pnBasic.add(jpS,?BorderLayout.SOUTH);
setContentPane(pnBasic);
setBounds(400,?250,?500,?500);
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
pack();
in.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
if?(box.getText()?!=?null??box.getText()?!=?"")?{
try?{
double?count?=?Double.parseDouble(box.getText());
if?(count??0)?{
atm.save(count);
total.setText("您現(xiàn)在的賬戶(hù)余額是:"?+?atm.getMoney()?+?"元");
box.setText("");
}
}?catch?(Exception?e1)?{
System.out.println("您輸入的數(shù)值必須是數(shù)字");
}
}
}
});
take.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
if?(box.getText()?!=?null??box.getText()?!=?"")?{
try?{
double?count?=?Double.parseDouble(box.getText());
if?(count?=?0??count?=?atm.getMoney())?{
atm.draw(count);
total.setText("您現(xiàn)在的賬戶(hù)余額是:"?+?atm.getMoney()?+?"元");
box.setText("");
}?else?{
System.out.println("你的余額不足,取款失敗");
}
}?catch?(Exception?e1)?{
System.out.println("您輸入的數(shù)值必須是數(shù)字");
}
}
}
});
create.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
total.setText("您現(xiàn)在的賬戶(hù)余額是:0元");
atm.setMoney(0);
box.setText("");
}
});
}
public?static?void?main(String[]?args)?{
new?TestATM();
}
}
最佳答案代碼如下:
public class ElevatorModel implenent ElevatorModelListener{
ElevatorShaft elevatorShaft;//電梯車(chē)箱
Floor firstFloor;
Floor secondFloor;
public void addPerson(Person person);
//實(shí)現(xiàn)ElevatorModelListener接口的所有方法
}
public abstract class Location{
String locationName;//可能占據(jù)的三個(gè)位置firstFloor; secondFloor;elevator中的其中一個(gè)位置。
public Button getButton();
public Door getDoor();
}
public class Floor extends Location{
public Floor(String firstFloor,String secondFloor);
public Button getButton();//具體實(shí)現(xiàn)
public Door getDoor();//具體實(shí)現(xiàn)
public ElevatorShaft getElevatorShaft();
}
public class Door implenent ElevatorMoveListener{
boolean open;//門(mén)是開(kāi)的關(guān)的
public void openDoor();//開(kāi)門(mén)
public viod clossDoor();//關(guān)門(mén)
}
public class Button implenent ElevatorMoveListener{
boolean pressed;
public void pressButton();//按下按鈕
public void resetButton();//釋放按鈕
public void elevatorArrived();//電梯到來(lái)
}
public class ElevatorShaft{//電梯乘箱
Elevator elevator;//電梯
Button firstFloorButton,secondFloorButton;//出發(fā)層的按鈕和到達(dá)層的按鈕 Door firstFloorDoor,secondFloorDoor;//出發(fā)的門(mén)和到達(dá)的門(mén)
Light firstFloorLight,secondFloorLight;//燈
//監(jiān)聽(tīng)器
DoorListener doorListener;
ButtonListener buttonListener;
LightListener ligthListener;
BellListener bellListener;
ElevatorMoveListener elevatorMoveListener;
Set set;
}
public class Light implenent ElevatorMoveListener{
boolean lightOn;//代表燈的狀態(tài),亮或滅
public void turnOnLight();
public void turnOffLight();
public void elevatorDeparted();//具體實(shí)現(xiàn)
public void elevatorArrived();//具體實(shí)現(xiàn)
}
public class Bell implenent ElevatorMoveListener{
public void ringBell();
public void elevatorDeparted();//具體實(shí)現(xiàn)
public void elevatorArrived();//具體實(shí)現(xiàn)
}
public class Elevator extends Location implenets
ButtonListener,DoorListener,BellListener,Runable
{//電梯乘箱
boolean moving;
boolean summoned;//描述Elevator是在移動(dòng)還是已經(jīng)補(bǔ)召喚
Floor currentFloor;//當(dāng)前正在提供服務(wù)的Floor
Floor destinationFloor ; //代表將要到達(dá)的Floor
Button elevatorButton;
Door elevatorDoor;
Bell bell;
public void run();// 實(shí)現(xiàn)
public void requestElevator();//請(qǐng)求Elevator并產(chǎn)生排隊(duì)請(qǐng)求。 }
public class Person extends Thread{
Location類(lèi)對(duì)象(或是位于Floor上,或是出處在Elevator中); int ID;
boolean moving;
public void run();//具體實(shí)現(xiàn)
public void enterAndRideElevator();
}
//TV類(lèi)
public class Tv {
public void open(){
System.out.println("打開(kāi)電視機(jī)");
}
public void close(){
System.out.println("關(guān)閉電視機(jī)");
}
public void search(int x){
System.out.println("您搜索到的是"+x+"號(hào)頻道");
}
public void ctrlVol(String s){
int vol=10;
if(s.equals("+"))
vol++;
if(s.equals("-"))
vol--;
System.out.println("當(dāng)前音量為"+vol);
}
public void changeChannel(int x){
if( x 29 )
System.out.println("沒(méi)有該頻道");
else
System.out.println("當(dāng)前"+x+"號(hào)頻道");
}
public void changeChannel(String s){
int x=0;
if(s.equals("+"))
x++;
if(s.equals("-"))
x--;
System.out.println("當(dāng)前"+x+"號(hào)頻道");
}
}
//Controller遙控器類(lèi)
public class Controller{
public void openTv(Tv tv){
tv.open();
}
public void closeTv(Tv tv){
tv.close();
}
public void searchTv(Tv tv){
int x=(int)(Math.random()*30);
tv.search(x);
}
public void ctrlTvVol(Tv tv){
tv.ctrlVol("+");
}
public void changeTvChannel(Tv tv){
tv.changeChannel("+");
tv.changeChannel(23);
}
}
我的思路:
1.定義一個(gè)開(kāi)關(guān)的接口(ISWitcher),用于設(shè)定開(kāi)關(guān)的功能
2.定義開(kāi)關(guān)類(lèi)實(shí)現(xiàn)開(kāi)關(guān)接口的功能
3.定義電器類(lèi),所有電器的父類(lèi)
4.對(duì)電燈,投影儀,風(fēng)扇等電器進(jìn)行定義
5.進(jìn)行測(cè)試
}else if(comm.equals("SORT")){
String course=s.next();
sort(course);
}else if(comm.equals("GET")){
String student=s.next();
String course=s.next();
get(student,course);
}else if(comm.equals("EXIT")){
break;
}else{
System.out.println("命令格式不正確,請(qǐng)重新輸入!");
}
}