需要为一个景区实现计算景区门票的程序,已知成年人的门票价格是100元,儿童票打3折,老年票打5折。使用抽象类来为任意多张不同类型的票计算总价。其UML类图如下所示。
要求能够输入不同类型门票的数量,输出总的票价。
- Ticket抽象类
- YoungTicket类成人票
- ChildTicket类儿童票
- OldTicket类老年票
- TicketBox类
- TestClass测试类
- TicketJframe类窗体
package javaapplication1; public abstract class Ticket { float price; public Ticket() { } public Ticket(float price) { this.price = price; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public float singprice(){ this.price=this.hashCode(); return this.price; } public abstract float shouldPay(float totalPrice); }YoungTicket类成人票
package javaapplication1; public class YoungTicket extends Ticket{ public YoungTicket() { } public YoungTicket(float price) { super(price); } @Override public float shouldPay(float totalPrice) { return totalPrice; } }ChildTicket类儿童票
package javaapplication1; public class ChildTicket extends Ticket{ public ChildTicket() { } public ChildTicket(float price) { super(price); } @Override public float shouldPay(float totalPrice) { return totalPrice*0.3f; } }OldTicket类老年票
package javaapplication1; public class OldTicket extends Ticket{ public OldTicket() { } public OldTicket(float price) { super(price); } @Override public float shouldPay(float totalPrice) { return totalPrice*0.3f; } }TicketBox类
package calculatescenicspottickets; public class TicketBox { public static double slide(Ticket ticket,double totalPrice){ return ticket.consume(totalPrice); } }TestClass测试类
package calculatescenicspottickets; public class TestClass { public static YoungTicket youngticket =new YoungTicket("成年票",0); public static ChildTicket childticket =new ChildTicket("儿童票",0); public static OldTicket oldticket =new OldTicket("老年票",0); }TicketJframe类窗体
package calculatescenicspottickets; public class TicketJframe extends javax.swing.Jframe { private Ticket ticket; public TicketJframe() { initComponents(); } @SuppressWarnings("unchecked") //private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jTextField3 = new javax.swing.JTextField(); jTextField4 = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jLabel10 = new javax.swing.JLabel(); jLabel11 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("门票单价"); jLabel2.setText("成人票"); jLabel3.setText("老年票(5折)"); jLabel4.setText("儿童票(3折)"); jLabel5.setText("总价"); jButton1.setText("计算"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jLabel7.setText("元"); jLabel8.setText("张"); jLabel9.setText("张"); jLabel10.setText("张"); jLabel11.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N jLabel11.setText("景区门票计费系统"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(105, 105, 105) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel5) .addGap(74, 74, 74) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jButton1) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addComponent(jLabel3) .addComponent(jLabel4) .addComponent(jLabel1)) .addGap(45, 45, 45) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel9)) .addGroup(layout.createSequentialGroup() .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel8)) .addGroup(layout.createSequentialGroup() .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel10)) .addGroup(layout.createSequentialGroup() .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel7)))) .addGroup(layout.createSequentialGroup() .addGap(40, 40, 40) .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(130, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(32, 32, 32) .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(49, 49, 49) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(jLabel2) .addGap(20, 20, 20) .addComponent(jLabel3) .addGap(18, 18, 18) .addComponent(jLabel4)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.baseLINE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.baseLINE) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8)) .addGap(14, 14, 14) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.baseLINE) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.baseLINE) .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel10)))) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(79, 79, 79) .addComponent(jButton1) .addContainerGap(106, Short.MAX_VALUE)) ); pack(); }// private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: double t1=0.0d,t2=0.0d,t3=0.0d,t4=0.0d; double a1=Double.parseDouble(this.jTextField1.getText().trim()); int a2=Integer.parseInt(this.jTextField2.getText().trim()); int a3=Integer.parseInt(this.jTextField3.getText().trim()); int a4=Integer.parseInt(this.jTextField4.getText().trim()); ticket=TestClass.youngticket; t1= TicketBox.slide(ticket,a2*a1); ticket=TestClass.oldticket; t2=TicketBox.slide(ticket, a3*a1); ticket=TestClass.childticket; t3=TicketBox.slide(ticket, a4*a1); t4=t1+t2+t3; t4=(double) Math.round(t4*100)/100;//保留了两位小数 this.jLabel6.setText(t4+"元"); } public static void main(String args[]) { //try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(TicketJframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TicketJframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TicketJframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TicketJframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TicketJframe().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField4; // End of variables declaration }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)