package com.zuikc.bigtest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
import com.zuikc.bean.AESUtil;
import com.zuikc.bean.FinancialCalculations;
import com.zuikc.bean.Tool;
/**
最课程 JavaSE 部分结束
作业:造简单的利息计算器
类:Main_Interface:主类测试类,包含页面;
Tool:工具类提供方法,简便计算
FinancialCalculations:金融计算类,提供数据的计算打印及保存的方法
AESUtil:加密工具类,提供加密及解密方法
@author
@version 1.0
*/
public class Main_Interface {
/**
* @param main: 主页面
* @param Interface_1(sc)//等额本息页面方法
* @param Interface_2(sc)//按月付息到期页面方法
* @param Interface_3(sc)//一次性还本付息页面方法
* @param Interface_4(sc)//导出计算结果页面方法
* */
public static void main(String[] args) throws Exception {
//提示
String str ="请输入您想进行的操作:"
+"1.计算等额本息"+"\r\n"
+"2.计算按月付息到期还本"+"\r\n"
+"3.计算一次性还本付息"+"\r\n"
+"4.导出计算结果"+"\r\n"
+"0.退出";
System.out.println(str);
Scanner sc = new Scanner(System.in);
while(true){
//接收指令
String s = sc.nextLine();
s.trim();
switch (s) {
case "1":
System.out.println("等额本息页面");
Interface_1(sc);
//主页提示
System.out.println(str);
break;
case "2":
System.out.println("按月付息到期页面");
Interface_2(sc);
//主页提示
System.out.println(str);
break;
case "3":
System.out.println("一次性还本付息页面");
Interfoace_3(sc);
//主页提示
System.out.println(str);
break;
case "4":
//导出结果页
System.out.println("导出结果");
Interface_4(sc);
case "0":
//结束程序
return;
default:
System.out.println("您的输入有误,请重新输入");
}
}
}
//导出结果页面
public static void Interface_4(Scanner sc) throws Exception {
//判断是否进行过计算
File file1 = new File("voucher.txt");
if(!file1.exists()){
System.out.println("对不起,您还没有进行过任何一次计算!");
//Esc 返回主界面
Tool.inputEsc(sc);
}else{
//判断文件夹是否存在,不存在则创建文件夹
File file =new File("C:\\result");
if(!file.exists()){
file.mkdir();
}
//写入数据到指定文件
BufferedReader br = new BufferedReader(new FileReader("voucher.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:/result/result.txt"));
String len;
//读取密文
while((len = br.readLine())!=null){
//解密
len = AESUtil.jiemi(len, "25d55ad283aa400afeas238adse3a2t4");
bw.write(len);
bw.newLine();
}
br.close();
bw.close();
//导出结果
System.out.println("文件导出路径为:\tC:/result/result.txt");
return;
}
}
//一次性还本付息页面
public static void Interfoace_3(Scanner sc) throws Exception {
//接收计算器对象
FinancialCalculations in3 = Tool.judge(sc);
//打印并处存数据
in3.setVoucher(in3.getTableHead("1"), null);
//按Esc返回面主界面
Tool.inputEsc(sc);
}
//按月付息到期还本页面
public static void Interface_2(Scanner sc) throws Exception {
//接受计算机对象
FinancialCalculations in2 = Tool.judge(sc);
//打印并储存数据
in2.setVoucher(in2.getTableHead("1"),in2.getTablePerson("1"));
//Esc返回主页面
Tool.inputEsc(sc);
}
//等额本息页面
public static void Interface_1(Scanner sc) throws Exception {
//接收计算器对象
FinancialCalculations in1 = Tool.judge(sc);
//打印并储存数据
in1.setVoucher(in1.getTableHead(),in1.getTablePerson());
//Esc返回主界面
Tool.inputEsc(sc);
}
}
package com.zuikc.bean;
import java.util.Scanner;
public class Tool {
//判断是不是数字
public static boolean isNum(String str){
for (char c : str.toCharArray()) {
if(c<'0' || c>'9'){
return false;
}
}
return true;
}
//判断是不是被100整除
public static boolean By100(String str){
return Integer.parseInt(str)%100 == 0;
}
//按Esc返回主界面
public static void inputEsc(Scanner sc) {
System.out.println("按Esc,回到主页面");
while(true){
String inputEsc = sc.nextLine();
if(!"Esc".equals(inputEsc))
System.out.println("输入错误,请重新输入");
else
return;
}
}
//接收录入数据并返回计算器对象
public static FinancialCalculations judge(Scanner sc){
System.out.println("请输入融资金额(单位为: 元, 是100的倍数)");
while(true){
String s10 = sc.nextLine();//获取本金
if(!Tool.isNum(s10)){
System.out.println("您的输入错误请重新输入:");
}else if(!Tool.By100(s10)){
System.out.println("您的输入错误请重新输入:");
}else{
System.out.println("请输入融资月份,单位为:月,可输入3-120)");
while(true){
//输入月份
String s11 = sc.nextLine(); //获取月数
if(!Tool.isNum(s11)){
System.out.println("您的输入错误请重新输入:");
}else if(Integer.parseInt(s11) < 3 || Integer.parseInt(s11) > 120 ){
System.out.println("期限输入有误,请重新输入:");
}else{
System.out.println("请输入融资利率:单位为:%,可输入0-36)");
while(true){
//输入利率
String s12 = sc.nextLine(); //获取利率
if(!Tool.isNum(s12)){
System.out.println("您的输入错误请重新输入:");
}else if(Integer.parseInt(s12) < 0 || Integer.parseInt(s11) > 36 ){
System.out.println("期限输入有误,请重新输入:");
}else{
int invest = Integer.parseInt(s10);
int month = Integer.parseInt(s11);
int yearRate = Integer.parseInt(s12);
FinancialCalculations in3 = new FinancialCalculations(invest,month,yearRate);
return in3;
}
}
}
}
}
}
}
}
借鉴
作者:ReStartForTD 来源:CSDN
原文:https://blog.csdn.net/wjb_2016/article/details/71078034?utm_source=copy
package com.zuikc.bean;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class FinancialCalculations {
private int Principal; //本金
private double yearRate;//贷款利率利率
private int months;//月数
private double monthRate;//月利率
private String line ="_________________________________________________________________";
public FinancialCalculations() {
super();
}
//传入数据
public FinancialCalculations(int invest, int month, double rate) {
super();
this.Principal = invest;
this.yearRate = rate/100;
this.months = month;
this.monthRate = rate/months/100;
}
//获取每月本息 每月本息 =货款本金*货款月利率(1+货款月利率)^还款总月数/[(1+货款月利率)^还款总月数-1
public double getMonthInterest(){
double monthInterest = Principal*monthRate*Math.pow(1+monthRate,months)/(Math.pow(1+monthRate, months)-1);
return monthInterest;
}
//获取i月本金 每月本金 = 本金×月利率×(1+月利率)^(还款月序号-1)÷((1+月利率)^还款月数-1))
public double getMonthPrincipal(int i){
double monthPrincipal = Principal* monthRate * Math.pow(1+monthRate,i-1)/(Math.pow(1+monthRate,months)-1);
return monthPrincipal;
}
//每月应还利息 =贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
public double getMonthInterest(int i){
double monthPrincipal = Principal* monthRate * Math.pow(1+monthRate,i-1)/(Math.pow(1+monthRate,months)-1);
double Interest = getMonthInterest()-monthPrincipal;
return Interest;
}
//应还总利息
public double getInterest(){
double Interest = 0;
for (int i = 1; i <= months; i++) {
Interest += this.getMonthInterest(i);
}
return Interest;
}
//应还总额
public double getTotal(){
double total = Principal+getInterest();
return total;
}
//剩余回款总额=(剩余还款约束*每月本息)
public double getBalance(int i){
double balance = getMonthInterest()*(months-i);
return balance;
}
//等额等息 表头部分
public ArrayList<String> getTableHead(){
ArrayList<String> list = new ArrayList<>();
DecimalFormat df = new DecimalFormat("0.00");
list.add(line);
list.add("计算结果");
list.add("(本结果仅供参考,不作为最终结算凭证)");
String str ="融资金额"+Principal+"\t"+"融资期限:"+months+"个月"+"\t"
+"利息收益:"+df.format(getInterest())+"\t"+"本息合计"+df.format(getTotal());
list.add(str);
list.add(line);
return list;
}
//方法重载 按月付息 表头部分 Array每一个字符串元素是一行
public ArrayList<String> getTableHead(String s){
DecimalFormat df = new DecimalFormat("0.00");
ArrayList<String> list = new ArrayList<>();
list.add(line);
list.add("计算结果");
list.add("(本结果仅供参考,不作为最终结算凭证)");
String str ="融资金额:"+Principal+" \t"+"融资期限:"+months+"个月"+"\t"
+"利息收益:"+df.format(Principal*yearRate)+"\t"+"本息合计"+df.format(Principal*(1+yearRate));
list.add(str);
return list;
}
//方法重载 按月付息 表格身体部分
public ArrayList<String> getTablePerson(String S){
DecimalFormat df = new DecimalFormat("0.00");
ArrayList<String> list = new ArrayList<>();
list.add(line);
list.add("本息收款列表");
list.add("期数\t每月还款本息\t应还本金\t\t应还利息\t\t剩余还款本息");
double mi = Principal*monthRate; //每月应还利息
for (int i = 1; i < months; i++) {
list.add(" "+i+"\t "+df.format(mi)
+"元"+"\t"+0+"元"+"\t\t"+df.format(mi)
+"元"+"\t\t"+df.format(Principal*(1+monthRate)*(months-i))+"元");
}
list.add(" "+months+""+"\t"+df.format(Principal+mi)+"元"+"\t"+Principal+"元"+"\t\t"+df.format(mi)+"元"+"\t\t"+0+"元");
list.add(line);
return list;
}
//表格身体部分 ArrayList每一个字符串元素是一行
public ArrayList<String> getTablePerson(){
DecimalFormat df = new DecimalFormat("0.00");
ArrayList<String> list = new ArrayList<>();
list.add(line);
list.add("本息收款列表");
list.add("期数\t每月还款本息\t应还本金\t\t应还利息"+"\t\t剩余还款本息");
for (int i = 1; i <= months; i++) {
list.add(" "+i+""+"\t "+df.format(getMonthInterest())
+"元"+"\t"+df.format(getMonthPrincipal(i))
+"元"+"\t\t"+df.format(getMonthInterest(i))
+"元"+"\t\t"+df.format(getBalance(i))+"元");
}
list.add(line);
/*for (String string : list) {
System.out.print(string);
}*/
return list;
}
//打印和储存文本
public void setVoucher(ArrayList<String> head,ArrayList<String> person) throws Exception{
BufferedWriter bw = new BufferedWriter(new FileWriter("voucher.txt"));
String key ="25d55ad283aa400afeas238adse3a2t4";
//打印和储存表头部分
for (String s : head) {
//打印
System.out.println(s);
//加密
String content =(s);
//写入
s = AESUtil.jiami(content, key);
bw.write(s);
bw.newLine();
}
//打印和储存表格身体部分
if(person != null){
for (String s : person) {
System.out.println(s);
//打印
String content =(s);
//加密
s = AESUtil.jiami(content, key);
//写入
bw.write(s);
bw.newLine();
}
}
bw.close();
}
}
//这个木学到…找了个最清晰的copy了过来
作者:saindy5828 来源:CSDN
转自https://blog.csdn.net/saindy5828/article/details/45827101?utm_source=copy
package com.zuikc.bean;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
**
* AES128 算法,加密模式为ECB,填充模式为 pkcs7(实际就是pkcs5)
*
*
*/
public class AESUtil {
/**
* 加密
*
* @param content 需要加密的内容
* @param key 加密密码
* @return
*/
public static byte[] encrypt(String content, String key) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new java.security.SecureRandom(key.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("gbk");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**解密
* @param content 待解密内容
* @param key 解密密钥
* @return
*/
public static byte[] decrypt(byte[] content, String key) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new java.security.SecureRandom(key.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 字符串加密
* @param content 要加密的字符串
* @param key 加密的AES Key
* @return
*/
public static String encryptString(String content, String key) {
return parseByte2HexStr(encrypt(content, key));
}
/**
* 字符串解密
* @param content 要解密的字符串
* @param key 解密的AES Key
* @return
*/
public static String decryptString(String content, String key){
byte[] decryptFrom = parseHexStr2Byte(content);
byte[] decryptResult = decrypt(decryptFrom,key);
return new String(decryptResult);
}
/**将16进制转换为二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
/**将二进制转换成16进制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
public static String jiami(String content,String key){
try{
String encrypt = AESUtil.encryptString(content, key);
return encrypt;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String jiemi(String encrypt,String key){
try{
String decrypt = AESUtil.decryptString(encrypt, key);
return decrypt;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/*
public static void main(String aregs[]){
String content = "融资金额:10000 融资期限:12个月 利息收益:1200.00 本息合计11200.00";
String key = "25d55ad283aa400af464c76d713c0dee";
try {
//加密
System.out.println("加密前:" + content);
String encrypt = AESUtil.encryptString(content, key);
System.out.println("加密后:" + encrypt);
//解密
String decrypt = AESUtil.decryptString(encrypt, key);
System.out.println("解密后:" + decrypt);
} catch (Exception e) {
e.printStackTrace();
}
}*/
}
```批改不是很好,给了一些思路,重头理一下
1.需要一个主界面,进行客户各项操作,与print方法对接
2.需要一个实现计算功能的接口,然后根据不同利息的计算方式去做两个实现类
3.需要一个print方法进行打印和加密,与利息计算接口与加密工具类对接
4.需要的加密工具类