最近刚刚复习了一下java的面向对象三大特性,这里跟着hsp做个小零钱通实践一下,以下记录了学习和编写过程
使用java 开发零钱通项目, 模仿微信实现简易功能,可以完成收益入账,消费,查看明细,退出系统等功能,先按照一般方法写,后期在改进为oop
预期界面:(实际可能不同)
面对这样一个需求,先化繁为简
写一个菜单完成零钱通明细.完成收益入账消费退出用户输入4退出时,给出提示”你确定要退出吗? y/n”,必须输入正确的y/n ,否则循环输入指令,直到输入y 或者 n在收益入账和消费时,判断金额是否合理,并给出相应的提示先完成显示菜单,并可以选择菜单,并且给出对应提示
public static void main(string[] args) { // define related variables scanner scanner = new scanner(system.in); string key = ""; boolean loop = true; do { system.out.println("==========small change menu=========="); system.out.println("\t\t\t1 show change details"); system.out.println("\t\t\t2 income entry"); system.out.println("\t\t\t3 consumption"); system.out.println("\t\t\t4 exit"); system.out.println("plea choo 1-4:"); key = scanner.next(); //u switch to control switch (key) { ca "1": system.out.println("1 show change details"); break; ca "2": system.out.println("2 income entry"); break; ca "3": system.out.println("3 consumption"); break; ca "4": system.out.println("4 exit"); system.out.println(" y唐山科技职业技术学院ou have exit the smallchange"); loop = fal; break; default: system.out.println("err plea choo again"); } } while (loop); }
思路
(1) 可以把收益入账和消费保存到数组
(2) 可以使用对象
(3) 简单的话可以使用string拼接
这里直接采取第三种方式
改变一下switch的ca1
string details = "-----------------零钱通明细------------------";
ca "1": system.out.println(details); break;
完成收益入账
定义新的变量
double money = 0; double balance = 0; date date = null; // date 是 java.util.date 类型,表示日期 //if you don't like the default format of displaying date ,change it with sdf simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm");
修改switch中的ca2
system.out.print("income recorded amount:"); money = scanner.nextdouble(); //the range of money should be limited //give the hits of the illegal money value 就直接break balance += money; //拼接收益入账信息到 details date = new date(); //get the c函授本科是什么urrent time details += "\n收益入账\t+" + money + "\t" + sdf.format(date)+ "\t" + balance; break;
效果演示:
保证入账>0
定义新的变量
string note = "";
修改switch中的ca3
ca "3": system.out.print("consumption amount:"); money = scanner.nextdouble(); //the range of money should be limited system.out.print("consumption description:"); note = scanner.next(); balance -= money; //splicing consumption information to details date = new date();//get the current time details += "\n"+note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance; break;
效果演示:
给出确认,是否要退出
用户输入4退出时,给出提示”你确定要退出吗? y/n”,必须输入正确的y/n ,
否则循环输入指令,直到输入y 或者 n
(1) 定义一个变量 choice, 接收用户的输入
(2) 使用 while + break, 来处理接收到的输入时 y 或者 n
(3) 退出while后,再判断choice是y还是n ,就可以决定是否退出
(4) 建议一段代码完成功能,不混在一起
ca "4": string choice = ""; while (true) { //the ur is required to enter y / n, otherwi it will cycle all the time system.out.println("你确定要退出吗? y/n"); choice = scanner.next(); if ("y".equals(choice) || "n".equals(choice)) { break; } //scheme 2// if("y".equals(choice)) {// loop = fal;// break;// } el if ("n".equals(choice)) {// break;// } } if (choice.equals("y")) { loop = fal; } break;
效果演示:
收入时
if (money <= 0) { system.out.println("the income entry amount must be greater than 0"); break; }
支出时
if (money <= 0 || money > balance) { system.out.println("your consumption amount should be 0-" + balance); break; }
效果演示
import java.text.simpledateformat;import java.util.date;import java.util.scanner;public class smallchangesys { // try to reduce complexity to simplicity//1. first complete the display menu,// and you can lect the menu to give the corresponding prompt//2. complete change details//3. complete income entry//4. consumption//5. exit//6. when the ur enters 4 to exit, the prompt "are you sure you want to exit?// y / n" will be given. you must enter the correct y / n,// otherwi cycle the input instruction until y or n is entered//7. when the income is recorded and consumed,// judge whether the amount is reasonable and give corresponding tips public static void main(string[] args) { // define related variables scanner scanner = new scanner(system.in); string key = ""; boolean loop = true; //2. complete the change details //(1) 可以把收益入账和消费,保存到数组 (2) 可以使用对象 (3) 简单的话可以使用string拼接 string details = 周公诫子翻译"-----------------change details------------------"; //3. complete income entry double money = 0; double balance = 0; date date = null; // date 是 java.util.date 类型,表示日期 //if you don't like the default format of displaying date ,change it with sdf simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm"); //4. consumption //define new variable,store the reason why consume string note = ""; do { system.out.println("\n==========small change menu=========="); system.out.println("\t\t\t1 show change details"); system.out.println("\t\t\t2 income entry"); system.out.println("\t\t\t3 consumption"); system.out.println("\t\t\t4 exit"); system.out.println("plea choo 1-4:"); key = scanner.next(); //u switch to control switch (key) { ca "1": system.out.println(details); break; ca "2": system.out.print("income recorded amount:"); money = scanner.nextdouble(); //the range of money should be limited //commonly u <if> to judge the wrong situation make the code easy to read //give the hits of the illegal money value 就直接break if (money <= 0) { system.out.println("the income entry amount must be greater than 0"); break; } balance += money; //splicing consumption information to details date = new date(); //get the current time details += "\n" + "income " + "\t" + "+" + money + "\t" + sdf.format(date) + "\t" + balance; break; ca "3": system.out.print("consumption amount:"); money = scanner.nextdouble(); //the range of money should be limited if (money <= 0 || money > balance) { system.out.println("your consumption amount should be 0-" + balance); break; } system.out.print("consumption description:"); note = scanner.next(); balance -= money; //splicing consumption information to details date = new date();//get the current time details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance; break; ca "4": string choice = ""; while (true) { //the ur is required to enter y / n, otherwi it will cycle all the time system.out.println("你确定要退出吗? y/n"); choice = scanner.next(); if ("y".equals(choice) || "n".equals(choice)) { break; } //scheme 2// if("y".equals(choice)) {// loop = fal;// break;// } el if ("n".equals(choice)) {// break;// } } if (choice.equals("y")) { loop = fal; 英雄作文素材 } break; default: system.out.println("err plea choo again"); } } while (loop); system.out.println(" you have exit the smallchange"); }}
很多东西可以直接复制过来变成方法,把原来的改过来是简单的
那么先有一个执行的主类smallchangesysapp
//call the object directly and display the main menupublic class smallchangesysapp { public static void main(string[] args) { new smallchangesysoop().mainmenu(); }}
还有一个类专门是对象,我们叫它为smallchangesysoop
import java.text.simpledateformat;import java.util.date;import java.util.scanner;/** * this class is ud to complete various functions of zero money pass * using oop (object-oriented programming) * each function corresponds to a method */public class smallchangesysoop { //basic variables boolean loop = true; scanner scanner = new scanner(system.in); string key = ""; //display details string details = "-----------------change details------------------"; //income double money = 0; double balance = 0; date date = null; simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm"); // consume string note = ""; public void mainmenu() { do { system.out.println("\n================small change menu(oop)==============="); system.out.println("\t\t\t1 show change details"); system.out.println("\t\t\t2 income entry"); system.out.println("\t\t\t3 consumption"); system.out.println("\t\t\t4 exit"); system.out.println("plea choo 1-4:"); key = scanner.next(); switch (key) { ca "1": this.detail(); break; ca "2": this.income(); break; ca "3": this.pay(); break; ca "4": this.exit(); break; default: system.out.println("choo the wrong number plea choo again"); } } while (loop); } public void detail() { system.out.println(details); } public void income() { system.out.print("income recorded amount:"); money = scanner.nextdouble(); if (money <= 0) { system.out.println("the income entry amount must be greater than 0"); return; //exit and do not execute next ntence.change break to return } balance += money; date = new date(); details += "\nincome \t+" + money + "\t" + sdf.format(date) + "\t" + balance; } public void pay() { system.out.print("consumption amount:"); money = scanner.nextdouble(); if (money <= 0 || money > balance) { system.out.println("your consumption amount should be 0-" + balance); re公共事业费turn; } system.out.print("consumption description:"); note = scanner.next(); balance -= money; date = new date(); details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance; } //退出 public void exit() { //when the ur enters 4 to exit, the prompt "are you sure you want to exit? // y / n" will be given. you must enter the correct y / n string choice = ""; while (true) { system.out.println("are you really gonna exit? y/n"); choice = scanner.next(); if ("y".equals(choice) || "n".equals(choice)) { break; } //scheme 2// if("y".equals(choice)) {// loop = fal;// break;// } el if ("n".equals(choice)) {// break;// } } if (choice.equals("y")) { loop = fal; } }}
oop版主函数很简单,只要new这个对象就可以了,关于这个对象的其他方法也好属性也好,不用放在主函数里面,那样在主函数也可以自由加上想加得到内容,未来假如有他人要用,不用把整个文件拷过去,只要把类交给对方即可,这样扩展和可读性大大提升,要加什么功能就再写方法原先的扩展功能很麻烦,要来回切
以上就是java模仿微信实现零钱通简易功能(两种版本)的详细内容,更多关于java的资料请关注www.887551.com其它相关文章!
本文发布于:2023-04-04 02:43:38,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/c7934e4c39a8199e9735cd5c62e08769.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java模仿微信实现零钱通简易功能(两种版本).doc
本文 PDF 下载地址:Java模仿微信实现零钱通简易功能(两种版本).pdf
留言与评论(共有 0 条评论) |