java的类,输入框,转换和格式化

  1. /*
  2. 输入股票代码、名称、价格等信息
  3. 打印出盈亏情况
  4. */
  5. import java.util.*;
  6. import javax.swing.*;           //JOptionPane窗口录入用
  7. import java.text.DecimalFormat; //格式化小数点用
  8. // ---------- 主函数 --------------------
  9. public class GP_Test {
  10.     public static void main(String[] args) {
  11.         // 读取数据
  12.         String cm = JOptionPane.showInputDialog("股票代码: ");
  13.         String n = JOptionPane.showInputDialog("股票名称: ");
  14.         String input1 = JOptionPane.showInputDialog("买入成本: ");
  15.         double c = Double.parseDouble(input1);      //string 转 double
  16.         String input2 = JOptionPane.showInputDialog("现在价格: ");
  17.         double c_n = Double.parseDouble(input2);    //string 转 double
  18.         String input3 = JOptionPane.showInputDialog("买入数量: ");
  19.         int a = Integer.parseInt(input3);           //string 转 int
  20.         // 构造类,调用方法
  21.         GP gp = new GP(cm, n, c, c_n, a);
  22.         double pal_r = gp.profitAndLoss_ratio();    //盈亏百分比
  23.         double pal = pal_r * (c*a);                 //盈亏额
  24.         // 格式化(只保留两位小数)
  25.         double pal_r1 = new Double( new DecimalFormat( ".00" ).format( pal_r ) );
  26.         double pal1 = new Double( new DecimalFormat( ".00" ).format( pal ) ); 
  27.     
  28.         // 输出
  29.         gp.gpInfo();
  30.         gp.printProfitAndLoss(pal_r, pal1, pal_r1);
  31.         
  32.     }
  33. }
  34. // ---------- 类部分 --------------------
  35. class GP {
  36.     // 构造器
  37.     public GP(String cm, String n, double c, double c_n, int a) {
  38.         code = cm;
  39.         name = n;
  40.         cost = c;
  41.         cost_now = c_n;
  42.         amount = a;
  43.     }
  44.     // 方法
  45.     public double profitAndLoss_ratio() {   //盈亏百分比
  46.         return (cost_now - cost)/cost;
  47.     }
  48.     public void gpInfo() {                  //打印股票信息
  49.         System.out.println("代码: " + code);
  50.         System.out.println("名称: " + name);
  51.         System.out.println("成本: " + cost);
  52.         System.out.println("现价: " + cost_now);
  53.         System.out.println("股数: " + amount);
  54.     }
  55.     //打印盈亏情况
  56.     public void printProfitAndLoss(double pal_r, double pal1, double pal_r1) {
  57.         System.out.print("盈亏情况:" + (pal_r1 * 100) + "%,");
  58.         if (pal_r > 0)
  59.             System.out.print("盈利" + pal1 + "元");
  60.         else if (pal_r < 0)
  61.             System.out.print("亏损" + (pal1) + "元");
  62.         else
  63.             System.out.print("持平");
  64.         System.out.println();
  65.     }
  66.     // 成员
  67.     private String code;        //股票代码
  68.     private String name;        //名称
  69.     private double cost;        //成本
  70.     private double cost_now;    //现价
  71.     private int amount;         //股数
  72. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值