项目需求
- 使用java开发零钱通项目,可以完成收益入账,消费,查看明细,退出系统等功能。
- 项目的界面
思路:先使用过程编程,然后转OOP来体会面向对象的便利性
过程编程实现(1)
一个小时完成,虽然整体条件满足,但是发现有非常多的局限性
- 消费金额必须输入负数才能计算
- 查询时只能查询最近的一条信息
源码放出来记录一下
package com.tao.project.smallchangesys;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/**
* Create By 刘鸿涛
* 2021/12/21 17:16
*/
public class SmallChangeSys01 {
public static void main(String[] args) {
//获取系统时间
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Scanner sc = new Scanner(System.in);
double total = 0; //总余额
double income = 0; //收入 定义到这里的目的是为了case 1时选择参数
double consume = 0; //消费金额
String consumeItem = "";
boolean judge = true; //初始为真
while(judge){ //为真运行,为假停止
System.out.println("--------零钱通菜单---------");
System.out.println("\t\t1 零钱通明细");
System.out.println("\t\t2 收益入账");
System.out.println("\t\t3 消费");
System.out.println("\t\t4 退 出");
System.out.println("请选择(1-4)");
int select = sc.nextInt(); //判断
switch (select){
case 1:
if(income >= 0){
System.out.println("--------零钱通明细---------");
System.out.print("收益入账\t" + income + "\t");
System.out.print(simpleDateFormat.format(date)); //调用Date类方法得到时间
System.out.println("\t余额" + total);
}else{
System.out.println("--------零钱通明细---------");
System.out.print( consumeItem + " " + income + "\t");
System.out.print(simpleDateFormat.format(date)); //调用Date类方法得到时间
System.out.println("\t余额" + total);
}
break;
case 2:
System.out.println("请输入入账金额/元:");
income = sc.nextInt(); //输入收入
total += income; //统计剩余金额
break;
case 3:
System.out.println("请输入消费金额/元(用负数来表示):");
income = sc.nextInt(); //消费金额
System.out.println("请输入消费项目:");
consumeItem = sc.next(); //消费项目
total += income; //总计剩余金额
break;
case 4:
judge = false; //终止while循环
break;
}
}
}
}
面向过程(改正)
- 增加了退出功能
- 增加了字符串连接
- 增加了输入金额判断(过关斩将思维)
package com.tao.project.smallchangesys;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/**
* Create By 刘鸿涛
* 2021/12/21 17:16
*/
public class SmallChangeSys01 {
public static void main(String[] args) {
//获取系统时间
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Scanner sc = new Scanner(System.in);
double total = 0; //总余额
double income = 0; //收入 定义到这里的目的是为了选择参数
String consumeItem = "";
String details = "";
boolean judge = true; //初始为真
while(judge){ //为真运行,为假停止
System.out.println("--------零钱通菜单---------");
System.out.println("\t\t1 零钱通明细");
System.out.println("\t\t2 收益入账");
System.out.println("\t\t3 消费");
System.out.println("\t\t4 退 出");
System.out.println("请选择(1-4)");
int select = sc.nextInt(); //判断
switch (select){
case 1:
System.out.println("--------零钱通明细---------");
System.out.println(details);
break;
case 2:
System.out.println("请输入入账金额/元:");
income = sc.nextInt(); //输入收入
if(income <= 0){
System.out.println("收益金额有误,请重新输入");
break;
}
total += income; //统计剩余金额
details += "\n收益入账\t+" + income + "\t" + simpleDateFormat.format(date) + "\t余额:" + total;
break;
case 3:
System.out.println("请输入消费金额/元");
income = sc.nextInt(); //消费金额
if(income > total || income <= 0 ){
System.out.println("您的消费金额应该在0 - " + income + "之间");
break;
}
System.out.println("请输入消费项目:");
consumeItem = sc.next(); //消费项目
total -= income; //总计剩余金额
details += "\n"+ consumeItem +"\t-" + income + "\t" + simpleDateFormat.format(date) + "\t余额:" + total;
break;
case 4:
while(true){
System.out.println("你确定要退出吗?y/n");
char c = sc.next().charAt(0);
if(c == 'y'){
judge = false; //终止while循环
break;
}else if(c == 'n'){
break;
}else{
continue;
}
}
break;
default:
System.out.println("输入错误,请重新输入");
}
}
}
}
OOP实现(2)
- 静态变量
- Date类
- SimpleDateFormate类
- 把所有方法封装到一个类中,然后用main调用即可
- 这里的get、set方法只是走个形式,没有作用
- 静态方法块
又从0重写了一遍,用了一个小时完成
OOP代码源码
SmallChangeSysOOP类
package com.tao.project.smallchangesys.OOP_;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/**
* Create By 刘鸿涛
* 2021/12/22 9:41
*/
public class SmallChangeSysOOP {
Scanner sc = new Scanner(System.in);
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm" );
private double money; //金额
private static double total; //静态变量:总金额
private static String details = ""; //静态变量:连接明细字符串
static{
System.out.println("\n\t\t鬼鬼公司");
}
boolean judge = true;
public void main01(){
while (judge){
System.out.println("--------零钱通菜单---------");
System.out.println("\t\t1 零钱通明细");
System.out.println("\t\t2 收益入账");
System.out.println("\t\t3 消费");
System.out.println("\t\t4 退 出");
System.out.println("请选择(1-4)");
String chioce = sc.next();
switch (chioce){
case "1":
this.check(); //调用查看明细方法
break;
case "2":
this.income(); //调用收益方法
break;
case "3":
this.consume(); //调用消费方法
break;
case "4": //调用退出方法
this.exit();
break;
default:
break;
}
}
}
//get,set方法
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
//1.查看明细方法
public void check(){
System.out.println(details);
}
//2.收益入账方法
public void income(){
System.out.println("请输入收益金额");
money = sc.nextDouble();
total += money;
details += "\n收益入账\t+" + money + "\t" + simpleDateFormat.format(date) + "\t余额:" + total;
}
//3.消费方法
public void consume(){
System.out.println("请输入消费金额");
money = sc.nextDouble();
total -= money;
System.out.println("请输入消费项目");
String item = sc.next();
details += "\n"+ item + "\t-" + money + "\t" + simpleDateFormat.format(date) + "\t余额:" + total;
}
//4.退出方法
public void exit(){
boolean judge1 = true;
while (judge1){
System.out.println("确定退出吗y/n");
char chioce = sc.next().charAt(0);
if(chioce == 'y' || chioce == 'n'){
judge1 = false;
}
if(chioce == 'y'){
judge = false;
System.out.println("程序已退出...");
break;
}
if(chioce == 'n'){
continue;
} else{
System.out.println("输入有误,请重新输入");
continue;
}
}
}
}
SmallChangeSysApp类
package com.tao.project.smallchangesys.OOP_;
/**
* Create By 刘鸿涛
* 2021/12/22 9:41
*/
public class SmallChangeSysApp {
public static void main(String[] args) {
SmallChangeSysOOP change = new SmallChangeSysOOP(); //“零钱”对象,启动类,运行静态代码块
change.main01();
}
}