设计金融产品类Financial,属性包括产品名称、产品介绍、起投金额、产品期限(int)、年化收益(百分数);方法包括发布、截止、投资。
设计金融产品类子类:
-
基金产品Fund,继承金融产品类,属性包括投资地区、投资人、投资金额
-
股票产品Shares,继承金融产品类,属性包括股票价值、购买价格、购买 笔数
要求:
1、所有父子类均完成get和set封装,以及构造方法(无参构造方法和有参构造方法)封装。
2、子类继承父类时,重写父类发布、截止、投资方法,方法体为输出发布、截止时间。
3、输入购买产品,判断实例化哪个子类对象,初始化父子类属性
4、投资,投资基金产品时,判断投资金额是否超过起投金额,如果未超过,提示投资失败,如果超过,输出基金产品完整信息并计算年化收益。(年化收益=投资金额期限年化收益)
5、投资,投资股票产品时,判断投资金额是否超过起投金额,如果未超过,提示投资失败,如果超过,输出股票产品完整信息并计算年化收益。年化收益=投资金额笔数期限*年化收益)
package com.jsu.java;public class Financial {private String fname="中欧混合";//产品名称、private String fintroduce="光伏";//产品介绍、private double flauchMoney=6000.01;//起投金额、private int fdeadline=5;//产品期限(int)、private double fannualized=0.01;//年化收益(百分数)public void print(){System.out.println("产品名称为:"+this.getFname()+"\n"+"产品介绍:"+this.getFintroduce()+"\n"+"起投金额:"+this.getFlauchMoney()+"\n"+"产品期限(int):"+this.getFdeadline()+"\n"+"年化收益(百分数):"+this.getFannualized());}public Financial() {}public Financial(String fname, String fintroduce, double flauchMoney, int fdeadline, double fannualized) {this.fname = fname;this.fintroduce = fintroduce;this.flauchMoney = flauchMoney;this.fdeadline = fdeadline;this.fannualized = fannualized;}public String getFname() {return fname;}public void setFname(String fname) {this.fname = fname;}public String getFintroduce() {return fintroduce;}public void setFintroduce(String fintroduce) {this.fintroduce = fintroduce;}public double getFlauchMoney() {return flauchMoney;}public void setFlauchMoney(double flauchMoney) {this.flauchMoney = flauchMoney;}public int getFdeadline() {return fdeadline;}public void setFdeadline(int fdeadline) {this.fdeadline = fdeadline;}public double getFannualized() {return fannualized;}public void setFannualized(double fannualized) {this.fannualized = fannualized;}public void issue(){}public void abort(){}public void invest(){}
}
package com.jsu.java;import java.util.Scanner;public class Fund extends Financial{private String area;//投资地区private String investor;//投资人private double inMoney;//投资金额public Fund() {super();}public Fund(String area, String investor, double inMoney) {super();this.area = area;this.investor = investor;this.inMoney = inMoney;}public String getArea() {return area;}public void setArea(String area) {this.area = area;}public String getInvestor() {return investor;}public void setInvestor(String investor) {this.investor = investor;}public double getInMoney() {return inMoney;}public void setInMoney(double inMoney) {this.inMoney = inMoney;}public Scanner getScanner() {return scanner;}public void setScanner(Scanner scanner) {this.scanner = scanner;}Scanner scanner = new Scanner(System.in);@Overridepublic void issue() {System.out.println("发布时间为:2021年9月30日 8:00");}@Overridepublic void abort() {System.out.println("截止时间为:2200年9月30日 18:00");}@Overridepublic void invest() {System.out.println("投资");System.out.println("请输入投资金额:");this.inMoney = scanner.nextDouble();if(this.inMoney < super.getFlauchMoney()){System.out.println("起投金额未超过,投资失败");}else{this.print();}}@Overridepublic void print(){
// this.invest();System.out.println("产品名称为:"+super.getFname()+"\n"+"产品介绍:"+super.getFintroduce()+"\n"+"起投金额:"+super.getFlauchMoney()+"\n"+"产品期限(int):"+super.getFdeadline()+"\n"+"年化收益(百分数):"+super.getFannualized());System.out.println("投资地区:"+this.getArea()+"\n"+"投资人:"+this.getInvestor()+"\n"+"投资金额"+this.getInMoney());System.out.println("年化收益为:"+(this.getInMoney()*super.getFdeadline()*this.getFannualized()));}}
package com.jsu.java;import java.util.Scanner;public class Shares extends Financial{private double value;//股票价值private double price;//购买价格private int num;//购买笔数public Shares() {}public Shares(double value, double price, int num) {this.value = value;this.price = price;this.num = num;}public double getValue() {return value;}public void setValue(double value) {this.value = value;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}Scanner scanner = new Scanner(System.in);@Overridepublic void issue() {System.out.println("发布时间为:2021年9月30日 9:00");}@Overridepublic void abort() {System.out.println("截止时间为:2021年9月30日 19:00");}@Overridepublic void invest() {System.out.println("投资");System.out.println("请输入投资金额:");this.price = scanner.nextDouble();if(this.price<super.getFlauchMoney()){System.out.println("投资失败!");}if(this.price < super.getFlauchMoney()){System.out.println("起投金额未超过,投资失败");}else{this.print();}}@Overridepublic void print(){System.out.println("产品名称为:"+super.getFname()+"\n"+"产品介绍:"+super.getFintroduce()+"\n"+"起投金额:"+super.getFlauchMoney()+"\n"+"产品期限(int):"+super.getFdeadline()+"\n"+"年化收益(百分数):"+super.getFannualized());System.out.println("股票价值:"+this.getValue()+"\n"+"购买价格:"+this.getPrice()+"\n"+"购买笔数"+this.getNum());System.out.println("年化收益为:"+(this.getPrice()*this.getNum()*super.getFdeadline()*this.getFannualized()));}}
package com.jsu;import com.jsu.java.Financial;
import com.jsu.java.Fund;
import com.jsu.java.Shares;import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);Financial financial = new Financial("中欧混合","光伏",6000.0,5,0.01);String witch = null;financial.print();while(true){System.out.println("输入你要购买产品:");witch = scanner.next();financial = choice(witch);financial.invest();financial.issue();financial.abort();}}private static Financial choice(String witch) {Financial financial = null;if("基金产品".equals(witch)){financial = new Fund("山东","张三",9000.0);}else if("股票产品".equals(witch)){financial = new Shares(1000.0,5000.0,10);}else {System.out.println("输入错误,没有该产品");financial = new Financial();}return financial;}
}
Fund结果展示:
Share结果展示: